Thu, 28 Feb 2013 08:15:15 +0100
regained support for microsoft c++ compiler
39 | 1 | /* |
2 | * | |
3 | */ | |
4 | ||
5 | #include "string_tests.h" | |
6 | ||
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
7 | UCX_TEST_IMPLEMENT(test_sstr) { |
74
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
8 | sstr_t s1 = sstr((char*)"1234"); |
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
9 | sstr_t s2 = sstrn((char*)"ab", 2); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
10 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
11 | UCX_TEST_BEGIN |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
12 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
13 | UCX_TEST_ASSERT(s1.length == 4, "s1 length must be 4"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
14 | UCX_TEST_ASSERT(s2.length == 2, "s2 length must be 2"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
15 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
16 | UCX_TEST_END |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
17 | } |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
18 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
19 | UCX_TEST_IMPLEMENT(test_sstr_len_cat) { |
74
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
20 | sstr_t s1 = S("1234"); |
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
21 | sstr_t s2 = S(".:.:."); |
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
22 | sstr_t s3 = S("X"); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
23 | |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
24 | size_t len = sstrnlen(3, s1, s2, s3); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
25 | sstr_t cat; |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
26 | cat.ptr = (char*) malloc(16); |
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
27 | cat.length = 16; |
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
28 | cat = sstrncat(3, cat, s1, s2, s3); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
29 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
30 | UCX_TEST_BEGIN |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
31 | |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
32 | UCX_TEST_ASSERT(len == 10, "sstrnlen returned wrong size"); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
33 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
34 | UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
35 | UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
36 | UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
37 | UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
38 | UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
39 | UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
40 | UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content"); |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
41 | UCX_TEST_ASSERT(cat.length == 10, "sstrncat, wrong length"); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
42 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
43 | UCX_TEST_END |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
44 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
45 | free(cat.ptr); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
46 | } |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
47 | |
39 | 48 | UCX_TEST_IMPLEMENT(test_sstrsplit) { |
49 | ||
50 | const char *original = "this,is,a,csv,string"; | |
74
dc8bade7f2a3
made the code work with g++ and without warnings
Mike Becker <universe@uap-core.de>
parents:
71
diff
changeset
|
51 | sstr_t test = S("this,is,a,csv,string"); /* use copy of original here */ |
39 | 52 | size_t n; |
53 | sstr_t *list; | |
54 | ||
55 | UCX_TEST_BEGIN | |
56 | ||
57 | /* Nullpointer check */ | |
58 | n = 0; | |
59 | UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL, | |
60 | "empty delimiter must return NULL"); | |
61 | ||
62 | /* no delimiter occurence (ndo) */ | |
63 | n = 0; | |
64 | list = sstrsplit(test, ST("z"), &n); | |
65 | UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); | |
66 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " | |
67 | "original string shall be returned as single list element"); | |
68 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
69 | "ndo, original has been modified"); | |
70 | free(list); | |
71 | ||
72 | /* partially matching delimiter (pmd) */ | |
73 | n = 0; | |
74 | list = sstrsplit(test, ST("stringbuilder"), &n); | |
75 | UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); | |
76 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " | |
77 | "original string shall be returned as single list element"); | |
78 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
79 | "pmd, original has been modified"); | |
80 | free(list); | |
81 | ||
82 | /* matching single-char delimiter (mscd) */ | |
83 | n = 0; | |
84 | list = sstrsplit(test, ST(","), &n); | |
85 | UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); | |
86 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); | |
87 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); | |
88 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); | |
89 | UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); | |
90 | UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch"); | |
91 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
92 | "mscd, original has been modified"); | |
93 | free(list); | |
94 | ||
95 | /* matching multi-char delimiter (mmcd) */ | |
96 | n = 0; | |
97 | list = sstrsplit(test, ST("is"), &n); | |
98 | UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); | |
99 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); | |
100 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); | |
101 | UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, | |
102 | "mmcd, item 2 mismatch"); | |
103 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
104 | "mmcd, original has been modified"); | |
105 | free(list); | |
106 | ||
107 | /* bounded list using single-char delimiter (blsc) */ | |
108 | n = 3; | |
109 | list = sstrsplit(test, ST(","), &n); | |
110 | UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); | |
111 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); | |
112 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); | |
113 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, | |
114 | "blsc, item 2 mismatch"); | |
115 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
116 | "blsc, original has been modified"); | |
117 | free(list); | |
118 | ||
119 | /* bounded list using multi-char delimiter (blmc) */ | |
120 | n = 2; | |
121 | list = sstrsplit(test, ST("is"), &n); | |
122 | UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); | |
123 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); | |
124 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
125 | "blmc, item 1 mismatch"); | |
126 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
127 | "blmc, original has been modified"); | |
128 | free(list); | |
129 | ||
130 | /* start with delimiter (swd) */ | |
131 | n = 0; | |
132 | list = sstrsplit(test, ST("this"), &n); | |
133 | UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); | |
134 | UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); | |
135 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
136 | "swd, second item corrupt"); | |
137 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
138 | "swd, original has been modified"); | |
139 | free(list); | |
140 | ||
141 | /* end with delimiter (ewd) */ | |
142 | n = 0; | |
143 | list = sstrsplit(test, ST("string"), &n); | |
144 | UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); | |
145 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, | |
146 | "swd, first item corrupt"); | |
147 | UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); | |
148 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
149 | "ewd, original has been modified"); | |
150 | free(list); | |
151 | ||
152 | /* exact match (exm) */ | |
153 | n = 0; | |
154 | list = sstrsplit(test, ST("this,is,a,csv,string"), &n); | |
71
303dabadff1c
made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
155 | UCX_TEST_ASSERT(n == 0, "exm, list length must be 0"); |
303dabadff1c
made the code work with g++ without errors (but warnings)
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
156 | UCX_TEST_ASSERT(list == NULL, "exm, list must be NULL"); |
39 | 157 | free(list); |
158 | ||
159 | /* substring (subs) */ | |
160 | n = 0; | |
161 | list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n); | |
162 | UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); | |
163 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, | |
164 | "subs, single item must be the original string"); | |
165 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
166 | "subs, original has been modified"); | |
167 | free(list); | |
168 | ||
169 | UCX_TEST_END | |
170 | } | |
97
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
171 | |
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
172 | UCX_TEST_IMPLEMENT(test_sstrtrim) { |
100
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
173 | sstr_t test = sstrtrim(sstr((char*)" ein test ")); |
e0ec80179a5d
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
Mike Becker <universe@uap-core.de>
parents:
98
diff
changeset
|
174 | sstr_t empty = sstrtrim(sstr((char*)"")); |
97
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
175 | UCX_TEST_BEGIN |
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
176 | UCX_TEST_ASSERT(strncmp(test.ptr, "ein test", test.length) == 0, "failed"); |
98
0a752853f792
fixed sstrtrim for empty strings
Mike Becker <universe@uap-core.de>
parents:
97
diff
changeset
|
177 | UCX_TEST_ASSERT(empty.length == 0, "empty string failed"); |
97
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
178 | UCX_TEST_END |
499e1b465d77
fixed execution order of tests + added test for sstrtrim
Mike Becker <universe@uap-core.de>
parents:
74
diff
changeset
|
179 | } |