Add Point and Rect tests

This commit is contained in:
Dmitry Marakasov 2014-02-11 05:43:12 +04:00
parent 66f4d5b57a
commit 07bf0aa3d5
4 changed files with 274 additions and 0 deletions

View File

@ -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)

View File

@ -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})

122
tests/test_pointrect.cc Normal file
View File

@ -0,0 +1,122 @@
#include <SDL2pp/Point.hh>
#include <SDL2pp/Rect.hh>
#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()

140
tests/testing.h Normal file
View File

@ -0,0 +1,140 @@
/*
* Copyright (c) 2011-2013 Dmitry Marakasov <amdmi3@amdmi3.ru>
* 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 <iostream>
#include <exception>
// 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