1include <../std.scad>
2include <../strings.scad>
3
4
5module test_upcase() {
6 assert(upcase("") == "");
7 assert(upcase("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
8 assert(upcase("abcdefghijklmnopqrstuvwxyz") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
9 assert(upcase("1234567890!@#$%^&*()") == "1234567890!@#$%^&*()");
10 assert(upcase("_+-=[]\\{}|;':\",./<>?`~") == "_+-=[]\\{}|;':\",./<>?`~");
11}
12test_upcase();
13
14
15module test_downcase() {
16 assert(downcase("") == "");
17 assert(downcase("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == "abcdefghijklmnopqrstuvwxyz");
18 assert(downcase("abcdefghijklmnopqrstuvwxyz") == "abcdefghijklmnopqrstuvwxyz");
19 assert(downcase("1234567890!@#$%^&*()") == "1234567890!@#$%^&*()");
20 assert(downcase("_+-=[]\\{}|;':\",./<>?`~") == "_+-=[]\\{}|;':\",./<>?`~");
21}
22test_downcase();
23
24module test_substr_match(){
25 assert(substr_match("abcde",2,"cd"));
26 assert(!substr_match("abcde",2,"cx"));
27 assert(!substr_match("abcde",2,"cdef"));
28 assert(!substr_match("abcde",-2,"cd"));
29 assert(!substr_match("abcde",19,"cd"));
30 assert(substr_match("abc",1,""));
31 assert(!substr_match("",0,"a"));
32 assert(substr_match("",0,""));
33}
34
35
36module test_starts_with() {
37 assert(!starts_with("", "abc"));
38 assert(!starts_with("", "123"));
39 assert(!starts_with("defabc", "abc"));
40 assert(!starts_with("123def", "def"));
41 assert(!starts_with("def123def", "123"));
42 assert(starts_with("abcdef", "abc"));
43 assert(starts_with("abcabc", "abc"));
44 assert(starts_with("123def", "123"));
45}
46test_starts_with();
47
48
49module test_ends_with() {
50 assert(!ends_with("", "abc"));
51 assert(!ends_with("", "123"));
52 assert(!ends_with("abcdef", "abc"));
53 assert(ends_with("defabc", "abc"));
54 assert(ends_with("abcabc", "abc"));
55 assert(ends_with("def123", "123"));
56}
57test_ends_with();
58
59
60module test_format_int() {
61 assert(format_int(0,6) == "000000");
62 assert(format_int(3,6) == "000003");
63 assert(format_int(98765,6) == "098765");
64 assert(format_int(-3,6) == "-000003");
65 assert(format_int(-98765,6) == "-098765");
66}
67test_format_int();
68
69
70module test_format_fixed() {
71 assert(format_fixed(-PI*100,8) == "-314.15926536");
72 assert(format_fixed(-PI,8) == "-3.14159265");
73 assert(format_fixed(-3,8) == "-3.00000000");
74 assert(format_fixed(3,8) == "3.00000000");
75 assert(format_fixed(PI*100,8) == "314.15926536");
76 assert(format_fixed(PI,8) == "3.14159265");
77 assert(format_fixed(0,8) == "0.00000000");
78 assert(format_fixed(-PI*100,3) == "-314.159");
79 assert(format_fixed(-PI,3) == "-3.142");
80 assert(format_fixed(-3,3) == "-3.000");
81 assert(format_fixed(3,3) == "3.000");
82 assert(format_fixed(PI*100,3) == "314.159");
83 assert(format_fixed(PI,3) == "3.142");
84}
85test_format_fixed();
86
87
88module test_format_float() {
89 assert(format_float(-PI*100,8) == "-314.15927");
90 assert(format_float(-PI,8) == "-3.1415927");
91 assert(format_float(-3,8) == "-3");
92 assert(format_float(3,8) == "3");
93 assert(format_float(PI*100,8) == "314.15927");
94 assert(format_float(PI,8) == "3.1415927");
95 assert(format_float(0,8) == "0");
96 assert(format_float(-PI*100,3) == "-314");
97 assert(format_float(-PI,3) == "-3.14");
98 assert(format_float(-3,3) == "-3");
99 assert(format_float(3,3) == "3");
100 assert(format_float(PI*100,3) == "314");
101 assert(format_float(PI,3) == "3.14");
102}
103test_format_float();
104
105
106module test_is_digit() {
107 for (i=[32:126]) {
108 if (i>=ord("0") && i <=ord("9")) {
109 assert(is_digit(chr(i)));
110 } else {
111 assert(!is_digit(chr(i)));
112 }
113 }
114 assert(!is_digit("475B3"));
115 assert(is_digit("478"));
116}
117test_is_digit();
118
119
120module test_is_hexdigit() {
121 for (i=[32:126]) {
122 if (
123 (i>=ord("0") && i <=ord("9")) ||
124 (i>=ord("A") && i <=ord("F")) ||
125 (i>=ord("a") && i <=ord("f"))
126 ) {
127 assert(is_hexdigit(chr(i)));
128 } else {
129 assert(!is_hexdigit(chr(i)));
130 }
131 }
132}
133test_is_hexdigit();
134
135
136module test_is_letter() {
137 for (i=[32:126]) {
138 if (
139 (i>=ord("A") && i <=ord("Z")) ||
140 (i>=ord("a") && i <=ord("z"))
141 ) {
142 assert(is_letter(chr(i)));
143 } else {
144 assert(!is_letter(chr(i)));
145 }
146 }
147}
148test_is_letter();
149
150
151module test_is_lower() {
152 for (i=[32:126]) {
153 if (
154 (i>=ord("a") && i <=ord("z"))
155 ) {
156 assert(is_lower(chr(i)));
157 } else {
158 assert(!is_lower(chr(i)));
159 }
160 }
161 assert(is_lower("abcdefghijklmnopqrstuvwxyz"));
162 assert(!is_lower("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
163 assert(!is_lower("abcdefghijKlmnopqrstuvwxyz"));
164 assert(!is_lower("abcdefghijklmnopqrstuvwxyZ"));
165}
166test_is_lower();
167
168
169module test_is_upper() {
170 for (i=[32:126]) {
171 if (
172 (i>=ord("A") && i <=ord("Z"))
173 ) {
174 assert(is_upper(chr(i)));
175 } else {
176 assert(!is_upper(chr(i)));
177 }
178 }
179 assert(is_upper("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
180 assert(!is_upper("abcdefghijklmnopqrstuvwxyz"));
181 assert(!is_upper("ABCDEFGHIJkLMNOPQRSTUVWXYZ"));
182 assert(!is_upper("ABCDEFGHIJKLMNOPQRSTUVWXYz"));
183}
184test_is_upper();
185
186
187module test_parse_float() {
188 assert(parse_float("3.1416") == 3.1416);
189 assert(parse_float("-3.1416") == -3.1416);
190 assert(parse_float("3.000") == 3.0);
191 assert(parse_float("-3.000") == -3.0);
192 assert(parse_float("3") == 3.0);
193 assert(parse_float("0") == 0.0);
194}
195test_parse_float();
196
197
198module test_parse_frac() {
199 assert(parse_frac("") == 0);
200 assert(parse_frac("1/2") == 1/2);
201 assert(parse_frac("+1/2") == 1/2);
202 assert(parse_frac("-1/2") == -1/2);
203 assert(parse_frac("7/8") == 7/8);
204 assert(parse_frac("+7/8") == 7/8);
205 assert(parse_frac("-7/8") == -7/8);
206 assert(parse_frac("1 1/2") == 1 + 1/2);
207 assert(parse_frac("+1 1/2") == 1 + 1/2);
208 assert(parse_frac("-1 1/2") == -(1 + 1/2));
209 assert(parse_frac("768 3/4") == 768 + 3/4);
210 assert(parse_frac("+768 3/4") == 768 + 3/4);
211 assert(parse_frac("-768 3/4") == -(768 + 3/4));
212 assert(parse_frac("19") == 19);
213 assert(parse_frac("+19") == 19);
214 assert(parse_frac("-19") == -19);
215 assert(parse_frac("3/0") == INF);
216 assert(parse_frac("-3/0") == -INF);
217 assert(is_nan(parse_frac("0/0")));
218}
219test_parse_frac();
220
221
222module test_parse_num() {
223 assert(parse_num("") == 0);
224 assert(parse_num("1/2") == 1/2);
225 assert(parse_num("+1/2") == 1/2);
226 assert(parse_num("-1/2") == -1/2);
227 assert(parse_num("7/8") == 7/8);
228 assert(parse_num("+7/8") == 7/8);
229 assert(parse_num("-7/8") == -7/8);
230 assert(parse_num("1 1/2") == 1 + 1/2);
231 assert(parse_num("+1 1/2") == 1 + 1/2);
232 assert(parse_num("-1 1/2") == -(1 + 1/2));
233 assert(parse_num("768 3/4") == 768 + 3/4);
234 assert(parse_num("+768 3/4") == 768 + 3/4);
235 assert(parse_num("-768 3/4") == -(768 + 3/4));
236 assert(parse_num("19") == 19);
237 assert(parse_num("+19") == 19);
238 assert(parse_num("-19") == -19);
239 assert(parse_num("3/0") == INF);
240 assert(parse_num("-3/0") == -INF);
241 assert(parse_num("3.14159") == 3.14159);
242 assert(parse_num("-3.14159") == -3.14159);
243 assert(is_nan(parse_num("0/0")));
244}
245test_parse_num();
246
247
248module test_parse_int() {
249 assert(parse_int("0") == 0);
250 assert(parse_int("3") == 3);
251 assert(parse_int("7655") == 7655);
252 assert(parse_int("+3") == 3);
253 assert(parse_int("+7655") == 7655);
254 assert(parse_int("-3") == -3);
255 assert(parse_int("-7655") == -7655);
256 assert(parse_int("ffff",16) == 65535);
257}
258test_parse_int();
259
260
261module test_str_join() {
262 assert(str_join(["abc", "D", "ef", "ghi"]) == "abcDefghi");
263 assert(str_join(["abc", "D", "ef", "ghi"], "--") == "abc--D--ef--ghi");
264}
265test_str_join();
266
267
268module test_str_split() {
269 assert(str_split("abc-def+ghi-jkl", "-") == ["abc","def+ghi","jkl"]);
270 assert(str_split("abc-def+ghi-jkl", "-+") == ["abc","def","ghi","jkl"]);
271 assert(str_split("abc--def-ghi", "-", true) == ["abc","","def","ghi"]);
272 assert(str_split("abc--def-ghi", "-", false) == ["abc","def","ghi"]);
273 assert(str_split("abc-+def-ghi", "-+", true) == ["abc","","def","ghi"]);
274 assert(str_split("abc-+def-ghi", "-+", false) == ["abc","def","ghi"]);
275}
276test_str_split();
277
278
279module test_str_strip() {
280 assert(str_strip("abcdef", " ") == "abcdef");
281 assert(str_strip(" abcdef", " ") == "abcdef");
282 assert(str_strip(" abcdef", " ") == "abcdef");
283 assert(str_strip("abcdef ", " ") == "abcdef");
284 assert(str_strip("abcdef ", " ") == "abcdef");
285 assert(str_strip(" abcdef ", " ") == "abcdef");
286 assert(str_strip(" abcdef ", " ") == "abcdef");
287 assert(str_strip("abcdef", " ",start=true) == "abcdef");
288 assert(str_strip(" abcdef", " ",start=true) == "abcdef");
289 assert(str_strip(" abcdef", " ",start=true) == "abcdef");
290 assert(str_strip("abcdef ", " ",start=true) == "abcdef ");
291 assert(str_strip("abcdef ", " ",start=true) == "abcdef ");
292 assert(str_strip(" abcdef ", " ",start=true) == "abcdef ");
293 assert(str_strip(" abcdef ", " ",start=true) == "abcdef ");
294 assert(str_strip("abcdef", " ",end=true) == "abcdef");
295 assert(str_strip(" abcdef", " ",end=true) == " abcdef");
296 assert(str_strip(" abcdef", " ",end=true) == " abcdef");
297 assert(str_strip("abcdef ", " ",end=true) == "abcdef");
298 assert(str_strip("abcdef ", " ",end=true) == "abcdef");
299 assert(str_strip(" abcdef ", " ",end=true) == " abcdef");
300 assert(str_strip(" abcdef ", " ",end=true) == " abcdef");
301 assert(str_strip("123abc321","12") == "3abc3");
302 assert(str_strip("123abc321","12",start=true,end=true) == "3abc3");
303 assert(str_strip("123abc321","12",start=true,end=false) == "3abc321");
304 assert(str_strip("123abc321","12",start=false,end=false) == "123abc321");
305 assert(str_strip("123abc321","12",start=false,end=true) == "123abc3");
306 assert(str_strip("123abc321","12",start=false) == "123abc3");
307 assert(str_strip("123abc321","12",start=true) == "3abc321");
308 assert(str_strip("123abc321","12",end=false) == "3abc321");
309 assert(str_strip("123abc321","12",end=true) == "123abc3");
310 assert(str_strip("abcde","abcde")=="");
311 assert(str_strip("","abc")=="");
312}
313test_str_strip();
314
315
316module test_substr() {
317 assert(substr("abcdefg",3,3) == "def");
318 assert(substr("abcdefg",2) == "cdefg");
319 assert(substr("abcdefg",len=3) == "abc");
320 assert(substr("abcdefg",[2,4]) == "cde");
321 assert(substr("abcdefg",len=-2) == "");
322}
323test_substr();
324
325
326module test_suffix() {
327 assert(suffix("abcdefghi",0) == "");
328 assert(suffix("abcdefghi",1) == "i");
329 assert(suffix("abcdefghi",2) == "hi");
330 assert(suffix("abcdefghi",3) == "ghi");
331 assert(suffix("abcdefghi",6) == "defghi");
332 assert(suffix("abc",4) == "abc");
333}
334test_suffix();
335
336
337module test_str_find() {
338 assert(str_find("abc123def123abc","123") == 3);
339 assert(str_find("abc123def123abc","b") == 1);
340 assert(str_find("abc123def123abc","1234") == undef);
341 assert(str_find("abc","") == 0);
342 assert(str_find("abc123def123", "123", start=4) == 9);
343 assert(str_find("abc123def123abc","123",last=true) == 9);
344 assert(str_find("abc123def123abc","b",last=true) == 13);
345 assert(str_find("abc123def123abc","1234",last=true) == undef);
346 assert(str_find("abc","",last=true) == 3);
347 assert(str_find("abc123def123", "123", start=8, last=true) == 3);
348 assert(str_find("abc123def123abc","123",all=true) == [3,9]);
349 assert(str_find("abc123def123abc","b",all=true) == [1,13]);
350 assert(str_find("abc123def123abc","1234",all=true) == []);
351 assert(str_find("abc","",all=true) == [0,1,2]);
352}
353test_str_find();
354
355
356module test_format() {
357 assert(format("The value of {} is {:.14f}.", ["pi", PI]) == "The value of pi is 3.14159265358979.");
358 assert(format("The value {1:f} is known as {0}.", ["pi", PI]) == "The value 3.141593 is known as pi.");
359 assert(format("We use a very small value {1:.6g} as {0}.", ["EPSILON", EPSILON]) == "We use a very small value 1e-9 as EPSILON.");
360 assert(format("{:-5s}{:i}{:b}", ["foo", 12e3, 5]) == "foo 12000true");
361 assert(format("{:-10s}{:.3f}", ["plecostamus",27.43982]) == "plecostamus27.440");
362 assert(format("{:-10.9s}{:.3f}", ["plecostamus",27.43982]) == "plecostam 27.440");
363}
364test_format();
365
366
367/*
368module test_echofmt() {
369}
370test_echofmt();
371*/
372
373
374module test_str_pad() {
375 assert_equal(str_pad("abc",5,"x"), "abcxx");
376 assert_equal(str_pad("abc",5), "abc ");
377 assert_equal(str_pad("abc",5,"x",left=true), "xxabc");
378 assert_equal(str_pad("", 5, "x"), "xxxxx");
379 assert_equal(str_pad("", 5, "x", left=true), "xxxxx");
380}
381test_str_pad();
382
383module test_str_replace_char() {
384 assert_equal(str_replace_char("abcabc", "b", "xyz"), "axyzcaxyzc");
385 assert_equal(str_replace_char("abcabc", "b", ""), "acac");
386 assert_equal(str_replace_char("", "b", "xyz"), "");
387 assert_equal(str_replace_char("acdacd", "b", "xyz"), "acdacd");
388}
389test_str_replace_char();
390
391
392
393// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap