v / vlib / strings
Raw file | 107 loc (96 sloc) | 2.35 KB | Latest commit hash 017ace6ea
1import strings
2
3fn test_repeat() {
4 assert strings.repeat(`x`, 10) == 'xxxxxxxxxx'
5 assert strings.repeat(`a`, 1) == 'a'
6 assert strings.repeat(`a`, 0) == ''
7}
8
9fn test_repeat_string() {
10 assert strings.repeat_string('abc', 3) == 'abcabcabc'
11 assert strings.repeat_string('abc', 1) == 'abc'
12 assert strings.repeat_string('abc', 0) == ''
13 assert strings.repeat_string('', 200) == ''
14}
15
16const test_rune_and_byte = [
17 'xxx[ok1]xxx',
18 'xxx[[ok2]okok]',
19 'xxx[ok3[[[ok]okok]]]',
20 'yyy[ok4]',
21 '[]',
22 ']',
23 '[',
24 'yyy[ok5][]zzz',
25 'yyy[xxx',
26 'xxx[xxx
27 xxx]',
28]
29
30const test_strings = [
31 'xxx/*ok1*/xxx',
32 'xxx/*/*ok2*/okok*/',
33 'xxx/*ok3/*/*/*ok*/okok*/*/*/',
34 'yyy/*ok4*/',
35 '/**/',
36 '*/',
37 '/*',
38 'yyy/*ok5*//**/zzz',
39 'yyy/*xxx',
40 'xxx/*xxx
41 xxx*/xxx',
42]
43
44const expected_rune_and_byte_outputs = [
45 'ok1',
46 '[ok2]okok',
47 'ok3[[[ok]okok]]',
48 'ok4',
49 '',
50 '',
51 '',
52 'ok5',
53 '',
54 'xxx
55 xxx',
56]
57
58const expected_string_outputs = [
59 'ok1',
60 '/*ok2*/okok',
61 'ok3/*/*/*ok*/okok*/*/',
62 'ok4',
63 '',
64 '',
65 '',
66 'ok5',
67 '',
68 'xxx
69 xxx',
70]
71
72fn test_find_between_pair_family() {
73 assert strings.find_between_pair_rune('xx♡ok❦yy', `♡`, `❦`) == 'ok'
74 assert strings.find_between_pair_u8('xx{ok}yy', `{`, `}`) == 'ok'
75 assert strings.find_between_pair_string('xx/*ok*/yy', '/*', '*/') == 'ok'
76 assert strings.find_between_pair_u8('xx{ok}yy', `{`, `}`) == 'ok'
77 assert strings.find_between_pair_string('xxxxokyyyy', 'xxx', 'yyy') == 'xok'
78
79 for i, tstr in test_rune_and_byte {
80 e1 := strings.find_between_pair_rune(tstr, `[`, `]`)
81 e2 := expected_rune_and_byte_outputs[i]
82 assert '${e1}' == '${e2}'
83 }
84
85 for i, tstr in test_rune_and_byte {
86 e1 := strings.find_between_pair_u8(tstr, `[`, `]`)
87 e2 := expected_rune_and_byte_outputs[i]
88 assert '${e1}' == '${e2}'
89 }
90
91 for i, tstr in test_strings {
92 e1 := strings.find_between_pair_string(tstr, '/*', '*/')
93 e2 := expected_string_outputs[i]
94 assert '${e1}' == '${e2}'
95 }
96}
97
98fn test_split_capital() {
99 assert strings.split_capital('') == []
100 assert strings.split_capital('abc') == ['abc']
101 assert strings.split_capital('X') == ['X']
102 assert strings.split_capital('XX') == ['X', 'X']
103 assert strings.split_capital('XYZ') == ['X', 'Y', 'Z']
104 assert strings.split_capital('JohnWilliams') == ['John', 'Williams']
105 assert strings.split_capital('JDStar') == ['J', 'D', 'Star']
106 assert strings.split_capital('cpDumpRotarySpring') == ['cp', 'Dump', 'Rotary', 'Spring']
107}