File: test_dwarfstring.c

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (515 lines) | stat: -rw-r--r-- 16,443 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
Copyright (c) 2019-2019, David Anderson
All rights reserved.

Redistribution and use in source and binary forms, with
or without modification, are permitted provided that the
following conditions are met:

    Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
    disclaimer.

    Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials
    provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>  /* for printf */
#include <stdlib.h>
#include <string.h>
#include "dwarfstring.h"
#ifndef TRUE
#define TRUE 1
#endif /* TRUE */
#ifndef FALSE
#define FALSE 0
#endif /* FALSE */

static int errcount;

static void
check_string(const char *msg,char *exp,
    char *actual,int line)
{
    if(!strcmp(exp,actual)) {
        printf("PASS got \"%s\" line %d\n",exp,line);
        return;
    }
    printf("FAIL %s expected \"%s\" got \"%s\" test line %d\n",
        msg,exp,actual,line);
    ++errcount;
}
static void
check_value_i(const char *msg,unsigned long exp,
    unsigned long actual,int line)
{
    if(exp == actual) {
        return;
    }
    printf("FAIL %s expected %lu got %lu test line %d\n",
        msg,exp,actual,line);
    ++errcount;
}

/* Checks the return value of the dwarfstring function. */
static void
check_value(const char *msg,unsigned long exp,
    unsigned long actual,int line)
{
    if(exp == actual) {
        return;
    }
    printf("FAIL %s return expected %lu got %lu test line %d\n",
        msg,exp,actual,line);
    ++errcount;
}

static int
test1(int tnum)
{
    struct dwarfstring_s g;
    char *d = 0;
    const char *expstr = "";
    int res = 0;
    unsigned long biglen = 0;
    char *bigstr = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbyyyybbbbbbbbbbbbccc";
    char *mediumstr = "1234567890aaaaabbbbbb0123";

    dwarfstring_constructor(&g);
    d = dwarfstring_string(&g);
    check_string("expected empty string",(char *)expstr,d,__LINE__);

    /* caller coding error here, works  no % but we call it TRUE anyway */
    res = dwarfstring_append_printf_i(&g,"\nabc\n",54);
    check_value_i("expected TRUE  ",TRUE,res,__LINE__);
    check_string("expected ",(char *)"\nabc\n",dwarfstring_string(&g),__LINE__);

    dwarfstring_reset(&g);
    res = dwarfstring_append_printf_s(&g,"x%-15sy",mediumstr);
    check_value_i("expected TRUE  ",TRUE,res,__LINE__);
    check_string("expected ",(char *)"x1234567890aaaaabbbbbb0123y",
        dwarfstring_string(&g),__LINE__);

    dwarfstring_reset(&g);
    res = dwarfstring_append_printf_i(&g,"\nabc%d\n",54);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    check_string("expected ",(char *)"\nabc54\n",dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);

    res = dwarfstring_append_printf_i(&g,"\nabc%d\n",54);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    check_string("expected ",(char *)"\nabc54\n",dwarfstring_string(&g),__LINE__);

    dwarfstring_destructor(&g);
    dwarfstring_constructor(&g);

    res = dwarfstring_append_printf_i(&g,"\nabc%lld\n",-54);
    check_value("expected ok  ",TRUE,res,__LINE__);
    check_string("expected ",(char *)"\nabc-54\n",dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);

    /* in this call a %x is not allowed */
    res = dwarfstring_append_printf_i(&g,"\nabc%x\n",-54);
    check_value("expected error  ",FALSE,res,__LINE__);
    check_string("expected ",
        (char *)"\nabc<ERROR: format %x or %X passed to dwarfstring_append_printf_i>%x\n",
        dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);

    res = dwarfstring_append_printf_u(&g,"\nabc%x\n",-54);
    check_value("expected error  ",FALSE,res,__LINE__);
    check_string("expected ",(char *)"\nabcffffffffffffffca\n",dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);

    res = dwarfstring_append_printf_u(&g,"\nabc 0x%x\n",-54);
    check_value("expected error  ",FALSE,res,__LINE__);
    check_string("expected ",(char *)"\nabc 0xffffffffffffffca\n",dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);

#if 0
    res = dwarfstring_append_printf_i(&g,"\nabc%x\n",-54);
    check_value("expected error  ",FALSE,res,__LINE__);
    check_string("expected ",(char *)"\nabc0xffffffffffffffca\n",dwarfstring_string(&g),__LINE__);
    dwarfstring_reset(&g);
#endif

    res = dwarfstring_append(&g,"abc");
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected abc ",(char *)"abc",d,__LINE__);

    dwarfstring_destructor(&g);
    dwarfstring_constructor(&g);

    res = dwarfstring_append(&g,"xy");
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected abcxy ",(char *)"xy",d,__LINE__);

    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    res = dwarfstring_append(&g,bigstr);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected bigstring ",bigstr,d,__LINE__);
    biglen = dwarfstring_strlen(&g);
    check_value("expected 120  ",strlen(bigstr),biglen,__LINE__);

    dwarfstring_append_length(&g,"xxyyzz",3);

    biglen = dwarfstring_strlen(&g);
    check_value("expected 123  ",strlen(bigstr)+3,biglen,__LINE__);
    dwarfstring_destructor(&g);
    return 0;
}

static int
test2(int tnum)
{
    struct dwarfstring_s g;
    char *d = 0;
    const char *expstr = "";
    int res = 0;
    unsigned long biglen = 0;
    char *bigstr = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbbbbbbbbbbbbbbbbbccc"
        "ccccccbbbbbyyyybbbbbbbbbbbbccc";

    dwarfstring_constructor_fixed(&g,10);

    d = dwarfstring_string(&g);
    check_string("expected empty string",(char *)expstr,d,__LINE__);

    res = dwarfstring_append(&g,"abc");
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected abc ",(char *)"abc",d,__LINE__);

    res = dwarfstring_append(&g,"xy");
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected abcxy ",(char *)"abcxy",d,__LINE__);

    dwarfstring_destructor(&g);

    dwarfstring_constructor_fixed(&g,3);
    res = dwarfstring_append(&g,bigstr);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected bigstring ",bigstr,d,__LINE__);
    biglen = dwarfstring_strlen(&g);
    check_value("expected 120  ",strlen(bigstr),biglen,__LINE__);
    dwarfstring_destructor(&g);
    return 0;
}

static int
test3(int tnum)
{
    struct dwarfstring_s g;
    char *d = 0;
    const char *expstr = "";
    int res = 0;
    unsigned long biglen = 0;
    char *bigstr = "a012345";
    char *targetbigstr = "a012345xy";

    dwarfstring_constructor_fixed(&g,10);

    d = dwarfstring_string(&g);
    check_string("expected empty string",(char *)expstr,d,__LINE__);

    res = dwarfstring_append(&g,bigstr);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected a012345 ",(char *)bigstr,d,__LINE__);

    res = dwarfstring_append_length(&g,"xyzzz",2);
    check_value("expected TRUE  ",TRUE,res,__LINE__);

    check_value("expected 9  ", 9,(unsigned)dwarfstring_strlen(&g),
        __LINE__);

    d = dwarfstring_string(&g);
    check_string("expected a012345xy ",
        (char *)targetbigstr,d,__LINE__);
    dwarfstring_destructor(&g);
    return 0;
}

static int
test4(int tnum)
{
    struct dwarfstring_s g;
    char *d = 0;
    const char *expstr = "";
    int res = 0;
    unsigned long biglen = 0;
    char *mystr = "a01234";
    char *targetmystr = "a01234xyz";
    char fixedarea[7];

    dwarfstring_constructor_static(&g,fixedarea,sizeof(fixedarea));

    d = dwarfstring_string(&g);
    check_string("expected empty string",(char *)expstr,d,__LINE__);
    res = dwarfstring_append(&g,mystr);
    check_value("expected TRUE  ",TRUE,res,__LINE__);
    d = dwarfstring_string(&g);
    check_string("expected a01234 ",(char *)mystr,d,__LINE__);

    if (d != fixedarea) {
        printf(" FAIL  reallocated but should not line %d ",
            __LINE__);
        ++errcount;
    }
    res = dwarfstring_append(&g,"xyz");
    d = dwarfstring_string(&g);
    check_string("expected a01234xyz ",(char *)targetmystr,
        d,__LINE__);
    check_value("expected 9",9,dwarfstring_strlen(&g),__LINE__);

    if (d == fixedarea) {
        printf(" FAIL  not reallocated but should  be! line %d ",
            __LINE__);
        ++errcount;
    }
    dwarfstring_destructor(&g);
    return 0;
}

static int
test5(int tnum)
{
    struct dwarfstring_s g;
    char *d = 0;
    const char *expstr = "";
    int res = 0;
    unsigned long biglen = 0x654a;
    signed long lminus= -55;
    char *mystr = "a01234";
    char *targetmystr = "a01234xyz";

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_s(&g,"initialstring-%s-finalstring",
        mystr);
    d = dwarfstring_string(&g);
    expstr = "initialstring-a01234-finalstring";
    check_string("from pct-s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"initialstring--0x%x-finalstring",
        biglen);
    d = dwarfstring_string(&g);
    expstr = "initialstring--0x654a-finalstring";
    check_string("from pct-s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"initialstring: %d (",
        lminus);
    dwarfstring_append_printf_u(&g,"0x%08x) -finalstring",
        lminus);
    d = dwarfstring_string(&g);
    expstr = "initialstring: -55 (0xffffffffffffffc9) -finalstring";
    check_string("from pct-s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"smallhex (0x%08x) -finalstring",
        biglen);
    d = dwarfstring_string(&g);
    expstr = "smallhex (0x0000654a) -finalstring";
    check_string("from pct-s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"longlead (%08u) -end",
        20);
    d = dwarfstring_string(&g);
    expstr = "longlead (00000020) -end";
    check_string("from pct-s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);
    return 0;
}
static int
test6(int tnum)
{
    dwarfstring g;
    char *d = 0;
    const char *expstr = 0;

    dwarfstring_constructor(&g);
    dwarfstring_append(&g,0);
    d = dwarfstring_string(&g);
    expstr = "";
    check_string("from append",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_s(&g,"%s",0);
    d = dwarfstring_string(&g);
    expstr="<ERROR: null string pointer to "
        "dwarfstring_append_printf_s>";
    check_string("from _s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_s(&g,0,"abc");
    d = dwarfstring_string(&g);
    expstr="<ERROR: null format pointer to "
        "dwarfstring_append_printf_s>";
    check_string("from _s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,0,12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: null format pointer to "
        "dwarfstring_append_printf_u>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,0,12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: null format pointer to "
        "dwarfstring_append_printf_i>";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);


    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"%s",12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: format %s passed to "
        "dwarfstring_append_printf_i>";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"%x",12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: format %x or %X passed to "
        "dwarfstring_append_printf_i>%x";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"%Xx",12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: format %x or %X passed to "
        "dwarfstring_append_printf_i>%Xx";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"%llld",12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: format has too many %x/d/u/l passed to "
        "dwarfstring_append_printf_i>";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_i(&g,"%+-d",12);
    d = dwarfstring_string(&g);
    expstr="<ERROR: format disallowed. +- passed to "
        "dwarfstring_append_printf_i>";
    check_string("from _i",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,0,43);
    d = dwarfstring_string(&g);
    expstr="<ERROR: null format pointer to "
        "dwarfstring_append_printf_u>";
    check_string("from _s",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"aa%- bb",100);
    d = dwarfstring_string(&g);
    expstr="aa<ERROR: format - passed to "
        "dwarfstring_append_printf_u "
        "cannot be handled>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"aa%s bb",77);
    d = dwarfstring_string(&g);
    expstr="aa<ERROR: format %s passed to "
        "dwarfstring_append_printf_u "
        "cannot be handled>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"aa%xXd ubb",77);
    d = dwarfstring_string(&g);
    expstr="aa<ERROR: format %x X d u repeats to "
        "dwarfstring_append_printf_u "
        "cannot be handled>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"aa%llld ubb",77);
    d = dwarfstring_string(&g);
    expstr="aa<ERROR: format % lll to "
        "dwarfstring_append_printf_u "
        "cannot be handled>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);

    dwarfstring_constructor(&g);
    dwarfstring_append_printf_u(&g,"aa%ld ubb",77);
    d = dwarfstring_string(&g);
    expstr="aa<ERROR: format %d to "
        "dwarfstring_append_printf_u "
        "cannot be handled>";
    check_string("from _u",(char *)expstr,d,__LINE__);
    dwarfstring_destructor(&g);
}


int main()
{
    test1(1);
    test2(2);
    test3(3);
    test4(4);
    test5(5);
    test6(6);
    if (errcount) {
        exit(1);
    }
    exit(0);
}