Skip to content

Commit

Permalink
Merge pull request #146 from delroth/tests
Browse files Browse the repository at this point in the history
Add more tests for Common and Core/MMIO
  • Loading branch information
delroth committed Mar 9, 2014
2 parents 0ea58cd + aabd524 commit b003fd7
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/UnitTests/CMakeLists.txt
Expand Up @@ -8,4 +8,5 @@ macro(add_dolphin_test target srcs libs)
add_test(NAME ${target} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Tests/${target})
endmacro(add_dolphin_test)

add_subdirectory(Common)
add_subdirectory(Core)
4 changes: 4 additions & 0 deletions Source/UnitTests/Common/CMakeLists.txt
@@ -0,0 +1,4 @@
add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp common)
add_dolphin_test(FifoQueueTest FifoQueueTest.cpp common)
add_dolphin_test(FixedSizeQueueTest FixedSizeQueueTest.cpp common)
add_dolphin_test(MathUtilTest MathUtilTest.cpp common)
46 changes: 46 additions & 0 deletions Source/UnitTests/Common/CommonFuncsTest.cpp
@@ -0,0 +1,46 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <gtest/gtest.h>

#include "Common/CommonFuncs.h"

TEST(CommonFuncs, ArraySizeMacro)
{
char test[4];
u32 test2[42];

EXPECT_EQ(4, ArraySize(test));
EXPECT_EQ(42, ArraySize(test2));
}

TEST(CommonFuncs, RoundUpPow2Macro)
{
EXPECT_EQ(4, ROUND_UP_POW2(3));
EXPECT_EQ(4, ROUND_UP_POW2(4));
EXPECT_EQ(8, ROUND_UP_POW2(6));
EXPECT_EQ(0x40000000, ROUND_UP_POW2(0x23456789));
}

TEST(CommonFuncs, CrashMacro)
{
EXPECT_DEATH({ Crash(); }, "");
}

TEST(CommonFuncs, MinMax)
{
EXPECT_EQ(4, min(4, 5));
EXPECT_EQ(-1, min(-1, 1));

EXPECT_EQ(5, max(4, 5));
EXPECT_EQ(1, max(-1, 1));
}

TEST(CommonFuncs, Swap)
{
EXPECT_EQ(0xf0, Common::swap8(0xf0));
EXPECT_EQ(0x1234, Common::swap16(0x3412));
EXPECT_EQ(0x12345678, Common::swap32(0x78563412));
EXPECT_EQ(0x123456789abcdef0ull, Common::swap64(0xf0debc9a78563412ull));
}
67 changes: 67 additions & 0 deletions Source/UnitTests/Common/FifoQueueTest.cpp
@@ -0,0 +1,67 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <gtest/gtest.h>
#include <thread>

#include "Common/FifoQueue.h"

TEST(FifoQueue, Simple)
{
Common::FifoQueue<u32> q;

EXPECT_EQ(0, q.Size());
EXPECT_TRUE(q.Empty());

q.Push(1);
EXPECT_EQ(1, q.Size());
EXPECT_FALSE(q.Empty());

u32 v; q.Pop(v);
EXPECT_EQ(1, v);
EXPECT_EQ(0, q.Size());
EXPECT_TRUE(q.Empty());

// Test the FIFO order.
for (u32 i = 0; i < 1000; ++i)
q.Push(i);
EXPECT_EQ(1000, q.Size());
for (u32 i = 0; i < 1000; ++i)
{
u32 v2; q.Pop(v2);
EXPECT_EQ(i, v2);
}
EXPECT_TRUE(q.Empty());

for (u32 i = 0; i < 1000; ++i)
q.Push(i);
EXPECT_FALSE(q.Empty());
q.Clear();
EXPECT_TRUE(q.Empty());
}

TEST(FifoQueue, MultiThreaded)
{
Common::FifoQueue<u32> q;

auto inserter = [&q]() {
for (u32 i = 0; i < 100000; ++i)
q.Push(i);
};

auto popper = [&q]() {
for (u32 i = 0; i < 100000; ++i)
{
while (q.Empty());
u32 v; q.Pop(v);
EXPECT_EQ(i, v);
}
};

std::thread popper_thread(popper);
std::thread inserter_thread(inserter);

popper_thread.join();
inserter_thread.join();
}
33 changes: 33 additions & 0 deletions Source/UnitTests/Common/FixedSizeQueueTest.cpp
@@ -0,0 +1,33 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <gtest/gtest.h>

