~ubuntu-branches/ubuntu/warty/ncbi-tools6/warty

« back to all changes in this revision

Viewing changes to demo/testcore.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Id: testcore.c,v 6.2 1999/11/29 20:00:37 vakatov 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.2  1999/11/29 20:00:37  vakatov
 
33
 * Added "TestParseArgs()" to test new public command-line parsing functions
 
34
 * Nlm_ParseCmdLineArguments(), Nlm_FreeCmdLineArguments()
 
35
 *
 
36
 * ===========================================================================
 
37
 */
 
38
 
 
39
#include <ncbi.h>
 
40
 
 
41
 
 
42
#define TEST (CTX_RESERVED+1)
 
43
 
 
44
 
 
45
/*** our prototyped functions ***/
 
46
static void BuildBS(ByteStorePtr bsp);
 
47
 
 
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);
 
56
 
 
57
 
 
58
/*** our arguments ***/
 
59
#define NUMARGS 8
 
60
 
 
61
Args testargs[NUMARGS] = {
 
62
    { "test Boolean",
 
63
      "T", NULL, NULL, FALSE, 'b', ARG_BOOLEAN, 0.0, 0, NULL },
 
64
    { "test Integer",
 
65
      "42", "41", "43", TRUE, 'i', ARG_INT, 0.0, 0, NULL },
 
66
    { "test Float",
 
67
      "3.14159", NULL, NULL, FALSE, 'f', ARG_FLOAT, 0.0, 0, NULL },
 
68
    { "test String",
 
69
      NULL, NULL, NULL, TRUE, 's', ARG_STRING, 0.0, 0, NULL },
 
70
    { "test File-in",
 
71
      NULL, NULL, NULL, TRUE, 'w', ARG_FILE_IN, 0.0, 0, NULL },
 
72
    { "test File-out",
 
73
      NULL, NULL, NULL, TRUE, 'x', ARG_FILE_OUT, 0.0, 0, NULL },
 
74
    { "test Data-in",
 
75
      NULL, "Fake-type", NULL, TRUE, 'y', ARG_DATA_IN, 0.0, 0, NULL },
 
76
    { "test Data-out",
 
77
      NULL, "Fake-out", NULL, TRUE, 'z', ARG_DATA_OUT, 0.0, 0, NULL }
 
78
};
 
79
  
 
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" };
 
85
 
 
86
 
 
87
 
 
88
/*** use Int2 Main(), not main(argc, argv)  ***/
 
89
Int2 Main (void)
 
90
{
 
91
    Char lbuf[100];
 
92
    Int4 seconds;
 
93
 
 
94
    /*** provide opening arguments ***/
 
95
    if ( !GetArgs("TestCore 1.0", NUMARGS, testargs) )
 
96
        return 1; /*** leave if nothing happens ***/
 
97
 
 
98
    seconds = GetSecs();
 
99
 
 
100
    TestErrors();
 
101
 
 
102
    TestMessages();
 
103
 
 
104
    TestSettings();
 
105
 
 
106
    TestMemory();
 
107
 
 
108
    TestByteStores();
 
109
 
 
110
    TestMisc();
 
111
 
 
112
    TestParseArgs();
 
113
 
 
114
    seconds = GetSecs() - seconds;
 
115
    DayTimeStr(lbuf, TRUE, TRUE);
 
116
    Message(MSG_OK, "Today is: %s.  Test took %ld seconds.",
 
117
            lbuf, (long)seconds);
 
118
 
 
119
    VERIFY( FreeArgs(NUMARGS, testargs) );
 
120
    VERIFY( FreeArgs(NUMARGS, testargs) );
 
121
    return 0;
 
122
}
 
123
 
 
124
 
 
125
static void TestErrors (void)
 
126
{
 
127
    remove ("testcore.log");
 
128
    ErrSetLog ("testcore.log");
 
129
 
 
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);
 
135
}
 
136
 
 
137
 
 
138
static void TestMessages (void)
 
139
{
 
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.")
 
145
        != ANS_ABORT)
 
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"));
 
149
}
 
150
 
 
151
 
 
152
static void TestSettings (void)
 
153
{
 
154
    char buffer[6];
 
155
 
 
156
    if ( ! SetAppParam ("junk", "test", "key", "value"))
 
157
        ERRPOST((TEST, 1, fmsg, "SetAppParam"));
 
158
 
 
159
    GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
 
160
    if (strcmp(buffer, "value"))
 
161
        ERRPOST((TEST, 1, fmsg, "GetAppParam"));
 
162
 
 
163
    GetAppParam ("junk", "test", "foo", "foo", buffer, sizeof buffer);
 
164
    if (strcmp(buffer, "foo"))
 
165
        ERRPOST((TEST, 1, "GetAppParam: default value not returned"));
 
166
 
 
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"))
 
171
        ERRPOST((TEST, 1,
 
172
                 "GetAppParam: TransientSetAppParam value not returned"));
 
173
    if ( ! TransientSetAppParam ("junk", "test", NULL, NULL))
 
174
        ERRPOST((TEST, 1, "TransientSetAppParam section"));
 
175
 
 
176
    GetAppParam ("junk", "test", "key", "foo", buffer, sizeof buffer);
 
177
    if (strcmp(buffer, "foo"))
 
178
        ERRPOST((TEST, 1, fmsg, "GetAppParam on empty section"));
 
179
 
 
180
    if ( ! SetAppParam ("junk", "test", "key", "val2"))
 
181
        ERRPOST((TEST, 1, fmsg, "SetAppParam"));
 
182
 
 
183
    GetAppParam ("junk", "test", "key", NULL, buffer, sizeof buffer);
 
184
    if (strcmp(buffer, "val2"))
 
185
        ERRPOST((TEST, 1, fmsg, "GetAppParam on new value"));
 
186
}
 
187
 
 
188
 
 
189
static void TestMemory (void)
 
190
{
 
191
    CharPtr cpnt;
 
192
    Handle hand;
 
193
    Uint2 x = 0xdead;
 
194
    unsigned char *y;
 
195
 
 
196
    if (sizeof(Int1) != 1) {
 
197
        ERRPOST((TEST, 1, fmsg,
 
198
                 "Int1 is really %d bytes long!", sizeof(Int1)));
 
199
    }
 
200
 
 
201
    if (sizeof(Int2) != 2) {
 
202
        ERRPOST((TEST, 1, fmsg,
 
203
                 "Int2 is really %d bytes long!", sizeof(Int2)));
 
204
    }
 
205
 
 
206
    if (sizeof(Int4) != 4) {
 
207
        ERRPOST((TEST, 1, fmsg,
 
208
                 "Int4 is really %d bytes long!", sizeof(Int4)));
 
209
    }
 
210
 
 
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"));
 
217
        }
 
218
#endif /* IS_LITTLE_ENDIAN */
 
219
#ifdef IS_BIG_ENDIAN
 
220
        if (y[1] == 0xde && y[0] == 0xad) {
 
221
            ERRPOST((TEST, 1, fmsg,
 
222
                     "Declared big-endian mach. appears to be little-endian"));
 
223
        }
 
224
#endif /* IS_BIG_ENDIAN */
 
225
    }
 
226
 
 
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"));
 
232
    HandUnlock(hand);
 
233
    HandFree(hand);
 
234
 
 
235
    if ((cpnt = (CharPtr) MemNew((size_t)60000)) == NULL)
 
236
        ERRPOST((TEST, 1, fmsg, "MemNew"));
 
237
    else
 
238
        MemFree(cpnt);
 
239
}
 
240
 
 
241
 
 
242
static void TestByteStores (void)
 
243
{
 
244
    CharPtr cpnt = NULL;
 
245
    ByteStorePtr bsp;
 
246
    Char lbuf[100];
 
247
    Int2 ctr;
 
248
 
 
249
    if ((bsp = BSNew(60000)) == NULL)
 
250
        ERRPOST((TEST, 1, fmsg, "BSNew"));
 
251
 
 
252
    /*** test ByteStore ***/
 
253
    BuildBS(bsp);                        /*** fill 60k ***/
 
254
    if (BSLen(bsp) != 60000)
 
255
        Message(MSG_ERROR, fmsg, "BSLen");
 
256
 
 
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 ***/
 
263
 
 
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);
 
270
 
 
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);
 
277
 
 
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);
 
284
 
 
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);
 
290
 
 
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);
 
299
 
 
300
    /*** check add ***/
 
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 */
 
305
    /*   storage **/
 
306
    BSMerge(bsp, lbuf);            /*** merge to previous storage ***/
 
307
    Message(MSG_OK, "BS add [%s]", lbuf);
 
308
    bsp = BSFree(bsp);
 
309
}
 
310
 
 
311
 
 
312
/*****************************************************************************
 
313
*
 
314
*   BuildBS(bsp);
 
315
*
 
316
*****************************************************************************/
 
317
 
 
318
static void BuildBS (ByteStorePtr bsp)
 
319
{
 
320
    Char lbuf[20], tbuf[20];
 
321
    Int4 value;
 
322
 
 
323
    StringCpy(lbuf, "....:....|");
 
324
    for (value = 0; value < 60000; value += 10)
 
325
        {
 
326
            sprintf(tbuf, "%ld", value);
 
327
            MemCopy(lbuf, tbuf, StringLen(tbuf));
 
328
            BSWrite(bsp, lbuf, 10);
 
329
        }
 
330
    return;
 
331
}
 
332
 
 
333
 
 
334
static void TestStrings (void)
 
