Last active
September 8, 2015 14:52
-
-
Save warmwaffles/50de862b38e325bad72b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "minitest.h" | |
| int main(int argc, const char *argv[]) { | |
| ASSERT_TRUE(0, "expected 0 to be true"); | |
| ASSERT_FALSE(0, "expected 0 to be false"); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "minitest.h" | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <stdint.h> | |
| #include <stdlib.h> | |
| #define ANSI_BLACK "\033[0;30m" | |
| #define ANSI_RED "\033[0;31m" | |
| #define ANSI_GREEN "\033[0;32m" | |
| #define ANSI_YELLOW "\033[0;33m" | |
| #define ANSI_BLUE "\033[0;34m" | |
| #define ANSI_MAGENTA "\033[0;35m" | |
| #define ANSI_CYAN "\033[0;36m" | |
| #define ANSI_GREY "\033[0;37m" | |
| #define ANSI_BBLACK "\033[01;30m" | |
| #define ANSI_BRED "\033[01;31m" | |
| #define ANSI_BGREEN "\033[01;32m" | |
| #define ANSI_BYELLOW "\033[01;33m" | |
| #define ANSI_BBLUE "\033[01;34m" | |
| #define ANSI_BMAGENTA "\033[01;35m" | |
| #define ANSI_BCYAN "\033[01;36m" | |
| #define ANSI_BWHITE "\033[01;37m" | |
| #define ANSI_NORMAL "\033[0m" | |
| static void minitest_fail(const char* msg, const char* caller, int line) | |
| { | |
| fprintf(stdout, "%s[FAIL] %s%s %s:%d\n", ANSI_BRED, msg, ANSI_NORMAL, caller, line); | |
| } | |
| static void minitest_pass(const char* msg) | |
| { | |
| fprintf(stdout, "%s[PASS] %s%s\n", ANSI_BGREEN, msg, ANSI_NORMAL); | |
| } | |
| void assert_true(int actual, const char* msg, const char* caller, int line) | |
| { | |
| if (actual) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void assert_false(int actual, const char* msg, const char* caller, int line) | |
| { | |
| if (!actual) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void assert_int_equal(int expected, int actual, const char* msg, const char* caller, int line) | |
| { | |
| if (expected == actual) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void refute_int_equal(int expected, int actual, const char* msg, const char* caller, int line) | |
| { | |
| if (expected == actual) { | |
| minitest_fail(msg, caller, line); | |
| } else { | |
| minitest_pass(msg); | |
| } | |
| } | |
| static int is_dbl_near(double expected, double actual, double epsilon) | |
| { | |
| double diff = expected - actual; | |
| double absolute = diff; | |
| if(diff < 0) { | |
| absolute *= -1; | |
| } | |
| return (absolute <= epsilon) ? 1 : 0; | |
| } | |
| void assert_dbl_near(double expected, double actual, double epsilon, const char* msg, const char* caller, int line) | |
| { | |
| if (is_dbl_near(expected, actual, epsilon)) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void refute_dbl_near(double expected, double actual, double epsilon, const char* msg, const char* caller, int line) | |
| { | |
| if (is_dbl_near(expected, actual, epsilon)) { | |
| minitest_fail(msg, caller, line); | |
| } else { | |
| minitest_pass(msg); | |
| } | |
| } | |
| static int is_float_near(float expected, float actual, float epsilon) | |
| { | |
| float diff = expected - actual; | |
| float absolute = diff; | |
| if(diff < 0) { | |
| absolute *= -1; | |
| } | |
| return (absolute <= epsilon) ? 1 : 0; | |
| } | |
| void assert_float_near(float expected, float actual, float epsilon, const char* msg, const char* caller, int line) | |
| { | |
| if (is_float_near(expected, actual, epsilon)) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void refute_float_near(float expected, float actual, float epsilon, const char* msg, const char* caller, int line) | |
| { | |
| if (is_float_near(expected, actual, epsilon)) { | |
| minitest_fail(msg, caller, line); | |
| } else { | |
| minitest_pass(msg); | |
| } | |
| } | |
| void assert_null(void* actual, const char* msg, const char* caller, int line) | |
| { | |
| if ((actual) == NULL) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } | |
| void refute_null(void* actual, const char* msg, const char* caller, int line) | |
| { | |
| if ((actual) == NULL) { | |
| minitest_fail(msg, caller, line); | |
| } else { | |
| minitest_pass(msg); | |
| } | |
| } | |
| void assert_fail(const char* msg, const char* caller, int line) | |
| { | |
| minitest_fail(msg, caller, line); | |
| } | |
| static int is_str_equal(const char* expected, const char* actual) | |
| { | |
| if ( | |
| (expected == NULL && actual != NULL) || | |
| (expected != NULL && actual == NULL) || | |
| (expected && actual && strcmp(expected, actual) != 0) | |
| ) { | |
| return 1; | |
| } else { | |
| return 0; | |
| } | |
| } | |
| void assert_str_equal(const char* expected, const char* actual, const char* msg, const char* caller, int line) | |
| { | |
| if (is_str_equal(expected, actual)) { | |
| minitest_fail(msg, caller, line); | |
| } else { | |
| minitest_pass(msg); | |
| } | |
| } | |
| void refute_str_equal(const char* expected, const char* actual, const char* msg, const char* caller, int line) | |
| { | |
| if (is_str_equal(expected, actual)) { | |
| minitest_pass(msg); | |
| } else { | |
| minitest_fail(msg, caller, line); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef MINITEST_H | |
| #define MINITEST_H | |
| void assert_true(int actual, const char* msg, const char* caller, int line); | |
| #define ASSERT_TRUE(actual, msg) assert_true(actual, msg, __FILE__, __LINE__) | |
| void assert_false(int actual, const char* msg, const char* caller, int line); | |
| #define ASSERT_FALSE(actual, msg) assert_false(actual, msg, __FILE__, __LINE__) | |
| void assert_int_equal(int expected, int actual, const char* msg, const char* caller, int line); | |
| #define ASSERT_INT_EQUAL(expected, actual, msg) assert_int_equal(expected, actual, msg, __FILE__, __LINE__) | |
| void refute_int_equal(int expected, int actual, const char* msg, const char* caller, int line); | |
| #define REFUTE_INT_EQUAL(expected, actual, msg) refute_int_equal(expected, actual, msg, __FILE__, __LINE__) | |
| void assert_dbl_near(double expected, double actual, double epsilon, const char* msg, const char* caller, int line); | |
| #define ASSERT_DBL_NEAR(expected, actual, epsilon, msg) assert_dbl_near(expected, actual, epsilon, msg, __FILE__, __LINE__) | |
| void refute_dbl_near(double expected, double actual, double epsilon, const char* msg, const char* caller, int line); | |
| #define REFUTE_DBL_NEAR(expected, actual, epsilon, msg) refute_dbl_near(expected, actual, epsilon, msg, __FILE__, __LINE__) | |
| void assert_float_near(float expected, float actual, float epsilon, const char* msg, const char* caller, int line); | |
| #define ASSERT_FLOAT_NEAR(expected, actual, epsilon, msg) assert_float_near(expected, actual, epsilon, msg, __FILE__, __LINE__) | |
| void refute_float_near(float expected, float actual, float epsilon, const char* msg, const char* caller, int line); | |
| #define REFUTE_FLOAT_NEAR(expected, actual, epsilon, msg) refute_float_near(expected, actual, epsilon, msg, __FILE__, __LINE__) | |
| void refute_str_equal(const char* expected, const char* actual, const char* msg, const char* caller, int line); | |
| #define REFUTE_STR_EQUAL(expected, actual, msg) refute_str_equal(exp, actual, msg, __FILE__, __LINE__); | |
| void assert_str_equal(const char* expected, const char* actual, const char* msg, const char* caller, int line); | |
| #define ASSERT_STR_EQUAL(expected, actual, msg) assert_str_equal(exp, actual, msg, __FILE__, __LINE__); | |
| void assert_fail(const char* msg, const char* caller, int line); | |
| #define ASSERT_FAIL(msg) assert_fail(msg, __FILE__, __LINE__) | |
| void refute_null(void* actual, const char* msg, const char* caller, int line); | |
| #define REFUTE_NULL(actual, msg) refute_null(actual, msg, __FILE__, __LINE__) | |
| void assert_null(void* actual, const char* msg, const char* caller, int line); | |
| #define ASSERT_NULL(actual, msg) assert_null(actual, msg, __FILE__, __LINE__) | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [FAIL] expected 0 to be true tests/main.c:4 | |
| [PASS] expected 0 to be false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment