Skip to content

Commit

Permalink
Feature/testing (#35)
Browse files Browse the repository at this point in the history
* add basis for minunit.h
* add license and author information to c files
* update readme
  • Loading branch information
barrust authored Mar 24, 2020
1 parent 34150db commit 0f153bb
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 20 deletions.
75 changes: 69 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
#include <minunit.h>
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.
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ comment:
ignore:
- "./tests/"
- "./examples/"
- "./src/minunit.h"
7 changes: 5 additions & 2 deletions src/bitarray.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@

/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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)))
Expand Down
5 changes: 5 additions & 0 deletions src/dllist.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down
6 changes: 6 additions & 0 deletions src/fileutils.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <string.h> /* strlen, strcmp, strchr, strncpy, strpbrk */
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -8,6 +13,7 @@
#include <errno.h>
#include "fileutils.h"


typedef struct __file_struct {
size_t filesize;
mode_t mode;
Expand Down
6 changes: 5 additions & 1 deletion src/graph.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2020
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
Expand Down
5 changes: 5 additions & 0 deletions src/llist.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2020
*******************************************************************************/
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down
5 changes: 5 additions & 0 deletions src/stack.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2020
*******************************************************************************/
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down
5 changes: 5 additions & 0 deletions src/stringlib.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*******************************************************************************
*** Author: Tyler Barrus
*** email: [email protected]
*** License: MIT 2019
*******************************************************************************/
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
Expand Down
3 changes: 1 addition & 2 deletions tests/bitarray_test.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "../src/minunit.h"
#include "../src/bitarray.h"
#include "minunit.h"


void test_setup(void) {}

Expand Down
2 changes: 1 addition & 1 deletion tests/doubly_linked_list_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/dllist.h"


Expand Down
2 changes: 1 addition & 1 deletion tests/fileutils_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/fileutils.h"


Expand Down
5 changes: 3 additions & 2 deletions tests/graph_test.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <stdlib.h>
#include <stdbool.h>
#include "../src/graph.h"
#include "minunit.h"
#if defined (_OPENMP)
#include <omp.h>
#endif
#include "../src/minunit.h"
#include "../src/graph.h"


graph_t g;

Expand Down
2 changes: 1 addition & 1 deletion tests/linked_list_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/llist.h"

llist_t l;
Expand Down
2 changes: 1 addition & 1 deletion tests/queue_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/queue.h"


Expand Down
2 changes: 1 addition & 1 deletion tests/stack_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/stack.h"

stack_list_t stk;
Expand Down
3 changes: 2 additions & 1 deletion tests/stringlib_test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include "../src/minunit.h"
#include "../src/stringlib.h"
#include "minunit.h"



char* foostring = NULL;
Expand Down
2 changes: 1 addition & 1 deletion tests/timing_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <unistd.h> /*sleep*/
#include "minunit.h"
#include "../src/minunit.h"
#include "../src/timing.h"


Expand Down

0 comments on commit 0f153bb

Please sign in to comment.