imported brians code
[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 { 
102         printf("%d\n", 0xffffff);
103         printf("%u\n", 0xffffffU);
104         printf("%li\n",0xffffffL);
105         printf("%lu\n",0xffffffUL);
106
107         
108         for(i=0;i<argc;i++)
109             printf("argv[%d] = \"%s\"\n",i,argv[i]);
110         for(i=0;user_info[i];i++)
111             printf("user_info[%d] = \"%s\"\n",i,user_info[i]);
112         
113         printf("getenv(\"USER\") = \"%s\"\n",getenv("USER"));
114         printf("getenv(\"HOME\") = \"%s\"\n",getenv("HOME"));
115         printf("getenv(\"TZ\") = \"%s\"\n",getenv("TZ"));
116     
117         time(&now);
118         tzset();
119         printf("%s %s %d\n",tzname[0],tzname[1],(int)_timezone);
120         
121         printf("Running ctime\n");
122         s = ctime(&now);
123         printf("ctime returned: %p\n",s);
124         printf("Current time: %s",s);
125         
126         printf("Trying to open /nonexistent\n");
127         fd = open("/nonexistent",O_RDONLY);
128         if(fd < 0) perror("open");
129         else close(fd);
130         
131         printf("Tyring to mkdir .mkdirtest\n");
132         if(mkdir(".mkdirtest",0700) < 0) perror("mkdir");
133         
134         printf("Trying to opendir /\n");
135         dir = opendir("/");
136         if(dir) {
137             while((dent=readdir(dir))!=NULL)
138                 printf("\t[%s] %lu\n",dent->d_name,dent->d_ino);
139             closedir(dir);
140         } else {
141             perror("opendir");
142         }
143                 
144     
145 #if 0
146         printf("Sleeping...\n");
147         sleep(1);
148         printf("Done\n");
149 #endif
150         
151         fd = open("test.txt",O_RDONLY);
152         if(fd != -1) {
153             printf("Opened test.txt\n");
154             n = read(fd,sbuf,sizeof(sbuf));
155             printf("n: %d\n",n);
156             if(n < 0) perror("read");
157             ubuf[n] = '\0';
158             printf("buf: %s\n",buf);
159             for(i=0;i<n/2;i++) {
160                 printf("Char %d: [%x]\n",i,sbuf[i]);
161             }
162         }
163         
164         {
165             static double f = 1.574;
166             int n;
167             printf("%e\n",f);
168             f += 20.001;
169             f *= -2.0;
170             n = (int) f;
171             printf("%el\n",f);
172             printf("%d\n",n);
173             printf("%e\n",f);
174             printf("%e\n",fabs(f));
175         }
176     }
177         
178     printf("exiting\n");
179     return 0;
180 }
181
182 void suckram() {
183     int total = 0;
184     fprintf(stderr,"Eating up all available memory\n");
185     while(malloc(1024*1024) != NULL) total ++;
186     fprintf(stderr,"Ate up %d megs\n",total);
187 }
188
189 __attribute__((constructor)) static void my_ctor()  { printf("Constructor!\n"); }
190 __attribute__((destructor)) static void my_dtor()  { printf("Destructor!\n"); }
191
192 int callme(int a1,int a2, int a3, int a4, int a5, int a6)  __attribute__((section(".text")));
193 int callme(int a1,int a2, int a3, int a4, int a5, int a6) {
194     printf("You said: %d %d %d %d %d %d\n",a1,a2,a3,a4,a5,a6);
195     return a1+a2+a3+a4+a5+a6;
196 }
197
198 void echo(const char *string, int count)  __attribute__((section(".text")));
199 void echo(const char *string, int count) {
200     int i;
201     for(i=0;i<count;i++)
202         printf("%d: %s\n",i,string);
203 }
204
205 void backinmips()  __attribute__((section(".text")));
206 void backinmips() {
207     fprintf(stderr,"In backinmips() in mips\n");
208 }