You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2KB

  1. /*
  2. *
  3. * MINCTEST - Minimal C Test Library - 0.1
  4. *
  5. * Copyright (c) 2014, 2015, 2016 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. * MINCTEST - Minimal testing library for C
  28. *
  29. *
  30. * Example:
  31. *
  32. * void test1() {
  33. * lok('a' == 'a');
  34. * }
  35. *
  36. * void test2() {
  37. * lequal(5, 6);
  38. * lfequal(5.5, 5.6);
  39. * }
  40. *
  41. * int main() {
  42. * lrun("test1", test1);
  43. * lrun("test2", test2);
  44. * lresults();
  45. * return lfails != 0;
  46. * }
  47. *
  48. *
  49. *
  50. * Hints:
  51. * All functions/variables start with the letter 'l'.
  52. *
  53. */
  54. #ifndef __MINCTEST_H__
  55. #define __MINCTEST_H__
  56. #include <stdio.h>
  57. #include <math.h>
  58. #include <time.h>
  59. /* How far apart can floats be before we consider them unequal. */
  60. #define LTEST_FLOAT_TOLERANCE 0.001
  61. /* Track the number of passes, fails. */
  62. /* NB this is made for all tests to be in one file. */
  63. static int ltests = 0;
  64. static int lfails = 0;
  65. /* Display the test results. */
  66. #define lresults() do {\
  67. if (lfails == 0) {\
  68. printf("ALL TESTS PASSED (%d/%d)\n", ltests, ltests);\
  69. } else {\
  70. printf("SOME TESTS FAILED (%d/%d)\n", ltests-lfails, ltests);\
  71. }\
  72. } while (0)
  73. /* Run a test. Name can be any string to print out, test is the function name to call. */
  74. #define lrun(name, test) do {\
  75. const int ts = ltests;\
  76. const int fs = lfails;\
  77. const clock_t start = clock();\
  78. printf("\t%-14s", name);\
  79. test();\
  80. printf("pass:%2d fail:%2d %4dms\n",\
  81. (ltests-ts)-(lfails-fs), lfails-fs,\
  82. (int)((clock() - start) * 1000 / CLOCKS_PER_SEC));\
  83. } while (0)
  84. /* Assert a true statement. */
  85. #define lok(test) do {\
  86. ++ltests;\
  87. if (!(test)) {\
  88. ++lfails;\
  89. printf("%s:%d error \n", __FILE__, __LINE__);\
  90. }} while (0)
  91. /* Assert two integers are equal. */
  92. #define lequal(a, b) do {\
  93. ++ltests;\
  94. if ((a) != (b)) {\
  95. ++lfails;\
  96. printf("%s:%d (%d != %d)\n", __FILE__, __LINE__, (a), (b));\
  97. }} while (0)
  98. /* Assert two floats are equal (Within LTEST_FLOAT_TOLERANCE). */
  99. #define lfequal(a, b) do {\
  100. ++ltests;\
  101. if (fabs((double)(a)-(double)(b)) > LTEST_FLOAT_TOLERANCE) {\
  102. ++lfails;\
  103. printf("%s:%d (%f != %f)\n", __FILE__, __LINE__, (double)(a), (double)(b));\
  104. }} while (0)
  105. #endif /*__MINCTEST_H__*/