Merge pull request #119 from delroth/unittests

Add support for GTest based UnitTests
This commit is contained in:
Matthew Parlane 2014-03-03 14:53:24 +13:00
commit 6176424ac4
5 changed files with 102 additions and 1 deletions

View File

@ -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,15 @@ if(NOT OPENMP_FOUND)
message("OpenMP parallelization disabled")
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 'make unittests'")
else()
message("GTest NOT found, disabling unit tests")
endif(GTEST_FOUND)
if(NOT ANDROID)
include(FindOpenGL)

View File

@ -52,6 +52,9 @@ if (DSPTOOL)
add_subdirectory(DSPTool)
endif()
if (GTEST_FOUND)
add_subdirectory(UnitTests)
endif()

View File

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

View File

@ -0,0 +1 @@
add_dolphin_test(MMIOTest MMIOTest.cpp core)

View File

@ -0,0 +1,77 @@
#include <unordered_set>
#include <gtest/gtest.h>
#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<u32> 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);
}
}
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<u8>(0x42), MMIO::Nop<u8>());
m_mapping->Register(0xCC001234, MMIO::Constant<u16>(0x1234), MMIO::Nop<u16>());
m_mapping->Register(0xCC001234, MMIO::Constant<u32>(0xdeadbeef), MMIO::Nop<u32>());
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<u8>(&target_8), MMIO::DirectWrite<u8>(&target_8));
m_mapping->Register(0xCC001234, MMIO::DirectRead<u16>(&target_16), MMIO::DirectWrite<u16>(&target_16));
m_mapping->Register(0xCC001234, MMIO::DirectRead<u32>(&target_32), MMIO::DirectWrite<u32>(&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);
}
}