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.
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.
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
18 * Author : Maxim Mamontov
23 $Date: 2009/06/10 10:31:15 $
29 #include <arpa/inet.h>
42 int functions = 0, ok = 0;
44 cout << "Testing common.lib" << endl << "---------------\
45 --------------------" << endl;
50 if (!Teststrtodouble2())
59 if (!TestEncodeDecode())
62 if (!TestParseIPString())
68 if (!TestDaysInMonth())
77 if (!Testinet_ntostr())
80 if (!TestParseTariffTimeStr())
83 if (!TestStr2XX2Str())
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;
92 return (functions != ok);
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;
102 res = res && (strcmp(IntToKMG(1024 * 1024 + 1), TEST1_1) == 0);
103 //cout << IntToKMG(1024 * 1024 + 1) << " " << TEST1_1 << endl;
105 res = res && (strcmp(IntToKMG(0), TEST1_0) == 0);
106 //cout << IntToKMG(0) << " " << TEST1_0 << endl;
108 res = res && (strcmp(IntToKMG(LONG_LONG_MIN), TEST1_LLMIN) == 0);
109 //cout << IntToKMG(LONG_LONG_MIN) << " " << TEST1_LLMIN << endl;
112 cout << "OK" << endl;
114 cout << "Fail" << endl;
118 int Teststrtodouble2()
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);
138 cout << "OK" << endl;
140 cout << "Fail" << endl;
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);
154 cout << "OK" << endl;
156 cout << "Fail" << endl;
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);
170 cout << "OK" << endl;
172 cout << "Fail" << endl;
176 int TestEncodeDecode()
178 char enc[256], dec[512];
180 cout << "Testing EncodeDecode: \t\t";
181 Encode12(enc, TEST2_STRING, strlen(TEST2_STRING));
183 res = res && !strcmp(dec, TEST2_STRING);
184 Encode12(enc, TEST2_STRING, 256); // Overflow
186 res = res && !strcmp(dec, TEST2_STRING);
187 Encode12(enc, TEST2_STRING, 5); // Underflow
189 res = res && !strcmp(dec, TEST2_PART);
191 cout << "OK" << endl;
193 cout << "Fail" << endl;
197 int TestParseIPString()
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;
207 cout << "OK" << endl;
209 cout << "Fail" << endl;
215 char enc[256], dec[256];
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);
228 cout << "OK" << endl;
230 cout << "Fail" << endl;
234 int TestDaysInMonth()
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);
255 cout << "OK" << endl;
257 cout << "Fail" << endl;
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);
275 cout << "OK" << endl;
277 cout << "Fail" << endl;
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);
292 cout << "OK" << endl;
294 cout << "Fail" << endl;
298 int Testinet_ntostr()
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);
310 cout << "OK" << endl;
312 cout << "Fail" << endl;
316 int TestParseTariffTimeStr()
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);
330 cout << "OK" << endl;
332 cout << "Fail" << endl;
335 //-----------------------------------------------------------------------------
338 cout << "Testing Str2XX2Str: \t\t";
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))
351 for (int i = -5000000; i < 5000000; i+=10)
357 cout << "Fail" << endl;
366 cout << INT32_MIN << " " << s << endl;
367 cout << INT32_MIN << " " << xx << endl;
368 cout << "Fail" << endl;
376 cout << "Fail" << endl;
379 cout << "OK" << endl;
382 //-----------------------------------------------------------------------------