1_debug_info = 1; () = evalfile ("inc.sl");
2
3print ("Testing string functions...");
4
5variable s;
6
7s = strcompress (" \t  \tA\n\ntest\t", " \t\n");
8if (s != "A test") failed ("strcompress");
9
10s = " \t hello world\n\t";
11if ("hello world" != strtrim (s)) failed ("strtrim");
12if ("hello world\n\t" != strtrim_beg (s)) failed ("strtrim_beg");
13if (" \t hello world" != strtrim_end (s)) failed ("strtrim_beg");
14
15if ("hello wor" != strtrim (s, " \t\nld")) failed ("strtrim with whitespace");
16
17if ("" != strcat ("", ""))
18  failed ("strcat 0");
19if ("1" != strcat ("", "1"))
20  failed ("strcat 1");
21
22if ("abcdefg" != strcat ("a", "b", "c", "d", "e", "f", "g")) failed ("strcat");
23if ("abcdefg" != strcat ("abcdefg")) failed ("strcat 2");
24
25if ((strtok (s)[0] != "hello")
26    or (strtok(s)[1] != "world")
27    or (strtok (s, "^a-z")[0] != "hello")
28    or (strtok (s, "^a-z")[1] != "world")
29    or (2 != length (strtok (s)))
30    or (2 != length (strtok (s, "^a-z")))) failed ("strtok");
31
32define test_create_delimited_string ()
33{
34   variable n = ();
35   variable args = __pop_args (_NARGS - 3);
36   variable delim = ();
37   variable eresult = ();
38   variable result;
39
40   result = create_delimited_string (delim, __push_args (args), n);
41   if (eresult != result)
42     failed ("create_delimited_string: expected: %s, got: %s",
43	     eresult, result);
44
45   if (n)
46     result = strjoin ([__push_args (args)], delim);
47   else
48     result = strjoin (String_Type[0], delim);
49
50   if (eresult != result)
51     failed ("strjoin: expected: %s, got: %s",
52	     eresult, result);
53}
54
55
56test_create_delimited_string ("aXXbXXcXXdXXe",
57			      "XX",
58			      "a", "b", "c", "d", "e",
59			      5);
60
61
62test_create_delimited_string ("", "", "", 1);
63test_create_delimited_string ("a", ",", "a", 1);
64test_create_delimited_string (",", ",", "", "", 2);
65test_create_delimited_string (",,", ",", "", "", "", 3);
66test_create_delimited_string ("", "XXX", 0);
67
68static define test_strtrans (s, from, to, ans)
69{
70   variable s1 = strtrans (s, from, to);
71   if (ans != s1)
72     failed ("strtrans(%s, %s, %s) --> %s", s, from, to, s1);
73}
74
75test_strtrans ("hello world", "^a-zA-Z", "X", "helloXworld");
76test_strtrans ("hello", "", "xxxx", "hello");
77test_strtrans ("hello", "l", "", "heo");
78test_strtrans ("hello", "helo", "abcd", "abccd");
79test_strtrans ("hello", "hl", "X", "XeXXo");
80test_strtrans ("", "hl", "X", "");
81test_strtrans ("hello", "a-z", "A-Z", "HELLO");
82test_strtrans ("hello", "a-mn-z", "A-MN-Z", "HELLO");
83test_strtrans ("abcdefg", "a-z", "Z-A", "ZYXWVUT");
84test_strtrans ("hejklo", "k-l", "L-L---", "hejL-o");
85test_strtrans ("hello", "he", "-+", "-+llo");
86test_strtrans ("hello", "", "", "hello");
87test_strtrans ("hello", "helo", "", "");
88test_strtrans ("hello", "o", "", "hell");
89test_strtrans ("hello", "hlo", "", "e");
90test_strtrans ("", "hlo", "", "");
91test_strtrans ("HeLLo", "A-Ze", "", "o");
92test_strtrans ("HeLLo", "^A-Z", "", "HLL");
93
94define test_str_replace (a, b, c, result, n)
95{
96   variable new;
97   variable m;
98
99   (new, m) = strreplace (a, b, c, n);
100
101   if (new != result)
102     failed ("strreplace (%s, %s, %s, %d) ==> %s", a, b, c, n, new);
103
104   if (n == 1)
105     {
106	n = str_replace (a, b, c);
107	!if (n) a;
108	new = ();
109	if (new != result)
110	  failed ("str_replace (%s, %s, %s) ==> %s", a, b, c, new);
111     }
112}
113
114test_str_replace ("a", "b", "x", "a", 1);
115test_str_replace ("a", "b", "x", "a", -1);
116test_str_replace ("a", "b", "x", "a", -10);
117test_str_replace ("a", "b", "x", "a", 10);
118test_str_replace ("a", "b", "x", "a", 0);
119test_str_replace ("blafoofbarfoobar", "", "xyyy", "blafoofbarfoobar", 0);
120test_str_replace ("blafoofbarfoobar", "", "xyyy", "blafoofbarfoobar", 1);
121test_str_replace ("blafoofbarfoobar", "", "xyyy", "blafoofbarfoobar", -1);
122test_str_replace ("blafoofbarfoobar", "", "xyyy", "blafoofbarfoobar", -10);
123
124test_str_replace ("blafoofbarfoobar", "foo", "XY", "blafoofbarfoobar", 0);
125test_str_replace ("blafoofbarfoobar", "foo", "XY", "blaXYfbarfoobar", 1);
126test_str_replace ("blafoofbarfoobar", "foo", "XY", "blaXYfbarXYbar", 2);
127test_str_replace ("blafoofbarfoobar", "foo", "XY", "blaXYfbarXYbar", 10);
128test_str_replace ("blafoofbarfoobar", "foo", "XY", "blafoofbarXYbar", -1);
129test_str_replace ("blafoofbarfoobar", "foo", "XY", "blaXYfbarXYbar", -2);
130test_str_replace ("blafoofbarfoobar", "r", "", "blafoofbarfoobar", 0);
131test_str_replace ("blafoofbarfoobar", "r", "", "blafoofbafoobar", 1);
132test_str_replace ("blafoofbarfoobar", "r", "", "blafoofbafooba", 2);
133test_str_replace ("blafoofbarfoobar", "r", "", "blafoofbarfooba", -1);
134test_str_replace ("blafoofbarfoobar", "r", "", "blafoofbafooba", -2);
135test_str_replace ("bla", "bla", "", "", -2);
136test_str_replace ("bla", "bla", "foo", "foo", -2);
137test_str_replace ("bla", "bla", "foo", "foo", 1);
138
139
140print ("Ok\n");
141exit (0);
142