From da0b90ee45ae7655c16ee152240afb86a7e6ea7a Mon Sep 17 00:00:00 2001 From: Lukasz Gromanowski Date: Fri, 30 Aug 2013 21:51:23 +0200 Subject: [PATCH] Converted tests from components/misc into google unittests. Signed-off-by: Lukasz Gromanowski --- apps/openmw_test_suite/CMakeLists.txt | 4 +- .../components/misc/test_slicearray.cpp | 33 ++++++++ .../components/misc/test_stringops.cpp | 76 ++++++++++++++++++- apps/openmw_test_suite/openmw_test_suite.cpp | 4 +- 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 apps/openmw_test_suite/components/misc/test_slicearray.cpp diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index cdf8bc74b0..9fe7890acc 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -11,14 +11,14 @@ if (GTEST_FOUND AND GMOCK_FOUND) include_directories(${GMOCK_INCLUDE_DIRS}) file(GLOB UNITTEST_SRC_FILES - components/misc/*.cpp + components/misc/test_*.cpp ) source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES}) add_executable(openmw_test_suite openmw_test_suite.cpp ${UNITTEST_SRC_FILES}) - target_link_libraries(openmw_test_suite ${GMOCK_BOTH_LIBRARIES} ${GTEST_BOTH_LIBRARIES}) + target_link_libraries(openmw_test_suite ${GMOCK_BOTH_LIBRARIES} ${GTEST_BOTH_LIBRARIES} components) # Fix for not visible pthreads functions for linker with glibc 2.15 if (UNIX AND NOT APPLE) target_link_libraries(openmw_test_suite ${CMAKE_THREAD_LIBS_INIT}) diff --git a/apps/openmw_test_suite/components/misc/test_slicearray.cpp b/apps/openmw_test_suite/components/misc/test_slicearray.cpp new file mode 100644 index 0000000000..ab63e56c4f --- /dev/null +++ b/apps/openmw_test_suite/components/misc/test_slicearray.cpp @@ -0,0 +1,33 @@ +#include +#include "components/misc/slice_array.hpp" + +struct SliceArrayTest : public ::testing::Test +{ + protected: + virtual void SetUp() + { + } + + virtual void TearDown() + { + } +}; + +TEST_F(SliceArrayTest, hello_string) +{ + Misc::SString s("hello"); + ASSERT_EQ(sizeof("hello") - 1, s.length); + ASSERT_FALSE(s=="hel"); + ASSERT_FALSE(s=="hell"); + ASSERT_TRUE(s=="hello"); +} + +TEST_F(SliceArrayTest, othello_string_with_offset_2_and_size_4) +{ + Misc::SString s("othello" + 2, 4); + ASSERT_EQ(sizeof("hell") - 1, s.length); + ASSERT_FALSE(s=="hel"); + ASSERT_TRUE(s=="hell"); + ASSERT_FALSE(s=="hello"); +} + diff --git a/apps/openmw_test_suite/components/misc/test_stringops.cpp b/apps/openmw_test_suite/components/misc/test_stringops.cpp index b125841966..3ae033db79 100644 --- a/apps/openmw_test_suite/components/misc/test_stringops.cpp +++ b/apps/openmw_test_suite/components/misc/test_stringops.cpp @@ -1,7 +1,79 @@ #include +#include "components/misc/stringops.hpp" -TEST(simple_test, dummy) +struct StringOpsTest : public ::testing::Test { - EXPECT_EQ(true, true); + protected: + virtual void SetUp() + { + } + + virtual void TearDown() + { + } +}; + +TEST_F(StringOpsTest, begins_matching) +{ + ASSERT_EQ(true, Misc::begins("abc", "a")); + ASSERT_EQ(true, Misc::begins("abc", "ab")); + ASSERT_EQ(true, Misc::begins("abc", "abc")); + ASSERT_EQ(true, Misc::begins("abcd", "abc")); +} + +TEST_F(StringOpsTest, begins_not_matching) +{ + ASSERT_EQ(false, Misc::begins("abc", "b")); + ASSERT_EQ(false, Misc::begins("abc", "bc")); + ASSERT_EQ(false, Misc::begins("abc", "bcd")); + ASSERT_EQ(false, Misc::begins("abc", "abcd")); +} + +TEST_F(StringOpsTest, ibegins_matching) +{ + ASSERT_EQ(true, Misc::ibegins("Abc", "a")); + ASSERT_EQ(true, Misc::ibegins("aBc", "ab")); + ASSERT_EQ(true, Misc::ibegins("abC", "abc")); + ASSERT_EQ(true, Misc::ibegins("abcD", "abc")); +} + +TEST_F(StringOpsTest, ibegins_not_matching) +{ + ASSERT_EQ(false, Misc::ibegins("abc", "b")); + ASSERT_EQ(false, Misc::ibegins("abc", "bc")); + ASSERT_EQ(false, Misc::ibegins("abc", "bcd")); + ASSERT_EQ(false, Misc::ibegins("abc", "abcd")); +} + +TEST_F(StringOpsTest, ends_matching) +{ + ASSERT_EQ(true, Misc::ends("abc", "c")); + ASSERT_EQ(true, Misc::ends("abc", "bc")); + ASSERT_EQ(true, Misc::ends("abc", "abc")); + ASSERT_EQ(true, Misc::ends("abcd", "abcd")); +} + +TEST_F(StringOpsTest, ends_not_matching) +{ + ASSERT_EQ(false, Misc::ends("abc", "b")); + ASSERT_EQ(false, Misc::ends("abc", "ab")); + ASSERT_EQ(false, Misc::ends("abc", "bcd")); + ASSERT_EQ(false, Misc::ends("abc", "abcd")); +} + +TEST_F(StringOpsTest, iends_matching) +{ + ASSERT_EQ(true, Misc::iends("Abc", "c")); + ASSERT_EQ(true, Misc::iends("aBc", "bc")); + ASSERT_EQ(true, Misc::iends("abC", "abc")); + ASSERT_EQ(true, Misc::iends("abcD", "abcd")); +} + +TEST_F(StringOpsTest, iends_not_matching) +{ + ASSERT_EQ(false, Misc::iends("abc", "b")); + ASSERT_EQ(false, Misc::iends("abc", "ab")); + ASSERT_EQ(false, Misc::iends("abc", "bcd")); + ASSERT_EQ(false, Misc::iends("abc", "abcd")); } diff --git a/apps/openmw_test_suite/openmw_test_suite.cpp b/apps/openmw_test_suite/openmw_test_suite.cpp index 0a09b3028a..81476325ea 100644 --- a/apps/openmw_test_suite/openmw_test_suite.cpp +++ b/apps/openmw_test_suite/openmw_test_suite.cpp @@ -5,8 +5,8 @@ int main(int argc, char** argv) { // The following line causes Google Mock to throw an exception on failure, // which will be interpreted by your testing framework as a test failure. - ::testing::GTEST_FLAG(throw_on_failure) = true; + ::testing::GTEST_FLAG(throw_on_failure) = false; ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +}