Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from delroth/tests
Add more tests for Common and Core/MMIO
- Loading branch information
Showing
7 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters