minctest.nut 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // MINCTEST - Minimal SquiLu Test Library
  3. // based on MINCTEST - Minimal Lua Test Library - 0.1.1
  4. // This is based on minctest.h (https://codeplea.com/minctest)
  5. //
  6. // Copyright (c) 2014, 2015, 2016 Lewis Van Winkle
  7. // Copyright (c) 2017 Domingo Alvarez Duarte
  8. //
  9. // http://CodePlea.com
  10. //
  11. // This software is provided 'as-is', without any express or implied
  12. // warranty. In no event will the authors be held liable for any damages
  13. // arising from the use of this software.
  14. //
  15. // Permission is granted to anyone to use this software for any purpose,
  16. // including commercial applications, and to alter it and redistribute it
  17. // freely, subject to the following restrictions:
  18. //
  19. // 1. The origin of this software must not be misrepresented; you must not
  20. // claim that you wrote the original software. If you use this software
  21. // in a product, an acknowledgement in the product documentation would be
  22. // appreciated but is not required.
  23. // 2. Altered source versions must be plainly marked as such, and must not be
  24. // misrepresented as being the original software.
  25. // 3. This notice may not be removed or altered from any source distribution.
  26. // MINCTEST - Minimal testing library for C
  27. //
  28. //
  29. // Example:
  30. //
  31. //
  32. // #include "minctest.nut"
  33. // local l = minctest();
  34. //
  35. // l.run("test1", function(){
  36. // l.ok('a' == 'a'); //assert true
  37. // });
  38. //
  39. // l.run("test2", function(){
  40. // l.equal(5, 6); //compare integers
  41. // l.fequal(5.5, 5.6); //compare floats
  42. // });
  43. //
  44. // return l.results(); //show results
  45. //
  46. //
  47. // Hints:
  48. // All functions/variables start with the letter 'l'.
  49. //
  50. //
  51. function minctest()
  52. {
  53. local self = {};
  54. const LTEST_FLOAT_TOLERANCE = 1e-12; //1e-6; 0.001;
  55. local ltests = 0;
  56. local lfails = 0;
  57. local start_clock = os.clock();
  58. self.results <- function()
  59. {
  60. local total_time = math.floor((os.clock() - start_clock) * 1000) + "ms";
  61. if (lfails == 0)
  62. print("ALL TESTS PASSED (" + ltests + "/" + ltests + ") " + total_time);
  63. else
  64. print("SOME TESTS FAILED (" + (ltests-lfails) + "/" + ltests + ") " + total_time);
  65. return lfails != 0;
  66. }
  67. self.run <- function(name, testfunc)
  68. {
  69. local ts = ltests;
  70. local fs = lfails;
  71. local lclock = os.clock();
  72. local msg = format("\t%-24s", name);
  73. stdout.write(msg);
  74. testfunc();
  75. msg = format("pass: %6d fail: %6d %4dms\n",
  76. (ltests-ts)-(lfails-fs), lfails-fs,
  77. math.floor((os.clock() - lclock) * 1000));
  78. if(lfails != fs) stdout.write(format("\n\t%-24s", " "));
  79. stdout.write(msg);
  80. }
  81. self.ok <- function(test)
  82. {
  83. ++ltests;
  84. if ( ! test )
  85. {
  86. ++lfails;
  87. local stack_info = getstackinfos(2);
  88. stdout.write(format("\n%s:%d:0 error",
  89. stack_info.src,
  90. stack_info.line));
  91. }
  92. }
  93. self.equal <- function(a, b)
  94. {
  95. ++ltests;
  96. if (a != b)
  97. {
  98. ++lfails;
  99. local stack_info = getstackinfos(2);
  100. stdout.write(format("\n%s:%d (%d != %d)",
  101. stack_info.src,
  102. stack_info.line,
  103. a, b));
  104. }
  105. }
  106. self.fequal <- function(a, b)
  107. {
  108. ++ltests;
  109. if (math.fabs(a - b) > LTEST_FLOAT_TOLERANCE)
  110. {
  111. ++lfails;
  112. local stack_info = getstackinfos(2);
  113. stdout.write(format("\n%s:%d (%f != %f)",
  114. stack_info.src,
  115. stack_info.line,
  116. a, b));
  117. }
  118. }
  119. self.fails <- function() {return lfails;}
  120. return self;
  121. }