3ca38795b2a7ace3706f08508fca84b25b8a69d8
[platform/upstream/isl.git] / isl_test.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <limits.h>
4 #include <isl_ctx.h>
5 #include <isl_set.h>
6 #include <isl_constraint.h>
7
8 static char *srcdir;
9
10 /* Construct the basic set { [i] : 5 <= i <= N } */
11 void test_construction(struct isl_ctx *ctx)
12 {
13         isl_int v;
14         struct isl_basic_set *bset;
15         struct isl_constraint *c;
16
17         isl_int_init(v);
18
19         bset = isl_basic_set_universe(ctx, 1, 1);
20
21         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
22         isl_int_set_si(v, -1);
23         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
24         isl_int_set_si(v, 1);
25         isl_constraint_set_coefficient(c, isl_dim_param, 0, v);
26         bset = isl_basic_set_add_constraint(bset, c);
27
28         c = isl_inequality_alloc(isl_dim_copy(bset->dim));
29         isl_int_set_si(v, 1);
30         isl_constraint_set_coefficient(c, isl_dim_set, 0, v);
31         isl_int_set_si(v, -5);
32         isl_constraint_set_constant(c, v);
33         bset = isl_basic_set_add_constraint(bset, c);
34
35         isl_basic_set_free(bset);
36
37         isl_int_clear(v);
38 }
39
40 void test_application_case(struct isl_ctx *ctx, const char *name)
41 {
42         char filename[PATH_MAX];
43         FILE *input;
44         int n;
45         struct isl_basic_set *bset1, *bset2;
46         struct isl_basic_map *bmap;
47
48         n = snprintf(filename, sizeof(filename),
49                         "%s/test_inputs/%s.omega", srcdir, name);
50         assert(n < sizeof(filename));
51         input = fopen(filename, "r");
52         assert(input);
53
54         bset1 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_OMEGA);
55         bmap = isl_basic_map_read_from_file(ctx, input, 0, ISL_FORMAT_OMEGA);
56
57         bset1 = isl_basic_set_apply(bset1, bmap);
58
59         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_OMEGA);
60
61         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
62
63         isl_basic_set_free(bset1);
64         isl_basic_set_free(bset2);
65
66         fclose(input);
67 }
68
69 void test_application(struct isl_ctx *ctx)
70 {
71         test_application_case(ctx, "application");
72         test_application_case(ctx, "application2");
73 }
74
75 void test_affine_hull_case(struct isl_ctx *ctx, const char *name)
76 {
77         char filename[PATH_MAX];
78         FILE *input;
79         int n;
80         struct isl_basic_set *bset1, *bset2;
81
82         n = snprintf(filename, sizeof(filename),
83                         "%s/test_inputs/%s.polylib", srcdir, name);
84         assert(n < sizeof(filename));
85         input = fopen(filename, "r");
86         assert(input);
87
88         bset1 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
89         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
90
91         bset1 = isl_basic_set_affine_hull(bset1);
92
93         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
94
95         isl_basic_set_free(bset1);
96         isl_basic_set_free(bset2);
97
98         fclose(input);
99 }
100
101 void test_affine_hull(struct isl_ctx *ctx)
102 {
103         test_affine_hull_case(ctx, "affine2");
104         test_affine_hull_case(ctx, "affine");
105 }
106
107 void test_convex_hull_case(struct isl_ctx *ctx, const char *name)
108 {
109         char filename[PATH_MAX];
110         FILE *input;
111         int n;
112         struct isl_basic_set *bset1, *bset2;
113         struct isl_set *set;
114
115         n = snprintf(filename, sizeof(filename),
116                         "%s/test_inputs/%s.polylib", srcdir, name);
117         assert(n < sizeof(filename));
118         input = fopen(filename, "r");
119         assert(input);
120
121         bset1 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
122         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
123
124         set = isl_basic_set_union(bset1, bset2);
125         bset1 = isl_set_convex_hull(set);
126
127         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
128
129         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
130
131         isl_basic_set_free(bset1);
132         isl_basic_set_free(bset2);
133
134         fclose(input);
135 }
136
137 void test_convex_hull(struct isl_ctx *ctx)
138 {
139         test_convex_hull_case(ctx, "convex0");
140         test_convex_hull_case(ctx, "convex1");
141         test_convex_hull_case(ctx, "convex2");
142         test_convex_hull_case(ctx, "convex3");
143         test_convex_hull_case(ctx, "convex4");
144         test_convex_hull_case(ctx, "convex5");
145         test_convex_hull_case(ctx, "convex6");
146         test_convex_hull_case(ctx, "convex7");
147         test_convex_hull_case(ctx, "convex8");
148         test_convex_hull_case(ctx, "convex9");
149         test_convex_hull_case(ctx, "convex10");
150         test_convex_hull_case(ctx, "convex11");
151 }
152
153 void test_gist_case(struct isl_ctx *ctx, const char *name)
154 {
155         char filename[PATH_MAX];
156         FILE *input;
157         int n;
158         struct isl_basic_set *bset1, *bset2;
159         struct isl_set *set;
160
161         n = snprintf(filename, sizeof(filename),
162                         "%s/test_inputs/%s.polylib", srcdir, name);
163         assert(n < sizeof(filename));
164         input = fopen(filename, "r");
165         assert(input);
166
167         bset1 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
168         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
169
170         bset1 = isl_basic_set_gist(bset1, bset2);
171
172         bset2 = isl_basic_set_read_from_file(ctx, input, 0, ISL_FORMAT_POLYLIB);
173
174         assert(isl_basic_set_is_equal(bset1, bset2) == 1);
175
176         isl_basic_set_free(bset1);
177         isl_basic_set_free(bset2);
178
179         fclose(input);
180 }
181
182 void test_gist(struct isl_ctx *ctx)
183 {
184         test_gist_case(ctx, "gist1");
185 }
186
187 int main()
188 {
189         struct isl_ctx *ctx;
190
191         srcdir = getenv("srcdir");
192
193         ctx = isl_ctx_alloc();
194         test_construction(ctx);
195         test_application(ctx);
196         test_affine_hull(ctx);
197         test_convex_hull(ctx);
198         test_gist(ctx);
199         isl_ctx_free(ctx);
200         return 0;
201 }