1 /* $Id: testcore.c,v 6.3 2011/12/19 18:40:17 gouriano Exp $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 *
27 * File Description:
28 * Test CORELIB functionality
29 *
30 * ---------------------------------------------------------------------------
31 * $Log: testcore.c,v $
32 * Revision 6.3 2011/12/19 18:40:17 gouriano
33 * Corrected printf formatting. NOJIRA
34 *
35 * Revision 6.2 1999/11/29 20:00:37 vakatov
36 * Added "TestParseArgs()" to test new public command-line parsing functions
37 * Nlm_ParseCmdLineArguments(), Nlm_FreeCmdLineArguments()
38 *
39 * ===========================================================================
40 */
41
42 #include <ncbi.h>
43
44
45 #define TEST (CTX_RESERVED+1)
46
47
48 /*** our prototyped functions ***/
49 static void BuildBS(ByteStorePtr bsp);
50
51 static void TestMessages(void);
52 static void TestErrors(void);
53 static void TestSettings(void);
54 static void TestMemory(void);
55 static void TestByteStores(void);
56 static void TestStrings(void);
57 static void TestMisc(void);
58 static void TestParseArgs(void);
59
60
61 /*** our arguments ***/
62 #define NUMARGS 8
63
64 Args testargs[NUMARGS] = {
65 { "test Boolean",
66 "T", NULL, NULL, FALSE, 'b', ARG_BOOLEAN, 0.0, 0, NULL },
67 { "test Integer",
68 "42", "41", "43", TRUE, 'i', ARG_INT, 0.0, 0, NULL },
69 { "test Float",
70 "3.14159", NULL, NULL, FALSE, 'f', ARG_FLOAT, 0.0, 0, NULL },
71 { "test String",
72 NULL, NULL, NULL, TRUE, 's', ARG_STRING, 0.0, 0, NULL },
73 { "test File-in",
74 NULL, NULL, NULL, TRUE, 'w', ARG_FILE_IN, 0.0, 0, NULL },
75 { "test File-out",
76 NULL, NULL, NULL, TRUE, 'x', ARG_FILE_OUT, 0.0, 0, NULL },
77 { "test Data-in",
78 NULL, "Fake-type", NULL, TRUE, 'y', ARG_DATA_IN, 0.0, 0, NULL },
79 { "test Data-out",
80 NULL, "Fake-out", NULL, TRUE, 'z', ARG_DATA_OUT, 0.0, 0, NULL }
81 };
82
83 static char* tmsg = "Test of %s",
84 * fmsg = "Fail on %s",
85 * omsg = "[overwrite at 50]",
86 * imsg = "[inserted at 10]",
87 * stest[4] = { "The", "quick", "brown", "fox" };
88
89
90
91 /*** use Int2 Main(), not main(argc, argv) ***/
Main(void)92 Int2 Main (void)
93 {
94 Char lbuf[100];
95 Int4 seconds;
96
97 /*** provide opening arguments ***/
98 if ( !GetArgs("TestCore 1.0", NUMARGS, testargs) )
99 return 1; /*** leave if nothing happens ***/
100
101 seconds = GetSecs();
102
103 TestErrors();
104
105 TestMessages();
106
107 TestSettings();
108
109 TestMemory();
110
111 TestByteStores();
112
113 TestMisc();
114
115 TestParseArgs();
116
117 seconds = GetSecs() - seconds;
118 DayTimeStr(lbuf, TRUE, TRUE);
119 Message(MSG_OK, "Today is: %s. Test took %ld seconds.",
120 lbuf, (long)seconds);
121
122 VERIFY( FreeArgs(NUMARGS, testargs) );
123 VERIFY( FreeArgs(NUMARGS, testargs) );
124 return 0;
125 }
126
127
TestErrors(void)128 static void TestErrors (void)
129 {
130 remove ("testcore.log");
131 ErrSetLog ("testcore.log");
132
133 ERRPOST((CTX_DEBUG, 0, "Test post of a debugging message"));
134 ErrSetOpts (0, ERR_LOG_OFF);
135 ERRPOST((CTX_DEBUG, 1,
136 "Logging was not disabled as it should be"));
137 ErrSetOpts (0, ERR_LOG_ON);
138 }
139
140
TestMessages(void)141 static void TestMessages (void)
142 {
143 Message(MSG_ERROR, tmsg, "non-fatal error message");
144 Message(MSG_OK, tmsg, "OK message");
145 if (Message(MSG_RC, tmsg, "retry/cancel message. Hit c.") != ANS_CANCEL)
146 ERRPOST((TEST, 1, "Did not get retry"));
147 if (Message(MSG_ARI, tmsg, "abort/retry/ignore message. Hit a.")
148 != ANS_ABORT)
149 ERRPOST((TEST, 1, "Did not get abort"));
150 if (! Message(MSG_YN, tmsg, "yes/no message. Hit y."))
151 ERRPOST((TEST, 1, "Did not get yes"));
152 }
153
154
TestSettings(void)155 static void TestSettings (void)
156 {
157 char buffer[6];
158
159 if ( ! SetAppParam ("junk", "test", "key", "value"))
160 ERRPOST((TEST, 1, fmsg, "SetAppParam"));
161
162 GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
163 if (strcmp(buffer, "value"))
164 ERRPOST((TEST, 1, fmsg, "GetAppParam"));
165
166 GetAppParam ("junk", "test", "foo", "foo", buffer, sizeof buffer);
167 if (strcmp(buffer, "foo"))
168 ERRPOST((TEST, 1, "GetAppParam: default value not returned"));
169
170 if ( ! TransientSetAppParam ("junk", "test", "foo", "foo2"))
171 ERRPOST((TEST, 1, "TransientSetAppParam"));
172 GetAppParam ("junk", "test", "foo", "foo", buffer, sizeof buffer);
173 if (strcmp(buffer, "foo2"))
174 ERRPOST((TEST, 1,
175 "GetAppParam: TransientSetAppParam value not returned"));
176 if ( ! TransientSetAppParam ("junk", "test", NULL, NULL))
177 ERRPOST((TEST, 1, "TransientSetAppParam section"));
178
179 GetAppParam ("junk", "test", "key", "foo", buffer, sizeof buffer);
180 if (strcmp(buffer, "foo"))
181 ERRPOST((TEST, 1, fmsg, "GetAppParam on empty section"));
182
183 if ( ! SetAppParam ("junk", "test", "key", "val2"))
184 ERRPOST((TEST, 1, fmsg, "SetAppParam"));
185
186 GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
187 if (strcmp(buffer, "val2"))
188 ERRPOST((TEST, 1, fmsg, "GetAppParam on new value"));
189 }
190
191
TestMemory(void)192 static void TestMemory (void)
193 {
194 CharPtr cpnt;
195 Handle hand;
196 Uint2 x = 0xdead;
197 unsigned char *y;
198
199 if (sizeof(Int1) != 1) {
200 ERRPOST((TEST, 1, fmsg,
201 "Int1 is really %d bytes long!", sizeof(Int1)));
202 }
203
204 if (sizeof(Int2) != 2) {
205 ERRPOST((TEST, 1, fmsg,
206 "Int2 is really %d bytes long!", sizeof(Int2)));
207 }
208
209 if (sizeof(Int4) != 4) {
210 ERRPOST((TEST, 1, fmsg,
211 "Int4 is really %d bytes long!", sizeof(Int4)));
212 }
213
214 if (sizeof(x) == 2) {
215 y = (unsigned char*) &x;
216 #ifdef IS_LITTLE_ENDIAN
217 if (y[0] == 0xde && y[1] == 0xad) {
218 ERRPOST((TEST, 1, fmsg,
219 "Declared little-endian mach. appears to be big-endian"));
220 }
221 #endif /* IS_LITTLE_ENDIAN */
222 #ifdef IS_BIG_ENDIAN
223 if (y[1] == 0xde && y[0] == 0xad) {
224 ERRPOST((TEST, 1, fmsg,
225 "Declared big-endian mach. appears to be little-endian"));
226 }
227 #endif /* IS_BIG_ENDIAN */
228 }
229
230 /*** test some memory ***/
231 if ((hand = HandNew((size_t)60000)) == NULL)
232 ERRPOST((TEST, 1, fmsg, "HandNew"));
233 if ((cpnt = (CharPtr) HandLock(hand)) == NULL)
234 ERRPOST((TEST, 1, fmsg, "HandLock"));
235 HandUnlock(hand);
236 HandFree(hand);
237
238 if ((cpnt = (CharPtr) MemNew((size_t)60000)) == NULL)
239 ERRPOST((TEST, 1, fmsg, "MemNew"));
240 else
241 MemFree(cpnt);
242 }
243
244
TestByteStores(void)245 static void TestByteStores (void)
246 {
247 CharPtr cpnt = NULL;
248 ByteStorePtr bsp;
249 Char lbuf[100];
250 Int2 ctr;
251
252 if ((bsp = BSNew(60000)) == NULL)
253 ERRPOST((TEST, 1, fmsg, "BSNew"));
254
255 /*** test ByteStore ***/
256 BuildBS(bsp); /*** fill 60k ***/
257 if (BSLen(bsp) != 60000)
258 Message(MSG_ERROR, fmsg, "BSLen");
259
260 BSSeek(bsp, 31980, SEEK_SET);
261 if (BSTell(bsp) != 31980)
262 Message(MSG_ERROR, fmsg, "BSTell");
263 MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
264 BSRead(bsp, lbuf, 50);
265 Message(MSG_OK, "BS50 [%s]", lbuf); /*** show 31980-32029 ***/
266
267 BSSeek(bsp, 31990, SEEK_SET);
268 BSDelete(bsp, 20); /*** show delete at 31990 **/
269 BSSeek(bsp, 31980, SEEK_SET);
270 MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
271 BSRead(bsp, lbuf, 50);
272 Message(MSG_OK, "BSdel [%s]", lbuf);
273
274 BSSeek(bsp, 50, SEEK_SET); /*** show overwrite at 50 **/
275 BSWrite(bsp, omsg, StringLen(omsg));
276 BSSeek(bsp,40, SEEK_SET);
277 MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
278 BSRead(bsp, lbuf, 50);
279 Message(MSG_OK, "BSow [%s]", lbuf);
280
281 BSSeek(bsp, 10, SEEK_SET); /*** show insert at 10 **/
282 BSInsert(bsp, imsg, StringLen(imsg));
283 BSSeek(bsp, 0, SEEK_SET);
284 MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
285 BSRead(bsp, lbuf, 50);
286 Message(MSG_OK, "BS i [%s]", lbuf);
287
288 BSSeek(bsp, 0, SEEK_SET); /*** show getbyte ***/
289 MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
290 for (ctr = 0; ctr < 50; ctr++)
291 lbuf[ctr] = (Byte) BSGetByte(bsp);
292 Message(MSG_OK, "BS g [%s]", lbuf);
293
294 /*** check freeing of memory ***/
295 cpnt = (CharPtr) MemFree(cpnt);
296 cpnt = (CharPtr) BSMerge(bsp, NULL); /*** check merge ***/
297 MemFill(lbuf, '\0', 100);
298 MemCopy(lbuf, cpnt, 50);
299 Message(MSG_OK, "BS m [%s]", lbuf);
300 bsp = BSFree(bsp); /*** free the ByteStore */
301 cpnt = (CharPtr) MemFree(cpnt);
302
303 /*** check add ***/
304 bsp = BSNew(0); /*** no initial storage allocated ***/
305 for (ctr = 0; ctr < 4; ctr++)
306 BSWrite(bsp, stest[ctr], StringLen(stest[ctr]));
307 MemFill(lbuf, '\0', 100); /*** merge doesn't add '\0' to previous */
308 /* storage **/
309 BSMerge(bsp, lbuf); /*** merge to previous storage ***/
310 Message(MSG_OK, "BS add [%s]", lbuf);
311 bsp = BSFree(bsp);
312 }
313
314
315 /*****************************************************************************
316 *
317 * BuildBS(bsp);
318 *
319 *****************************************************************************/
320
BuildBS(ByteStorePtr bsp)321 static void BuildBS (ByteStorePtr bsp)
322 {
323 Char lbuf[20], tbuf[20];
324 Int4 value;
325
326 StringCpy(lbuf, "....:....|");
327 for (value = 0; value < 60000; value += 10)
328 {
329 sprintf(tbuf, "%d", value);
330 MemCopy(lbuf, tbuf, StringLen(tbuf));
331 BSWrite(bsp, lbuf, 10);
332 }
333 return;
334 }
335
336
TestStrings(void)337 static void TestStrings (void)
338 {
339 CharPtr cpnt;
340 Char lbuf[100];
341 Int2 ctr;
342 /*** some string functions ***/
343 for (ctr = 0; ctr < 4; ctr++) {
344 if (StringCmp("brown", stest[ctr])) {
345 if (ctr == 2)
346 Message(MSG_ERROR, fmsg, "StringCmp");
347 }
348 else if (ctr != 2) {
349 Message(MSG_ERROR, fmsg, "StringCmp2");
350 }
351 }
352
353 for (ctr = 0; ctr < 4; ctr++) {
354 if (StringICmp("Brown", stest[ctr])) {
355 if (ctr == 2)
356 Message(MSG_ERROR, fmsg, "StringICmp");
357 }
358 else if (ctr != 2) {
359 Message(MSG_ERROR, fmsg, "StringICmp2");
360 }
361 }
362
363 for (ctr = 0; ctr < 4; ctr++) {
364 if (StringNCmp("brow", stest[ctr], 4)) {
365 if (ctr == 2)
366 Message(MSG_ERROR, fmsg, "StringNCmp");
367 }
368 else if (ctr != 2) {
369 Message(MSG_ERROR, fmsg, "StringNCmp2");
370 }
371 }
372
373 for (ctr = 0; ctr < 4; ctr++) {
374 if (StringNICmp("Brow", stest[ctr], 4)) {
375 if (ctr == 2)
376 Message(MSG_ERROR, fmsg, "StringNICmp");
377 }
378 else if (ctr != 2) {
379 Message(MSG_ERROR, fmsg, "StringNICmp2");
380 }
381 }
382
383 MemFill(lbuf, '\0', 100);
384 for (ctr = 0; ctr < 4; ctr++) {
385 StringCat(lbuf, stest[ctr]);
386 }
387 Message(MSG_OK, "StringCat [%s]", lbuf);
388
389 MemFill(lbuf, '\0', 100);
390 for (ctr = 0; ctr < 4; ctr++) {
391 StringNCat(lbuf, stest[ctr], 4);
392 }
393 Message(MSG_OK, "StringNCat 4 [%s]", lbuf);
394
395 cpnt = StringSave(lbuf);
396 StringCpy(lbuf, cpnt); /* copy to local buf for MSWindows */
397 Message(MSG_OK, "StringSave [%s]", lbuf);
398 cpnt = (CharPtr) MemFree(cpnt);
399 }
400
401
TestMisc(void)402 static void TestMisc (void)
403 {
404 /*** test ncbistd.h ***/
405 if (MIN(0.8, 1.0) != 0.8)
406 Message(MSG_ERROR, fmsg, "MIN");
407 if (MAX(0.8, 1.0) != 1.0)
408 Message(MSG_ERROR, fmsg, "MAX");
409 if (ABS(-2) != 2)
410 Message(MSG_ERROR, fmsg, "ABS");
411 if (ROUNDUP(24, 5) != 25)
412 Message(MSG_ERROR, fmsg, "ROUNDUP");
413 }
414
415
TestParseArgs(void)416 static void TestParseArgs(void)
417 {
418 int i;
419 typedef struct {
420 const char* prog_name;
421 const char* cmd_line;
422 } STestParseArgs;
423
424 static const STestParseArgs x_test[] = {
425 { 0, 0 },
426 { "", 0 },
427 { 0, "" },
428 { "", "" },
429 { 0, "a bb ccc" },
430 { "", "-aaa bbb -ccc +ddd eee" },
431 { "MyProgName1", "aa bb - +" },
432 { "MyProgName 2", "aa \"bb cc\"" },
433 { 0, "'aa' 'bb cc' \"ee dd\"" },
434 { 0, "'\"aa' 'bb\" cc' \"ee ' dd\"" }
435 };
436
437 ErrSetMessageLevel(SEV_MIN);
438 ErrSetLogLevel(SEV_MIN);
439
440 ErrPostEx(SEV_INFO, 0,0, "[BEGIN] TEST of Nlm_ParseCmdLineArguments()");
441 for (i = 0; i < DIM(x_test); i++) {
442 int x_argc, j;
443 char** x_argv;
444 ErrPostEx(SEV_INFO, 0,0, "Program: `%s'",
445 x_test[i].prog_name ? x_test[i].prog_name : "<NULL>");
446 ErrPostEx(SEV_INFO, 0,0, "CmdLine: `%s'",
447 x_test[i].cmd_line ? x_test[i].cmd_line : "<NULL>");
448
449 /* ProcessQuotes */
450 VERIFY( ParseCmdLineArguments
451 (x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
452 eProcessQuotes) );
453 ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
454 for (j = 0; j < x_argc; j++) {
455 ASSERT( x_argv[j] );
456 ErrPostEx(SEV_INFO, 0,0,
457 "ProcessQuotes::argv[%d] = %s", j, x_argv[j]);
458 }
459 ASSERT( !x_argv[x_argc] );
460 FreeCmdLineArguments(x_argv);
461
462 /* IgnoreQuotes */
463 VERIFY( ParseCmdLineArguments
464 (x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
465 eIgnoreQuotes) );
466 ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
467 for (j = 0; j < x_argc; j++) {
468 ASSERT( x_argv[j] );
469 ErrPostEx(SEV_INFO, 0,0,
470 "IgnoreQuotes::argv[%d] = %s", j, x_argv[j]);
471 }
472 ASSERT( !x_argv[x_argc] );
473 FreeCmdLineArguments(x_argv);
474 }
475 ErrPostEx(SEV_INFO, 0,0, "[END] TEST of Nlm_ParseCmdLineArguments()");
476 }
477