minunit icon indicating copy to clipboard operation
minunit copied to clipboard

Added MU_RUN_TEST_PARAMETERIZED and refactored code

Open Ledmington opened this issue 2 years ago • 0 comments

I added the function MU_RUN_TEST_PARAMETERIZED that allows to define an array of values of the same type and a test. This test is run each time with a new value taken from the array. This should simplify some testing scenarios performing some kind of grid search.

Since the MU_RUN_TEST and MU_RUN_TEST_PARAMETERIZED function are very similar (the same function for now, but the latter could implement a more specific error message like "the test T failed when the value was X"), i refactored the code extracting the MU_SETUP_TIMER and MU_CHECK_AND_FAIL macros.

For now i only implemented the simple parameterized runner iterating over only 1 parameter array, but from here is trivial to add more arrays to iterate. (i can implement them myself, if needed)

Here is an example of usage of this function:

#include <stdio.h>
#include <string.h>
#include "minunit.h"

int x;
char *s;

MU_TEST(simple) {
	mu_check(x % 2 == 0);
}

MU_TEST(strings) {
	mu_check(strlen(s) == 5);
}

MU_TEST_SUITE(suite) {
	MU_SUITE_CONFIGURE(NULL, NULL);

	int v[] = {0, 2, 4, 6};
	MU_RUN_TEST_PARAMETERIZED(simple, x, v);

	int odd[] = {1, 3, 5, 7};
	MU_RUN_TEST_PARAMETERIZED(simple, x, odd);

	char* words[] = {"hello", "world", "apple"};
	MU_RUN_TEST_PARAMETERIZED(strings, s, words);
}

int main(int argc, char *argv[]) {
	MU_RUN_SUITE(suite);
	MU_REPORT();
	return 0;
}

Note: for now i didn't find a better way to pass the values to the test function than to use global variables.

Ledmington avatar Mar 31 '22 22:03 Ledmington