077fabe14a212836865cf70b389f7804133106bb
[platform/framework/web/crosswalk.git] / src / native_client / tests / barebones / barebones_vaarg.c
1 /*
2  * Copyright 2010 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can
4  * be found in the LICENSE file.
5  */
6
7 /*
8  * NaCl test for super simple program not using newlib
9  */
10
11 #include "barebones.h"
12 #include <stdarg.h>
13
14 /* globals are intentional to confuse optimizer */
15 int errors = 55;
16 int count = 0;
17 char buffer[16];
18
19 int cmp(long l) {
20   ++count;
21
22   if (l != count) {
23     myprint("Error is ");
24     myhextochar(l, buffer);
25     myprint(buffer);
26     myprint(" expected ");
27     myhextochar(count, buffer);
28     myprint(buffer);
29     myprint("\n");
30
31     return 1;
32   }
33
34   return 0;
35 }
36
37
38 int dump(const char *fmt, ...) {
39   int errors = 0;
40   int i;
41
42   va_list ap;
43   va_start(ap, fmt);
44
45   myprint(fmt);
46   myprint("\n");
47   count = 0;
48
49   for (i = 0; fmt[i]; ++i) {
50     if (fmt[i] == 'L') {
51       long l = va_arg(ap, long);
52       errors += cmp(l);
53     } else if (fmt[i] == 'Q') {
54       long long ll = (long long) va_arg(ap, long long);
55       errors += cmp((long)((ll >> 32) & 0xffffffffL));
56       errors += cmp((long)(ll & 0xffffffffL));
57     } else {
58       myprint("Error bad format");
59       errors += 100;
60     }
61   }
62   va_end(ap);
63
64   return errors;
65 }
66
67
68 void test(void) {
69   errors += dump("QQQL",
70                  0x0000000100000002LL,
71                  0x0000000300000004LL,
72                  0x0000000500000006LL,
73                  0x00000007L);
74
75   errors += dump("QQLQ",
76                  0x0000000100000002LL,
77                  0x0000000300000004LL,
78                  0x00000005L,
79                  0x0000000600000007LL);
80
81   errors += dump("QLQQ",
82                  0x0000000100000002LL,
83                  0x00000003L,
84                  0x0000000400000005LL,
85                  0x0000000600000007LL);
86
87   errors += dump("LQQQ",
88                  0x00000001L,
89                  0x0000000200000003LL,
90                  0x0000000400000005LL,
91                  0x0000000600000007LL);
92
93   errors += dump("QQQ",
94                  0x0000000100000002LL,
95                  0x0000000300000004LL,
96                  0x0000000500000006LL);
97
98   errors += dump("LLLLLL",
99                  0x00000001L,
100                  0x00000002L,
101                  0x00000003L,
102                  0x00000004L,
103                  0x00000005L,
104                  0x00000006L);
105 }
106
107
108 int main(void) {
109   test();
110   NACL_SYSCALL(exit)(errors);
111   /* UNREACHABLE */
112   return 0;
113 }