initial checkin
[org.ibex.nanogoat.git] / upstream / mips / 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
15 char *user_info[1024];
16
17 extern void _pause();
18 extern int _call_java(int a, int b, int c, int d);
19
20 void suckram();
21
22 int main(int argc, char **argv) {
23     int i,n,fd;
24     time_t now;
25     DIR *dir;
26     struct dirent *dent;
27     char buf[1024];
28     
29     printf("Entered main()\n");
30     
31     if(argc > 1 && strcmp(argv[1],"calltest")==0)  {
32         printf("pausing for call test\n");
33         _pause();
34         printf("unpaused from call test\n");
35         
36         for(i=1;i<=3;i++) {
37             char *s = (char*)_call_java(i,0,0,0);
38             printf("_call_java(%d,0,0,0) = \"%s\" (%d chars)\n",i,s,strlen(s));
39             free(s);
40         }
41         fd = _call_java(4,0,0,0);
42         if(fd != -1) {
43             FILE *fp;
44             fprintf(stderr,"fd: %i\n",fd);
45             fp = fdopen(fd,"w");
46             if(fp != NULL) {
47                 fprintf(fp,"It worked! fp is %p - Hello, Java!\n",fp);
48                 fclose(fp);
49             } else {
50                 fprintf(stderr,"fdopen failed\n");
51                 close(fd);
52             }
53         } else {
54             fprintf(stderr,"fd == -1\n");
55         }
56         
57         printf("In main() in MIPS\n");
58         _call_java(5,0,0,0);
59         printf("Back in main() in MIPS\n");
60     } else if(argc > 2 && strcmp(argv[1],"fdtest")==0)  {
61         printf("opening %s\n",argv[2]);
62         fd = open(argv[2],O_RDONLY);
63         if(fd < 0) { perror("open"); exit(1); }
64         
65         printf("reading up to 64 bytes\n");
66         n = read(fd,buf,64);
67         if(n < 0) {perror("read"); exit(1); }
68         printf("read %d bytes\n",n);
69         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
70         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
71         
72         printf("seeking back to pos 4...\n");
73         if(lseek(fd,4,SEEK_SET) < 0) { perror("lseek"); exit(1); }
74         
75         printf("reading up to 64 bytes\n");
76         n = read(fd,buf,64);
77         if(n < 0) {perror("read"); exit(1); }
78         printf("read %d bytes\n",n);
79         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
80         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
81
82         printf("reading up to 64 bytes\n");
83         n = read(fd,buf,64);
84         if(n < 0) {perror("read"); exit(1); }
85         printf("read %d bytes\n",n);
86         for(i=0;i<n;i++) if(buf[i]=='\n' || buf[i]=='\r') { buf[i] = '\0'; break; }
87         printf("Read \"%s\"...\n",n == 0 ? NULL : buf);
88     } else if(argc > 1 && strcmp(argv[1],"fptest")==0)  {
89         double d = 0.0;
90         while(d != 10.0) {
91             printf("d: %f\n",d);
92             d += 2.5;
93         }
94     } else {
95         for(i=0;i<argc;i++)
96             printf("argv[%d] = \"%s\"\n",i,argv[i]);
97         for(i=0;user_info[i];i++)
98             printf("user_info[%d] = \"%s\"\n",i,user_info[i]);
99         
100         printf("getenv(\"USER\") = \"%s\"\n",getenv("USER"));
101         printf("getenv(\"HOME\") = \"%s\"\n",getenv("HOME"));
102         printf("getenv(\"TZ\") = \"%s\"\n",getenv("TZ"));
103     
104         time(&now);
105         tzset();
106         printf("%s %s %d\n",tzname[0],tzname[1],(int)_timezone);
107         printf("Current time: %s",ctime(&now));
108
109         printf("Trying to open /nonexistent\n");
110         fd = open("/nonexistent",O_RDONLY);
111         if(fd < 0) perror("open");
112         else close(fd);
113         
114         printf("Tyring to mkdir .mkdirtest\n");
115         if(mkdir(".mkdirtest",0700) < 0) perror("mkdir");
116         
117         printf("Trying to opendir /\n");
118         dir = opendir("/");
119         if(dir) {
120             while((dent=readdir(dir))!=NULL)
121                 printf("\t[%s] %lu\n",dent->d_name,dent->d_ino);
122             closedir(dir);
123         } else {
124             perror("opendir");
125         }
126                 
127     
128         printf("Sleeping...\n");
129         sleep(1);
130         printf("Done\n");
131         
132         /*suckram();*/
133     }
134     {
135         wchar_t wstring[] = L"My wchar_t string";
136         printf("sizeof(wstring) = %u wcslen(wstring) = %u",sizeof(wstring),wcslen(wstring));
137         
138     }
139         
140     printf("exiting\n");
141     return 0;
142 }
143
144 void suckram() {
145     int total = 0;
146     fprintf(stderr,"Eating up all available memory\n");
147     while(malloc(1024*1024) != NULL) total ++;
148     fprintf(stderr,"Ate up %d megs\n",total);
149 }
150
151 __attribute__((constructor)) static void my_ctor()  { printf("Constructor!\n"); }
152 __attribute__((destructor)) static void my_dtor()  { printf("Destructor!\n"); }
153
154 int callme(int a1,int a2, int a3, int a4, int a5, int a6)  __attribute__((section(".text")));
155 int callme(int a1,int a2, int a3, int a4, int a5, int a6) {
156     printf("You said: %d %d %d %d %d %d\n",a1,a2,a3,a4,a5,a6);
157     return a1+a2+a3+a4+a5+a6;
158 }
159
160 void echo(const char *string, int count)  __attribute__((section(".text")));
161 void echo(const char *string, int count) {
162     int i;
163     for(i=0;i<count;i++)
164         printf("%d: %s\n",i,string);
165 }
166
167 void backinmips()  __attribute__((section(".text")));
168 void backinmips() {
169     fprintf(stderr,"In backinmips() in mips\n");
170 }