StringUtil cleanup. Nothing seems broken.
[dolphin.git] / Source / UnitTests / UnitTests.cpp
blobe2a1975c53165dd0ecfbecb2b17d2e256d2d6944
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");
97 EXPECT_EQ(StripNewline(" abc \n"), " abc ");
98 EXPECT_EQ(StripNewline(" abc \n "), " abc \n ");
100 EXPECT_EQ(StripQuotes("\"abc\""), "abc");
101 EXPECT_EQ(StripQuotes("\"abc\" "), "\"abc\" ");
103 EXPECT_EQ(TabsToSpaces(4, "a\tb"), "a b");
104 EXPECT_EQ(ThousandSeparate(1234567, 15), " 1,234,567");
106 int i = 7;
107 EXPECT_EQ(false, TryParse("FF", &i));
108 EXPECT_EQ(7, i);
109 EXPECT_EQ(true, TryParse("22", &i));
110 EXPECT_EQ(22, i);
112 std::vector<std::string> strs;
113 SplitString("blah,foo,bar", ',', strs);
115 EXPECT_EQ(3, strs.size());
116 EXPECT_EQ("bar", strs[2]);
118 std::string path, fname, ext;
119 SplitPath("C:/some/path/test.jpg", &path, &fname, &ext);
120 EXPECT_EQ("C:/some/path/", path);
121 EXPECT_EQ("test", fname);
122 EXPECT_EQ(".jpg", ext);
124 SplitPath("C:/so.me/path/", &path, &fname, &ext);
125 EXPECT_EQ("C:/so.me/path/", path);
126 EXPECT_EQ("", fname);
127 EXPECT_EQ("", ext);
129 SplitPath("test.file.jpg", &path, &fname, &ext);
130 EXPECT_EQ("", path);
131 EXPECT_EQ("test.file", fname);
132 EXPECT_EQ(".jpg", ext);
136 int main(int argc, char* argv[])
138 AudioJitTests();
140 CoreTests();
141 MathTests();
142 StringTests();
143 if (fail_count == 0)
145 printf("All tests passed.\n");
147 return 0;
151 // Pretend that we are a host so we can link to core.... urgh.
152 //==============================================================
153 void Host_UpdateMainFrame(){}
154 void Host_UpdateDisasmDialog(){}
155 void Host_UpdateLogDisplay(){}
156 void Host_UpdateMemoryView(){}
157 void Host_NotifyMapLoaded(){}
158 void Host_ShowJitResults(unsigned int address){}
159 void Host_UpdateBreakPointView(){}
160 void Host_SetDebugMode(bool enable){}
162 void Host_SetWaitCursor(bool enable){}
164 void Host_UpdateStatusBar(const char* _pText, int Filed = 0){}
166 void Host_SysMessage(const char *fmt, ...){}
167 void Host_SetWiiMoteConnectionState(int _State){}
169 void Host_UpdateLeds(int bits){}
170 void Host_UpdateSpeakerStatus(int index, int bits){}
171 void Host_UpdateStatus(){}
172 void Host_Message(int){}