test/string_tests.c

Thu, 04 Oct 2012 16:03:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Oct 2012 16:03:18 +0200
changeset 42
ff3dd1ee7dee
parent 39
bf8ab7bb74ff
child 47
22fb97e9f58a
permissions
-rw-r--r--

(broken-commit) - added load and store functions, tests failing

some evil crash happens when executing the test - remove the strcmp calls in the test case for the store and load function and everything "works"

the error must be somewhere else - maybe something that should not be freed is freed during the test

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

mercurial