]> git.stg.codes - stg.git/blob - stglibs/common.lib/test.cpp
Добавление исходников
[stg.git] / stglibs / common.lib / test.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
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 for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov
19  */
20
21  /*
22  $Revision: 1.6 $
23  $Date: 2009/06/10 10:31:15 $
24  $Author: faust $
25  */
26
27 #include <iostream>
28 #include <limits.h>
29 #include <arpa/inet.h>
30
31 using namespace std;
32
33 #include "common.h"
34 #include "test.h"
35
36 time_t stgTime;
37
38 int main(void)
39 {
40 char buf1[256];
41 BLOWFISH_CTX ctx;
42 int functions = 0, ok = 0;
43
44 cout << "Testing common.lib" << endl << "---------------\
45 --------------------" << endl;
46
47 if (!TestIntToKMG())
48     ok++;
49 functions++;
50 if (!Teststrtodouble2())
51     ok++;
52 functions++;
53 if (!TestIsDigit())
54     ok++;
55 functions++;
56 if (!TestIsAlpha())
57     ok++;
58 functions++;
59 if (!TestEncodeDecode())
60     ok++;
61 functions++;
62 if (!TestParseIPString())
63     ok++;
64 functions++;
65 if (!TestKOIToWIN())
66     ok++;
67 functions++;
68 if (!TestDaysInMonth())
69     ok++;
70 functions++;
71 if (!TestBlowfish())
72     ok++;
73 functions++;
74 if (!TestMin8())
75     ok++;
76 functions++;
77 if (!Testinet_ntostr())
78     ok++;
79 functions++;
80 if (!TestParseTariffTimeStr())
81     ok++;
82 functions++;
83 if (!TestStr2XX2Str())
84     ok++;
85 functions++;
86
87 cout << "------------------------------------" << endl;
88 cout << "Functions: \t\t\t" << functions << endl;
89 cout << "OK's: \t\t\t\t" << ok << endl;
90 cout << "Fails: \t\t\t\t" << functions - ok << endl;
91
92 return (functions != ok);
93 }
94
95 int TestIntToKMG()
96 {
97 int res = 1;
98 cout << "Testing IntToKMG: \t\t";
99 res = res && (strcmp(IntToKMG(LONG_LONG_MAX), TEST1_LLMAX) == 0);
100 //cout << IntToKMG(LONG_LONG_MAX) << " " << TEST1_LLMAX << endl;
101
102 res = res && (strcmp(IntToKMG(1024 * 1024 + 1), TEST1_1) == 0);
103 //cout << IntToKMG(1024 * 1024 + 1) << " " << TEST1_1 << endl;
104
105 res = res && (strcmp(IntToKMG(0), TEST1_0) == 0);
106 //cout << IntToKMG(0) << " " << TEST1_0 << endl;
107
108 res = res && (strcmp(IntToKMG(LONG_LONG_MIN), TEST1_LLMIN) == 0);
109 //cout << IntToKMG(LONG_LONG_MIN) << " " << TEST1_LLMIN << endl;
110
111 if (res)
112     cout << "OK" << endl;
113 else
114     cout << "Fail" << endl;
115 return !res;
116 }
117
118 int Teststrtodouble2()
119 {
120 double a;
121 int res = 1;
122 cout << "Testing strtodouble2: \t\t";
123 res = res && !strtodouble2("0.0", a);
124 res = res && (a == 0.0);
125 res = res && !strtodouble2("0.123456", a);
126 res = res && (a == 0.123456);
127 res = res && !strtodouble2("123456.0", a);
128 res = res && (a == 123456.0);
129 res = res && !strtodouble2("123456.123456", a);
130 res = res && (a == 123456.123456);
131 res = res && !strtodouble2("-0.123456", a);
132 res = res && (a == -0.123456);
133 res = res && !strtodouble2("-123456.0", a);
134 res = res && (a == -123456.0);
135 res = res && !strtodouble2("-123456.123456", a);
136 res = res && (a == -123456.123456);
137 if (res)
138     cout << "OK" << endl;
139 else
140     cout << "Fail" << endl;
141 return !res;
142 }
143
144 int TestIsDigit()
145 {
146 char a;
147 int res = 1;
148 cout << "Testing IsDigit: \t\t";
149 for(a = '0'; a < '9'; a++)
150     res = res && IsDigit(a);
151 for(a = 'a'; a < 'z'; a++)
152     res = res && !IsDigit(a);
153 if (res)
154     cout << "OK" << endl;
155 else
156     cout << "Fail" << endl;
157 return !res;
158 }
159
160 int TestIsAlpha()
161 {
162 char a;
163 int res = 1;
164 cout << "Testing IsAlpha: \t\t";
165 for(a = '0'; a < '9'; a++)
166     res = res && !IsAlpha(a);
167 for(a = 'a'; a < 'z'; a++)
168     res = res && IsAlpha(a);
169 if (res)
170     cout << "OK" << endl;
171 else
172     cout << "Fail" << endl;
173 return !res;
174 }
175
176 int TestEncodeDecode()
177 {
178 char enc[256], dec[512];
179 int res = 1;
180 cout << "Testing EncodeDecode: \t\t";
181 Encode12(enc, TEST2_STRING, strlen(TEST2_STRING));
182 Decode21(dec, enc);
183 res = res && !strcmp(dec, TEST2_STRING);
184 Encode12(enc, TEST2_STRING, 256); // Overflow
185 Decode21(dec, enc);
186 res = res && !strcmp(dec, TEST2_STRING);
187 Encode12(enc, TEST2_STRING, 5); // Underflow
188 Decode21(dec, enc);
189 res = res && !strcmp(dec, TEST2_PART);
190 if (res)
191     cout << "OK" << endl;
192 else
193     cout << "Fail" << endl;
194 return !res;
195 }
196
197 int TestParseIPString()
198 {
199 unsigned int ips[4];
200 int res = 1;
201 cout << "Testing ParseIPString: \t\t";
202 res = res && (ParseIPString("127.0.0.1, 192.168.58.1, 10.0.0.1", ips, 4) == 0);
203 res = res && ips[0] == 0x0100007F;
204 res = res && ips[1] == 0x013AA8C0;
205 res = res && ips[2] == 0x0100000A;
206 if (res)
207     cout << "OK" << endl;
208 else
209     cout << "Fail" << endl;
210 return !res;
211 }
212
213 int TestKOIToWIN()
214 {
215 char enc[256], dec[256];
216 int res = 1;
217 cout << "Testing KOIToWin: \t\t";
218 KOIToWin(TEST3_STRING, enc, 256);
219 WinToKOI(enc, dec, 256);
220 res = res && !strcmp(dec, TEST3_STRING);
221 KOIToWin(TEST3_STRING, enc, strlen(TEST3_STRING) - 5);
222 WinToKOI(enc, dec, strlen(TEST3_STRING) - 5);
223 res = res && !strcmp(dec, TEST3_STRING);
224 KOIToWin(TEST3_STRING, enc, strlen(TEST3_STRING) + 5);
225 WinToKOI(enc, dec, strlen(TEST3_STRING) + 5);
226 res = res && !strcmp(dec, TEST3_STRING);
227 if (res)
228     cout << "OK" << endl;
229 else
230     cout << "Fail" << endl;
231 return !res;
232 }
233
234 int TestDaysInMonth()
235 {
236 int res = 1;
237 cout << "Testing DaysInMonth: \t\t";
238 res = res && (DaysInMonth(2000, 0) == 31);
239 res = res && (DaysInMonth(2000, 1) == 29);
240 res = res && (DaysInMonth(2001, 1) == 28);
241 res = res && (DaysInMonth(2100, 1) == 28);
242 res = res && (DaysInMonth(2400, 1) == 29);
243 res = res && (DaysInMonth(2000, 2) == 31);
244 res = res && (DaysInMonth(2000, 3) == 30);
245 res = res && (DaysInMonth(2000, 4) == 31);
246 res = res && (DaysInMonth(2000, 5) == 30);
247 res = res && (DaysInMonth(2000, 6) == 31);
248 res = res && (DaysInMonth(2000, 7) == 31);
249 res = res && (DaysInMonth(2000, 8) == 30);
250 res = res && (DaysInMonth(2000, 9) == 31);
251 res = res && (DaysInMonth(2000, 10) == 30);
252 res = res && (DaysInMonth(2000, 11) == 31);
253 res = res && (DaysInMonth(2000, 20) == 33);
254 if (res)
255     cout << "OK" << endl;
256 else
257     cout << "Fail" << endl;
258 return !res;
259 }
260
261 int TestBlowfish()
262 {
263 BLOWFISH_CTX ctx;
264 char enc[256], dec[256];
265 int res = 1, i, len = strlen(TEST4_STRING);
266 cout << "Testing Blowfish: \t\t";
267 EnDecodeInit(TEST4_PASSWORD, strlen(TEST4_PASSWORD), &ctx);
268 strcpy(dec, TEST4_STRING);
269 for(i = 0; i < len; i += 8)
270     EncodeString(&enc[i], &dec[i], &ctx);
271 for(i = 0; i < len; i += 8)
272     DecodeString(&dec[i], &enc[i], &ctx);
273 res = res && !strcmp(dec, TEST4_STRING);
274 if (res)
275     cout << "OK" << endl;
276 else
277     cout << "Fail" << endl;
278 return !res;
279 }
280
281 int TestMin8()
282 {
283 int res = 1;
284 cout << "Testing Min8: \t\t\t";
285 res = res && (Min8(INT_MAX) == INT_MAX + 1);
286 res = res && (Min8(INT_MIN) == INT_MIN);
287 res = res && (Min8(0) == 0);
288 res = res && (Min8(7) == 8);
289 res = res && (Min8(8) == 8);
290 res = res && (Min8(9) == 16);
291 if (res)
292     cout << "OK" << endl;
293 else
294     cout << "Fail" << endl;
295 return !res;;
296 }
297
298 int Testinet_ntostr()
299 {
300 unsigned long ip;
301 char buf[32];
302 int res = 1;
303 cout << "Testing inet_ntostr: \t\t";
304 res = res && (strcmp(inet_ntostr(inet_addr("127.0.0.1")), "127.0.0.1") == 0);
305 res = res && (strcmp(inet_ntostr(inet_addr("255.255.255.255")), "255.255.255.255") == 0);
306 res = res && (strcmp(inet_ntostr(inet_addr("0.0.0.0")), "0.0.0.0") == 0);
307 res = res && (strcmp(inet_ntostr(inet_addr("10.0.0.1")), "10.0.0.1") == 0);
308 res = res && (strcmp(inet_ntostr(inet_addr("192.168.58.240")), "192.168.58.240") == 0);
309 if (res)
310     cout << "OK" << endl;
311 else
312     cout << "Fail" << endl;
313 return !res;
314 }
315
316 int TestParseTariffTimeStr()
317 {
318 int h1, m1, h2, m2;
319 int res = 1;
320 cout << "Testing ParseTariffTimeStr: \t";
321 res = res && !ParseTariffTimeStr("00:00-00:00", h1, m1, h2, m2);
322 res = res && (h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0);
323 res = res && !ParseTariffTimeStr("0:0-0:0", h1, m1, h2, m2);
324 res = res && (h1 == 0 && m1 == 0 && h2 == 0 && m2 == 0);
325 res = res && !ParseTariffTimeStr("99:99-99:99", h1, m1, h2, m2);
326 res = res && (h1 == 99 && m1 == 99 && h2 == 99 && m2 == 99);
327 res = res && !ParseTariffTimeStr("12:34-56:78", h1, m1, h2, m2);
328 res = res && (h1 == 12 && m1 == 34 && h2 == 56 && m2 == 78);
329 if (res)
330     cout << "OK" << endl;
331 else
332     cout << "Fail" << endl;
333 return !res;
334 }
335 //-----------------------------------------------------------------------------
336 int TestStr2XX2Str()
337 {
338 cout << "Testing Str2XX2Str: \t\t";
339
340 # define INT8_MIN       (-128)
341 # define INT16_MIN      (-32767-1)
342 # define INT32_MIN      (-2147483647-1)
343 # define INT64_MIN      (-__INT64_C(9223372036854775807)-1)
344 # define INT8_MAX       (127)
345 # define INT16_MAX      (32767)
346 # define INT32_MAX      (2147483647)
347 # define INT64_MAX      (__INT64_C(9223372036854775807))
348
349 int xx;
350 string s;
351 for (int i = -5000000; i < 5000000; i+=10)
352 {
353     x2str(i, s);
354     str2x(s, xx);
355     if (i != xx)
356     {
357     cout << "Fail" << endl;
358         return 1;
359     }
360 }
361
362 x2str(INT32_MIN, s);
363 str2x(s, xx);
364 if (xx != INT32_MIN)
365 {
366     cout << INT32_MIN << " " << s << endl;
367     cout << INT32_MIN << " " << xx << endl;
368 cout << "Fail" << endl;
369     return 1;
370 }
371
372 x2str(INT32_MAX, s);
373 str2x(s, xx);
374 if (xx != INT32_MAX)
375 {
376 cout << "Fail" << endl;
377     return 1;
378 }
379 cout << "OK" << endl;
380 return 0;
381 }
382 //-----------------------------------------------------------------------------
383