335
{
 
336
    CharPtr cpnt;
 
337
    Char lbuf[100];
 
338
    Int2 ctr;
 
339
    /*** some string functions ***/
 
340
    for (ctr = 0; ctr < 4; ctr++) {
 
341
        if (StringCmp("brown", stest[ctr])) {
 
342
            if (ctr == 2)
 
343
                Message(MSG_ERROR, fmsg, "StringCmp");
 
344
        }
 
345
        else if (ctr != 2) {
 
346
            Message(MSG_ERROR, fmsg, "StringCmp2");
 
347
        }
 
348
    }
 
349
 
 
350
    for (ctr = 0; ctr < 4; ctr++) {
 
351
        if (StringICmp("Brown", stest[ctr])) {
 
352
            if (ctr == 2)
 
353
                Message(MSG_ERROR, fmsg, "StringICmp");
 
354
        }
 
355
        else if (ctr != 2) {
 
356
            Message(MSG_ERROR, fmsg, "StringICmp2");
 
357
        }
 
358
    }
 
359
 
 
360
    for (ctr = 0; ctr < 4; ctr++) {
 
361
        if (StringNCmp("brow", stest[ctr], 4)) {
 
362
            if (ctr == 2)
 
363
                Message(MSG_ERROR, fmsg, "StringNCmp");
 
364
        }
 
365
        else if (ctr != 2) {
 
366
            Message(MSG_ERROR, fmsg, "StringNCmp2");
 
367
        }
 
368
    }
 
369
 
 
370
    for (ctr = 0; ctr < 4; ctr++) {
 
371
        if (StringNICmp("Brow", stest[ctr], 4)) {
 
372
            if (ctr == 2)
 
373
                Message(MSG_ERROR, fmsg, "StringNICmp");
 
374
        }
 
375
        else if (ctr != 2) {
 
376
            Message(MSG_ERROR, fmsg, "StringNICmp2");
 
377
        }
 
378
    }
 
379
 
 
380
    MemFill(lbuf, '\0', 100);
 
381
    for (ctr = 0; ctr < 4; ctr++) {
 
382
        StringCat(lbuf, stest[ctr]);
 
383
    }
 
384
    Message(MSG_OK, "StringCat [%s]", lbuf);
 
385
 
 
386
    MemFill(lbuf, '\0', 100);
 
387
    for (ctr = 0; ctr < 4; ctr++) {
 
388
        StringNCat(lbuf, stest[ctr], 4);
 
389
    }
 
390
    Message(MSG_OK, "StringNCat 4 [%s]", lbuf);
 
391
 
 
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);
 
396
}
 
397
 
 
398
 
 
399
static void TestMisc (void)
 
400
{
 
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");
 
406
    if (ABS(-2) != 2)
 
407
        Message(MSG_ERROR, fmsg, "ABS");
 
408
    if (ROUNDUP(24, 5) != 25)
 
409
        Message(MSG_ERROR, fmsg, "ROUNDUP");
 
410
}
 
411
 
 
412
 
 
413
static void TestParseArgs(void)
 
414
{
 
415
    int i;
 
416
    typedef struct {
 
417
        const char* prog_name;
 
418
        const char* cmd_line;
 
419
    } STestParseArgs;
 
420
 
 
421
    static const STestParseArgs x_test[] = {
 
422
        {  0,  0  },
 
423
        { "",  0  },
 
424
        {  0,  "" },
 
425
        { "",  "" },
 
426
        { 0, "a bb ccc" },
 
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\"" }
 
432
    };
 
433
 
 
434
    ErrSetMessageLevel(SEV_MIN);
 
435
    ErrSetLogLevel(SEV_MIN);
 
436
 
 
437
    ErrPostEx(SEV_INFO, 0,0, "[BEGIN] TEST of Nlm_ParseCmdLineArguments()");
 
438
    for (i = 0;  i < DIM(x_test);  i++) {
 
439
        int    x_argc, j;
 
440
        char** x_argv;
 
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>");
 
445
 
 
446
        /* ProcessQuotes */
 
447
        VERIFY( ParseCmdLineArguments
 
448
                (x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
 
449
                 eProcessQuotes) );
 
450
        ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
 
451
        for (j = 0;  j < x_argc;  j++) {
 
452
            ASSERT( x_argv[j] );
 
453
            ErrPostEx(SEV_INFO, 0,0,
 
454
                      "ProcessQuotes::argv[%d] = %s", j, x_argv[j]);
 
455
        }
 
456
        ASSERT( !x_argv[x_argc] );
 
457
        FreeCmdLineArguments(x_argv);
 
458
 
 
459
        /* IgnoreQuotes */
 
460
        VERIFY( ParseCmdLineArguments
 
461
                (x_test[i].prog_name, x_test[i].cmd_line, &x_argc, &x_argv,
 
462
                 eIgnoreQuotes) );
 
463
        ErrPostEx(SEV_INFO, 0,0, "argc = %d", x_argc);
 
464
        for (j = 0;  j < x_argc;  j++) {
 
465
            ASSERT( x_argv[j] );
 
466
            ErrPostEx(SEV_INFO, 0,0,
 
467
                      "IgnoreQuotes::argv[%d] = %s", j, x_argv[j]);
 
468
        }
 
469
        ASSERT( !x_argv[x_argc] );
 
470
        FreeCmdLineArguments(x_argv);
 
471
    }
 
472
    ErrPostEx(SEV_INFO, 0,0, "[END] TEST of Nlm_ParseCmdLineArguments()");
 
473
}