* standalone/test-gh.c: new test code (migrated from
[bpt/guile.git] / test-suite / standalone / test-gh.c
CommitLineData
51a186f7
RB
1/* Copyright (C) 1999,2000,2001,2003 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18/* some bits originally by Jim Blandy <jimb@red-bean.com> */
19
20#include "libguile.h"
21#include "libguile/gh.h"
22
23#include <assert.h>
24#include <string.h>
25
26static int
27string_equal (SCM str, char *lit)
28{
29 int len = strlen (lit);
30 int result;
31
32 result = ((SCM_STRING_LENGTH (str) == len)
33 && (!memcmp (SCM_STRING_CHARS (str), lit, len)));
34 scm_remember_upto_here_1 (str);
35 return result;
36}
37
38static void
39test_gh_set_substr ()
40{
41 SCM string;
42
43 string = gh_str02scm ("Free, darnit!");
44 assert (gh_string_p (string));
45
46 gh_set_substr ("dammit", string, 6, 6);
47 assert (string_equal (string, "Free, dammit!"));
48
49 /* Make sure that we can use the string itself as a source.
50
51 I guess this behavior isn't really visible, since the GH API
52 doesn't provide any direct access to the string contents. But I
53 think it should, eventually. You can't write efficient string
54 code if you have to copy the string just to look at it. */
55
56 /* Copy a substring to an overlapping region to its right. */
57 gh_set_substr (SCM_STRING_CHARS (string), string, 4, 6);
58 assert (string_equal (string, "FreeFree, it!"));
59
60 string = gh_str02scm ("Free, darnit!");
61 assert (gh_string_p (string));
62
63 /* Copy a substring to an overlapping region to its left. */
64 gh_set_substr (SCM_STRING_CHARS (string) + 6, string, 2, 6);
65 assert (string_equal (string, "Frdarnitrnit!"));
66}
67
68int
69main (int argc, char *argv[])
70{
71 scm_init_guile ();
72 test_gh_set_substr ();
73 return 0;
74}