#include "Common/FixedSizeQueue.h"

TEST(FixedSizeQueue, Simple)
{
FixedSizeQueue<int, 5> q;

EXPECT_EQ(0, q.size());

q.push(0);
q.push(1);
q.push(2);
q.push(3);
q.push(4);
for (int i = 0; i < 1000; ++i)
{
EXPECT_EQ(i, q.front());
EXPECT_EQ(i, q.pop_front());
q.push(i + 5);
}
EXPECT_EQ(1000, q.pop_front());
EXPECT_EQ(1001, q.pop_front());
EXPECT_EQ(1002, q.pop_front());
EXPECT_EQ(1003, q.pop_front());
EXPECT_EQ(1004, q.pop_front());

EXPECT_EQ(0, q.size());
}
56 changes: 56 additions & 0 deletions Source/UnitTests/Common/MathUtilTest.cpp
@@ -0,0 +1,56 @@
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <cmath>
#include <gtest/gtest.h>

#include "Common/MathUtil.h"

template <typename T>
T ClampAndReturn(const T& val, const T& min, const T& max)
{
T ret = val;
MathUtil::Clamp(&ret, min, max);
return ret;
}

TEST(MathUtil, Clamp)
{
EXPECT_EQ(1, ClampAndReturn(1, 0, 2));
EXPECT_EQ(1.0, ClampAndReturn(1.0, 0.0, 2.0));

EXPECT_EQ(2, ClampAndReturn(4, 0, 2));
EXPECT_EQ(2.0, ClampAndReturn(4.0, 0.0, 2.0));

EXPECT_EQ(0, ClampAndReturn(-1, 0, 2));
EXPECT_EQ(0.0, ClampAndReturn(-1.0, 0.0, 2.0));
}

TEST(MathUtil, IsNAN)
{
EXPECT_TRUE(MathUtil::IsNAN(nan("")));
}

TEST(MathUtil, IsQNAN)
{
// TODO
}

TEST(MathUtil, IsSNAN)
{
// TODO
}

TEST(MathUtil, Log2)
{
EXPECT_EQ(0, Log2(1));
EXPECT_EQ(1, Log2(2));
EXPECT_EQ(2, Log2(4));
EXPECT_EQ(3, Log2(8));
EXPECT_EQ(63, Log2(0x8000000000000000ull));

// Rounding behavior.
EXPECT_EQ(3, Log2(15));
EXPECT_EQ(63, Log2(0xFFFFFFFFFFFFFFFFull));
}
33 changes: 33 additions & 0 deletions Source/UnitTests/Core/MMIOTest.cpp
Expand Up @@ -23,6 +23,15 @@ TEST(UniqueID, UniqueEnough)
}
}

TEST(IsMMIOAddress, SpecialAddresses)
{
// WG Pipe address, should not be handled by MMIO.
EXPECT_FALSE(MMIO::IsMMIOAddress(0xCC008000));

// Memory zone used by games using the "MMU Speedhack".
EXPECT_FALSE(MMIO::IsMMIOAddress(0xE0000000));
}

class MappingTest : public testing::Test
{
protected:
Expand Down Expand Up @@ -75,3 +84,27 @@ TEST_F(MappingTest, ReadWriteDirect)
val32 += 1; m_mapping->Write(0xCC001234, val32);
}
}

TEST_F(MappingTest, ReadWriteComplex)
{
bool read_called = false, write_called = false;

m_mapping->Register(0xCC001234,
MMIO::ComplexRead<u8>([&read_called](u32 addr) {
EXPECT_EQ(0xCC001234, addr);
read_called = true;
return 0x12;
}),
MMIO::ComplexWrite<u8>([&write_called](u32 addr, u8 val) {
EXPECT_EQ(0xCC001234, addr);
EXPECT_EQ(0x34, val);
write_called = true;
})
);

u8 val; m_mapping->Read(0xCC001234, &val); EXPECT_EQ(0x12, val);
m_mapping->Write(0xCC001234, (u8)0x34);

EXPECT_TRUE(read_called);
EXPECT_TRUE(write_called);
}

0 comments on commit b003fd7

Please sign in to comment.