1 /* 2 * 3 * MINCTEST - Minimal C Test Library - 0.1 4 * 5 * Copyright (c) 2014, 2015 Lewis Van Winkle 6 * 7 * http://CodePlea.com 8 * 9 * This software is provided 'as-is', without any express or implied 10 * warranty. In no event will the authors be held liable for any damages 11 * arising from the use of this software. 12 * 13 * Permission is granted to anyone to use this software for any purpose, 14 * including commercial applications, and to alter it and redistribute it 15 * freely, subject to the following restrictions: 16 * 17 * 1. The origin of this software must not be misrepresented; you must not 18 * claim that you wrote the original software. If you use this software 19 * in a product, an acknowledgement in the product documentation would be 20 * appreciated but is not required. 21 * 2. Altered source versions must be plainly marked as such, and must not be 22 * misrepresented as being the original software. 23 * 3. This notice may not be removed or altered from any source distribution. 24 * 25 */ 26 27 28 29 /* 30 * MINCTEST - Minimal testing library for C 31 * 32 * 33 * Example: 34 * 35 * void test1() { 36 * lok('a' == 'a'); 37 * } 38 * 39 * void test2() { 40 * lequal(5, 6); 41 * lfequal(5.5, 5.6); 42 * } 43 * 44 * int main() { 45 * lrun("test1", test1); 46 * lrun("test2", test2); 47 * lresults(); 48 * return lfails != 0; 49 * } 50 * 51 * 52 * 53 * Hints: 54 * All functions/variables start with the letter 'l'. 55 * 56 */ 57 58 59 #ifndef __MINCTEST_H__ 60 #define __MINCTEST_H__ 61 62 #include <stdio.h> 63 #include <math.h> 64 #include <time.h> 65 66 67 /* How far apart can floats be before we consider them unequal. */ 68 #define LTEST_FLOAT_TOLERANCE 0.001 69 70 71 /* Track the number of passes, fails. */ 72 /* NB this is made for all tests to be in one file. */ 73 static int ltests = 0; 74 static int lfails = 0; 75 76 77 /* Display the test results. */ 78 #define lresults() do {\ 79 if (lfails == 0) {\ 80 printf("ALL TESTS PASSED (%d/%d)\n", ltests, ltests);\ 81 } else {\ 82 printf("SOME TESTS FAILED (%d/%d)\n", ltests-lfails, ltests);\ 83 }\ 84 } while (0) 85 86 87 /* Run a test. Name can be any string to print out, test is the function name to call. */ 88 #define lrun(name, test) do {\ 89 const int ts = ltests;\ 90 const int fs = lfails;\ 91 const clock_t start = clock();\ 92 printf("\t%-14s", name);\ 93 test();\ 94 printf("pass:%2d fail:%2d %4dms\n",\ 95 (ltests-ts)-(lfails-fs), lfails-fs,\ 96 (int)((clock() - start) * 1000 / CLOCKS_PER_SEC));\ 97 } while (0) 98 99 100 /* Assert a true statement. */ 101 #define lok(test) do {\ 102 ++ltests;\ 103 if (!(test)) {\ 104 ++lfails;\ 105 printf("%s:%d error \n", __FILE__, __LINE__);\ 106 }} while (0) 107 108 109 /* Assert two integers are equal. */ 110 #define lequal(a, b) do {\ 111 ++ltests;\ 112 if ((a) != (b)) {\ 113 ++lfails;\ 114 printf("%s:%d (%d != %d)\n", __FILE__, __LINE__, (a), (b));\ 115 }} while (0) 116 117 118 /* Assert two floats are equal (Within LTEST_FLOAT_TOLERANCE). */ 119 #define lfequal(a, b) do {\ 120 ++ltests;\ 121 const double __LF_COMPARE = fabs((double)(a)-(double)(b));\ 122 if (__LF_COMPARE > LTEST_FLOAT_TOLERANCE || (__LF_COMPARE != __LF_COMPARE)) {\ 123 ++lfails;\ 124 printf("%s:%d (%f != %f)\n", __FILE__, __LINE__, (double)(a), (double)(b));\ 125 }} while (0) 126 127 128 #endif /*__MINCTEST_H__*/ 129