get_submodules.sh: take into account imath submodule of isl
[ppn.git] / tests / testlib.cc
blob4c7b7b8e275f889307d1edef980a2637cd8fd773
1 #include <assert.h>
2 #include <limits.h>
3 #include <stdlib.h>
4 #ifdef WIN32
5 #include <windows.h>
6 #endif
7 #include "testlib.h"
8 #include "adg_parse.h"
10 #include "config.h"
12 /* For MinGW */
13 #ifndef WEXITSTATUS
14 #define WEXITSTATUS(status) status
15 #define WIFEXITED(status) 1
16 #endif
18 using pdg::PDG;
20 static char *tmp_dir = NULL;
22 #ifdef WIN32
23 static char *create_temp_dir()
25 static char temp_name[MAX_PATH];
27 GetTempFileName(".", "isa_test_", 0, temp_name);
28 DeleteFile(temp_name);
29 CreateDirectory(temp_name, NULL);
30 return strdup(temp_name);
32 #else
33 static char *create_temp_dir()
35 return mkdtemp(strdup("isa_test_XXXXXX"));
37 #endif
39 void remove_tmp_dir(void)
41 char command[2*PATH_MAX+100];
42 int n, ret;
44 n = snprintf(command, sizeof(command), "rm -r %s", tmp_dir);
45 assert(n < sizeof(command));
46 ret = system(command);
47 assert(WEXITSTATUS(ret) == 0);
49 free(tmp_dir);
52 void construct_pdg(const char *base, const char *options, const char *dir)
54 char command[2*PATH_MAX+100];
55 char *srcdir;
56 int n, ret;
58 srcdir = getenv("srcdir");
59 assert(srcdir);
61 if (!tmp_dir) {
62 tmp_dir = create_temp_dir();
63 assert(tmp_dir);
64 atexit(remove_tmp_dir);
67 n = snprintf(command, sizeof(command), "cp %s/inputs/%s/%s.c %s",
68 srcdir, dir ? dir : "", base, tmp_dir);
69 assert(n < sizeof(command));
70 ret = system(command);
71 assert(WEXITSTATUS(ret) == 0);
73 n = snprintf(command, sizeof(command), "../c2pdg %s %s/%s.c",
74 options ? options : "", tmp_dir, base);
75 assert(n < sizeof(command));
76 ret = system(command);
77 assert(WEXITSTATUS(ret) == 0);
80 pdg::PDG *get_pdg(isl_ctx *ctx, const char *base, const char *options)
82 char command[2*PATH_MAX+100];
83 int n;
84 construct_pdg(base, options);
86 FILE *in;
87 n = snprintf(command, sizeof(command), "%s/%s.yaml", tmp_dir, base);
88 assert(n < sizeof(command));
89 in = fopen(command, "r");
90 assert(in);
92 PDG *pdg;
93 pdg = PDG::Load(in, ctx);
94 assert(pdg);
95 fclose(in);
97 return pdg;
100 void construct_pn(const char *base, const char *options)
102 char command[2*PATH_MAX+100];
103 int n, ret;
105 n = snprintf(command, sizeof(command), "../pn %s --input %s/%s.yaml",
106 options ? options : "",
107 tmp_dir, base);
108 assert(n < sizeof(command));
109 ret = system(command);
110 assert(WEXITSTATUS(ret) == 0);
113 pdg::PDG *get_pn(isl_ctx *ctx, const char *base, const char *options)
115 char command[2*PATH_MAX+100];
116 int n;
118 construct_pn(base, options);
119 FILE *in;
120 n = snprintf(command, sizeof(command), "%s/%s_pn.yaml", tmp_dir, base);
121 assert(n < sizeof(command));
122 in = fopen(command, "r");
123 assert(in);
125 PDG *pdg;
126 pdg = PDG::Load(in, ctx);
127 assert(pdg);
128 fclose(in);
130 return pdg;
133 void construct_da(const char *base, const char *options)
135 char command[2*PATH_MAX+100];
136 int n, ret;
138 n = snprintf(command, sizeof(command), "../da %s --input %s/%s.yaml",
139 options ? options : "",
140 tmp_dir, base);
141 assert(n < sizeof(command));
142 ret = system(command);
143 assert(WEXITSTATUS(ret) == 0);
146 pdg::PDG *get_da(isl_ctx *ctx, const char *base, const char *options)
148 char command[2*PATH_MAX+100];
149 int n;
151 construct_da(base, options);
152 FILE *in;
153 n = snprintf(command, sizeof(command), "%s/%s_da.yaml", tmp_dir, base);
154 assert(n < sizeof(command));
155 in = fopen(command, "r");
156 assert(in);
158 PDG *pdg;
159 pdg = PDG::Load(in, ctx);
160 assert(pdg);
161 fclose(in);
163 return pdg;
166 void check_eqv(const char *test, const char *options, const char *output)
168 char command[2*PATH_MAX+100];
169 char *srcdir;
170 char dir[PATH_MAX];
171 int n, ret;
173 srcdir = getenv("srcdir");
174 assert(srcdir);
176 n = snprintf(dir, sizeof(dir), "eqv/%s", test);
177 assert(n < sizeof(dir));
179 construct_pdg("1", NULL, dir);
180 construct_da("1", "-t flow,output -A");
182 construct_pdg("2", NULL, dir);
183 construct_da("2", "-t flow,output -A");
185 n = snprintf(command, sizeof(command),
186 "../eqv %s %s/1_da.yaml %s/2_da.yaml > %s/output",
187 options, tmp_dir, tmp_dir, tmp_dir);
188 assert(n < sizeof(command));
189 ret = system(command);
190 assert(WEXITSTATUS(ret) == 0);
192 n = snprintf(command, sizeof(command),
193 "../eqv_cmp %s/inputs/%s/%s %s/output",
194 srcdir, dir, output, tmp_dir);
195 assert(n < sizeof(command));
196 ret = system(command);
197 assert(WEXITSTATUS(ret) == 0);
200 void construct_adg(const char *base, const char *options)
202 char command[2 * PATH_MAX + 100];
203 int n, ret;
205 n = snprintf(command, sizeof(command), "../pn2adg %s -i %s/%s_pn.yaml",
206 options ? options : "",
207 tmp_dir, base);
208 assert(n < sizeof(command));
209 ret = system(command);
210 assert(WEXITSTATUS(ret) == 0);
213 adg *get_adg(isl_ctx *ctx, const char *base)
215 FILE *in;
216 char path[PATH_MAX];
217 int n;
218 adg *adg;
220 construct_adg(base);
221 n = snprintf(path, sizeof(path), "%s/%s_pn_adg.yaml", tmp_dir, base);
222 assert(n < sizeof(path));
223 in = fopen(path, "r");
224 assert(in);
226 adg = adg_parse(ctx, in);
227 assert(adg);
228 fclose(in);
230 return adg;