Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #146 from delroth/tests

Add more tests for Common and Core/MMIO
  • Loading branch information...
commit b003fd79d7bee1780d980465b03b23d0fd266e9e 2 parents 0ea58cd + aabd524
Pierre Bourdon authored
1  Source/UnitTests/CMakeLists.txt
@@ -8,4 +8,5 @@ macro(add_dolphin_test target srcs libs)
8 8
 	add_test(NAME ${target} COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Tests/${target})
9 9
 endmacro(add_dolphin_test)
10 10
 
  11
+add_subdirectory(Common)
11 12
 add_subdirectory(Core)
4  Source/UnitTests/Common/CMakeLists.txt
... ...
@@ -0,0 +1,4 @@
  1
+add_dolphin_test(CommonFuncsTest CommonFuncsTest.cpp common)
  2
+add_dolphin_test(FifoQueueTest FifoQueueTest.cpp common)
  3
+add_dolphin_test(FixedSizeQueueTest FixedSizeQueueTest.cpp common)
  4
+add_dolphin_test(MathUtilTest MathUtilTest.cpp common)
46  Source/UnitTests/Common/CommonFuncsTest.cpp
... ...
@@ -0,0 +1,46 @@
  1
+// Copyright 2014 Dolphin Emulator Project
  2
+// Licensed under GPLv2
  3
+// Refer to the license.txt file included.
  4
+
  5
+#include <gtest/gtest.h>
  6
+
  7
+#include "Common/CommonFuncs.h"
  8
+
  9
+TEST(CommonFuncs, ArraySizeMacro)
  10
+{
  11
+	char test[4];
  12
+	u32 test2[42];
  13
+
  14
+	EXPECT_EQ(4, ArraySize(test));
  15
+	EXPECT_EQ(42, ArraySize(test2));
  16
+}
  17
+
  18
+TEST(CommonFuncs, RoundUpPow2Macro)
  19
+{
  20
+	EXPECT_EQ(4, ROUND_UP_POW2(3));
  21
+	EXPECT_EQ(4, ROUND_UP_POW2(4));
  22
+	EXPECT_EQ(8, ROUND_UP_POW2(6));
  23
+	EXPECT_EQ(0x40000000, ROUND_UP_POW2(0x23456789));
  24
+}
  25
+
  26
+TEST(CommonFuncs, CrashMacro)
  27
+{
  28
+	EXPECT_DEATH({ Crash(); }, "");
  29
+}
  30
+
  31
+TEST(CommonFuncs, MinMax)
  32
+{
  33
+	EXPECT_EQ(4, min(4, 5));
  34
+	EXPECT_EQ(-1, min(-1, 1));
  35
+
  36
+	EXPECT_EQ(5, max(4, 5));
  37
+	EXPECT_EQ(1, max(-1, 1));
  38
+}
  39
+
  40
+TEST(CommonFuncs, Swap)
  41
+{
  42
+	EXPECT_EQ(0xf0, Common::swap8(0xf0));
  43
+	EXPECT_EQ(0x1234, Common::swap16(0x3412));
  44
+	EXPECT_EQ(0x12345678, Common::swap32(0x78563412));
  45
+	EXPECT_EQ(0x123456789abcdef0ull, Common::swap64(0xf0debc9a78563412ull));
  46
+}
67  Source/UnitTests/Common/FifoQueueTest.cpp
... ...
@@ -0,0 +1,67 @@
  1
+// Copyright 2014 Dolphin Emulator Project
  2
+// Licensed under GPLv2
  3
+// Refer to the license.txt file included.
  4
+
  5
+#include <gtest/gtest.h>
  6
+#include <thread>
  7
+
  8
+#include "Common/FifoQueue.h"
  9
+
  10
+TEST(FifoQueue, Simple)
  11
