]> pd.if.org Git - lice/blob - misc/argsgen.c
autocommit for files dated 2014-11-17 20:15:26
[lice] / misc / argsgen.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "../util.h"
6 #define EMAX       128 // maximum arguments
7 #define SPLIT_CALL 8   // argument count to split by newline on function calls
8 #define SPLIT_ARG  4   // argument count to split by newline in function decls
9
10 int main() {
11     FILE *fp = fopen("tests/args.c", "w");
12     fprintf(fp, "// function arguments\n\n");
13
14     const char *types[] = {
15         "int", "short", "long",
16         "$"
17     };
18
19     table_t *table = table_create(NULL); // table for last thing
20
21     for (int e = 0; e < sizeof(types)/sizeof(types[0]); e++) {
22         if (types[e][0] != '$')
23             fprintf(fp, "void test_%s(", types[e]);
24         else
25             fprintf(fp, "void test_mix(");
26
27         for (int i = 1; i < EMAX; i++) {
28             if (types[e][0] != '$')
29                 fprintf(fp, "%-6s value%-3d", types[e], i);
30             else {
31                 string_t *string = string_create();
32                 const char *type = types[rand() % (sizeof(types)/sizeof(types[0]) - 1)];
33                 fprintf(fp, "%-6s value%-3d", type, i);
34                 string_catf(string, "%d", i);
35                 table_insert(table, string_buffer(string), (void*)type);
36             }
37             if (i != EMAX-1)
38                 fprintf(fp, ", ");
39             if (i % SPLIT_ARG == 0) {
40                 fprintf(fp, "\n");
41                 if (types[e][0] != '$') {
42                     fprintf(fp, "           "); // void test_ whitespace
43                     for (int j = 0; j < strlen(types[e]); j++)
44                         fprintf(fp, " ");
45                 } else {
46                     fprintf(fp, "              ");
47                 }
48             }
49         }
50
51         fprintf(fp, ") {\n");
52         for (int i = 1; i < EMAX; i++) {
53             char t = types[e][0];
54             if (t == '$') {
55                 string_t *key = string_create();
56                 string_catf(key, "%d", i);
57                 const char *type = table_find(table, string_buffer(key));
58                 t = type[0];
59             }
60             if (types[e][0] == 'd')
61                 fprintf(fp, "    expect%c(value%d, 0.%d);\n", t, i, i);
62             else if (types[e][0] == 'f')
63                 fprintf(fp, "    expect%c(value%d, 0.%d);\n", t, i, i);
64             else
65                 fprintf(fp, "    expect%c(value%d, %d);\n", t, i, i);
66         }
67         fprintf(fp, "}\n");
68     }
69
70     fprintf(fp, "int main() {\n");
71     for (int i = 0; i < sizeof(types)/sizeof(types[0]) - 1; i++) {
72         fprintf(fp, "    test_%s(", types[i]);
73         for (int j = 1; j < EMAX; j++) {
74             if (types[i][0] == 'f') {
75                 string_t *len = string_create();
76                 string_catf(len, "0.%df", j);
77                 fprintf(fp, string_buffer(len), j);
78                 for (int p = strlen(string_buffer(len)); p <= 5; p++)
79                     fprintf(fp, " ");
80             } else if (types[i][0] == 'd')
81                 fprintf(fp, "0.%-3d", j);
82             else
83                 fprintf(fp, "%-3d", j);
84
85             if (i != EMAX-1)
86                 fprintf(fp, ", ");
87
88             if (j % SPLIT_CALL == 0) {
89                 fprintf(fp, "\n    ");
90                 fprintf(fp, "      "); // test_
91                 for (int k = 0; k < strlen(types[i]); k++)
92                     fprintf(fp, " ");
93             }
94         }
95         fprintf(fp, "\n    );\n");
96     }
97     // mix is special since it needs to align everything
98     // at 0.ffff <-- six things
99     fprintf(fp, "    test_mix(");
100     for (int j = 1; j < EMAX; j++) {
101         string_t *key = string_create();
102         string_catf(key, "%d", j);
103         const char *type = table_find(table, string_buffer(key));
104
105         if (type[0] == 'f') {
106             string_t *len = string_create();
107             string_catf(len, "0.%df", j);
108             fprintf(fp, string_buffer(len), j);
109             for (int p = strlen(string_buffer(len)); p <= 5; p++)
110                 fprintf(fp, " ");
111         } else if (type[0] == 'd')
112             fprintf(fp, "0.%-4d", j);
113         else
114             fprintf(fp, "%-6d", j);
115
116         if (j != EMAX-1)
117             fprintf(fp, ", ");
118
119         if (j % SPLIT_CALL == 0) {
120             fprintf(fp, "\n    ");
121             fprintf(fp, "         "); // test_mix
122         }
123     }
124     fprintf(fp, "\n    );\n");
125     fprintf(fp, "    return 0;\n}\n");
126     fclose(fp);
127 }