-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.cpp
231 lines (181 loc) · 6.86 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// oh baby a testing thing
#include <iostream>
#include <chrono>
#include "lib/steamid.hpp"
// ****************************************************************************
void PrintLine( const std::string &text ) {
std::cout << text << std::endl;
}
// ****************************************************************************
void PrintSubTest( const std::string &text ) {
std::cout << " " << text << std::endl;
}
// ****************************************************************************
class Test {
public:
std::string m_name;
std::function<bool()> m_method;
bool operator()() {
return m_method();
}
Test( const std::string &name, std::function<bool()> method ) {
m_name = name;
m_method = method;
}
};
// ****************************************************************************
class Tests {
std::vector<Test> tests;
public:
void Run() {
PrintLine( "---" );
PrintLine( "Running all tests." );
auto begin = std::chrono::high_resolution_clock::now();
for( auto & test: tests ) {
PrintLine( "" );
std::cout << '"' << test.m_name << '"' << std::endl;
if( test() ) {
PrintLine( "--- Passed. ---" );
} else {
PrintLine( "*** Failed! ***" );
}
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << (double)(std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count())/1000.0 << "ms" << std::endl;
}
Tests& operator <<( Test& test ) {
tests.push_back(test);
return *this;
}
};
// ****************************************************************************
long long random( long long min, long long max ) {
double a = rand();
a = a * (double)(max-min);
a = a / (double)RAND_MAX;
return min + (long long)floor(a+0.5);
}
void RunTests() {
Tests tests;
tests
// ************************************************************************
<< Test( "Conversion Test", [] {
SteamID steamid;
PrintSubTest( "32-bit detect" );
steamid = SteamID::Parse( " STEAM_1:1:54499221 " );
if( steamid.Empty() ) return false;
PrintSubTest( "32-bit direct" );
steamid = SteamID::Parse( "STEAM_0:1:54499221", SteamID::Formats::STEAMID32 );
if( steamid.Empty() ) return false;
PrintSubTest( "32-bit as 64 error" );
steamid = SteamID::Parse( "STEAM_1:1:54499221", SteamID::Formats::STEAMID64 );
if( *steamid ) return false;
PrintSubTest( "64-bit detect" );
steamid = SteamID::Parse( "76561198069264171" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "64-bit direct" );
steamid = SteamID::Parse( "76561198069264171", SteamID::Formats::STEAMID64 );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "64-bit errornous" );
steamid = SteamID::Parse( "76533611981069263334171", SteamID::Formats::STEAMID64 );
if( *steamid ) return false;
PrintSubTest( "64-bit as raw" );
steamid = SteamID::Parse( "76561198069264171", SteamID::Formats::RAW );
if( *steamid ) return false;
PrintSubTest( "v3 detect" );
steamid = SteamID::Parse( "[U:1:108998443]" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "v3 direct" );
steamid = SteamID::Parse( "[U:1:108998443]", SteamID::Formats::STEAMID3 );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "s32 detect" );
steamid = SteamID::Parse( "108998443" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "s32 direct" );
steamid = SteamID::Parse( "108998443", SteamID::Formats::S32 );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "raw direct" );
steamid = SteamID::Parse( "108998443", SteamID::Formats::RAW );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "community URL 1" );
steamid = SteamID::Parse( "http://www.steamcommunity.com/profiles/76561198069264171" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "community URL 2" );
steamid = SteamID::Parse( "http://steamcommunity.com/profiles/76561198069264171" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "community URL 3" );
steamid = SteamID::Parse( "www.steamcommunity.com/profiles/76561198069264171" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "community URL 4" );
steamid = SteamID::Parse( "steamcommunity.com/profiles/76561198069264171" );
if( steamid.Empty() ) return false;
if( steamid[ SteamID::Formats::STEAMID32 ] != "STEAM_1:1:54499221" ) return false;
PrintSubTest( "random conversions..." );
for( int i = 0; i < 5000; i++ ) {
// get a 32bit unsigned value
long long raw = random( 0, 4294967295 );
steamid = SteamID::Parse( std::to_string(raw), SteamID::Formats::RAW );
for( int j = 0; j < 8; j++ ) {
SteamID::Formats format = (SteamID::Formats)random( 1, 5 );
std::string formatted = steamid[ format ];
SteamID steamid2 = SteamID::Parse( formatted );
if( steamid2.Empty() || steamid2.Format( format ) != formatted ) {
PrintSubTest( "failure:" );
std::cout << *steamid;
return false;
}
steamid = steamid2;
}
}
return true;
})
// ************************************************************************
<< Test( "Large SteamID conversions", [] {
for( int i = 0; i < 255; i++ ) {
// get a 64bit-ish value
long long a = random( 1, SteamID::MAX_VALUE );
SteamID steamid = SteamID::Parse( std::to_string(a), SteamID::Formats::RAW );
for( int j = 0; j < 8; j++ ) {
SteamID::Formats format = (SteamID::Formats)random(1,5);
std::string formatted = steamid[ format ];
if( format == SteamID::Formats::S32 ) {
// for S32 check for proper failure.
if( formatted == "" ) {
if( *steamid >= 4294967296L ) {
continue;
}
}
}
SteamID steamid2 = SteamID::Parse( formatted );
if( !steamid2 || steamid2[ format ] != formatted ) {
PrintSubTest( "failure: (format=format)" );
std::cout << *steamid;
return false;
}
steamid = steamid2;
}
}
return true;
});
// ************************************************************************
SteamID::ParseRawDefault( true );
try {
tests.Run();
} catch( std::exception &e ) {
std::cout << "Exception: " << e.what() << std::endl;
}
}
int main() {
RunTests();
getchar();
}