From 747021e0c89d50f93ded1c9809fd33e14a4b3575 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 2 Mar 2014 04:15:33 +0100 Subject: [PATCH 1/3] CMake: detect GTest if it is installed --- CMakeLists.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a38af9c37..c80f1d6f13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ ######################################## # General setup # -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8) option(ANDROID "Enables a build for Android" OFF) option(USE_EGL "Enables EGL OpenGL Interface" OFF) @@ -331,6 +331,14 @@ if(NOT OPENMP_FOUND) message("OpenMP parallelization disabled") endif() +include(FindGTest OPTIONAL) +if(GTEST_FOUND) + include_directories(${GTEST_INCLUDE_DIRS}) + message("GTest found, unit tests can be compiled and ran with 'ctest'") +else() + message("GTest NOT found, disabling unit tests") +endif(GTEST_FOUND) + if(NOT ANDROID) include(FindOpenGL) From d4ed4adacea53df3f511deb5e5cf84ce5c9b9b6e Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 2 Mar 2014 04:44:56 +0100 Subject: [PATCH 2/3] Add the infrastructure required to easily add unit tests and test it with a very simple test file. --- CMakeLists.txt | 3 ++- Source/CMakeLists.txt | 3 +++ Source/UnitTests/CMakeLists.txt | 11 +++++++++++ Source/UnitTests/Core/CMakeLists.txt | 1 + Source/UnitTests/Core/MMIOTest.cpp | 24 ++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Source/UnitTests/CMakeLists.txt create mode 100644 Source/UnitTests/Core/CMakeLists.txt create mode 100644 Source/UnitTests/Core/MMIOTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c80f1d6f13..96655a3af7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -333,8 +333,9 @@ endif() include(FindGTest OPTIONAL) if(GTEST_FOUND) + enable_testing() include_directories(${GTEST_INCLUDE_DIRS}) - message("GTest found, unit tests can be compiled and ran with 'ctest'") + message("GTest found, unit tests can be compiled and ran with 'make unittests'") else() message("GTest NOT found, disabling unit tests") endif(GTEST_FOUND) diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 458487257d..825f88fc5d 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -52,6 +52,9 @@ if (DSPTOOL) add_subdirectory(DSPTool) endif() +if (GTEST_FOUND) + add_subdirectory(UnitTests) +endif() diff --git a/Source/UnitTests/CMakeLists.txt b/Source/UnitTests/CMakeLists.txt new file mode 100644 index 0000000000..9b7338532c --- /dev/null +++ b/Source/UnitTests/CMakeLists.txt @@ -0,0 +1,11 @@ +add_custom_target(unittests) +add_custom_command(TARGET unittests POST_BUILD COMMAND ${CMAKE_CTEST_COMMAND}) + +macro(add_dolphin_test target srcs libs) + add_executable(Tests/${target} EXCLUDE_FROM_ALL ${srcs}) + target_link_libraries(Tests/${target} ${libs} ${GTEST_BOTH_LIBRARIES}) + add_dependencies(unittests Tests/${target}) + add_test(NAME ${target} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Tests/${target}) +endmacro(add_dolphin_test) + +add_subdirectory(Core) diff --git a/Source/UnitTests/Core/CMakeLists.txt b/Source/UnitTests/Core/CMakeLists.txt new file mode 100644 index 0000000000..e52290e387 --- /dev/null +++ b/Source/UnitTests/Core/CMakeLists.txt @@ -0,0 +1 @@ +add_dolphin_test(MMIOTest MMIOTest.cpp core) diff --git a/Source/UnitTests/Core/MMIOTest.cpp b/Source/UnitTests/Core/MMIOTest.cpp new file mode 100644 index 0000000000..97cabda658 --- /dev/null +++ b/Source/UnitTests/Core/MMIOTest.cpp @@ -0,0 +1,24 @@ +#include +#include + +#include "Common/CommonTypes.h" +#include "Core/HW/MMIO.h" + +// Tests that the UniqueID function returns a "unique enough" identifier +// number: that is, it is unique in the address ranges we care about. +TEST(UniqueID, UniqueEnough) +{ + std::unordered_set ids; + for (u32 i = 0xCC000000; i < 0xCC010000; ++i) + { + u32 unique_id = MMIO::UniqueID(i); + EXPECT_EQ(ids.end(), ids.find(unique_id)); + ids.insert(unique_id); + } + for (u32 i = 0xCD000000; i < 0xCD010000; ++i) + { + u32 unique_id = MMIO::UniqueID(i); + EXPECT_EQ(ids.end(), ids.find(unique_id)); + ids.insert(unique_id); + } +} From a4ee1877110a1fe7c402ec4a72766883c219c445 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 2 Mar 2014 04:58:29 +0100 Subject: [PATCH 3/3] Tests: Add more MMIO tests as an example. --- Source/UnitTests/Core/MMIOTest.cpp | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Source/UnitTests/Core/MMIOTest.cpp b/Source/UnitTests/Core/MMIOTest.cpp index 97cabda658..c7e183f458 100644 --- a/Source/UnitTests/Core/MMIOTest.cpp +++ b/Source/UnitTests/Core/MMIOTest.cpp @@ -22,3 +22,56 @@ TEST(UniqueID, UniqueEnough) ids.insert(unique_id); } } + +class MappingTest : public testing::Test +{ +protected: + virtual void SetUp() + { + m_mapping = new MMIO::Mapping(); + } + + virtual void TearDown() + { + delete m_mapping; + } + + MMIO::Mapping* m_mapping; +}; + +TEST_F(MappingTest, ReadConstant) +{ + m_mapping->Register(0xCC001234, MMIO::Constant(0x42), MMIO::Nop()); + m_mapping->Register(0xCC001234, MMIO::Constant(0x1234), MMIO::Nop()); + m_mapping->Register(0xCC001234, MMIO::Constant(0xdeadbeef), MMIO::Nop()); + + u8 val8; m_mapping->Read(0xCC001234, &val8); + u16 val16; m_mapping->Read(0xCC001234, &val16); + u32 val32; m_mapping->Read(0xCC001234, &val32); + + EXPECT_EQ(0x42, val8); + EXPECT_EQ(0x1234, val16); + EXPECT_EQ(0xdeadbeef, val32); +} + +TEST_F(MappingTest, ReadWriteDirect) +{ + u8 target_8 = 0; + u16 target_16 = 0; + u32 target_32 = 0; + + m_mapping->Register(0xCC001234, MMIO::DirectRead(&target_8), MMIO::DirectWrite(&target_8)); + m_mapping->Register(0xCC001234, MMIO::DirectRead(&target_16), MMIO::DirectWrite(&target_16)); + m_mapping->Register(0xCC001234, MMIO::DirectRead(&target_32), MMIO::DirectWrite(&target_32)); + + for (int i = 0; i < 100; ++i) + { + u8 val8; m_mapping->Read(0xCC001234, &val8); EXPECT_EQ(i, val8); + u16 val16; m_mapping->Read(0xCC001234, &val16); EXPECT_EQ(i, val16); + u32 val32; m_mapping->Read(0xCC001234, &val32); EXPECT_EQ(i, val32); + + val8 += 1; m_mapping->Write(0xCC001234, val8); + val16 += 1; m_mapping->Write(0xCC001234, val16); + val32 += 1; m_mapping->Write(0xCC001234, val32); + } +}