Linux build fix
[dolphin.git] / Source / UnitTests / UnitTests.cpp
blobc732ea3d1e2d9a28b748dcc1da54f226c76ce774
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include <cmath>
19 #include <iostream>
21 #include "StringUtil.h"
22 #include "MathUtil.h"
23 #include "PowerPC/PowerPC.h"
24 #include "HW/SI_DeviceGCController.h"
26 void AudioJitTests();
28 using namespace std;
29 int fail_count = 0;
31 #define EXPECT_TRUE(a) \
32 if (!a) { \
33 cout << "FAIL (" << __FUNCTION__ << "): " << #a << " is false" << endl; \
34 cout << "Value: " << a << endl << "Expected: true" << endl; \
35 fail_count++; \
38 #define EXPECT_FALSE(a) \
39 if (a) { \
40 cout << "FAIL (" << __FUNCTION__ << "): " << #a << " is true" << endl; \
41 cout << "Value: " << a << endl << "Expected: false" << endl; \
42 fail_count++; \
45 #define EXPECT_EQ(a, b) \
46 if ((a) != (b)) { \
47 cout << "FAIL (" << __FUNCTION__ << "): " << #a << " is not equal to " << #b << endl; \
48 cout << "Actual: " << a << endl << "Expected: " << b << endl; \
49 fail_count++; \
52 void CoreTests()
56 void MathTests()
58 // Tests that our fp classifier is correct.
59 EXPECT_EQ(MathUtil::ClassifyDouble(1.0), MathUtil::PPC_FPCLASS_PN);
60 EXPECT_EQ(MathUtil::ClassifyDouble(-1.0), MathUtil::PPC_FPCLASS_NN);
61 EXPECT_EQ(MathUtil::ClassifyDouble(1235223.0), MathUtil::PPC_FPCLASS_PN);
62 EXPECT_EQ(MathUtil::ClassifyDouble(-1263221.0), MathUtil::PPC_FPCLASS_NN);
63 EXPECT_EQ(MathUtil::ClassifyDouble(1.0E-308), MathUtil::PPC_FPCLASS_PD);
64 EXPECT_EQ(MathUtil::ClassifyDouble(-1.0E-308), MathUtil::PPC_FPCLASS_ND);
65 EXPECT_EQ(MathUtil::ClassifyDouble(0.0), MathUtil::PPC_FPCLASS_PZ);
66 EXPECT_EQ(MathUtil::ClassifyDouble(-0.0), MathUtil::PPC_FPCLASS_NZ);
67 EXPECT_EQ(MathUtil::ClassifyDouble(HUGE_VAL), MathUtil::PPC_FPCLASS_PINF); // weird #define for infinity
68 EXPECT_EQ(MathUtil::ClassifyDouble(-HUGE_VAL), MathUtil::PPC_FPCLASS_NINF);
69 EXPECT_EQ(MathUtil::ClassifyDouble(sqrt(-1.0)), MathUtil::PPC_FPCLASS_QNAN);
71 // Float version
72 EXPECT_EQ(MathUtil::ClassifyFloat(1.0f), MathUtil::PPC_FPCLASS_PN);
73 EXPECT_EQ(MathUtil::ClassifyFloat(-1.0f), MathUtil::PPC_FPCLASS_NN);
74 EXPECT_EQ(MathUtil::ClassifyFloat(1235223.0f), MathUtil::PPC_FPCLASS_PN);
75 EXPECT_EQ(MathUtil::ClassifyFloat(-1263221.0f), MathUtil::PPC_FPCLASS_NN);
76 EXPECT_EQ(MathUtil::ClassifyFloat(1.0E-43f), MathUtil::PPC_FPCLASS_PD);
77 EXPECT_EQ(MathUtil::ClassifyFloat(-1.0E-43f), MathUtil::PPC_FPCLASS_ND);
78 EXPECT_EQ(MathUtil::ClassifyFloat(0.0f), MathUtil::PPC_FPCLASS_PZ);
79 EXPECT_EQ(MathUtil::ClassifyFloat(-0.0f), MathUtil::PPC_FPCLASS_NZ);
80 EXPECT_EQ(MathUtil::ClassifyFloat((float)HUGE_VAL), MathUtil::PPC_FPCLASS_PINF); // weird #define for infinity
81 EXPECT_EQ(MathUtil::ClassifyFloat((float)-HUGE_VAL), MathUtil::PPC_FPCLASS_NINF);
82 EXPECT_EQ(MathUtil::ClassifyFloat(sqrtf(-1.0f)), MathUtil::PPC_FPCLASS_QNAN);
84 EXPECT_FALSE(MathUtil::IsNAN(1.0));
85 EXPECT_TRUE(MathUtil::IsNAN(sqrt(-1.0)));
86 EXPECT_FALSE(MathUtil::IsSNAN(sqrt(-1.0)));
88 // EXPECT_TRUE(MathUtil::IsQNAN(sqrt(-1.0))); // Hmm...
89 EXPECT_EQ(pow2(2.0), 4.0);
90 EXPECT_EQ(pow2(-2.0), 4.0);
93 void StringTests()
95 EXPECT_EQ(StripSpaces(" abc "), "abc");
96 EXPECT_EQ(StripNewline(" abc \n"), " abc ");
97 EXPECT_EQ(StripNewline(" abc \n "), " abc \n ");
98 EXPECT_EQ(StripQuotes("\"abc\""), "abc");
99 EXPECT_EQ(StripQuotes("\"abc\" "), "\"abc\" ");
100 EXPECT_EQ(TabsToSpaces(4, "a\tb"), "a b");
104 int main(int argc, char* argv[])
106 AudioJitTests();
108 CoreTests();
109 MathTests();
110 StringTests();
111 if (fail_count == 0)
113 printf("All tests passed.\n");
115 return 0;
119 // Pretend that we are a host so we can link to core.... urgh.
120 //==============================================================
121 void Host_UpdateMainFrame(){}
122 void Host_UpdateDisasmDialog(){}
123 void Host_UpdateLogDisplay(){}
124 void Host_UpdateMemoryView(){}
125 void Host_NotifyMapLoaded(){}
126 void Host_ShowJitResults(unsigned int address){}
127 void Host_UpdateBreakPointView(){}
128 void Host_SetDebugMode(bool enable){}
130 void Host_SetWaitCursor(bool enable){}
132 void Host_UpdateStatusBar(const char* _pText, int Filed = 0){}
134 void Host_SysMessage(const char *fmt, ...){}
135 void Host_SetWiiMoteConnectionState(int _State){}
137 void Host_UpdateLeds(int bits){}
138 void Host_UpdateSpeakerStatus(int index, int bits){}
139 void Host_UpdateStatus(){}
140 void Host_Message(int){}