Skip to content

Commit fb64037

Browse files
authored
Minunit additional asserts (#37)
* added additional asserts [int_not_eq, double_between] and updated tests to use * add tests for all current minunit functions * add > and < tests for ints and doubles
1 parent 7668755 commit fb64037

11 files changed

+441
-40
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ script:
9292
- ./dist/queue
9393
- ./dist/stack
9494
- ./dist/graph
95+
- ./dist/minunit
9596
- if [ $TRAVIS_OS_NAME == linux ]; then cppcheck --error-exitcode=1 --inline-suppr --enable=warning,performance,information,style --template='{file}:{line},{severity},{id},{message}' ./src/ ; fi
9697
- gcov ./dist/*.gcno
9798

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test: CCFLAGS += -coverage
4343
test: libraries
4444
$(CC) $(STD) $(DISTDIR)/stringlib.o $(TESTDIR)/stringlib_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/strlib
4545
$(CC) $(STD) $(TESTDIR)/timing_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/timing
46+
$(CC) $(STD) $(TESTDIR)/minunit_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/minunit
4647
$(CC) $(STD) $(DISTDIR)/bitarray-lib.o $(TESTDIR)/bitarray_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/bitarray
4748
$(CC) $(STD) $(DISTDIR)/fileutils-lib.o $(TESTDIR)/fileutils_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/fileutils
4849
$(CC) $(STD) $(DISTDIR)/llist-lib.o $(TESTDIR)/linked_list_test.c $(CCFLAGS) $(COMPFLAGS) -o ./$(DISTDIR)/linkedlist

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,15 @@ int main() {
484484
* **mu_fail(message)**: Automatically fails the assertion and returns the provided message; useful for non-implemented features, etc.
485485
* **mu_assert(test, message)**: Assert that the boolean expression `test` is true, otherwise fail and print the passed `message`.
486486
* **mu_assert_int_eq(expected, result)**: Assert that the `expected` int is the same as the passed `result`.
487+
* **mu_assert_int_not_eq(expected, result)**: Assert that the `result` does not equal `expected`; not this is useful for checking comparison functions, etc.
488+
* **mu_assert_int_greater_than(val, result)**: Assert that `result` is greater than `val`.
489+
* **mu_assert_int_less_than(val, result)**: Assert that `result` is less than `val`.
490+
* **mu_assert_int_between(expected_lower, expected_upper, result)**: Assert that the `result` is between (inclusive) `expected_lower` and `expected_upper`; if upper and lower are reversed, then it is **not** between!
487491
* **mu_assert_int_in(expected, array_length, result)**: Assert that the `result` is a member of the `expected` array; `array_length` is needed to know the number of elements in the array.
488492
* **mu_assert_double_eq(expected, result)**: Assert that the double in `result` is the same as the `expected` double.
493+
* **mu_assert_double_greater_than(val, result)**: Assert that `result` is greater than `val`.
494+
* **mu_assert_double_less_than(val, result)**: Assert that `result` is less than `val`.
495+
* **mu_assert_double_between(expected_lower, expected_upper, result)**: Assert that `result` is between (inclusive) `expected_lower` and `expected_upper`; if upper and lower are reversed, then it is **not** between!
489496
* **mu_assert_string_eq(expected, result)**: Assert that the `result` string (char* or char[]) is the same as the `expected` string.
490497
* **mu_assert_null(result)**: Assert that the passed `result` pointer is `NULL`.
491498
* **mu_assert_not_null(result)**: Assert that the passed `result` pointer is not `NULL`.

src/minunit.h

+109
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,68 @@ static void (*minunit_teardown)(void) = NULL;
185185
}\
186186
)
187187

188+
#define mu_assert_int_not_eq(expected, result) MU__SAFE_BLOCK(\
189+
int minunit_tmp_e;\
190+
int minunit_tmp_r;\
191+
minunit_assert++;\
192+
minunit_tmp_e = (expected);\
193+
minunit_tmp_r = (result);\
194+
if (minunit_tmp_e == minunit_tmp_r) {\
195+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: expected different results but both were %d", __func__, __FILE__, __LINE__, minunit_tmp_e);\
196+
minunit_status = 1;\
197+
return;\
198+
} else {\
199+
printf(".");\
200+
}\
201+
)
202+
203+
#define mu_assert_int_greater_than(val, result) MU__SAFE_BLOCK(\
204+
int minunit_tmp_e;\
205+
int minunit_tmp_r;\
206+
minunit_assert++;\
207+
minunit_tmp_e = (val);\
208+
minunit_tmp_r = (result);\
209+
if (val >= minunit_tmp_r) {\
210+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %d <= %d", __func__, __FILE__, __LINE__, minunit_tmp_r, minunit_tmp_e);\
211+
minunit_status = 1;\
212+
return;\
213+
} else {\
214+
printf(".");\
215+
}\
216+
)
217+
218+
#define mu_assert_int_less_than(val, result) MU__SAFE_BLOCK(\
219+
int minunit_tmp_e;\
220+
int minunit_tmp_r;\
221+
minunit_assert++;\
222+
minunit_tmp_e = (val);\
223+
minunit_tmp_r = (result);\
224+
if (val <= minunit_tmp_r) {\
225+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %d >= %d", __func__, __FILE__, __LINE__, minunit_tmp_r, minunit_tmp_e);\
226+
minunit_status = 1;\
227+
return;\
228+
} else {\
229+
printf(".");\
230+
}\
231+
)
232+
233+
#define mu_assert_int_between(expected_lower, expected_upper, result) MU__SAFE_BLOCK(\
234+
int minunit_tmp_e;\
235+
int minunit_tmp_m;\
236+
int minunit_tmp_r;\
237+
minunit_assert++;\
238+
minunit_tmp_e = (expected_lower);\
239+
minunit_tmp_m = (expected_upper);\
240+
minunit_tmp_r = (result);\
241+
if (result < minunit_tmp_e || result > minunit_tmp_m) {\
242+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %d was not between (inclusive) %d and %d", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r, minunit_tmp_m);\
243+
minunit_status = 1;\
244+
return;\
245+
} else {\
246+
printf(".");\
247+
}\
248+
)
249+
188250
#define mu_assert_int_in(expected, array_length, result) MU__SAFE_BLOCK(\
189251
int minunit_tmp_r;\
190252
minunit_assert++;\
@@ -228,6 +290,53 @@ static void (*minunit_teardown)(void) = NULL;
228290
}\
229291
)
230292

293+
#define mu_assert_double_greater_than(val, result) MU__SAFE_BLOCK(\
294+
double minunit_tmp_e;\
295+
double minunit_tmp_r;\
296+
minunit_assert++;\
297+
minunit_tmp_e = (val);\
298+
minunit_tmp_r = (result);\
299+
if (val >= minunit_tmp_r) {\
300+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %f <= %f", __func__, __FILE__, __LINE__, minunit_tmp_r, minunit_tmp_e);\
301+
minunit_status = 1;\
302+
return;\
303+
} else {\
304+
printf(".");\
305+
}\
306+
)
307+
308+
#define mu_assert_double_less_than(val, result) MU__SAFE_BLOCK(\
309+
double minunit_tmp_e;\
310+
double minunit_tmp_r;\
311+
minunit_assert++;\
312+
minunit_tmp_e = (val);\
313+
minunit_tmp_r = (result);\
314+
if (val <= minunit_tmp_r) {\
315+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %f >= %f", __func__, __FILE__, __LINE__, minunit_tmp_r, minunit_tmp_e);\
316+
minunit_status = 1;\
317+
return;\
318+
} else {\
319+
printf(".");\
320+
}\
321+
)
322+
323+
#define mu_assert_double_between(expected_lower, expected_upper, result) MU__SAFE_BLOCK(\
324+
double minunit_tmp_e;\
325+
double minunit_tmp_m;\
326+
double minunit_tmp_r;\
327+
minunit_assert++;\
328+
minunit_tmp_e = (expected_lower);\
329+
minunit_tmp_m = (expected_upper);\
330+
minunit_tmp_r = (result);\
331+
if (result < minunit_tmp_e || result > minunit_tmp_m) {\
332+
snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, "%s failed:\n\t%s:%d: %f was not between (inclusive) %f and %f", __func__, __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r, minunit_tmp_m);\
333+
minunit_status = 1;\
334+
return;\
335+
} else {\
336+
printf(".");\
337+
}\
338+
)
339+
231340
#define mu_assert_string_eq(expected, result) MU__SAFE_BLOCK(\
232341
const char* minunit_tmp_e = expected;\
233342
const char* minunit_tmp_r = result;\

tests/doubly_linked_list_test.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void test_teardown(void) {
3737
*******************************************************************************/
3838
MU_TEST(test_default_setup) {
3939
mu_assert_int_eq(0, dll_num_elements(l));
40-
mu_assert_string_eq(NULL, (void*)dll_first_node(l));
41-
mu_assert_string_eq(NULL, (void*)dll_last_node(l));
40+
mu_assert_null(dll_first_node(l));
41+
mu_assert_null(dll_last_node(l));
4242
}
4343

4444

@@ -132,7 +132,7 @@ MU_TEST(test_insert_beginning) {
132132
/* ensure pointing correct */
133133
mu_assert_int_eq(0, *(int*)n->next->data);
134134
mu_assert_int_eq(15, *(int*)n->next->prev->data);
135-
mu_assert_string_eq(NULL, (void*)n->prev);
135+
mu_assert_null(n->prev);
136136

137137
int w = 0;
138138
n = n->next;
@@ -220,7 +220,7 @@ MU_TEST(test_insert_negative) {
220220

221221
dll_node* n = dll_last_node(l);
222222
mu_assert_int_eq(15, *(int*)n->data);
223-
mu_assert_string_eq(NULL, (void*)n->next);
223+
mu_assert_null(n->next);
224224
}
225225

226226
MU_TEST(test_insert_negative_mid) {
@@ -237,7 +237,7 @@ MU_TEST(test_insert_negative_mid) {
237237

238238
dll_node* n = dll_last_node(l)->prev;
239239
mu_assert_int_eq(15, *(int*)n->data);
240-
mu_assert_string_eq((void*)dll_last_node(l), (void*)n->next);
240+
mu_assert_pointers_eq(dll_last_node(l), n->next);
241241
}
242242

243243
MU_TEST(test_insert_error) {
@@ -462,8 +462,8 @@ MU_TEST(test_remove_only_elm) {
462462
mu_assert_int_eq(0, dll_num_elements(l));
463463
free(val);
464464

465-
mu_assert_string_eq(NULL, (void*)dll_first_node(l));
466-
mu_assert_string_eq(NULL, (void*)dll_last_node(l));
465+
mu_assert_null(dll_first_node(l));
466+
mu_assert_null(dll_last_node(l));
467467
}
468468

469469
MU_TEST(test_remove_alt) {
@@ -475,6 +475,7 @@ MU_TEST(test_remove_alt) {
475475
dll_append(l, t);
476476
}
477477
dll_remove_alt(l, 0, true); /* remember it is 0 based */
478+
mu_assert_int_eq(4, dll_num_elements(l));
478479
}
479480

480481

@@ -488,7 +489,7 @@ MU_TEST(test_remove_error_neg) {
488489
mu_assert_int_eq(5, dll_num_elements(l));
489490

490491
void* res = dll_remove(l, -5); /* remember it is 0 based so this is out of bounds */
491-
mu_assert_string_eq(NULL, res);
492+
mu_assert_null(res);
492493
}
493494

494495
/*******************************************************************************

tests/fileutils_test.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ MU_TEST(test_resolve_path_no_exist) {
8787
}
8888

8989
MU_TEST(test_resolve_path_null) {
90-
mu_assert_string_eq(NULL, fs_resolve_path(NULL));
90+
mu_assert_null(fs_resolve_path(NULL));
9191
}
9292

9393
/*******************************************************************************
@@ -378,15 +378,15 @@ MU_TEST(test_list_dir) {
378378

379379
/* test the error case */
380380
char** val = fs_list_dir("./foo", &items);
381-
mu_assert_string_eq(NULL, (void*)val);
381+
mu_assert_null(val);
382382
mu_assert_int_eq(0, items);
383383
}
384384

385385
MU_TEST(test_combine_filepath) {
386386
char* filepath = __str_snprintf("%s/test.txt", test_dir);
387387

388388
/* test error cases */
389-
mu_assert_string_eq(NULL, fs_combine_filepath(NULL, NULL));
389+
mu_assert_null(fs_combine_filepath(NULL, NULL));
390390
char* res = fs_combine_filepath(NULL, "test.txt");
391391
mu_assert_string_eq("test.txt", res);
392392
free(res);
@@ -429,18 +429,19 @@ MU_TEST(test_file_t_init) {
429429
/* haven't loaded the file, so these should be the defaults! */
430430
mu_assert_int_eq(0 , f_number_lines(f));
431431
mu_assert_string_eq(NULL , f_buffer(f));
432-
mu_assert(f_lines(f) == NULL, "Expected lines to be NULL, if was not...");
432+
mu_assert_null(f_lines(f));
433+
// mu_assert(f_lines(f) == NULL, "Expected lines to be NULL, if was not...");
433434
free(filepath);
434435
f_free(f);
435436
}
436437

437438
MU_TEST(test_file_t_init_non_file) {
438439
file_t f = f_init(test_dir);
439-
mu_assert_string_eq(NULL, (void*)f);
440+
mu_assert_null(f);
440441

441442
char* filepath = __str_snprintf("%s/test-2.txt", test_dir);
442443
f = f_init(filepath);
443-
mu_assert_string_eq(NULL, (void*)f);
444+
mu_assert_null(f);
444445
free(filepath);
445446
}
446447

@@ -506,12 +507,11 @@ MU_TEST(test_dir_t_init) {
506507
MU_TEST(test_dir_init_fail) {
507508
char* filepath = __str_snprintf("%s/test.txt", test_dir);
508509
dir_t d = d_init(filepath);
509-
mu_assert_string_eq(NULL, (void*)d);
510+
mu_assert_null(d);
510511
free(filepath);
511512
d_free(d);
512513
}
513514

514-
515515
MU_TEST(test_dir_update_list) {
516516
dir_t d = d_init(test_dir_rel);
517517
/* now that everything is updated, let us add a new file... */

tests/graph_test.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ MU_TEST(test_add_vertices) {
5555
MU_TEST(test_add_verticies_idx_error) {
5656
vertex_t v = g_vertex_add(g, __str_duplicate("we are here..."));
5757
v = g_vertex_add_alt(g, 0, NULL); // this should be an error!
58-
mu_assert_string_eq(NULL, (char*)v);
58+
mu_assert_null(v);
5959
}
6060

6161
MU_TEST(test_add_vertex_large_idx) {
@@ -89,7 +89,7 @@ MU_TEST(test_remove_vertices) {
8989

9090
/* check that something removed is clean! */
9191
v = g_vertex_remove(g, 0);
92-
mu_assert_string_eq(NULL, (char*)v);
92+
mu_assert_null(v);
9393
mu_assert_int_eq(1, g_num_vertices(g)); /* it shouldn't change the num vertices */
9494
}
9595

@@ -118,7 +118,7 @@ MU_TEST(test_updating_vertex_metadata) {
118118

119119
MU_TEST(test_get_vertex_errors) {
120120
__add_vertices(g, 5);
121-
mu_assert_string_eq(NULL, (char*)g_vertex_get(g, 10));
121+
mu_assert_null(g_vertex_get(g, 10));
122122
}
123123

124124

@@ -231,17 +231,17 @@ MU_TEST(test_updating_edge_metadata) {
231231
MU_TEST(test_edge_add_error) {
232232
__add_vertices(g, 5);
233233
edge_t e = g_edge_add(g, 0, 5, NULL); /* this should return NULL since dest is too large*/
234-
mu_assert_string_eq(NULL, (char*)e);
234+
mu_assert_null(e);
235235
e = g_edge_add(g, 6, 0, NULL);
236-
mu_assert_string_eq(NULL, (char*)e);
236+
mu_assert_null(e);
237237

238238
/* now remove a vertex and try to add an edge to it */
239239
vertex_t v = g_vertex_remove(g, 0);
240240
g_vertex_free(v);
241241
e = g_edge_add(g, 0, 1, NULL);
242-
mu_assert_string_eq(NULL, (char*)e);
242+
mu_assert_null(e);
243243
e = g_edge_add(g, 1, 0, NULL);
244-
mu_assert_string_eq(NULL, (char*)e);
244+
mu_assert_null(e);
245245
}
246246

247247
MU_TEST(test_edge_remove_error) {
@@ -252,18 +252,18 @@ MU_TEST(test_edge_remove_error) {
252252
g_edge_add(g, 3, 4, NULL);
253253

254254
edge_t e = g_edge_remove(g, 5);
255-
mu_assert_string_eq(NULL, (char*)e);
255+
mu_assert_null(e);
256256

257257
e = g_edge_remove(g, 3); /* this should be fine */
258258
g_edge_free(e);
259259
e = g_edge_remove(g, 3); /* now we should get a NULL back */
260-
mu_assert_string_eq(NULL, (char*)e);
260+
mu_assert_null(e);
261261
}
262262

263263
MU_TEST(test_edge_get_error) {
264264
__add_vertices(g, 5);
265265
g_edge_add(g, 0, 4, NULL);
266-
mu_assert_string_eq(NULL, (char*)g_edge_get(g, 2));
266+
mu_assert_null(g_edge_get(g, 2));
267267
}
268268

269269

@@ -378,8 +378,8 @@ MU_TEST(test_g_vertex_edge_error) {
378378
__add_vertices(g, 5);
379379
__add_edge(g, 0, 1, 0);
380380
vertex_t v = g_vertex_get(g, 0);
381-
mu_assert_string_eq(NULL, (char*)g_vertex_edge(v, 1));
382-
mu_assert_string_eq(NULL, (char*)g_vertex_edge(v, 17));
381+
mu_assert_null(g_vertex_edge(v, 1));
382+
mu_assert_null(g_vertex_edge(v, 17));
383383
}
384384

385385
/*******************************************************************************

tests/linked_list_test.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static void printlist(llist_t l) {
3535
*******************************************************************************/
3636
MU_TEST(test_default_setup) {
3737
mu_assert_int_eq(0, ll_num_elements(l));
38-
mu_assert_string_eq(NULL, (void*)ll_first_node(l));
38+
mu_assert_null(ll_first_node(l));
3939
}
4040

4141

@@ -254,6 +254,7 @@ MU_TEST(test_remove_alt) {
254254
ll_append(l, t);
255255
}
256256
ll_remove_alt(l, 0, true); /* remember it is 0 based */
257+
mu_assert_int_eq(4, ll_num_elements(l));
257258
}
258259

259260
MU_TEST(test_remove_error) {
@@ -266,7 +267,7 @@ MU_TEST(test_remove_error) {
266267
mu_assert_int_eq(5, ll_num_elements(l));
267268

268269
void* res = ll_remove(l, 5); /* remember it is 0 based so this is out of bounds */
269-
mu_assert_string_eq(NULL, res);
270+
mu_assert_null(res);
270271
}
271272

272273
/*******************************************************************************

0 commit comments

Comments
 (0)