Initial commit to Gerrit
[profile/ivi/orc.git] / testsuite / compile_parse_c.c
1
2 #include <orc/orc.h>
3 #include <orc-test/orctest.h>
4 #include <orc/orcparse.h>
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 static char * read_file (const char *filename);
10 void output_code (OrcProgram *p, FILE *output);
11 void output_code_header (OrcProgram *p, FILE *output);
12 void output_code_test (OrcProgram *p, FILE *output);
13
14 int error = FALSE;
15
16 int
17 main (int argc, char *argv[])
18 {
19   char *code;
20   int n;
21   int i;
22   OrcProgram **programs;
23   const char *filename = "test.orc";
24
25   orc_init ();
26   orc_test_init ();
27
28   if (argc >= 2) {
29     filename = argv[1];
30   }
31   code = read_file (filename);
32   if (!code) {
33     printf("compile_parse_test <file.orc>\n");
34     exit(1);
35   }
36
37   n = orc_parse (code, &programs);
38
39   for(i=0;i<n;i++){
40     OrcTestResult ret;
41
42     printf("%s:\n", programs[i]->name);
43     ret = orc_test_gcc_compile (programs[i]);
44     if (ret == ORC_TEST_FAILED) {
45       error = TRUE;
46     }
47   }
48
49   if (error) return 1;
50   return 0;
51 }
52
53
54 static char *
55 read_file (const char *filename)
56 {
57   FILE *file = NULL;
58   char *contents = NULL;
59   long size;
60   int ret;
61
62   file = fopen (filename, "r");
63   if (file == NULL) return NULL;
64
65   ret = fseek (file, 0, SEEK_END);
66   if (ret < 0) goto bail;
67
68   size = ftell (file);
69   if (size < 0) goto bail;
70
71   ret = fseek (file, 0, SEEK_SET);
72   if (ret < 0) goto bail;
73
74   contents = malloc (size + 1);
75   if (contents == NULL) goto bail;
76
77   ret = fread (contents, size, 1, file);
78   if (ret < 0) goto bail;
79
80   contents[size] = 0;
81
82   return contents;
83 bail:
84   /* something failed */
85   if (file) fclose (file);
86   if (contents) free (contents);
87
88   return NULL;
89 }
90