mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Add more tests for Common and Core/MMIO
This commit is contained in:
4
Source/UnitTests/Common/CMakeLists.txt
Normal file
4
Source/UnitTests/Common/CMakeLists.txt
Normal file
@ -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
Source/UnitTests/Common/CommonFuncsTest.cpp
Normal file
46
Source/UnitTests/Common/CommonFuncsTest.cpp
Normal file
@ -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
Source/UnitTests/Common/FifoQueueTest.cpp
Normal file
67
Source/UnitTests/Common/FifoQueueTest.cpp
Normal file
@ -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
Source/UnitTests/Common/FixedSizeQueueTest.cpp
Normal file
33
Source/UnitTests/Common/FixedSizeQueueTest.cpp
Normal file
@ -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
Source/UnitTests/Common/MathUtilTest.cpp
Normal file
56
Source/UnitTests/Common/MathUtilTest.cpp
Normal file
@ -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));
|
||||
}
|
Reference in New Issue
Block a user