From 07bf0aa3d588246b3a409cf8d951db02f9871491 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Tue, 11 Feb 2014 05:43:12 +0400 Subject: [PATCH] Add Point and Rect tests --- CMakeLists.txt | 2 + tests/CMakeLists.txt | 10 +++ tests/test_pointrect.cc | 122 ++++++++++++++++++++++++++++++++++ tests/testing.h | 140 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 tests/test_pointrect.cc create mode 100644 tests/testing.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 838cb66..b42578e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,8 @@ IF(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) TARGET_LINK_LIBRARIES(SDL2pp ${SDL2_LIBRARY}) # demos and tests + ENABLE_TESTING() + ADD_SUBDIRECTORY(demos) ADD_SUBDIRECTORY(tests) ELSE(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 65ace6b..fe68ffa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,3 +15,13 @@ SET(HEADER_TESTS FOREACH(TEST ${HEADER_TESTS}) ADD_EXECUTABLE(${TEST} ${TEST}.cc) ENDFOREACH(TEST ${TESTS}) + +SET(TESTS + test_pointrect +) + +FOREACH(TEST ${TESTS}) + ADD_EXECUTABLE(${TEST} ${TEST}.cc) + TARGET_LINK_LIBRARIES(${TEST} SDL2pp) + ADD_TEST(${TEST} ${TEST}) +ENDFOREACH(TEST ${TESTS}) diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc new file mode 100644 index 0000000..7553069 --- /dev/null +++ b/tests/test_pointrect.cc @@ -0,0 +1,122 @@ +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST() + { + // Point basic ops + Point p(1,2); + + EXPECT_TRUE(!p.IsNull()); + EXPECT_TRUE(p.GetX() == 1 && p.GetY() == 2); + EXPECT_TRUE(p == Point(1,2)); + EXPECT_TRUE(p != Point(1,1)); + EXPECT_TRUE(p != Point(2,2)); + EXPECT_TRUE(p != Point::Null()); + + p.SetX(4); + p.SetY(5); + + EXPECT_TRUE(p.GetX() == 4 && p.GetY() == 5); + EXPECT_TRUE(p == Point(4,5)); + + p = Point(6,7); + + EXPECT_TRUE(p.GetX() == 6 && p.GetY() == 7); + EXPECT_TRUE(p == Point(6,7)); + + p = Point::Null(); + + EXPECT_TRUE(p != Point(1,2)); + EXPECT_TRUE(p == Point::Null()); + EXPECT_TRUE(p.IsNull()); + } + + { + // Point self assignment + Point p(8,9); + p = Point(10,11); + + p = p; + + EXPECT_TRUE(p.GetX() == 10 && p.GetY() == 11); + } + + { + // Point self move-assignment + Point p(12,13); + p = Point(14,15); + + p = std::move(p); + + EXPECT_TRUE(p.GetX() == 14 && p.GetY() == 15); + } + + { + // Rect basic ops + Rect r(1,2,3,4); + + EXPECT_TRUE(!r.IsNull()); + EXPECT_TRUE(r.GetX() == 1 && r.GetY() == 2 && r.GetW() == 3 && r.GetH() == 4); + EXPECT_TRUE(r == Rect(1,2,3,4)); + EXPECT_TRUE(r != Rect(2,2,3,4)); + EXPECT_TRUE(r != Rect(1,3,3,4)); + EXPECT_TRUE(r != Rect(1,2,4,4)); + EXPECT_TRUE(r != Rect(1,2,3,5)); + EXPECT_TRUE(r != Rect::Null()); + + r.SetX(5); + r.SetY(6); + r.SetW(7); + r.SetH(8); + + EXPECT_TRUE(r.GetX() == 5 && r.GetY() == 6 && r.GetW() == 7 && r.GetH() == 8); + EXPECT_TRUE(r == Rect(5,6,7,8)); + + r = Rect(9,10,11,12); + + EXPECT_TRUE(r.GetX() == 9 && r.GetY() == 10 && r.GetW() == 11 && r.GetH() == 12); + EXPECT_TRUE(r == Rect(9,10,11,12)); + + r = Rect::Null(); + + EXPECT_TRUE(r != Rect(1,2,3,4)); + EXPECT_TRUE(r == Rect::Null()); + EXPECT_TRUE(r.IsNull()); + } + + { + // Rect self assignment + Rect r(13,14,15,16); + r = Rect(17,18,19,20); + + r = r; + + EXPECT_TRUE(r.GetX() == 17 && r.GetY() == 18 && r.GetW() == 19 && r.GetH() == 20); + } + + { + // Rect self move assignment + Rect r(21,22,23,24); + r = Rect(25,26,27,28); + + r = std::move(r); + + EXPECT_TRUE(r.GetX() == 25 && r.GetY() == 26 && r.GetW() == 27 && r.GetH() == 28); + } + + { + // Rect second point stuff + Rect r(50,100,5,10); + + EXPECT_TRUE(r.GetX2() == 54 && r.GetY2() == 109); + + r.SetX2(50+15); + r.SetY2(100+30); + + EXPECT_TRUE(r.GetW() == 16 && r.GetH() == 31); + } +END_TEST() diff --git a/tests/testing.h b/tests/testing.h new file mode 100644 index 0000000..308202c --- /dev/null +++ b/tests/testing.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2011-2013 Dmitry Marakasov + * All rights reserved. + * + * See https://github.com/AMDmi3/testing.h for updates, bug reports, + * forks etc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef TESTING_H_INCLUDED +#define TESTING_H_INCLUDED + +#include +#include + +// Define NO_TEST_COLOR before including testing.h to disable colors +#ifndef NO_TEST_COLOR +# define TEST_COLOR +#endif + +#define TEST_ESCAPE "\033" + +#ifdef TEST_COLOR +# define PASSED TEST_ESCAPE "[0;32mPASSED:" TEST_ESCAPE "[0m " +# define FAILED TEST_ESCAPE "[1;31mFAILED:" TEST_ESCAPE "[0m " +#else +# define PASSED "PASSED: " +# define FAILED "FAILED: " +#endif + +// Test begin/end +#define BEGIN_TEST(...) int main(__VA_ARGS__) { int num_failing_tests_ = 0; + +#define END_TEST() if (num_failing_tests_ > 0) std::cerr << num_failing_tests_ << " failures" << std::endl; return num_failing_tests_; } + +// Equality checks +#define EXPECT_TRUE(expr) { \ + if (!(expr)) { \ + std::cerr << FAILED #expr << std::endl; \ + ++num_failing_tests_; \ + } else { \ + std::cerr << PASSED #expr << std::endl; \ + } \ + } + +#define EXPECT_STRING(expr, expected) { \ + std::string result = (expr); \ + if (result != expected) { \ + std::cerr << FAILED #expr " returned \"" << result << "\", while expected \"" << expected << "\"" << std::endl; \ + ++num_failing_tests_; \ + } else { \ + std::cerr << PASSED #expr " == \"" << expected << "\"" << std::endl; \ + } \ + } + +#define EXPECT_EQUAL(type, expr, expected) { \ + type result = (expr); \ + if (result != expected) { \ + std::cerr << FAILED #expr " returned " << result << ", while expected " << expected << std::endl; \ + ++num_failing_tests_; \ + } else { \ + std::cerr << PASSED #expr " == " << expected << std::endl; \ + } \ + } + +// Range checks +#define EXPECT_VALUE_IN_RANGE(type, expr, from, to) { \ + type result = (expr); \ + if (from <= result && result <= to) { \ + std::cerr << PASSED #expr " returned " << result << ", which is in range [" << from << ", " << to << "] as expected" << std::endl; \ + } else { \ + std::cerr << FAILED #expr " returned " << result << ", which is out of expected range [" << from << ", " << to << "]" << std::endl; \ + ++num_failing_tests_; \ + } \ + } + +// Shortcuts for above; feel free to ask to add more +#define EXPECT_INT(expr, expected) EXPECT_EQUAL(int, expr, expected) +#define EXPECT_FLOAT_IN_RANGE(expr, from, to) EXPECT_VALUE_IN_RANGE(float, expr, from, to) + +// Exception checks +#define EXPECT_EXCEPTION(expr, exception) { \ + bool correct_catch = false; \ + try { \ + expr; \ + } catch (exception &e) { \ + correct_catch = true; \ + } catch (...) { \ + } \ + if (correct_catch) { \ + std::cerr << PASSED #expr " has thrown " #exception << " as expected " << std::endl; \ + } else { \ + std::cerr << FAILED #expr " hasn't thrown expected " #exception << std::endl; \ + ++num_failing_tests_; \ + } \ + } + +#define EXPECT_NO_EXCEPTION(expr) { \ + bool had_exception = false; \ + const char* what = NULL; \ + try { \ + expr; \ + } catch (std::exception& e) { \ + had_exception = true; \ + what = e.what(); \ + } catch (...) { \ + had_exception = true; \ + } \ + if (had_exception && what) { \ + std::cerr << FAILED #expr << " has thrown unexpected exception derived from std::exception, what() returned \"" << what << "\"" << std::endl; \ + ++num_failing_tests_; \ + } else if (had_exception) { \ + std::cerr << FAILED #expr << " has thrown unexpected exception not derived from std::exception" << std::endl; \ + ++num_failing_tests_; \ + } else { \ + std::cerr << PASSED #expr << " hasn't thrown any exceptions as expected" << std::endl; \ + } \ + } + +#endif // TESTING_H_INCLUDED