imcplugin demo: Extend to support stat() call
[nativeclient.git] / tests / app_lib / app_lib_test.c
blob9745cc16cf5d55c932b890f1f8226a726630f5ef
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
42 * TODO: enable thread local storage.
44 #define THREAD
46 char **environ;
48 char bss_buf[1024]; /* just to see ELF info */
49 int bss_int;
51 THREAD int tls_bss_int;
52 THREAD char tls_bss_buf[8192];
54 THREAD int tls_data_int = 10;
55 THREAD char tls_data_buf[128] = "Hello world\n";
58 void test_tls(void)
60 printf("%s\n", tls_data_buf);
64 void test_fmt(char *fmt, int val)
66 fputs(fmt, stdout); putc(':', stdout);
67 printf(fmt, val);
68 putc('\n', stdout);
71 void test_fp_fmt(char *fmt, double val)
73 fputs(fmt, stdout); putc(':', stdout);
74 printf(fmt, val);
75 putc('\n', stdout);
78 void test_fp_val(double val)
80 printf("\n123456789012345678901234567890"
81 "123456789012345678901234567890\n");
82 test_fp_fmt("%e", val);
83 test_fp_fmt("%30.15e.", val);
84 test_fp_fmt("%-30.15e.", val);
85 test_fp_fmt("%f.", val);
86 test_fp_fmt("%30.15f.", val);
87 test_fp_fmt("%-30.15f.", val);
90 void test_fp(void)
92 double d;
94 printf("one half is %e\n", 0.5);
95 printf("one half is %f\n", 0.5);
96 printf("one third is %e\n", 1.0/3.0);
97 printf("one third is %f\n", 1.0/3.0);
99 test_fp_val(0.0);
100 test_fp_fmt("%.500e", 1.0);
101 d = 1.234567e+10;
102 test_fp_val(d);
103 test_fp_val(-d);
105 d = 1.234567e308;
106 test_fp_val(d);
107 test_fp_val(-d);
109 d = 1.234567e-307;
110 test_fp_val(d);
111 test_fp_val(-d);
113 d = 1.234567e-310;
114 test_fp_val(d);
115 test_fp_val(-d);
116 #if 0
117 /* TODO: the code below doesn't compile on Windows
118 NOTE: disabled this code for all platforms
119 to be able to do with one golden file */
120 d = 1.234567e+310; /* inf */
121 test_fp_val(d);
122 test_fp_val(-d);
123 d = 1.234567e+310; /* inf */
124 test_fp_val(d);
125 test_fp_val(-d);
127 d = 0.0/0.0; /* nan */
128 test_fp_val(d);
129 #endif /*NACL_WINDOWS*/
132 void test_malloc(void)
134 char *p;
135 printf("test_malloc: entered\n");
136 p = malloc(10);
137 fprintf(stderr, "# got 0x%08x\n", (unsigned int) p);
138 printf("test_malloc: leaving\n");
141 void test_time(void)
143 time_t tval;
144 printf("test_time: entered\n");
145 tval = time(NULL);
146 fprintf(stderr, "# current time is %d \n", (int) tval);
147 printf("test_time: leaving\n");
150 void test_fopen(const char* fn)
152 FILE *fp;
153 int c;
154 char line[1024];
155 char *rv;
157 printf("test_fopen: entered\n");
158 fp = fopen(fn, "rb");
159 if (NULL == fp) {
160 printf("could not open file %s!\n", fn);
161 exit(-1);
162 } else {
163 printf("reading file\n");
164 while (EOF != (c = fgetc(fp))) {
165 if ((int)c != 13)
167 TODO: make sure the input files are identical
168 on all platforms, so we won't need the if.
170 putc(c, stdout);
172 fclose(fp);
175 while (0 != (rv = (fputs("input: ", stdout),
176 fflush(stdout),
177 fgets(line, sizeof line, stdin)))) {
178 printf("Got line: <<%s>>, %d\n", rv, strlen(rv));
180 printf("test_fopen: done\n");
184 int NaClMain(const char* fn)
186 char const *hello = "Hello world\n";
187 char const *bye = "Goodbye cruel world";
188 int r;
190 r = write(1, hello, strlen(hello));
192 printf("write returned %d.\n", r);
193 test_fmt("<%04d>", r);
194 test_fmt("<%-d>", r);
195 test_fmt("<%-04d>", r);
196 r = -r;
197 test_fmt("<%d>", r);
198 test_fmt("<%04d>", r);
199 test_fmt("<%-d>", r);
200 test_fmt("<%-04d>", r);
202 printf("The message was \"%s\".\n", hello);
204 printf("Bye message: \"%25s\".\n", bye);
205 printf("Bye message: \"%-25s\".\n", bye);
207 test_fp();
208 test_malloc();
209 test_time();
210 test_fopen(fn);
212 fflush(0);
213 fprintf(stderr, "# Hello is at address 0x%08lx\n", (long) hello);
214 fprintf(stderr, "# Hello is at address %p\n", hello);
216 short s = -1;
217 unsigned short us = (unsigned short) -1;
219 printf("s = %hd\n", s);
220 printf("us = %hu\n", us);
223 printf("Goodbye cruel world. Really.\n");
224 fprintf(stderr, "My pid is %d\n", getpid());
225 fflush(0);
226 return 0;
229 int SimpleMain(void)
231 static char const hello[] = "Hello from SimpleMain\n";
233 write(1, hello, -1 + sizeof(hello));
234 return 0;
237 int main(int argc, char *argv[], char **ep)
239 if (argc < 2) {
240 printf("you must specify a file to read\n");
241 exit(-1);
243 #if 1
244 return NaClMain(argv[1]);
245 #else
246 return SimpleMain();
247 #endif
250 void test_start(char *arg, ...)
252 int argc;
253 char **argv;
254 char **envp;
256 argv = &arg;
257 argc = *(int *)(argv-1);
258 envp = argv + argc + 1;
260 environ = envp;
262 exit(main(argc, argv, envp));