Skip to content

Commit 275fa2f

Browse files
committed
Added checks for memory allocation failures in tests.
1 parent 4e85435 commit 275fa2f

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

str_test.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void test_str_dup(void)
3636
{
3737
str s = str_null;
3838

39-
str_cpy(&s, str_lit("ZZZ"));
39+
assert(str_cpy(&s, str_lit("ZZZ")) == 0);
4040

4141
assert(str_len(s) == 3);
4242
assert(!str_is_ref(s));
@@ -53,7 +53,7 @@ void test_str_clear(void)
5353
{
5454
str s = str_null;
5555

56-
str_cpy(&s, str_lit("ZZZ"));
56+
assert(str_cpy(&s, str_lit("ZZZ")) == 0);
5757

5858
assert(str_len(s) == 3);
5959
assert(str_is_owner(s));
@@ -72,7 +72,7 @@ void test_str_move(void)
7272
{
7373
str s1 = str_null;
7474

75-
str_cpy(&s1, str_lit("ZZZ"));
75+
assert(str_cpy(&s1, str_lit("ZZZ")) == 0);
7676

7777
str s2 = str_move(&s1);
7878

@@ -164,13 +164,13 @@ void test_str_cat(void)
164164
{
165165
str s = str_null;
166166

167-
str_cat(&s, str_lit("AAA"), str_lit("BBB"), str_lit("CCC"));
167+
assert(str_cat(&s, str_lit("AAA"), str_lit("BBB"), str_lit("CCC")) == 0);
168168

169169
assert(str_eq(s, str_lit("AAABBBCCC")));
170170
assert(str_is_owner(s));
171171
assert(*str_end(s) == 0);
172172

173-
str_cat(&s, str_null, str_null, str_null); // this simply clears the target string
173+
assert(str_cat(&s, str_null, str_null, str_null) == 0); // this simply clears the target string
174174

175175
assert(str_is_empty(s));
176176
assert(str_is_ref(s));
@@ -183,37 +183,37 @@ void test_str_join(void)
183183
{
184184
str s = str_null;
185185

186-
str_join(&s, str_lit("_"), str_lit("AAA"), str_lit("BBB"), str_lit("CCC"));
186+
assert(str_join(&s, str_lit("_"), str_lit("AAA"), str_lit("BBB"), str_lit("CCC")) == 0);
187187

188188
assert(str_eq(s, str_lit("AAA_BBB_CCC")));
189189
assert(str_is_owner(s));
190190
assert(*str_end(s) == 0);
191191

192-
str_join(&s, str_lit("_"), str_null, str_lit("BBB"), str_lit("CCC"));
192+
assert(str_join(&s, str_lit("_"), str_null, str_lit("BBB"), str_lit("CCC")) == 0);
193193

194194
assert(str_eq(s, str_lit("_BBB_CCC")));
195195
assert(str_is_owner(s));
196196
assert(*str_end(s) == 0);
197197

198-
str_join(&s, str_lit("_"), str_lit("AAA"), str_null, str_lit("CCC"));
198+
assert(str_join(&s, str_lit("_"), str_lit("AAA"), str_null, str_lit("CCC")) == 0);
199199

200200
assert(str_eq(s, str_lit("AAA__CCC")));
201201
assert(str_is_owner(s));
202202
assert(*str_end(s) == 0);
203203

204-
str_join(&s, str_lit("_"), str_lit("AAA"), str_lit("BBB"), str_null);
204+
assert(str_join(&s, str_lit("_"), str_lit("AAA"), str_lit("BBB"), str_null) == 0);
205205

206206
assert(str_eq(s, str_lit("AAA_BBB_")));
207207
assert(str_is_owner(s));
208208
assert(*str_end(s) == 0);
209209

210-
str_join(&s, str_lit("_"), str_null, str_null, str_null);
210+
assert(str_join(&s, str_lit("_"), str_null, str_null, str_null) == 0);
211211

212212
assert(str_eq(s, str_lit("__")));
213213
assert(str_is_owner(s));
214214
assert(*str_end(s) == 0);
215215

216-
str_join(&s, str_null); // this simply clears the target string
216+
assert(str_join(&s, str_null) == 0); // this simply clears the target string
217217

218218
assert(str_is_empty(s));
219219
assert(str_is_ref(s));
@@ -226,8 +226,8 @@ void test_composition(void)
226226
{
227227
str s = str_lit(", ");
228228

229-
str_join(&s, s, str_lit("Here"), str_lit("there"), str_lit("and everywhere"));
230-
str_cat(&s, s, str_lit("..."));
229+
assert(str_join(&s, s, str_lit("Here"), str_lit("there"), str_lit("and everywhere")) == 0);
230+
assert(str_cat(&s, s, str_lit("...")) == 0);
231231

232232
assert(str_eq(s, str_lit("Here, there, and everywhere...")));
233233
assert(str_is_owner(s));
@@ -416,7 +416,7 @@ void test_cat_large_range_to_fd(void)
416416
char buff[100];
417417

418418
for(unsigned i = 0; i < n; i++)
419-
str_cpy(&src[i], str_ref_chars(buff, sprintf(buff, "%u\n", i)));
419+
assert(str_cpy(&src[i], str_ref_chars(buff, sprintf(buff, "%u\n", i))) == 0);
420420

421421
// write to file
422422
FILE* const tmp = tmpfile();
@@ -516,7 +516,7 @@ void test_join_large_range_to_fd(void)
516516
char buff[100];
517517

518518
for(unsigned i = 0; i < n; i++)
519-
str_cpy(&src[i], str_ref_chars(buff, sprintf(buff, "%u", i)));
519+
assert(str_cpy(&src[i], str_ref_chars(buff, sprintf(buff, "%u", i))) == 0);
520520

521521
// write to file
522522
FILE* const tmp = tmpfile();
@@ -631,7 +631,7 @@ void test_from_file(void)
631631
{
632632
str fname = str_null;
633633

634-
str_cat(&fname, str_lit("tmp_"), str_ref_chars(__func__, sizeof(__func__) - 1));
634+
assert(str_cat(&fname, str_lit("tmp_"), str_ref_chars(__func__, sizeof(__func__) - 1)) == 0);
635635

636636
FILE* const stream = fopen(str_ptr(fname), "w");
637637

0 commit comments

Comments
 (0)