+{
  12
+	Common::FifoQueue<u32> q;
  13
+
  14
+	EXPECT_EQ(0, q.Size());
  15
+	EXPECT_TRUE(q.Empty());
  16
+
  17
+	q.Push(1);
  18
+	EXPECT_EQ(1, q.Size());
  19
+	EXPECT_FALSE(q.Empty());
  20
+
  21
+	u32 v; q.Pop(v);
  22
+	EXPECT_EQ(1, v);
  23
+	EXPECT_EQ(0, q.Size());
  24
+	EXPECT_TRUE(q.Empty());
  25
+
  26
+	// Test the FIFO order.
  27
+	for (u32 i = 0; i < 1000; ++i)
  28
+		q.Push(i);
  29
+	EXPECT_EQ(1000, q.Size());
  30
+	for (u32 i = 0; i < 1000; ++i)
  31
+	{
  32
+		u32 v2; q.Pop(v2);
  33
+		EXPECT_EQ(i, v2);
  34
+	}
  35
+	EXPECT_TRUE(q.Empty());
  36
+
  37
+	for (u32 i = 0; i < 1000; ++i)
  38
+		q.Push(i);
  39
+	EXPECT_FALSE(q.Empty());
  40
+	q.Clear();
  41
+	EXPECT_TRUE(q.Empty());
  42
+}
  43
+
  44
+TEST(FifoQueue, MultiThreaded)
  45
+{
  46
+	Common::FifoQueue<u32> q;
  47
+
  48
+	auto inserter = [&q]() {
  49
+		for (u32 i = 0; i < 100000; ++i)
  50
+			q.Push(i);
  51
+	};
  52
+
  53
+	auto popper = [&q]() {
  54
+		for (u32 i = 0; i < 100000; ++i)
  55
+		{
  56
+			while (q.Empty());
  57
+			u32 v; q.Pop(v);
  58
+			EXPECT_EQ(i, v);
  59
+		}
  60
+	};
  61
+
  62
+	std::thread popper_thread(popper);
  63
+	std::thread inserter_thread(inserter);
  64
+
  65
+	popper_thread.join();
  66
+	inserter_thread.join();
  67
+}
33  Source/UnitTests/Common/FixedSizeQueueTest.cpp
... ...
@@ -0,0 +1,33 @@
  1
+// Copyright 2014 Dolphin Emulator Project
  2
+// Licensed under GPLv2
  3
+// Refer to the license.txt file included.
  4
+
  5
+#include <gtest/gtest.h>
  6
+
  7
+#include "Common/FixedSizeQueue.h"
  8
+
  9
+TEST(FixedSizeQueue, Simple)
  10
+{
  11
+	FixedSizeQueue<int, 5> q;
  12
+
  13
+	EXPECT_EQ(0, q.size());
  14
+
  15
+	q.push(0);
  16
+	q.push(1);
  17
+	q.push(2);
  18
+	q.push(3);
  19
+	q.push(4);
  20
+	for (int i = 0; i < 1000; ++i)
  21
+	{
  22
+		EXPECT_EQ(i, q.front());
  23
+		EXPECT_EQ(i, q.pop_front());
  24
+		q.push(i + 5);
  25
+	}
  26
+	EXPECT_EQ(1000, q.pop_front());
  27
+	EXPECT_EQ(1001, q.pop_front());
  28
+	EXPECT_EQ(1002, q.pop_front());
  29
+	EXPECT_EQ(1003, q.pop_front());
  30
+	EXPECT_EQ(1004, q.pop_front());
  31
+
  32
+	EXPECT_EQ(0, q.size());
  33
+}
56  Source/UnitTests/Common/MathUtilTest.cpp
... ...
@@ -0,0 +1,56 @@
  1
+// Copyright 2014 Dolphin Emulator Project
  2
+// Licensed under GPLv2
  3
+// Refer to the license.txt file included.
  4
+
  5
+#include <cmath>
  6
+#include <gtest/gtest.h>
  7
+
  8
+#include "Common/MathUtil.h"
  9
+
  10
+template <typename T>
  11
+T ClampAndReturn(const T& val, const T& min, const T& max)
  12
