tests/test_string.c

changeset 1304
57e062a4bb05
parent 1297
0811fb9a8dba
equal deleted inserted replaced
1303:4022e403de60 1304:57e062a4bb05
91 CX_TEST_ASSERT(dup.length == str.length); 91 CX_TEST_ASSERT(dup.length == str.length);
92 CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te")); 92 CX_TEST_ASSERT(0 == strcmp(dup.ptr, "te"));
93 ASSERT_ZERO_TERMINATED(dup); 93 ASSERT_ZERO_TERMINATED(dup);
94 } 94 }
95 cx_strfree(&dup); 95 cx_strfree(&dup);
96 }
97
98 CX_TEST(test_strcpy) {
99 CxTestingAllocator talloc;
100 cx_testing_allocator_init(&talloc);
101 const CxAllocator *alloc = &talloc.base;
102 cxstring str = CX_STR("test string");
103 str.length = 8; // test with a non-zero-terminated source
104 cxmutstr dup;
105 CX_TEST_DO {
106 // copy into a smaller string
107 dup = cx_strdup_a(alloc, CX_STR("hello"));
108 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
109 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
110 ASSERT_ZERO_TERMINATED(dup);
111 cx_strfree_a(alloc, &dup);
112
113 // copy into a larger string
114 dup = cx_strdup_a(alloc, CX_STR("hello, world!"));
115 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
116 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
117 ASSERT_ZERO_TERMINATED(dup);
118 cx_strfree_a(alloc, &dup);
119
120 // copy into an equal-length string
121 dup = cx_strdup_a(alloc, CX_STR("testing!"));
122 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
123 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
124 ASSERT_ZERO_TERMINATED(dup);
125 cx_strfree_a(alloc, &dup);
126
127 // copy into a NULL-string
128 dup.ptr = NULL;
129 CX_TEST_ASSERT(0 == cx_strcpy_a(alloc, &dup, str));
130 CX_TEST_ASSERT(0 == cx_strcmp(cx_strcast(dup), CX_STR("test str")));
131 ASSERT_ZERO_TERMINATED(dup);
132 cx_strfree_a(alloc, &dup);
133 }
134 cx_testing_allocator_destroy(&talloc);
96 } 135 }
97 136
98 CX_TEST(test_strlen) { 137 CX_TEST(test_strlen) {
99 cxstring s1 = CX_STR("1234"); 138 cxstring s1 = CX_STR("1234");
100 cxstring s2 = CX_STR(".:.:."); 139 cxstring s2 = CX_STR(".:.:.");
1261 1300
1262 cx_test_register(suite, test_string_construct); 1301 cx_test_register(suite, test_string_construct);
1263 cx_test_register(suite, test_strfree); 1302 cx_test_register(suite, test_strfree);
1264 cx_test_register(suite, test_strdup); 1303 cx_test_register(suite, test_strdup);
1265 cx_test_register(suite, test_strdup_shortened); 1304 cx_test_register(suite, test_strdup_shortened);
1305 cx_test_register(suite, test_strcpy);
1266 cx_test_register(suite, test_strlen); 1306 cx_test_register(suite, test_strlen);
1267 cx_test_register(suite, test_strsubs); 1307 cx_test_register(suite, test_strsubs);
1268 cx_test_register(suite, test_strchr); 1308 cx_test_register(suite, test_strchr);
1269 cx_test_register(suite, test_strrchr); 1309 cx_test_register(suite, test_strrchr);
1270 cx_test_register(suite, test_strstr); 1310 cx_test_register(suite, test_strstr);

mercurial