1 /*
2  * $Id: teststr.i,v 1.1 2005-09-18 22:06:18 dhmunro Exp $
3  * complete test of yorick string manipulation functions
4  */
5 /* Copyright (c) 2005, The Regents of the University of California.
6  * All rights reserved.
7  * This file is part of yorick (http://yorick.sourceforge.net).
8  * Read the accompanying LICENSE file for details.
9  */
10 
11 goofs = 0;  /* cumulative tally of errors detected */
12 write, "begin string manipulation test...";
13 s0 = string(0);
14 if (do_stats) "sA "+print(yorick_stats());
15 
16 /* ------------------------------------------------------------------------- */
17 /* check DOCUMENT comment examples in i0/std.i */
18 
19 if (anyof(strchar(["a","b","c"]) != ['a','\0','b','\0','c','\0']) ||
20     anyof(strchar([['a','\0','b'],['c','\0','\0']])
21           != ["a","b","c",string(0)])) {
22   goofs++;
23   "**FAILURE** (strchar) example";
24 }
25 
26 if (strpart("Hello, world!", 4:6) != "lo," ||
27     strpart("Hello, world!", [3,6]) != "lo," ||
28     strpart("Hello, world!", [3,3]) != "" ||
29     strpart("Hello, world!", [3,2]) ||
30     strpart("Hello, world!", [3,20]) ||
31     anyof(strpart("Hello, world!", [3,6,7,9]) != ["lo,","wo"]) ||
32     anyof(strpart(["one","two"], [[1,2],[0,1]]) != ["n","t"]) ||
33     anyof(strpart(["one","two"], [1,2,0,1]) != [["n","o"],["w","t"]])) {
34   goofs++;
35   "**FAILURE** (strpart) example";
36 }
37 
38 if (anyof(strword("  Hello, world!") != [2,15]) ||
39     anyof(strword("Hello, world!") != [0,13]) ||
40     anyof(strword("Hello, world!", , 2) != [0,6,7,13]) ||
41     anyof(strword("Hello, world!", , -2) != [0,6]) ||
42     anyof(strword("Hello, world!", ".!, \t\n", -2) != [0,5]) ||
43     anyof(strword("Hello, world!", [string(0), ".!, \t\n"], 0) != [0,12]) ||
44     anyof(strword("Hello, world!", "A-Za-z", 2) != [5,7,12,13]) ||
45     anyof(strword("Hello, world!", "^A-Za-z", 2) != [0,5,7,13]) ||
46     anyof(strword("Hello, world!", "^A-Za-z", 3) != [0,5,7,12,13,-1]) ||
47     anyof(strword("  Hello, world!", [" \t\n",".!, \t\n"]) != [2,7,9,15]) ||
48     anyof(strword("  Hello, world!", [" \t\n",".!, \t\n"], 2)
49           != [2,7,9,14,15,-1])) {
50   goofs++;
51   "**FAILURE** (strword) example";
52 }
53 
54 if (anyof(strglob("*.pdb",["a","b.pdb","c.pdbx"]) != [0,1,0]) ||
55     anyof(strglob("hack[0-9][0-9]", ["hack4a","hack63",""]) != [0,1,0])) {
56   goofs++;
57   "**FAILURE** (strglob) example";
58 }
59 
60 s = ["one two three", "four five six"];
61 if (anyof(strfind("o",s) != [[0,1], [1,2]]) ||
62     anyof(strfind(" t",s) != [[3,5], [13,-1]]) ||
63     anyof(strfind(" t",s,n=2) != [[3,5,7,9], [13,-1,13,-1]]) ||
64     anyof(strfind("e",s,n=2,back=1) != [[11,12,12,13], [0,-1,8,9]])) {
65   goofs++;
66   "**FAILURE** (strfind) example";
67 }
68 
69 s = "Hello, world!" ;
70 if (anyof(strgrep("(Hello|Goodbye), *([a-zA-Z]+)!", s) != [0,13]) ||
71     anyof(strgrep("(Hello|Goodbye), *([a-z]*|[A-Z]*)!", s, sub=[1,2])
72           != [0,5,7,12]) ||
73     anyof(strgrep("(Hello|Goodbye), *([a-z]*|[A-Z]*)!", s, sub=[0,2])
74           != [0,13,7,12]) ||
75     anyof(strgrep("(Hello|Goodbye), *(([A-Z]*)|([a-z]*))!", s, sub=[0,2,3,4])
76           != [0,13,7,12,13,-1,7,12])) {
77   goofs++;
78   "**FAILURE** (strgrep) example";
79 }
80 
81 if (streplace(s,[0,5], "Goodbye") != "Goodbye, world!" ||
82     streplace(s,[0,5,7,7], ["Goodbye","cruel "]) != "Goodbye, cruel world!" ||
83     streplace(s,[0,5,7,7,12,13], ["Goodbye","cruel ","?"]) !=
84     "Goodbye, cruel world?" ||
85     streplace(s,[0,5,0,-1,12,13], ["Goodbye","cruel ","?"]) !=
86     "Goodbye, world?" ||
87     anyof(streplace([s,s],[0,5], ["Goodbye", "Good bye"])
88           != ["Goodbye, world!", "Good bye, world!"]) ||
89     anyof(streplace([s,s],[0,5,7,7], [["Goodbye","cruel "], ["Good bye",""]])
90           != ["Goodbye, cruel world!", "Good bye, world!"])) {
91   goofs++;
92   "**FAILURE** (streplace) example";
93 }
94 
95 /* repeat testp.i tests */
96 
97 if (strlen("abc")!=3 ||
98     anyof(strlen([[string(),"","a"],["axx","ab","abcd"]])!=
99           [[0,0,1],[3,2,4]])) {
100   goofs++;
101   "**FAILURE** (strlen)";
102 }
103 
104 if (anyof(strtok("abc    1.23    xxx")!=["abc", "   1.23    xxx"]) ||
105     anyof(strtok(["abc    1.23    xxx","c","1.5"], "\t c")!=
106           [["ab", "    1.23    xxx"],string(),["1.5",string()]])) {
107   goofs++;
108   "**FAILURE** (strtok)";
109 }
110 
111 if (!strmatch("abc", "b") || strmatch("abc", "B") ||
112     !strmatch("abc", "B", 1) ||
113     anyof(strmatch(["abc","aBC"], "B")!=[0,1])) {
114   goofs++;
115   "**FAILURE** (strmatch)";
116 }
117 
118 if (strpart("abc", 1:1)!="a" || strpart("abc", 2:10)!="bc" ||
119     strpart("abc", :-1)!="ab" || strpart("abc", :-5)!="" ||
120     anyof(strpart(["abc","yowza"],3:)!=["c","wza"])) {
121   goofs++;
122   "**FAILURE** (strpart)";
123 }
124 
125 s = [];
126 if (do_stats) "sB "+print(yorick_stats());
127 /* ------------------------------------------------------------------------- */
128 
129 s = array(string, 2, 3, 4);
130 s(,1,) = "hi";
131 s(,2:3,1) = "";
132 x = strlen(s);
133 if (anyof(dimsof(x) != [3,2,3,4]) || anyof(x(,2:3,)) ||
134     anyof(x(,1,) != 2)) {
135   goofs++;
136   "**FAILURE** (strlen) comprehensive";
137 }
138 
139 s = strcase(1,s);
140 if (anyof(dimsof(s) != [3,2,3,4]) || anyof(s(,2:3,2:3)) ||
141     anyof(s(,1,) != "HI") || anyof(s(,2:3,1) != "")) {
142   goofs++;
143   "**FAILURE** (strcase) test 1";
144 }
145 s = strcase(0,s);
146 if (anyof(dimsof(s) != [3,2,3,4]) || anyof(s(,2:3,2:3)) ||
147     anyof(s(,1,) != "hi") || anyof(s(,2:3,1) != "")) {
148   goofs++;
149   "**FAILURE** (strcase) test 2";
150 }
151 strcase, 1, s;
152 if (anyof(s(,2:3,2:3)) || anyof(s(,1,) != "HI") || anyof(s(,2:3,1) != "")) {
153   goofs++;
154   "**FAILURE** (strcase) test 3";
155 }
156 
157 x = ['a','\0','\0','b','c','\0','\0','d','e','f','\0'];
158 y = ["a",string(0),"bc",string(0),"def"];
159 if (anyof(strchar(["a","","bc",string(0),"def"]) != x) ||
160     anyof(strchar(x) != y) || anyof(strchar(x(1:-1)) != y) ||
161     anyof(strchar(grow(x(1:-1),x(1:-1))) !=
162       ["a",string(0),"bc",string(0),"defa",string(0),"bc",string(0),"def"]) ||
163     anyof(strchar([x(1:-1),x(1:-1)]) != grow(y,y)) ||
164     anyof(strchar([x(1:-1),x(1:-1)](,,-:1:3)) != grow(y,y)(,-:1:3)(*))) {
165   goofs++;
166   "**FAILURE** (strchar) comprehensive";
167 }
168 
169 x = y = array(0, 255);
170 x('A':'Z') = 1;
171 x(192:214) = 1;  x(216:222) = 1;  /* iso8859-1 upper case */
172 y(where(x)+32) = 1;
173 s = char(indgen(255));
174 x = char(s + 32*x);  /* upper to lower */
175 y = char(s - 32*y);  /* lower to upper */
176 s = strchar(s);
177 if (strcase(0,s)!=strchar(x) || strcase(1,s)!=strchar(y)) {
178   goofs++;
179   "**FAILURE** (strcase) test 4";
180 }
181 
182 x = y = s = t = [];
183 if (do_stats) "sC "+print(yorick_stats());
184 
185 s = "Hello, world!";
186 x = strpart(s, [[[0,5],[7,12]],[[7,12],[0,5]],[[0,13],[5,5]]]);
187 y = [["Hello","world"],["world","Hello"],["Hello, world!",""]];
188 if (anyof(dimsof(x)!=dimsof(y)) || anyof(x!=y) ||
189     anyof(strpart(s, [[0,5,7,12],[7,12,0,5],[0,13,5,5]]) != y)) {
190   goofs++;
191   "**FAILURE** (strpart) test 1";
192 }
193 x = [[0,1,4,5],[3,5,5,-1]];
194 t = strpart(y,x);
195 s = [[["H","o"],["ld",s0]], [["w","d"],["lo",s0]], [["H","o"],[s0,s0]]];
196 if (anyof(dimsof(t)!=dimsof(s)) || anyof(t!=s) ||
197     anyof(strpart(y(,1:2),x) != s(,,1:2)) ||
198     anyof(strpart(y(,1:2),x(,-,)) !=
199           [[["H","o"],["w","d"]],[["ld",s0],["lo",s0]]])) {
200   goofs++;
201   "**FAILURE** (strpart) test 2";
202 }
203 strpart, y, x(1:2,);
204 if (anyof(y != [["H","ld"],["w","lo"],["H",s0]])) {
205   goofs++;
206   "**FAILURE** (strpart) in place";
207 }
208 
209 s = [["Now is the time"," for all good men "],
210      ["to come to the", " aid of their party."]];
211 x = strpart(s, strword(s, 4,,-2));
212 y = strpart(s, strword(s, [4,16](-,),,-2));
213 t = strpart(s, strword(s, [0,10](-,),["^a-z"," "," ."],-1));
214 s = strpart(s, strword(s, [4,16](-,-,),,-2));
215 if (anyof(x != [["is","all"],["ome","of"]]) ||
216     anyof(y != [["is","all"],[s0,"rty."]]) ||
217     anyof(t != [[["ow","is"],["for","all"]],[["the",s0],["eir","party"]]]) ||
218     anyof(s != [[["is","all"],["ome","of"]],[[s0,"n"],[s0,"rty."]]])) {
219   goofs++;
220   "**FAILURE** (strword) with offsets";
221 }
222 
223 s = ["No trim.", "  lead", "trail  ", "  both.  "];
224 if (anyof(strtrim(s) != ["No trim.", "lead", "trail", "both."]) ||
225     anyof(strtrim(s,1) != ["No trim.", "lead", "trail  ", "both.  "]) ||
226     anyof(strtrim(s,2) != ["No trim.", "  lead", "trail", "  both."]) ||
227     anyof(strtrim(s,3) != ["No trim.", "lead", "trail", "both."]) ||
228     anyof(strtrim(s,1,blank="^ ") != [" trim.","  lead","  ","  both.  "]) ||
229     anyof(strtrim(s,2,blank=" .") != ["No trim","  lead","trail","  both"]) ||
230     anyof(strtrim(s,blank=" .") != ["No trim", "lead", "trail", "both"])) {
231   goofs++;
232   "**FAILURE** (strtrim) comprehensive";
233 }
234 
235 /* strtok sufficiently tested? */
236 
237 x = y = s = t = [];
238 if (do_stats) "sD "+print(yorick_stats());
239 
240 s = ["one.c", "one.h", "one", "two.c", "two.h", "two"];
241 if (anyof(strglob("one.[ch]",s) != [1,1,0,0,0,0]) ||
242     anyof(strglob("*.[ch]",s) != [1,1,0,1,1,0]) ||
243     anyof(strglob("*o*.?",s) != [1,1,0,1,1,0]) ||
244     anyof(strglob("*\\o*.?",s,esc=1) != [1,1,0,1,1,0]) ||
245     anyof(strglob("*o*.\\?",s,esc=1) != [0,0,0,0,0,0]) ||
246     anyof(strglob("One*",s,case=0) != [1,1,1,0,0,0]) ||
247     anyof(strglob("One*",s,case=1) != [0,0,0,0,0,0]) ||
248     anyof(strglob("One*",s) != [0,0,0,0,0,0]) ||
249     !strglob("*.hit","/a/c.hit") || strglob(path=1,"*.hit","/a/c.hit") ||
250     !strglob("*.hit",".c.hit",path=1) || strglob(path=2,"*.hit",".c.hit") ||
251     !strglob(path=3,".*.hit",".c.hit")) {
252   goofs++;
253   "**FAILURE** (strglob) comprehensive";
254 }
255 
256 s = ["UpCaSe cAsE", "dnCASE case"];
257 if (anyof(strfind("case",s) != [[11,-1],[7,11]]) ||
258     anyof(strfind("case",s,back=1) != [[0,-1],[7,11]]) ||
259     anyof(strfind("CaSe",s,back=1) != [[2,6],[0,-1]]) ||
260     anyof(strfind("case",s,case=0) != [[2,6],[2,6]]) ||
261     anyof(strfind("caSe",s,case=0,back=1) != [[7,11],[7,11]]) ||
262     anyof(strfind("",s) != [[0,0],[0,0]]) ||
263     anyof(strfind("",back=1,s) != [[11,11],[11,11]]) ||
264     anyof(strfind("",s,case=0) != [[0,0],[0,0]]) ||
265     anyof(strfind("",back=1,s,case=0) != [[11,11],[11,11]]) ||
266     anyof(strfind(n=2,"",s) != [[0,0,0,0],[0,0,0,0]]) ||
267     anyof(strfind(n=2,"",back=1,s) != [[11,11,11,11],[11,11,11,11]]) ||
268     anyof(strfind(s0,s) != [[0,-1],[0,-1]]) ||
269     anyof(strfind(s0,back=1,s) != [[0,-1],[0,-1]]) ||
270     anyof(strfind(s0,s,case=0) != [[0,-1],[0,-1]]) ||
271     anyof(strfind(s0,back=1,s,case=0) != [[0,-1],[0,-1]]) ||
272     anyof(strfind(n=2,s0,s) != [[0,-1,0,-1],[0,-1,0,-1]]) ||
273     anyof(strfind(n=2,s0,back=1,s) != [[0,-1,0,-1],[0,-1,0,-1]])) {
274   goofs++;
275   "**FAILURE** (strglob) endcases and case, back keywords";
276 }
277 if (anyof(strgrep("case",s) != [[11,-1],[7,11]]) ||
278     anyof(strgrep("",s) != [[0,0],[0,0]]) ||
279     anyof(strgrep(n=2,"",s) != [[0,0,0,0],[0,0,0,0]]) ||
280     anyof(strgrep(s0,s) != [[0,-1],[0,-1]]) ||
281     anyof(strgrep(n=2,s0,s) != [[0,-1,0,-1],[0,-1,0,-1]])) {
282   goofs++;
283   "**FAILURE** (strgrep) endcases";
284 }
285 
286 s = ["ab","c","def"]+["0","123","45","6789"](-,);
287 x = "ab0c0def0ab123c123def123ab45c45def45ab6789c6789def6789";
288 y = ["ab0c0def0","ab123c123def123","ab45c45def45","ab6789c6789def6789"];
289 t = ["ab0ab123ab45ab6789","c0c123c45c6789","def0def123def45def6789"];
290 if (sum(s)!=x || anyof(s(sum,)!=y) || anyof(s(,sum)!=t)) {
291   goofs++;
292   "**FAILURE** sum index function for strings";
293 }
294 
295 x = y = s = t = [];
296 if (do_stats) "sE "+print(yorick_stats());
297 
298 s = array(sum(array("a",32)), 2,3,1,1,4);
299 x = [20,0,10] + [9,1,6,5](-,);
300 if (anyof(dimsof(strfind("a",s,x(-,,))) != [6,2,2,3,4,1,4]) ||
301     anyof(strfind("a",s,x(-,,)) != [0,1]+x(-,-,,)) ||
302     anyof(dimsof(strfind("a",s,x(-,-,,))) != [6,2,2,3,3,4,4]) ||
303     anyof(strfind("a",s,x(-,-,,)) != [0,1]+x(-,-,-,,)) ||
304     anyof(dimsof(strfind("a",s,x(-,-,-,,))) != [6,2,2,3,1,3,4]) ||
305     anyof(strfind("a",s,x(-,-,-,,)) != [0,1]+x(-,-,-,-,,)) ||
306     anyof(dimsof(strfind("a",s(,,1),x(-,,))) != [4,2,2,3,4]) ||
307     anyof(strfind("a",s(,,1),x(-,,)) != [0,1]+x(-,-,,))) {
308   goofs++;
309   "**FAILURE** (strfind) offset conformability";
310 }
311 
312 y = strfind("a",s,x(-,,));
313 s1 = strpart(s, [0,1]*x(-,-,,));
314 s2 = strpart(s, [1,0]*x(-,-,,)+[1,32]);
315 t = s;
316 streplace, t, y(,,,1:1,,), ["bc","def",s0](-,);
317 if (anyof(streplace(s,y,"b") != s1+"b"+s2) ||
318     anyof(streplace(s,y,["bc","def",s0](-,)) != s1+["bc","def",s0](-,)+s2) ||
319     anyof(t != s1(,,1:1,,)+["bc","def",s0](-,)+s2(,,1:1,,))) {
320   goofs++;
321   "**FAILURE** (streplace) comprehensive";
322 }
323 s1 = s2 = [];
324 
325 /* ------------------------------------------------------------------------- */
326 
327 x = y = s = t = [];
328 write, format= "end string manipulation test, %d goofs\n", goofs;
329 if (do_stats) "sZ "+print(yorick_stats());
330 s0 = [];
331