Solve a memory leak for cloned scops
[openscop.git] / tests / openscop_test.c
blob502f60aec492ece6d98b1751cf531cc0bec4c966
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** test.c **
6 **-----------------------------------------------------------------**
7 ** First version: 01/10/2010 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
35 * are met: *
36 * *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
44 * *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
55 * *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.Pouchet@inria.fr> *
60 * *
61 *****************************************************************************/
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <dirent.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <sys/wait.h>
69 #include <openscop/openscop.h>
71 //#define FORK // Comment that if you want only one process
72 // (best for debugging with valgrind but bad
73 // for make check since any error will
74 // stop the job).
76 #define TEST_DIR "." // Directory to scan for OpenScop files
77 #define TEST_SUFFIX ".scop" // Suffix of OpenScop files
80 /**
81 * test_file function
82 * This function tests an onpenscop file. A test has six steps:
83 * 1. read the file to raise the data up to OpenScop data structures,
84 * 2. clone the data structures,
85 * 3. compare the clone and the original one,
86 * 4. dump the data structures to a new OpenScop file,
87 * 5. read the generated file,
88 * 6. compare the data structures.
89 * If everything went well, the data structure of the two scops are the same.
90 * \param input_name The name of the input file.
91 * \param verbose Verbose option (1 to set, 0 not to set).
92 * \return 1 if the test is successful, 0 otherwise.
94 int test_file(char * input_name, int verbose) {
95 int success = 0;
96 int failure = 0;
97 int cloning = 0;
98 int dumping = 0;
99 int equal = 0;
100 char * output_name;
101 FILE * input_file, * output_file;
102 openscop_scop_p input_scop;
103 openscop_scop_p output_scop;
104 openscop_scop_p cloned_scop;
106 printf("Testing file %20s... \n", input_name);
108 // PART I. Raise from file.
109 input_file = fopen(input_name, "r");
110 if (input_file == NULL) {
111 fflush(stdout);
112 fprintf(stderr, "\nError: unable to open file %s\n", input_name);
113 exit(2);
115 input_scop = openscop_scop_read(input_file);
116 fclose(input_file);
118 // PART II. Clone and test.
119 cloned_scop = openscop_scop_clone(input_scop);
120 // Compare the two scops.
121 if (cloning = openscop_scop_equal(input_scop, cloned_scop))
122 printf("- cloning succeed\n");
123 else
124 printf("- cloning failed\n");
126 // PART III. Dump to file and test.
127 output_name = tmpnam(NULL);
128 output_file = fopen(output_name, "w");
129 //openscop_scop_dump(stdout, input_scop);
130 //openscop_scop_print(stdout, input_scop);
131 openscop_scop_print(output_file, input_scop);
132 fclose(output_file);
134 // Raise the generated file to data structures.
135 output_file = fopen(output_name, "r");
136 output_scop = openscop_scop_read(output_file);
137 //openscop_scop_dump(stdout, output_scop);
138 fclose(output_file);
140 if (verbose) {
141 printf("\n\n*************************************************\n\n");
142 openscop_scop_dump(stdout, output_scop);
143 openscop_scop_print(stdout, output_scop);
144 printf("\n*************************************************\n\n");
147 // Compare the two scops.
148 if (dumping = openscop_scop_equal(input_scop, output_scop))
149 printf("- dumping succeed\n");
150 else
151 printf("- dumping failed\n");
153 // PART IV. Report.
154 if (equal = (cloning + dumping > 0) ? 1 : 0)
155 printf("Success :-)\n");
156 else
157 printf("Failure :-(\n");
159 // Save the planet.
160 openscop_scop_free(input_scop);
161 openscop_scop_free(cloned_scop);
162 openscop_scop_free(output_scop);
163 remove(output_name);
165 return equal;
170 * OpenScop test program.
171 * Usage: openscop_test [-v] [openscop_file]
172 * This program scans a directory for openscop files and test each of them.
173 * Optionnally the user can provide a file name to check this file only. A
174 * verbose option is also provided to output more information during tests.
176 int main(int argc, char * argv[]) {
177 int total = 0; // Total number of tests.
178 int success = 0; // Number of successes.
179 int verbose = 0; // 1 if the verbose option is set, 0 otherwise.
180 int dirtest = 1; // 1 if we check a whole directory, 0 for a single file.
181 int fileidx = 0; // Index of the file to check in argv (0 if none).
182 int d_namlen;
183 int suffix_length;
184 DIR * dir;
185 struct dirent * dp;
187 // Process the command line information
188 if (((argc > 1) && (!strcmp(argv[1], "-v"))) ||
189 ((argc > 2) && (!strcmp(argv[2], "-v"))))
190 verbose = 1;
192 if ((argc > 3) || ((argc == 3) && (!verbose))) {
193 fprintf(stderr, "usage: openscop_test [-v] [openscop_file]\n");
194 exit(1);
197 if ((argc - verbose) > 1) {
198 dirtest = 0;
199 fileidx = (!strcmp(argv[1], "-v")) ? 2 : 1;
202 // Proceed with the test(s), either directory or single file
203 if (dirtest) {
204 suffix_length = strlen(TEST_SUFFIX);
206 // For each file in the directory to check...
207 dir = opendir(TEST_DIR);
208 while ((dp = readdir(dir)) != NULL) {
209 d_namlen = strlen(dp->d_name);
210 // If the file has the convenient suffix...
211 if ((d_namlen > suffix_length) &&
212 (!strcmp(dp->d_name+(d_namlen-suffix_length), TEST_SUFFIX))) {
213 // Test it !
214 #ifdef FORK
215 int report;
216 if (!fork())
217 exit(test_file(dp->d_name, verbose) ? 0 : 1);
218 wait(&report);
219 if (!WEXITSTATUS(report))
220 success++;
221 #else
222 success += test_file(dp->d_name, verbose);
223 #endif
224 total++;
227 closedir(dir);
229 else {
230 success = test_file(argv[fileidx], verbose);
231 total++;
234 printf("\n +-----------------------+\n");
235 printf(" | OpenScop Test Summary |\n");
236 printf(" |-----------------------|\n");
237 printf(" | total %4d |\n", total);
238 printf(" | success(es) %4d |\n", success);
239 printf(" | failure(s) %4d |\n", total - success);
240 printf(" +-----------------------+\n\n");
242 // Return 0 if all tests were successful, 1 otherwise.
243 if (total - success)
244 return 1;
245 else
246 return 0;