0e49b450c9cc1aaf1de7e86aff2f7baca7d410d6
[nestedvm.git] / src / tests / Test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/fcntl.h>
7 #include <sys/unistd.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <sys/signal.h>
11 #include <time.h>
12 #include <dirent.h>
13 #include <wchar.h>
14 #include <math.h>
15
16 char *user_info[1024];
17
18 extern void _pause();
19 extern int _call_java(int a, int b, int c, int d);
20
21 void suckram();
22
23 int main(int argc, char **argv) {
24     int i,n,fd;
25     time_t now;
26     DIR *dir;
27     struct dirent *dent;
28     char buf[1024];
29     unsigned char ubuf[1024];
30     unsigned short sbuf[1024];
31     char *s;
32     
33     printf("Entered main()\n");
34     
35     if(argc > 1 && strcmp(argv[1],"calltest")==0)  {
36         printf("pausing for call test\n");
37         _pause();
38         printf("unpaused from call test\n");
39         
40         for(i=1;i<=3;i++) {
41             char *s = (char*)_call_java(i,0,0,0);
42             printf("_call_java(%d,0,0,0) = \"%s\" (%d chars)\n",i,s,strlen(s));
43             free(s);
44         }
45         fd = _call_java(4,0,0,0);
46         if(fd != -1) {
47             FILE *fp;
48             fprintf(stderr,"fd: %i\n",fd);
49             fp = fdopen(fd,"w");
50             if(fp != NULL) {
51                 fprintf(fp,"It worked! fp is %p - Hello, Java!\n",fp);
52                 fclose(fp);
53             } else {
54                 fprintf(stderr,"fdopen failed\n");
55                 close(fd);
56             }
57         } else {
58             fprintf(stderr,"fd == -1\n");
59         }
60         
61         printf("In main() in MIPS\n");
62         _call_java(5,0,0,0);
63         printf("Back in main() in MIPS\n");
64     } else if(argc > 2 && strcmp(argv[1],"fdtest")==0)  {
65         printf("opening %s\n",argv[2]);
66         fd = open(argv[2],O_RDONLY);
67         if(fd < 0) { perror("open"); exit(1); }
68         
69         printf("reading up to 64 bytes\n");
70         n = read(fd,buf,64);
71         if(n < 0) {perror("read"); exit(1); }
72         printf("read %d bytes\n",n);
73         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
74         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
75         
76         printf("seeking back to pos 4...\n");
77         if(lseek(fd,4,SEEK_SET) < 0) { perror("lseek"); exit(1); }
78         
79         printf("reading up to 64 bytes\n");
80         n = read(fd,buf,64);
81         if(n < 0) {perror("read"); exit(1); }
82         printf("read %d bytes\n",n);
83         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
84         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
85
86         printf("reading up to 64 bytes\n");
87         n = read(fd,buf,64);
88         if(n < 0) {perror("read"); exit(1); }
89         printf("read %d bytes\n",n);
90         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
91         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
92     } else if(argc > 1 && strcmp(argv[1],"fptest")==0)  {
93         double d = 0.0;
94         while(d != 10.0) {
95             printf("d: %f\n",d);
96             d += 2.5;
97         }
98     } else if(argc > 1 && strcmp(argv[1],"nullderef")==0) {
99         volatile int *mem = 0;
100         *mem = 1;
101     } else if(argc > 2 && strcmp(argv[1],"crashme") == 0) {
102         volatile int *mem = (int*) atoi(argv[2]);
103         *mem = 1;
104     } else { 
105         printf("%d\n", 0xffffff);
106         printf("%u\n", 0xffffffU);
107         printf("%li\n",0xffffffL);
108         printf("%lu\n",0xffffffUL);
109
110         
111         for(i=0;i<argc;i++)
112             printf("argv[%d] = \"%s\"\n",i,argv[i]);
113         for(i=0;user_info[i];i++)
114             printf("user_info[%d] = \"%s\"\n",i,user_info[i]);
115         
116         printf("getenv(\"USER\") = \"%s\"\n",getenv("USER"));
117         printf("getenv(\"HOME\") = \"%s\"\n",getenv("HOME"));
118         printf("getenv(\"TZ\") = \"%s\"\n",getenv("TZ"));
119     
120         time(&now);
121         tzset();
122         printf("%s %s %d\n",tzname[0],tzname[1],(int)_timezone);
123         
124         printf("Running ctime\n");
125         s = ctime(&now);
126         printf("ctime returned: %p\n",s);
127         printf("Current time: %s",s);
128         
129         printf("Trying to open /nonexistent\n");
130         fd = open("/nonexistent",O_RDONLY);
131         if(fd < 0) perror("open");
132         else close(fd);
133         
134         printf("Tyring to mkdir .mkdirtest\n");
135         if(mkdir(".mkdirtest",0700) < 0) perror("mkdir");
136         
137         printf("Trying to opendir .\n");
138         dir = opendir(".");
139         if(dir) {
140             printf("Success!\n");
141             while((dent=readdir(dir))!=NULL) {
142                 struct stat statbuf;
143                 stat(dent->d_name,&statbuf);
144                 printf("\t[%s] %lu %i %i\n",dent->d_name,dent->d_ino,statbuf.st_ino,statbuf.st_dev);
145             }
146             if(errno != 0) { fprintf(stderr,"readdir errno: %d\n",errno); perror("readdir"); }
147             closedir(dir);
148         } else {
149             perror("opendir");
150         }
151                 
152     
153 #if 0
154         printf("Sleeping...\n");
155         sleep(1);
156         printf("Done\n");
157 #endif
158         
159         fd = open("test.txt",O_RDONLY);
160         if(fd != -1) {
161             printf("Opened test.txt\n");
162             n = read(fd,sbuf,sizeof(sbuf));
163             printf("n: %d\n",n);
164             if(n < 0) perror("read");
165             ubuf[n] = '\0';
166             printf("buf: %s\n",buf);
167             for(i=0;i<n/2;i++) {
168                 printf("Char %d: [%x]\n",i,sbuf[i]);
169             }
170         }
171         
172         {
173             static double f = 1.574;
174             int n;
175             printf("%e\n",f);
176             f += 20.001;
177             f *= -2.0;
178             n = (int) f;
179             printf("%el\n",f);
180             printf("%d\n",n);
181             printf("%e\n",f);
182             printf("%e\n",fabs(f));
183         }
184     }
185         
186     {
187         char buf[1024];
188         memcpy(buf,"Hello, World",sizeof("Hello, World"));
189         printf("%s\n",buf);
190     }
191     printf("cwd: %s\n",getcwd(NULL,0));
192     printf("isatty(0): %d\n",isatty(0));
193     printf("exiting\n");
194     return 0;
195 }
196
197 void suckram() {
198     int total = 0;
199     fprintf(stderr,"Eating up all available memory\n");
200     while(malloc(1024*1024) != NULL) total ++;
201     fprintf(stderr,"Ate up %d megs\n",total);
202 }
203
204 __attribute__((constructor)) static void my_ctor()  { printf("Constructor!\n"); }
205 __attribute__((destructor)) static void my_dtor()  { printf("Destructor!\n"); }
206
207 int callme(int a1,int a2, int a3, int a4, int a5, int a6)  __attribute__((section(".text")));
208 int callme(int a1,int a2, int a3, int a4, int a5, int a6) {
209     printf("You said: %d %d %d %d %d %d\n",a1,a2,a3,a4,a5,a6);
210     return a1+a2+a3+a4+a5+a6;
211 }
212
213 void echo(const char *string, int count)  __attribute__((section(".text")));
214 void echo(const char *string, int count) {
215     int i;
216     for(i=0;i<count;i++)
217         printf("%d: %s\n",i,string);
218 }
219
220 void backinmips()  __attribute__((section(".text")));
221 void backinmips() {
222     fprintf(stderr,"In backinmips() in mips\n");
223 }