cleaner gcc build process
[nestedvm.git] / src / tests / Test.c
index 88e1eb6..e8aa5df 100644 (file)
@@ -98,6 +98,39 @@ int main(int argc, char **argv) {
     } else if(argc > 1 && strcmp(argv[1],"nullderef")==0) {
         volatile int *mem = 0;
         *mem = 1;
+    } else if(argc > 2 && strcmp(argv[1],"crashme") == 0) {
+        volatile int *mem = (int*) atoi(argv[2]);
+        *mem = 1;
+    } else if(argc > 2 && strcmp(argv[1],"get") == 0) {
+        extern int _open_socket(const char *host, int port);
+        fd = _open_socket(argv[2],80);
+        if(fd == -1) { perror("open_socket"); exit(EXIT_FAILURE); }
+#define REQ "GET / HTTP/1.0\r\n\r\n"
+        n = write(fd,REQ,strlen(REQ));
+        if(n != strlen(REQ)) { perror("write"); exit(EXIT_FAILURE); }
+        for(;;) {
+            n = read(fd,buf,sizeof(buf));
+            if(n < 0) { perror("read"); exit(EXIT_FAILURE); }
+            if(n == 0) break;
+            write(1,buf,n);
+        }
+    } else if(argc > 1 && strcmp(argv[1],"server") == 0) {
+        extern int _listen_socket(int port);
+        extern int _accept(int fd);
+        
+        int server = _listen_socket(2000);
+        if(server< 0) { perror("server_socket"); exit(EXIT_FAILURE); }
+        while((fd = _accept(server)) >= 0) {
+            char buf2[1024];
+            int n = read(fd,buf,sizeof(buf));
+            if(n < 0) { perror("read"); continue; }
+            if(n == 0) continue;
+            while(n > 0 && (buf[n-1] == '\r' || buf[n-1] == '\n')) n--;
+            buf[n] = '\0';
+            snprintf(buf2,sizeof(buf2),"Hello, %s from nestedvm's socket support\r\n",buf);
+            write(fd,buf2,strlen(buf2));
+            close(fd);
+        }
     } else { 
         printf("%d\n", 0xffffff);
         printf("%u\n", 0xffffffU);
@@ -134,8 +167,13 @@ int main(int argc, char **argv) {
         printf("Trying to opendir .\n");
         dir = opendir(".");
         if(dir) {
-            while((dent=readdir(dir))!=NULL)
-                printf("\t[%s] %lu\n",dent->d_name,dent->d_ino);
+            printf("Success!\n");
+            while((dent=readdir(dir))!=NULL) {
+                struct stat statbuf;
+                stat(dent->d_name,&statbuf);
+                printf("\t[%s] %lu %i %i\n",dent->d_name,dent->d_ino,statbuf.st_ino,statbuf.st_dev);
+            }
+            if(errno != 0) { fprintf(stderr,"readdir errno: %d\n",errno); perror("readdir"); }
             closedir(dir);
         } else {
             perror("opendir");
@@ -175,10 +213,49 @@ int main(int argc, char **argv) {
         }
     }
         
-    printf("exiting\n");
+    {
+        char buf[1024];
+        memcpy(buf,"Hello, World",sizeof("Hello, World"));
+        printf("%s\n",buf);
+    }
+    
+    {
+        
+#define HOST_BITS_PER_WIDE_INT 64
+#define HOST_WIDE_INT long long
+        
+        extern int ri(int n);
+        int precision = ri(8);
+        long long l;
+        
+        l = (precision - HOST_BITS_PER_WIDE_INT > 0
+             ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
+            (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
+             ? (((HOST_WIDE_INT) 1
+                 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
+             : 0);
+        
+        printf("%llX\n",l);
+    }
+    
+    {
+        double d = -2.34;
+        //d = abs(d);
+        printf("abs(-2.24) = %g\n",d);
+    }
+        
+    
+    //printf("cwd: %s\n",getcwd(NULL,0));
+    //printf("isatty(0): %d\n",isatty(0));
+    //printf("exiting\n");
     return 0;
 }
 
+long long zero = 0;
+int izero = 0;
+long long rl(long long n) { return n + zero; }
+int ri(int n) { return n + izero; }
+
 void suckram() {
     int total = 0;
     fprintf(stderr,"Eating up all available memory\n");