+{
  13
+	T ret = val;
  14
+	MathUtil::Clamp(&ret, min, max);
  15
+	return ret;
  16
+}
  17
+
  18
+TEST(MathUtil, Clamp)
  19
+{
  20
+	EXPECT_EQ(1, ClampAndReturn(1, 0, 2));
  21
+	EXPECT_EQ(1.0, ClampAndReturn(1.0, 0.0, 2.0));
  22
+
  23
+	EXPECT_EQ(2, ClampAndReturn(4, 0, 2));
  24
+	EXPECT_EQ(2.0, ClampAndReturn(4.0, 0.0, 2.0));
  25
+
  26
+	EXPECT_EQ(0, ClampAndReturn(-1, 0, 2));
  27
+	EXPECT_EQ(0.0, ClampAndReturn(-1.0, 0.0, 2.0));
  28
+}
  29
+
  30
+TEST(MathUtil, IsNAN)
  31
+{
  32
+	EXPECT_TRUE(MathUtil::IsNAN(nan("")));
  33
+}
  34
+
  35
+TEST(MathUtil, IsQNAN)
  36
+{
  37
+	// TODO
  38
+}
  39
+
  40
+TEST(MathUtil, IsSNAN)
  41
+{
  42
+	// TODO
  43
+}
  44
+
  45
+TEST(MathUtil, Log2)
  46
+{
  47
+	EXPECT_EQ(0, Log2(1));
  48
+	EXPECT_EQ(1, Log2(2));
  49
+	EXPECT_EQ(2, Log2(4));
  50
+	EXPECT_EQ(3, Log2(8));
  51
+	EXPECT_EQ(63, Log2(0x8000000000000000ull));
  52
+
  53
+	// Rounding behavior.
  54
+	EXPECT_EQ(3, Log2(15));
  55
+	EXPECT_EQ(63, Log2(0xFFFFFFFFFFFFFFFFull));
  56
+}
33  Source/UnitTests/Core/MMIOTest.cpp
@@ -23,6 +23,15 @@
23 23
 	}
24 24
 }
25 25
 
  26
+TEST(IsMMIOAddress, SpecialAddresses)
  27
+{
  28
+	// WG Pipe address, should not be handled by MMIO.
  29
+	EXPECT_FALSE(MMIO::IsMMIOAddress(0xCC008000));
  30
+
  31
+	// Memory zone used by games using the "MMU Speedhack".
  32
+	EXPECT_FALSE(MMIO::IsMMIOAddress(0xE0000000));
  33
+}
  34
+
26 35
 class MappingTest : public testing::Test
27 36
 {
28 37
 protected:
@@ -75,3 +84,27 @@ class MappingTest : public testing::Test
75 84
 		val32 += 1; m_mapping->Write(0xCC001234, val32);
76 85
 	}
77 86
 }
  87
+
  88
+TEST_F(MappingTest, ReadWriteComplex)
  89
+{
  90
+	bool read_called = false, write_called = false;
  91
+
  92
+	m_mapping->Register(0xCC001234,
  93
+		MMIO::ComplexRead<u8>([&read_called](u32 addr) {
  94
+			EXPECT_EQ(0xCC001234, addr);
  95
+			read_called = true;
  96
+			return 0x12;
  97
+		}),
  98
+		MMIO::ComplexWrite<u8>([&write_called](u32 addr, u8 val) {
  99
+			EXPECT_EQ(0xCC001234, addr);
  100
+			EXPECT_EQ(0x34, val);
  101
+			write_called = true;
  102
+		})
  103
+	);
  104
+
  105
+	u8 val; m_mapping->Read(0xCC001234, &val); EXPECT_EQ(0x12, val);
  106
+	m_mapping->Write(0xCC001234, (u8)0x34);
  107
+
  108
+	EXPECT_TRUE(read_called);
  109
+	EXPECT_TRUE(write_called);
  110
+}

0 notes on commit b003fd7

Please sign in to comment.
Something went wrong with that request. Please try again.