Skip to content

Instantly share code, notes, and snippets.

@BlackGoku36
Last active September 16, 2022 18:47
Show Gist options
  • Save BlackGoku36/37d6c0b0b47bf22aacc624a598d04448 to your computer and use it in GitHub Desktop.
Save BlackGoku36/37d6c0b0b47bf22aacc624a598d04448 to your computer and use it in GitHub Desktop.
A small automated testing header
/*
Small automated testing header.
Example code:
#include "test.h"
int calculate_pi_approx(){
return 3;
}
int is_true(int truth){
return truth > 0;
}
int main(int argc, char* argv[]){
test_module test_module_1 = {};
test(test_module_1, calculate_pi_approx() == 3, "Calculate approcimate of PI");
test(test_module_1, is_true(0) != 1, "Check if value is true");
test(test_module_1, is_true(-1) == 1, "Check if value is true");
test_stat(test_module_1);
test_module test_module_2 = {};
test(test_module_2, '1' == 1, "Hmmmmmm......");
test(test_module_2, 43/32 == 43.0/32.0, "Hmmmmmm....");
test_stat(test_module_2);
return 0;
}
*/
#ifndef TEST_H
#define TEST_H
#include <stdio.h>
#define red(M) "\033[31m" M "\033[0m"
#define green(M) "\033[92m" M "\033[0m"
#define blue(M) "\033[96m" M "\033[0m"
typedef struct{
int no_of_tests;
int no_of_test_success;
} test_module;
#define test(T, B, M) T.no_of_tests += 1;\
if(!(B)){ fprintf(stderr, red("[TEST](" #T ") '" #B "' failed") ": " M "\n");\
}else{\
T.no_of_test_success += 1;\
fprintf(stderr, green("[TEST](" #T ") '" #B "' passed") ": " M "\n");}
#define test_stat(T) fprintf(stderr, blue("[INFO](" #T ")") ": Tests %d/%d passed" "\n\n", T.no_of_test_success, T.no_of_tests);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment