Fri, 12 Oct 2012 12:00:06 +0200
fixed memory leak in ucx_map_rehash
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) { |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
8 | sstr_t s1 = sstr("1234"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
9 | sstr_t s2 = sstrn("ab", 2); |
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) { |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
20 | sstr_t s1 = sstr("1234"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
21 | sstr_t s2 = sstr(".:.:."); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
22 | sstr_t s3 = sstr("X"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
23 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
24 | sstr_t cat; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
25 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
26 | size_t len = sstrnlen(3, s1, s2, s3); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
27 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
28 | UCX_TEST_BEGIN |
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_ASSERT(len == s1.length + s2.length + s3.length, |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
31 | "sstrnlen returned wrong size"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
32 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
33 | len = s1.length + s2.length + s3.length; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
34 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
35 | cat.ptr = (char*) malloc(len + 1); |
47
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
36 | cat.ptr[len] = 0; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
37 | cat.length = len; |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
38 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
39 | sstrncat(3, cat, s1, s2, s3); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
40 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
41 | UCX_TEST_ASSERT(cat.ptr[0] == '1', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
42 | UCX_TEST_ASSERT(cat.ptr[1] == '2', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
43 | UCX_TEST_ASSERT(cat.ptr[2] == '3', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
44 | UCX_TEST_ASSERT(cat.ptr[3] == '4', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
45 | UCX_TEST_ASSERT(cat.ptr[4] == '.', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
46 | UCX_TEST_ASSERT(cat.ptr[8] == '.', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
47 | UCX_TEST_ASSERT(cat.ptr[9] == 'X', "sstrncat, wrong content"); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
48 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
49 | UCX_TEST_END |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
50 | |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
51 | free(cat.ptr); |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
52 | } |
22fb97e9f58a
sstrncat uses memcpy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
39
diff
changeset
|
53 | |
39 | 54 | UCX_TEST_IMPLEMENT(test_sstrsplit) { |
55 | ||
56 | const char *original = "this,is,a,csv,string"; | |
57 | sstr_t test = sstr("this,is,a,csv,string"); /* use copy of original here */ | |
58 | size_t n; | |
59 | sstr_t *list; | |
60 | ||
61 | UCX_TEST_BEGIN | |
62 | ||
63 | /* Nullpointer check */ | |
64 | n = 0; | |
65 | UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL, | |
66 | "empty delimiter must return NULL"); | |
67 | ||
68 | /* no delimiter occurence (ndo) */ | |
69 | n = 0; | |
70 | list = sstrsplit(test, ST("z"), &n); | |
71 | UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); | |
72 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " | |
73 | "original string shall be returned as single list element"); | |
74 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
75 | "ndo, original has been modified"); | |
76 | free(list); | |
77 | ||
78 | /* partially matching delimiter (pmd) */ | |
79 | n = 0; | |
80 | list = sstrsplit(test, ST("stringbuilder"), &n); | |
81 | UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); | |
82 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " | |
83 | "original string shall be returned as single list element"); | |
84 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
85 | "pmd, original has been modified"); | |
86 | free(list); | |
87 | ||
88 | /* matching single-char delimiter (mscd) */ | |
89 | n = 0; | |
90 | list = sstrsplit(test, ST(","), &n); | |
91 | UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); | |
92 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); | |
93 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); | |
94 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); | |
95 | UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); | |
96 | UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch"); | |
97 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
98 | "mscd, original has been modified"); | |
99 | free(list); | |
100 | ||
101 | /* matching multi-char delimiter (mmcd) */ | |
102 | n = 0; | |
103 | list = sstrsplit(test, ST("is"), &n); | |
104 | UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); | |
105 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); | |
106 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); | |
107 | UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, | |
108 | "mmcd, item 2 mismatch"); | |
109 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
110 | "mmcd, original has been modified"); | |
111 | free(list); | |
112 | ||
113 | /* bounded list using single-char delimiter (blsc) */ | |
114 | n = 3; | |
115 | list = sstrsplit(test, ST(","), &n); | |
116 | UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); | |
117 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); | |
118 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); | |
119 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, | |
120 | "blsc, item 2 mismatch"); | |
121 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
122 | "blsc, original has been modified"); | |
123 | free(list); | |
124 | ||
125 | /* bounded list using multi-char delimiter (blmc) */ | |
126 | n = 2; | |
127 | list = sstrsplit(test, ST("is"), &n); | |
128 | UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); | |
129 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); | |
130 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
131 | "blmc, item 1 mismatch"); | |
132 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
133 | "blmc, original has been modified"); | |
134 | free(list); | |
135 | ||
136 | /* start with delimiter (swd) */ | |
137 | n = 0; | |
138 | list = sstrsplit(test, ST("this"), &n); | |
139 | UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); | |
140 | UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); | |
141 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
142 | "swd, second item corrupt"); | |
143 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
144 | "swd, original has been modified"); | |
145 | free(list); | |
146 | ||
147 | /* end with delimiter (ewd) */ | |
148 | n = 0; | |
149 | list = sstrsplit(test, ST("string"), &n); | |
150 | UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); | |
151 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, | |
152 | "swd, first item corrupt"); | |
153 | UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); | |
154 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
155 | "ewd, original has been modified"); | |
156 | free(list); | |
157 | ||
158 | /* exact match (exm) */ | |
159 | n = 0; | |
160 | list = sstrsplit(test, ST("this,is,a,csv,string"), &n); | |
161 | UCX_TEST_ASSERT(n == 1, "exm, list length must be 1"); | |
162 | UCX_TEST_ASSERT(list[0].length == 0, "exm, single item must be empty"); | |
163 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
164 | "exm, original has been modified"); | |
165 | free(list); | |
166 | ||
167 | /* substring (subs) */ | |
168 | n = 0; | |
169 | list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n); | |
170 | UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); | |
171 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, | |
172 | "subs, single item must be the original string"); | |
173 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
174 | "subs, original has been modified"); | |
175 | free(list); | |
176 | ||
177 | UCX_TEST_END | |
178 | } |