fix more fixmes/features
[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             while((dent=readdir(dir))!=NULL)
141                 printf("\t[%s] %lu\n",dent->d_name,dent->d_ino);
142             closedir(dir);
143         } else {
144             perror("opendir");
145         }
146                 
147     
148 #if 0
149         printf("Sleeping...\n");
150         sleep(1);
151         printf("Done\n");
152 #endif
153         
154         fd = open("test.txt",O_RDONLY);
155         if(fd != -1) {
156             printf("Opened test.txt\n");
157             n = read(fd,sbuf,sizeof(sbuf));
158             printf("n: %d\n",n);
159             if(n < 0) perror("read");
160             ubuf[n] = '\0';
161             printf("buf: %s\n",buf);
162             for(i=0;i<n/2;i++) {
163                 printf("Char %d: [%x]\n",i,sbuf[i]);
164             }
165         }
166         
167         {
168             static double f = 1.574;
169             int n;
170             printf("%e\n",f);
171             f += 20.001;
172             f *= -2.0;
173             n = (int) f;
174             printf("%el\n",f);
175             printf("%d\n",n);
176             printf("%e\n",f);
177             printf("%e\n",fabs(f));
178         }
179     }
180         
181     printf("exiting\n");
182     return 0;
183 }
184
185 void suckram() {
186     int total = 0;
187     fprintf(stderr,"Eating up all available memory\n");
188     while(malloc(1024*1024) != NULL) total ++;
189     fprintf(stderr,"Ate up %d megs\n",total);
190 }
191
192 __attribute__((constructor)) static void my_ctor()  { printf("Constructor!\n"); }
193 __attribute__((destructor)) static void my_dtor()  { printf("Destructor!\n"); }
194
195 int callme(int a1,int a2, int a3, int a4, int a5, int a6)  __attribute__((section(".text")));
196 int callme(int a1,int a2, int a3, int a4, int a5, int a6) {
197     printf("You said: %d %d %d %d %d %d\n",a1,a2,a3,a4,a5,a6);
198     return a1+a2+a3+a4+a5+a6;
199 }
200
201 void echo(const char *string, int count)  __attribute__((section(".text")));
202 void echo(const char *string, int count) {
203     int i;
204     for(i=0;i<count;i++)
205         printf("%d: %s\n",i,string);
206 }
207
208 void backinmips()  __attribute__((section(".text")));
209 void backinmips() {
210     fprintf(stderr,"In backinmips() in mips\n");
211 }