1
/* $Id: testcore.c,v 6.2 1999/11/29 20:00:37 vakatov Exp $
2
* ===========================================================================
5
* National Center for Biotechnology Information
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.
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
22
* Please cite the author in any work or product based on this material.
24
* ===========================================================================
28
* Test CORELIB functionality
30
* ---------------------------------------------------------------------------
31
* $Log: testcore.c,v $
32
* Revision 6.2 1999/11/29 20:00:37 vakatov
33
* Added "TestParseArgs()" to test new public command-line parsing functions
34
* Nlm_ParseCmdLineArguments(), Nlm_FreeCmdLineArguments()
36
* ===========================================================================
42
#define TEST (CTX_RESERVED+1)
45
/*** our prototyped functions ***/
46
static void BuildBS(ByteStorePtr bsp);
48
static void TestMessages(void);
49
static void TestErrors(void);
50
static void TestSettings(void);
51
static void TestMemory(void);
52
static void TestByteStores(void);
53
static void TestStrings(void);
54
static void TestMisc(void);
55
static void TestParseArgs(void);
58
/*** our arguments ***/
61
Args testargs[NUMARGS] = {
63
"T", NULL, NULL, FALSE, 'b', ARG_BOOLEAN, 0.0, 0, NULL },
65
"42", "41", "43", TRUE, 'i', ARG_INT, 0.0, 0, NULL },
67
"3.14159", NULL, NULL, FALSE, 'f', ARG_FLOAT, 0.0, 0, NULL },
69
NULL, NULL, NULL, TRUE, 's', ARG_STRING, 0.0, 0, NULL },
71
NULL, NULL, NULL, TRUE, 'w', ARG_FILE_IN, 0.0, 0, NULL },
73
NULL, NULL, NULL, TRUE, 'x', ARG_FILE_OUT, 0.0, 0, NULL },
75
NULL, "Fake-type", NULL, TRUE, 'y', ARG_DATA_IN, 0.0, 0, NULL },
77
NULL, "Fake-out", NULL, TRUE, 'z', ARG_DATA_OUT, 0.0, 0, NULL }
80
static char* tmsg = "Test of %s",
81
* fmsg = "Fail on %s",
82
* omsg = "[overwrite at 50]",
83
* imsg = "[inserted at 10]",
84
* stest[4] = { "The", "quick", "brown", "fox" };
88
/*** use Int2 Main(), not main(argc, argv) ***/
94
/*** provide opening arguments ***/
95
if ( !GetArgs("TestCore 1.0", NUMARGS, testargs) )
96
return 1; /*** leave if nothing happens ***/
114
seconds = GetSecs() - seconds;
115
DayTimeStr(lbuf, TRUE, TRUE);
116
Message(MSG_OK, "Today is: %s. Test took %ld seconds.",
117
lbuf, (long)seconds);
119
VERIFY( FreeArgs(NUMARGS, testargs) );
120
VERIFY( FreeArgs(NUMARGS, testargs) );
125
static void TestErrors (void)
127
remove ("testcore.log");
128
ErrSetLog ("testcore.log");
130
ERRPOST((CTX_DEBUG, 0, "Test post of a debugging message"));
131
ErrSetOpts (0, ERR_LOG_OFF);
132
ERRPOST((CTX_DEBUG, 1,
133
"Logging was not disabled as it should be"));
134
ErrSetOpts (0, ERR_LOG_ON);
138
static void TestMessages (void)
140
Message(MSG_ERROR, tmsg, "non-fatal error message");
141
Message(MSG_OK, tmsg, "OK message");
142
if (Message(MSG_RC, tmsg, "retry/cancel message. Hit c.") != ANS_CANCEL)
143
ERRPOST((TEST, 1, "Did not get retry"));
144
if (Message(MSG_ARI, tmsg, "abort/retry/ignore message. Hit a.")
146
ERRPOST((TEST, 1, "Did not get abort"));
147
if (! Message(MSG_YN, tmsg, "yes/no message. Hit y."))
148
ERRPOST((TEST, 1, "Did not get yes"));
152
static void TestSettings (void)
156
if ( ! SetAppParam ("junk", "test", "key", "value"))
157
ERRPOST((TEST, 1, fmsg, "SetAppParam"));
159
GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
160
if (strcmp(buffer, "value"))
161
ERRPOST((TEST, 1, fmsg, "GetAppParam"));
163
GetAppParam ("junk", "test", "foo", "foo", buffer, sizeof buffer);
164
if (strcmp(buffer, "foo"))
165
ERRPOST((TEST, 1, "GetAppParam: default value not returned"));
167
if ( ! TransientSetAppParam ("junk", "test", "foo", "foo2"))
168
ERRPOST((TEST, 1, "TransientSetAppParam"));
169
GetAppParam ("junk", "test", "foo", "foo", buffer, sizeof buffer);
170
if (strcmp(buffer, "foo2"))
172
"GetAppParam: TransientSetAppParam value not returned"));
173
if ( ! TransientSetAppParam ("junk", "test", NULL, NULL))
174
ERRPOST((TEST, 1, "TransientSetAppParam section"));
176
GetAppParam ("junk", "test", "key", "foo", buffer, sizeof buffer);
177
if (strcmp(buffer, "foo"))
178
ERRPOST((TEST, 1, fmsg, "GetAppParam on empty section"));
180
if ( ! SetAppParam ("junk", "test", "key", "val2"))
181
ERRPOST((TEST, 1, fmsg, "SetAppParam"));
183
GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
184
if (strcmp(buffer, "val2"))
185
ERRPOST((TEST, 1, fmsg, "GetAppParam on new value"));
189
static void TestMemory (void)
196
if (sizeof(Int1) != 1) {
197
ERRPOST((TEST, 1, fmsg,
198
"Int1 is really %d bytes long!", sizeof(Int1)));
201
if (sizeof(Int2) != 2) {
202
ERRPOST((TEST, 1, fmsg,
203
"Int2 is really %d bytes long!", sizeof(Int2)));
206
if (sizeof(Int4) != 4) {
207
ERRPOST((TEST, 1, fmsg,
208
"Int4 is really %d bytes long!", sizeof(Int4)));
211
if (sizeof(x) == 2) {
212
y = (unsigned char*) &x;
213
#ifdef IS_LITTLE_ENDIAN
214
if (y[0] == 0xde && y[1] == 0xad) {
215
ERRPOST((TEST, 1, fmsg,
216
"Declared little-endian mach. appears to be big-endian"));
218
#endif /* IS_LITTLE_ENDIAN */
220
if (y[1] == 0xde && y[0] == 0xad) {
221
ERRPOST((TEST, 1, fmsg,
222
"Declared big-endian mach. appears to be little-endian"));
224
#endif /* IS_BIG_ENDIAN */
227
/*** test some memory ***/
228
if ((hand = HandNew((size_t)60000)) == NULL)
229
ERRPOST((TEST, 1, fmsg, "HandNew"));
230
if ((cpnt = (CharPtr) HandLock(hand)) == NULL)
231
ERRPOST((TEST, 1, fmsg, "HandLock"));
235
if ((cpnt = (CharPtr) MemNew((size_t)60000)) == NULL)
236
ERRPOST((TEST, 1, fmsg, "MemNew"));
242
static void TestByteStores (void)
249
if ((bsp = BSNew(60000)) == NULL)
250
ERRPOST((TEST, 1, fmsg, "BSNew"));
252
/*** test ByteStore ***/
253
BuildBS(bsp); /*** fill 60k ***/
254
if (BSLen(bsp) != 60000)
255
Message(MSG_ERROR, fmsg, "BSLen");
257
BSSeek(bsp, 31980, SEEK_SET);
258
if (BSTell(bsp) != 31980)
259
Message(MSG_ERROR, fmsg, "BSTell");
260
MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
261
BSRead(bsp, lbuf, 50);
262
Message(MSG_OK, "BS50 [%s]", lbuf); /*** show 31980-32029 ***/
264
BSSeek(bsp, 31990, SEEK_SET);
265
BSDelete(bsp, 20); /*** show delete at 31990 **/
266
BSSeek(bsp, 31980, SEEK_SET);
267
MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
268
BSRead(bsp, lbuf, 50);
269
Message(MSG_OK, "BSdel [%s]", lbuf);
271
BSSeek(bsp, 50, SEEK_SET); /*** show overwrite at 50 **/
272
BSWrite(bsp, omsg, StringLen(omsg));
273
BSSeek(bsp,40, SEEK_SET);
274
MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
275
BSRead(bsp, lbuf, 50);
276
Message(MSG_OK, "BSow [%s]", lbuf);
278
BSSeek(bsp, 10, SEEK_SET); /*** show insert at 10 **/
279
BSInsert(bsp, imsg, StringLen(imsg));
280
BSSeek(bsp, 0, SEEK_SET);
281
MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
282
BSRead(bsp, lbuf, 50);
283
Message(MSG_OK, "BS i [%s]", lbuf);
285
BSSeek(bsp, 0, SEEK_SET); /*** show getbyte ***/
286
MemFill(lbuf, '\0', 100); /*** BSRead does not add '\0' at end */
287
for (ctr = 0; ctr < 50; ctr++)
288
lbuf[ctr] = (Byte) BSGetByte(bsp);
289
Message(MSG_OK, "BS g [%s]", lbuf);
291
/*** check freeing of memory ***/
292
cpnt = (CharPtr) MemFree(cpnt);
293
cpnt = (CharPtr) BSMerge(bsp, NULL); /*** check merge ***/
294
MemFill(lbuf, '\0', 100);
295
MemCopy(lbuf, cpnt, 50);
296
Message(MSG_OK, "BS m [%s]", lbuf);
297
bsp = BSFree(bsp); /*** free the ByteStore */
298
cpnt = (CharPtr) MemFree(cpnt);
301
bsp = BSNew(0); /*** no initial storage allocated ***/
302
for (ctr = 0; ctr < 4; ctr++)
303
BSWrite(bsp, stest[ctr], StringLen(stest[ctr]));
304
MemFill(lbuf, '\0', 100); /*** merge doesn't add '\0' to previous */
306
BSMerge(bsp, lbuf); /*** merge to previous storage ***/
307
Message(MSG_OK, "BS add [%s]", lbuf);
312
/*****************************************************************************
316
*****************************************************************************/
318
static void BuildBS (ByteStorePtr bsp)
320
Char lbuf[20], tbuf[20];
323
StringCpy(lbuf, "....:....|");
324
for (value = 0; value < 60000; value += 10)
326
sprintf(tbuf, "%ld", value);
327
MemCopy(lbuf, tbuf, StringLen(tbuf));
328
BSWrite(bsp, lbuf, 10);
334
static void TestStrings (void)
339
/*** some string functions ***/
340
for (ctr = 0; ctr < 4; ctr++) {
341
if (StringCmp("brown", stest[ctr])) {
343
Message(MSG_ERROR, fmsg, "StringCmp");
346
Message(MSG_ERROR, fmsg, "StringCmp2");
350
for (ctr = 0; ctr < 4; ctr++) {
351
if (StringICmp("Brown", stest[ctr])) {
353
Message(MSG_ERROR, fmsg, "StringICmp");
356
Message(MSG_ERROR, fmsg, "StringICmp2");
360
for (ctr = 0; ctr < 4; ctr++) {
361
if (StringNCmp("brow", stest[ctr], 4)) {
363
Message(MSG_ERROR, fmsg, "StringNCmp");
366
Message(MSG_ERROR, fmsg, "StringNCmp2");
370
for (ctr = 0; ctr < 4; ctr++) {
371
if (StringNICmp("Brow", stest[ctr], 4)) {
373
Message(MSG_ERROR, fmsg, "StringNICmp");
376
Message(MSG_ERROR, fmsg, "StringNICmp2");
380
MemFill(lbuf, '\0', 100);
381
for (ctr = 0; ctr < 4; ctr++) {
382
StringCat(lbuf, stest[ctr]);
384
Message(MSG_OK, "StringCat [%s]", lbuf);
386
MemFill(lbuf, '\0', 100);
387
for (ctr = 0; ctr < 4; ctr++) {
388
StringNCat(lbuf, stest[ctr], 4);
390
Message(MSG_OK, "StringNCat 4 [%s]", lbuf);
392
cpnt = StringSave(lbuf);
393
StringCpy(lbuf, cpnt); /* copy to local buf for MSWindows */
394
Message(MSG_OK, "StringSave [%s]", lbuf);
395
cpnt = (CharPtr) MemFree(cpnt);
399
static void TestMisc (void)
401
/*** test ncbistd.h ***/
402
if (MIN(0.8, 1.0) != 0.8)
403
Message(MSG_ERROR, fmsg, "MIN");
404
if (MAX(0.8, 1.0) != 1.0)
405
Message(MSG_ERROR, fmsg, "MAX");
407
Message(MSG_ERROR, fmsg, "ABS");
408
if (ROUNDUP(24, 5) != 25)
409
Message(MSG_ERROR, fmsg, "ROUNDUP");
413
static void TestParseArgs(void)
417
const char* prog_name;
418
const char* cmd_line;
421
static const STestParseArgs x_test[] = {
427
{ "", "-aaa bbb -ccc +ddd eee" },
428
{ "MyProgName1", "aa bb - +" },
429
{ "MyProgName 2", "aa \"bb cc\"" },
430
{ 0, "'aa' 'bb cc' \"ee dd\"" },
431
{ 0, "'\"aa' 'bb\" cc' \"ee ' dd\"" }
434
ErrSetMessageLevel(SEV_MIN);
435
ErrSetLogLevel(SEV_MIN);
437
ErrPostEx(SEV_INFO, 0,0, "[BEGIN] TEST of Nlm_ParseCmdLineArguments()");
438
for (i = 0; i < DIM(x_test); i++) {
441
ErrPostEx(SEV_INFO, 0,0, "Program: `%s'",
442
x_test[i].prog_name ? x_test[i].prog_name : "<NULL>");
443
ErrPostEx(SEV_INFO, 0,0, "CmdLine: `%s'",
444
x_test[i].cmd_line ? x_test[i].cmd_line : "<NULL>");
447
VERIFY( ParseCmdLineArguments
448
(x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
450
ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
451
for (j = 0; j < x_argc; j++) {
453
ErrPostEx(SEV_INFO, 0,0,
454
"ProcessQuotes::argv[%d] = %s", j, x_argv[j]);
456
ASSERT( !x_argv[x_argc] );
457
FreeCmdLineArguments(x_argv);
460
VERIFY( ParseCmdLineArguments
461
(x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
463
ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
464
for (j = 0; j < x_argc; j++) {
466
ErrPostEx(SEV_INFO, 0,0,
467
"IgnoreQuotes::argv[%d] = %s", j, x_argv[j]);
469
ASSERT( !x_argv[x_argc] );
470
FreeCmdLineArguments(x_argv);
472
ErrPostEx(SEV_INFO, 0,0, "[END] TEST of Nlm_ParseCmdLineArguments()");