Tue, 02 Oct 2012 13:43:17 +0200
added sstrsplit function
39 | 1 | /* |
2 | * | |
3 | */ | |
4 | ||
5 | #include "string_tests.h" | |
6 | ||
7 | UCX_TEST_IMPLEMENT(test_sstrsplit) { | |
8 | ||
9 | const char *original = "this,is,a,csv,string"; | |
10 | sstr_t test = sstr("this,is,a,csv,string"); /* use copy of original here */ | |
11 | size_t n; | |
12 | sstr_t *list; | |
13 | ||
14 | UCX_TEST_BEGIN | |
15 | ||
16 | /* Nullpointer check */ | |
17 | n = 0; | |
18 | UCX_TEST_ASSERT(sstrsplit(test, ST(""), &n) == NULL, | |
19 | "empty delimiter must return NULL"); | |
20 | ||
21 | /* no delimiter occurence (ndo) */ | |
22 | n = 0; | |
23 | list = sstrsplit(test, ST("z"), &n); | |
24 | UCX_TEST_ASSERT(n == 1, "ndo, list length must be 1"); | |
25 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "ndo, " | |
26 | "original string shall be returned as single list element"); | |
27 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
28 | "ndo, original has been modified"); | |
29 | free(list); | |
30 | ||
31 | /* partially matching delimiter (pmd) */ | |
32 | n = 0; | |
33 | list = sstrsplit(test, ST("stringbuilder"), &n); | |
34 | UCX_TEST_ASSERT(n == 1, "pmd, list length must be 1"); | |
35 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, "pmd, " | |
36 | "original string shall be returned as single list element"); | |
37 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
38 | "pmd, original has been modified"); | |
39 | free(list); | |
40 | ||
41 | /* matching single-char delimiter (mscd) */ | |
42 | n = 0; | |
43 | list = sstrsplit(test, ST(","), &n); | |
44 | UCX_TEST_ASSERT(n == 5, "mscd, list length must be 5"); | |
45 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "mscd, item 0 mismatch"); | |
46 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "mscd, item 1 mismatch"); | |
47 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a") == 0, "mscd, item 2 mismatch"); | |
48 | UCX_TEST_ASSERT(strcmp(list[3].ptr, "csv") == 0, "mscd, item 3 mismatch"); | |
49 | UCX_TEST_ASSERT(strcmp(list[4].ptr, "string")==0, "mscd, item 4 mismatch"); | |
50 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
51 | "mscd, original has been modified"); | |
52 | free(list); | |
53 | ||
54 | /* matching multi-char delimiter (mmcd) */ | |
55 | n = 0; | |
56 | list = sstrsplit(test, ST("is"), &n); | |
57 | UCX_TEST_ASSERT(n == 3, "mscd, list length must be 3"); | |
58 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "mmcd, item 0 mismatch"); | |
59 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",") == 0, "mmcd, item 1 mismatch"); | |
60 | UCX_TEST_ASSERT(strcmp(list[2].ptr, ",a,csv,string") == 0, | |
61 | "mmcd, item 2 mismatch"); | |
62 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
63 | "mmcd, original has been modified"); | |
64 | free(list); | |
65 | ||
66 | /* bounded list using single-char delimiter (blsc) */ | |
67 | n = 3; | |
68 | list = sstrsplit(test, ST(","), &n); | |
69 | UCX_TEST_ASSERT(n == 3, "blsc, list length must be 3"); | |
70 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this") == 0, "blsc, item 0 mismatch"); | |
71 | UCX_TEST_ASSERT(strcmp(list[1].ptr, "is") == 0, "blsc, item 1 mismatch"); | |
72 | UCX_TEST_ASSERT(strcmp(list[2].ptr, "a,csv,string") == 0, | |
73 | "blsc, item 2 mismatch"); | |
74 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
75 | "blsc, original has been modified"); | |
76 | free(list); | |
77 | ||
78 | /* bounded list using multi-char delimiter (blmc) */ | |
79 | n = 2; | |
80 | list = sstrsplit(test, ST("is"), &n); | |
81 | UCX_TEST_ASSERT(n == 2, "blmc, list length must be 2"); | |
82 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "th") == 0, "blmc, item 0 mismatch"); | |
83 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
84 | "blmc, item 1 mismatch"); | |
85 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
86 | "blmc, original has been modified"); | |
87 | free(list); | |
88 | ||
89 | /* start with delimiter (swd) */ | |
90 | n = 0; | |
91 | list = sstrsplit(test, ST("this"), &n); | |
92 | UCX_TEST_ASSERT(n == 2, "swd, list length must be 2"); | |
93 | UCX_TEST_ASSERT(list[0].length == 0, "swd, first item must be empty"); | |
94 | UCX_TEST_ASSERT(strcmp(list[1].ptr, ",is,a,csv,string") == 0, | |
95 | "swd, second item corrupt"); | |
96 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
97 | "swd, original has been modified"); | |
98 | free(list); | |
99 | ||
100 | /* end with delimiter (ewd) */ | |
101 | n = 0; | |
102 | list = sstrsplit(test, ST("string"), &n); | |
103 | UCX_TEST_ASSERT(n == 2, "ewd, list length must be 2"); | |
104 | UCX_TEST_ASSERT(strcmp(list[0].ptr, "this,is,a,csv,") == 0, | |
105 | "swd, first item corrupt"); | |
106 | UCX_TEST_ASSERT(list[1].length == 0, "ewd, second item must be empty"); | |
107 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
108 | "ewd, original has been modified"); | |
109 | free(list); | |
110 | ||
111 | /* exact match (exm) */ | |
112 | n = 0; | |
113 | list = sstrsplit(test, ST("this,is,a,csv,string"), &n); | |
114 | UCX_TEST_ASSERT(n == 1, "exm, list length must be 1"); | |
115 | UCX_TEST_ASSERT(list[0].length == 0, "exm, single item must be empty"); | |
116 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
117 | "exm, original has been modified"); | |
118 | free(list); | |
119 | ||
120 | /* substring (subs) */ | |
121 | n = 0; | |
122 | list = sstrsplit(test, ST("this,is,a,csv,string,with,extension"), &n); | |
123 | UCX_TEST_ASSERT(n == 1, "subs, list length must be 1"); | |
124 | UCX_TEST_ASSERT(strcmp(list[0].ptr, original) == 0, | |
125 | "subs, single item must be the original string"); | |
126 | UCX_TEST_ASSERT(strcmp(test.ptr, original) == 0, | |
127 | "subs, original has been modified"); | |
128 | free(list); | |
129 | ||
130 | UCX_TEST_END | |
131 | } |