Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BoardTest.h"
- void BoardTest::testIsWin1() {
- Board b;
- b.move(0, 0, cell_X);
- b.move(0, 1, cell_X);
- b.move(1, 0, cell_X);
- b.move(4, 5, cell_O);
- b.move(5, 5, cell_O);
- DO_CHECK(b.is_win() == game_in_progress);
- }
- void BoardTest::testIsWin2() {
- Board b;
- b.move(0, 0, cell_X);
- b.move(0, 1, cell_X);
- b.move(0, 2, cell_X);
- b.move(0, 3, cell_X);
- b.move(0, 4, cell_X);
- DO_CHECK(b.is_win() == game_X_win);
- }
- void BoardTest::testIsWin3() {
- Board b;
- b.move(0, 0, cell_O);
- b.move(1, 1, cell_O);
- b.move(2, 2, cell_O);
- b.move(3, 3, cell_O);
- b.move(4, 4, cell_O);
- DO_CHECK(b.is_win() == game_O_win);
- }
- void BoardTest::testMove1() {
- Board b;
- b.move(0, 0, cell_O);
- DO_CHECK(b.can_move(0, 0, cell_X) == false);
- }
- void BoardTest::testMove2() {
- Board b;
- b.move(0, 0, cell_O);
- DO_CHECK(b.get_cell(0, 0) == cell_O);
- }
- void BoardTest::testMove3() {
- Board b;
- b.move(2, 4, cell_X);
- DO_CHECK(b.get_cell(4, 2) == cell_X);
- }
- void BoardTest::testCanMove1() {
- Board b;
- DO_CHECK(b.can_move(0, 0, cell_X) == true);
- }
- void BoardTest::testCanMove2() {
- Board b;
- DO_CHECK(b.can_move(-1, 0, cell_X) == false);
- }
- void BoardTest::testCanMove3() {
- Board b;
- b.move(0, 0, cell_X);
- DO_CHECK(b.can_move(0, 0, cell_X) == false);
- }
- void BoardTest::runAllTests() {
- testIsWin1();
- testIsWin2();
- testIsWin3();
- testMove1();
- testMove2();
- testMove3();
- testCanMove1();
- testCanMove2();
- testCanMove3();
- }
- =============================================================================================================
- #include "TestClass.h"
- int Test::totalNum;
- int Test::failedNum;
- void Test::check(bool expr, const char *func, const char *filename, size_t lineNum) {
- if (!expr) {
- printf("βtest failed: %s in %s:%iβ\n", func, filename, (int)lineNum);
- Test::failedNum ++;
- }
- Test::totalNum++;
- }
- void Test::showFinalResult() {
- if (!Test::failedNum)
- printf("All test passed\n");
- else
- printf("failed %d of %d tests.\n", Test::failedNum, Test::totalNum);
- }
- ==============================================================================================
- #include "BoardTest.h"
- int main() {
- BoardTest bt;
- bt.runAllTests();
- bt.showFinalResult();
- return 0;
- }
- ==================================================================================================
- #pragma once
- #include "TestClass.h"
- #include "Board.h"
- class BoardTest: public Test {
- public:
- void testIsWin1();
- void testIsWin2();
- void testIsWin3();
- void testMove1();
- void testMove2();
- void testMove3();
- void testCanMove1();
- void testCanMove2();
- void testCanMove3();
- void runAllTests();
- };
- ======================================================================================
- #pragma once
- #include <stdlib.h>
- #include <cstdio>
- #define DO_CHECK(EXPR) check(EXPR, __FUNCTION__, __FILE__, __LINE__);
- class Test {
- protected:
- static int failedNum;
- static int totalNum;
- public:
- static void check(bool expr, const char *func, const char *filename, std::size_t lineNum);
- static void showFinalResult();
- virtual void runAllTests() = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement