File: vaargs.c

package info (click to toggle)
cc1111 2.9.0-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,692 kB
  • ctags: 132,262
  • sloc: ansic: 442,650; cpp: 37,006; sh: 10,334; makefile: 5,511; asm: 5,279; yacc: 2,953; lisp: 1,524; perl: 807; awk: 493; python: 468; lex: 447
file content (80 lines) | stat: -rw-r--r-- 1,728 bytes parent folder | download | duplicates (5)
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
/** Tests argument passing to functions via va_args.
    Assumes that up to the first two arguments can be passed in registers.

    type1: va_char, int
    type2: va_char, int
    type3: va_char, int
 */
#include <testfwk.h>
#include <stdarg.h>

/* gcc 3.3 throws a warning, if char is in '...' */
#if defined(PORT_HOST)
# define va_char int
#else
# define va_char char
#endif

static {type1}
returnFirstArg(int marker, ...)
{
    va_list ap;
    {type1} i;

    va_start(ap, marker);
    i = va_arg(ap, {type1});

    va_end(ap);

    LOG(("Returning %u\n", i));
    return i;
}

static {type2}
returnSecondArg(int marker, ...)
{
    va_list ap;
    {type2} i;

    va_start(ap, marker);
    UNUSED(va_arg(ap, {type1}));
    i = va_arg(ap, {type2});

    va_end(ap);

    LOG(("Returning %u\n", i));
    return i;
}

static {type3}
returnThirdArg(int marker, ...)
{
    va_list ap;
    {type3} i;

    va_start(ap, marker);
    UNUSED(va_arg(ap, {type1}));
    UNUSED(va_arg(ap, {type2}));
    i = va_arg(ap, {type3});

    va_end(ap);

    LOG(("Returning %u\n", i));
    return i;
}

void
testArgs(void)
{
    int marker = 12;

    LOG(("First arg: %u\n", returnFirstArg(marker, ({type1})123, ({type2})45, ({type3})67)));
    ASSERT(returnFirstArg(marker, ({type1})123, ({type2})45, ({type3})67) == 123);
    ASSERT(returnFirstArg(marker, ({type1})-123, ({type2})45, ({type3})67) == -123);

    ASSERT(returnSecondArg(marker, ({type1})1, ({type2})-23, ({type3})64) == -23);
    ASSERT(returnSecondArg(marker, ({type1})1, ({type2})8, ({type3})64) == 8);

    ASSERT(returnThirdArg(marker, ({type1})-33, ({type2})-34, ({type3})-35) == -35);
    ASSERT(returnThirdArg(marker, ({type1})-33, ({type2})-34, ({type3})35) == 35);
}