Advertisement
Guest User

Unit Testing in c++

a guest
Jan 20th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include "BoardTest.h"
  2.  
  3. void BoardTest::testIsWin1() {
  4. Board b;
  5. b.move(0, 0, cell_X);
  6. b.move(0, 1, cell_X);
  7. b.move(1, 0, cell_X);
  8. b.move(4, 5, cell_O);
  9. b.move(5, 5, cell_O);
  10. DO_CHECK(b.is_win() == game_in_progress);
  11. }
  12.  
  13. void BoardTest::testIsWin2() {
  14. Board b;
  15. b.move(0, 0, cell_X);
  16. b.move(0, 1, cell_X);
  17. b.move(0, 2, cell_X);
  18. b.move(0, 3, cell_X);
  19. b.move(0, 4, cell_X);
  20. DO_CHECK(b.is_win() == game_X_win);
  21. }
  22.  
  23. void BoardTest::testIsWin3() {
  24. Board b;
  25. b.move(0, 0, cell_O);
  26. b.move(1, 1, cell_O);
  27. b.move(2, 2, cell_O);
  28. b.move(3, 3, cell_O);
  29. b.move(4, 4, cell_O);
  30. DO_CHECK(b.is_win() == game_O_win);
  31. }
  32.  
  33.  
  34. void BoardTest::testMove1() {
  35. Board b;
  36. b.move(0, 0, cell_O);
  37. DO_CHECK(b.can_move(0, 0, cell_X) == false);
  38. }
  39.  
  40. void BoardTest::testMove2() {
  41. Board b;
  42. b.move(0, 0, cell_O);
  43. DO_CHECK(b.get_cell(0, 0) == cell_O);
  44. }
  45.  
  46. void BoardTest::testMove3() {
  47. Board b;
  48. b.move(2, 4, cell_X);
  49. DO_CHECK(b.get_cell(4, 2) == cell_X);
  50. }
  51.  
  52. void BoardTest::testCanMove1() {
  53. Board b;
  54. DO_CHECK(b.can_move(0, 0, cell_X) == true);
  55. }
  56.  
  57. void BoardTest::testCanMove2() {
  58. Board b;
  59. DO_CHECK(b.can_move(-1, 0, cell_X) == false);
  60. }
  61.  
  62. void BoardTest::testCanMove3() {
  63. Board b;
  64. b.move(0, 0, cell_X);
  65. DO_CHECK(b.can_move(0, 0, cell_X) == false);
  66. }
  67.  
  68. void BoardTest::runAllTests() {
  69. testIsWin1();
  70. testIsWin2();
  71. testIsWin3();
  72.  
  73. testMove1();
  74. testMove2();
  75. testMove3();
  76.  
  77. testCanMove1();
  78. testCanMove2();
  79. testCanMove3();
  80. }
  81.  
  82. =============================================================================================================
  83.  
  84. #include "TestClass.h"
  85. int Test::totalNum;
  86. int Test::failedNum;
  87.  
  88. void Test::check(bool expr, const char *func, const char *filename, size_t lineNum) {
  89. if (!expr) {
  90. printf("β€œtest failed: %s in %s:%i”\n", func, filename, (int)lineNum);
  91. Test::failedNum ++;
  92. }
  93.  
  94. Test::totalNum++;
  95. }
  96.  
  97. void Test::showFinalResult() {
  98. if (!Test::failedNum)
  99. printf("All test passed\n");
  100. else
  101. printf("failed %d of %d tests.\n", Test::failedNum, Test::totalNum);
  102. }
  103.  
  104. ==============================================================================================
  105.  
  106. #include "BoardTest.h"
  107.  
  108. int main() {
  109. BoardTest bt;
  110. bt.runAllTests();
  111. bt.showFinalResult();
  112. return 0;
  113. }
  114.  
  115. ==================================================================================================
  116.  
  117. #pragma once
  118. #include "TestClass.h"
  119. #include "Board.h"
  120.  
  121. class BoardTest: public Test {
  122. public:
  123. void testIsWin1();
  124. void testIsWin2();
  125. void testIsWin3();
  126.  
  127. void testMove1();
  128. void testMove2();
  129. void testMove3();
  130.  
  131. void testCanMove1();
  132. void testCanMove2();
  133. void testCanMove3();
  134.  
  135. void runAllTests();
  136. };
  137.  
  138. ======================================================================================
  139.  
  140. #pragma once
  141. #include <stdlib.h>
  142. #include <cstdio>
  143. #define DO_CHECK(EXPR) check(EXPR, __FUNCTION__, __FILE__, __LINE__);
  144.  
  145. class Test {
  146. protected:
  147. static int failedNum;
  148. static int totalNum;
  149.  
  150. public:
  151. static void check(bool expr, const char *func, const char *filename, std::size_t lineNum);
  152.  
  153. static void showFinalResult();
  154.  
  155. virtual void runAllTests() = 0;
  156. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement