diff --git a/README.md b/README.md index 29a680a..2e8d13c 100644 --- a/README.md +++ b/README.md @@ -9,30 +9,32 @@ This project provides a collection of utility libraries to help reduce the need If there are other commonly used code or data-structures that should be added, please add a feature request! ##### Table of Contents: -* [stringlib](#stringlib) -* [fileutils](#fileutils) +* [stringlib](#stringlib) - C-string utilities +* [fileutils](#fileutils) - File system utilities * [bitarray](#bitarray) * [linked list](#linkedlist) * [doubly linked list](#doublylinkedlist) * [stack](#stack) * [queue](#queue) * [graph](#graph) -* [timing-c](#timing-c) +* [timing-c](#timing-c) - Code timing utility +* [minunit](#minunit) - Unit testing ##### Recommended External Libraries * [set](https://github.com/barrust/set) * [hashmap](https://github.com/barrust/hashmap) +* [bloom filter](https://github.com/barrust/bloom) ##### Unit tests -Unit tests are provided using the [minunit](https://github.com/siu/minunit) library. Each function is, **hopefully**, fully covered. Any help in getting as close to 100% coverage would be much appreciated! +Unit tests are provided using the [minunit](#minunit) library. Each function is, **hopefully**, fully covered. Any help in getting as close to 100% coverage would be much appreciated! -To run the unittest suite, simply compile the test files using the provided `Makefile` with the command `make test`. Then you can execute the tests using the executables `./dist/bitarray`, `./dist/strlib`, `./dist/fileutils`, `./dist/graph`, or `./dist/timing`. +To run the unit-test suite, simply compile the test files using the provided `Makefile` with the command `make test`. Then you can execute the tests using the executables `./dist/bitarray`, `./dist/strlib`, `./dist/fileutils`, `./dist/graph`, `./dist/llist`, `./dist/dllist`, `./dist/stack`, `./dist/queue`, or `./dist/timing`. #### Issues -If an unexpected outcome occurs, please submit an issue on github. Please also provide a ***minimal code example*** that encapsulates the error. +If an unexpected outcome occurs, please submit an [issue on github](https://github.com/barrust/c-utils/issues). Please also provide a ***minimal code example*** that encapsulates the error. A great [issue](https://github.com/barrust/c-utils/issues) would provide the following: > s_remove_unwanted_chars shows duplicate entries after removal. @@ -428,3 +430,64 @@ char* output = format_time_diff(&t); printf("pretty output: %s\n", output); free(output); ``` + +## minunit + +This header utility is a testing framework for C programs. It is a fork of [siu/mununit](https://github.com/siu/minunit) that adds several assertions that are not in the base library. License ([MIT](https://github.com/siu/minunit/blob/master/MIT-LICENSE.txt)) information is contained in the header file. + +### Compiler Flags + +***NONE*** - There are no needed compiler flags for the `minunit.h` testing framework. + +### Usage + +For full examples, please view the tests in the `./test` folder. A quick run down of setting up the tests is provided below along with a quick set of function documentation. + +``` c +#include +#include +#include + +int arr[25]; + +void test_setup(void) { + for (int i = 0; i < 25; ++i) + arr[i] = i; +} + +void test_teardown(void) { + // no teardown required +} + +MU_TEST(test_simple) { + mu_assert_int_eq(0, arr[0]); +} + +// Set up the test suite by configuring and stating which tests should be run +MU_TEST_SUITE(test_suite) { + MU_SUITE_CONFIGURE(&test_setup, &test_teardown); + + MU_RUN_TEST(test_simple); +} + +int main() { + MU_RUN_SUITE(test_suite); + MU_REPORT(); + printf("Number failed tests: %d\n", minunit_fail); + return minunit_fail; +} +``` + +### Documentation + +* **mu_check(test)**: Checks to verify that the passed boolean expression test is `true`; fails otherwise. +* **mu_fail(message)**: Automatically fails the assertion and returns the provided message; useful for non-implemented features, etc. +* **mu_assert(test, message)**: Assert that the boolean expression `test` is true, otherwise fail and print the passed `message`. +* **mu_assert_int_eq(expected, result)**: Assert that the `expected` int is the same as the passed `result`. +* **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. +* **mu_assert_double_eq(expected, result)**: Assert that the double in `result` is the same as the `expected` double. +* **mu_assert_string_eq(expected, result)**: Assert that the `result` string (char* or char[]) is the same as the `expected` string. +* **mu_assert_null(result)**: Assert that the passed `result` pointer is `NULL`. +* **mu_assert_not_null(result)**: Assert that the passed `result` pointer is not `NULL`. +* **mu_assert_pointers_eq(pointer1, pointer2)**: Assert that `pointer1` and `pointer2` point to the same memory location. +* **mu_assert_pointers_not_eq(pointer1, pointer2)**: Assert that `pointer1` and `pointer2` do not point to the same memory location. diff --git a/codecov.yml b/codecov.yml index 5a35eb6..99eed85 100644 --- a/codecov.yml +++ b/codecov.yml @@ -34,3 +34,4 @@ comment: ignore: - "./tests/" - "./examples/" + - "./src/minunit.h" diff --git a/src/bitarray.c b/src/bitarray.c index 54bb7a5..f461a79 100644 --- a/src/bitarray.c +++ b/src/bitarray.c @@ -1,11 +1,14 @@ - +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2019 +*******************************************************************************/ #include #include #include #include "bitarray.h" - #define CHECK_BIT(A, k) (A[((k) / 8)] & (1 << ((k) % 8))) #define SET_BIT(A,k) (A[((k) / 8)] |= (1 << ((k) % 8))) #define CLEAR_BIT(A,k) (A[((k) / 8)] &= ~(1 << ((k) % 8))) diff --git a/src/dllist.c b/src/dllist.c index 355b582..d15fc7f 100644 --- a/src/dllist.c +++ b/src/dllist.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2019 +*******************************************************************************/ #include #include #include diff --git a/src/fileutils.c b/src/fileutils.c index 529a58f..862f15b 100644 --- a/src/fileutils.c +++ b/src/fileutils.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2019 +*******************************************************************************/ #include /* strlen, strcmp, strchr, strncpy, strpbrk */ #include #include @@ -8,6 +13,7 @@ #include #include "fileutils.h" + typedef struct __file_struct { size_t filesize; mode_t mode; diff --git a/src/graph.c b/src/graph.c index db688a9..27cd360 100644 --- a/src/graph.c +++ b/src/graph.c @@ -1,4 +1,8 @@ - +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2020 +*******************************************************************************/ #include #include #include diff --git a/src/llist.c b/src/llist.c index 6fa808e..cbcbcdb 100644 --- a/src/llist.c +++ b/src/llist.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2019 +*******************************************************************************/ #include #include #include diff --git a/tests/minunit.h b/src/minunit.h similarity index 100% rename from tests/minunit.h rename to src/minunit.h diff --git a/src/queue.c b/src/queue.c index 31981e5..6ab7e58 100644 --- a/src/queue.c +++ b/src/queue.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2020 +*******************************************************************************/ #include #include #include diff --git a/src/stack.c b/src/stack.c index a2e3b42..3d024f1 100644 --- a/src/stack.c +++ b/src/stack.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2020 +*******************************************************************************/ #include #include #include diff --git a/src/stringlib.c b/src/stringlib.c index 31b2393..81ddfbc 100644 --- a/src/stringlib.c +++ b/src/stringlib.c @@ -1,3 +1,8 @@ +/******************************************************************************* +*** Author: Tyler Barrus +*** email: barrust@gmail.com +*** License: MIT 2019 +*******************************************************************************/ #include #include #include diff --git a/tests/bitarray_test.c b/tests/bitarray_test.c index 0bee8fe..cf1f5ff 100644 --- a/tests/bitarray_test.c +++ b/tests/bitarray_test.c @@ -1,8 +1,7 @@ #include #include +#include "../src/minunit.h" #include "../src/bitarray.h" -#include "minunit.h" - void test_setup(void) {} diff --git a/tests/doubly_linked_list_test.c b/tests/doubly_linked_list_test.c index 2a99c02..7fe466d 100644 --- a/tests/doubly_linked_list_test.c +++ b/tests/doubly_linked_list_test.c @@ -1,7 +1,7 @@ #include #include #include -#include "minunit.h" +#include "../src/minunit.h" #include "../src/dllist.h" diff --git a/tests/fileutils_test.c b/tests/fileutils_test.c index 20beedd..ed1fa9c 100644 --- a/tests/fileutils_test.c +++ b/tests/fileutils_test.c @@ -2,7 +2,7 @@ #include #include #include -#include "minunit.h" +#include "../src/minunit.h" #include "../src/fileutils.h" diff --git a/tests/graph_test.c b/tests/graph_test.c index 886cc0e..fb25595 100644 --- a/tests/graph_test.c +++ b/tests/graph_test.c @@ -1,10 +1,11 @@ #include #include -#include "../src/graph.h" -#include "minunit.h" #if defined (_OPENMP) #include #endif +#include "../src/minunit.h" +#include "../src/graph.h" + graph_t g; diff --git a/tests/linked_list_test.c b/tests/linked_list_test.c index ba373cb..4f23b28 100644 --- a/tests/linked_list_test.c +++ b/tests/linked_list_test.c @@ -1,7 +1,7 @@ #include #include #include -#include "minunit.h" +#include "../src/minunit.h" #include "../src/llist.h" llist_t l; diff --git a/tests/queue_test.c b/tests/queue_test.c index e1882cd..38cd0e5 100644 --- a/tests/queue_test.c +++ b/tests/queue_test.c @@ -1,7 +1,7 @@ #include #include #include -#include "minunit.h" +#include "../src/minunit.h" #include "../src/queue.h" diff --git a/tests/stack_test.c b/tests/stack_test.c index a20cf74..0bfb1eb 100644 --- a/tests/stack_test.c +++ b/tests/stack_test.c @@ -1,7 +1,7 @@ #include #include #include -#include "minunit.h" +#include "../src/minunit.h" #include "../src/stack.h" stack_list_t stk; diff --git a/tests/stringlib_test.c b/tests/stringlib_test.c index 3f088ad..c9e4b26 100644 --- a/tests/stringlib_test.c +++ b/tests/stringlib_test.c @@ -1,6 +1,7 @@ #include +#include "../src/minunit.h" #include "../src/stringlib.h" -#include "minunit.h" + char* foostring = NULL; diff --git a/tests/timing_test.c b/tests/timing_test.c index 4a63f56..dbbeda9 100644 --- a/tests/timing_test.c +++ b/tests/timing_test.c @@ -1,5 +1,5 @@ #include /*sleep*/ -#include "minunit.h" +#include "../src/minunit.h" #include "../src/timing.h"