diff options
| author | shmibs <shmibs@gmail.com> | 2014-07-31 10:10:25 -0700 | 
|---|---|---|
| committer | shmibs <shmibs@gmail.com> | 2014-07-31 10:10:25 -0700 | 
| commit | fe2cc41034078b4de19d9b4fbd206cf688ddf598 (patch) | |
| tree | f3b80fa3f90dfe045cb37c82f3e15607fe4bd75d | |
| parent | 110e13da2ee0fbd8ab00b2744a3aa70a027a52a3 (diff) | |
| download | simple-test-fe2cc41034078b4de19d9b4fbd206cf688ddf598.tar.gz | |
initial commit!
| -rw-r--r-- | test.h | 150 | 
1 files changed, 150 insertions, 0 deletions
| @@ -0,0 +1,150 @@ +#ifndef _TEST_H_ +#define _TEST_H_ + +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> + +/************* + *  REGIONS  * + *************/ + +/* extra levels of abstraction are necessary + * to get __COUNTER__ to evaluate before + * being concatenated */ +#define _TEST_PASTE(x, y) \ +x ## y + +/* the whole thing is inside one + * big function. yay! */ +#define BEGIN_TEST \ +typedef void (*_test_cleanup_t)(void); \ +_test_cleanup_t _test_cleanup_current=NULL; \ +int _test_count_current=1; \ +int main(int argc, char *argv[]) \ +{ + +/* yaaaay! */ +#define END_TEST \ +	return 0; \ +} + +/* print a fancy description and + * reset the cleanup function + * pointer to NULL so, if it isn't + * defined again, no cleanup will + * be called */ +#define TEST(description) \ +_test_cleanup_current=NULL; \ +printf("\e[1m%3i :: \e[m\e[33m%s\e[m\n", _test_count_current++, description); + +/* pass in statements to be called on test + * exit. goes after varible declarations and + * before test body. */ +#define CLEANUP(statements) \ +_TEST_CLEANUP_UNIQ(statements, __COUNTER__) + +/* insert a unique cleanup function */ +#define _TEST_CLEANUP_UNIQ(statements, unique_count) \ +void _TEST_PASTE(_test_cleanup_, unique_count)(void) \ +{ \ +	statements \ +} \ +_test_cleanup_current=_TEST_PASTE(_test_cleanup_, unique_count); + +/* pretty printing for the current state within + * a test */ +#define STATE(str) printf("\e[1m    :: \e[m\e[34m%s...\e[m\n", str) + +/* one of these goes at the end of every test + * with CLEANUP (unless you want memory leaks). + * if you want, stick it in those without CLEANUP + * too. */ +#define RETURN() _TEST_RETURN(false) + +/* checks if there is a cleanup function to + * call and then either continues or terminates + * the test */ +#define _TEST_RETURN(fail) \ +do { \ +	if(_test_cleanup_current != NULL) \ +		(*_test_cleanup_current)(); \ +	if(fail) \ +		exit(1); \ +} while(0) + +/*********** + *  TESTS  * + ***********/ + +#define EXPECT_ZERO(summary, arg) \ +do { \ +	if(arg) \ +		FAIL_VAL(summary, "%i", "%i", 0, arg); \ +} while(0); + +#define EXPECT_ONE(summary, arg) \ +do { \ +	if(arg != 1) \ +		FAIL_VAL(summary, "%i", "%i", 1, arg); \ +} while(0); + +#define EXPECT_GREATER_THAN_ZERO(summary, arg) \ +do { \ +	if(arg <= 0) \ +		FAIL_VAL(summary, "%s", "%i", ">0", arg); \ +} while(0); + +#define EXPECT_INT(summary, arg1, arg2) \ +do { \ +	if(arg1 != arg2) \ +		FAIL_VAL(summary, "%i", "%i", arg1, arg2); \ +} while(0); + +#define EXPECT_EQUAL_INT(summary, arg1, arg2) \ +do { \ +	if(arg1 != arg2) \ +		FAIL_EQUAL(summary, "%i", "%i", arg1, arg2); \ +} while(0); + +#define EXPECT_UNEQUAL_INT(summary, arg1, arg2) \ +do { \ +	if(arg1 == arg2) \ +		FAIL_EQUAL(summary, "%i", "%i", arg1, arg2); \ +} while(0); + +#define EXPECT_STR(summary, arg1, arg2) \ +do { \ +	if( strcmp(arg1, arg2) ) \ +		FAIL_VAL(summary, "%s", "%s", arg1, arg2); \ +} while(0); + +#define EXPECT_EQUAL_STR(summary, arg1, arg2) \ +do { \ +	if( strcmp(arg1, arg2) ) \ +		FAIL_EQUAL(summary, "%s", "%s", arg1, arg2); \ +} while(0); + +#define EXPECT_UNEQUAL_STR(summary, arg1, arg2) \ +do { \ +	if( !strcmp(arg1, arg2) ) \ +		FAIL_EQUAL(summary, "%s", "%s", arg1, arg2); \ +} while(0); + +#define FAIL_VAL(summary, format1, format2, expected, actual) \ +do { \ +	puts("\e[1;41mFAIL: " summary "\e[m"); \ +	printf("  \e[1;32mexpected:\e[m " format1 \ +			"\n    \e[1;31mactual:\e[m " format2 "\n", expected, actual); \ +	_TEST_RETURN(true); \ +} while(0) + +#define FAIL_EQUAL(summary, format1, format2, arg1, arg2) \ +do { \ +	puts("\e[1;41mFAIL: " summary "\e[m"); \ +	printf("  arg1 == \e[1;31m" format1 "\e[m\n", arg1); \ +	printf("  arg2 == \e[1;31m" format2 "\e[m\n", arg2); \ +	_TEST_RETURN(true); \ +} while(0) + +#endif | 
