Skip to content

Instantly share code, notes, and snippets.

@itsyarkee
Created September 4, 2013 03:03
Show Gist options
  • Save itsyarkee/6432336 to your computer and use it in GitHub Desktop.
Save itsyarkee/6432336 to your computer and use it in GitHub Desktop.
This is a really minimal testing framework for C. (Referenced from redis source code)
/* This is a really minimal testing framework for C.
*
* Example:
*
* test_cond("Check if 1 == 1", 1==1)
* test_cond("Check if 5 > 10", 5 > 10)
* test_report()
*/
#include<stdio.h>
#include<stdlib.h>
#ifndef __TESTHELP_H
#define __TESTHELP_H
int __failed_tests = 0;
int __test_num = 0;
#define test_cond(descr,_c) do { \
__test_num++; printf("%d - %s: ", __test_num, descr); \
if(_c) printf("PASSED\n"); else {printf("FAILED\n"); __failed_tests++;} \
} while(0);
#define test_report() do { \
printf("%d tests, %d passed, %d failed\n", __test_num, \
__test_num-__failed_tests, __failed_tests); \
if (__failed_tests) { \
printf("=== WARNING === We have failed tests here...\n"); \
exit(1); \
} \
} while(0);
#endif
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int multi(int a, int b)
{
return a * b;
}
int main(int argc, char *argv[])
{
test_cond("Test add two int value", add(2, 1) == 3);
test_cond("Test sub two int value", sub(2, 1) == 1);
test_cond("Test multi two int value", multi(2, 1) == 2);
test_report();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment