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