1 /* 2 * MinizincBasedTestsHelper.java 3 * This file is part of JaCoP. 4 * <p> 5 * JaCoP is a Java Constraint Programming solver. 6 * <p> 7 * Copyright (C) 2000-2008 Krzysztof Kuchcinski and Radoslaw Szymanek 8 * <p> 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Affero General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * <p> 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Affero General Public License for more details. 18 * <p> 19 * Notwithstanding any other provision of this License, the copyright 20 * owners of this work supplement the terms of this License with terms 21 * prohibiting misrepresentation of the origin of this work and requiring 22 * that modified versions of this work be marked in reasonable ways as 23 * different from the original version. This supplement of the license 24 * terms is in accordance with Section 7 of GNU Affero General Public 25 * License version 3. 26 * <p> 27 * You should have received a copy of the GNU Affero General Public License 28 * along with this program. If not, see <http://www.gnu.org/licenses/>. 29 */ 30 31 package org.jacop; 32 33 import org.jacop.floats.core.FloatDomain; 34 import org.jacop.fz.Fz2jacop; 35 import org.junit.After; 36 import org.junit.BeforeClass; 37 38 import java.io.BufferedReader; 39 import java.io.File; 40 import java.io.FileReader; 41 import java.io.IOException; 42 import java.nio.charset.Charset; 43 import java.nio.charset.StandardCharsets; 44 import java.nio.file.Files; 45 import java.nio.file.Paths; 46 import java.util.ArrayList; 47 import java.util.Arrays; 48 import java.util.Collection; 49 import java.util.List; 50 51 import static org.junit.Assert.assertEquals; 52 import static org.junit.Assert.fail; 53 54 /** 55 * Test Helper used by all Minizinc based tests. 56 * 57 * @author Mariusz Świerkot and Radoslaw Szymanek 58 * @version 4.8 59 */ 60 61 public class MinizincBasedTestsHelper { 62 protected String testFilename; 63 protected static Fz2jacop fz2jacop; 64 protected static final String relativePath = "src/test/fz/"; 65 String timeCategory; 66 protected static final String listFileName = "list.txt"; 67 protected static final boolean printInfo = false; 68 private static int counter = 0; 69 MinizincBasedTestsHelper(String timeCategory)70 protected MinizincBasedTestsHelper(String timeCategory) { 71 this.timeCategory = timeCategory; 72 } 73 initialize()74 @BeforeClass public static void initialize() { 75 fz2jacop = new Fz2jacop(); 76 } 77 cleanUp()78 @After public void cleanUp() { 79 String outputFilename = relativePath + timeCategory + testFilename + ".fzn" + ".out"; 80 try { 81 Files.delete(Paths.get(outputFilename)); 82 } catch (IOException e) { 83 // e.printStackTrace(); // can be helpful when updating code and/or tests instances. 84 // File was not created (because the test timeout before it was created so deleting it failed. 85 } 86 System.gc(); 87 System.runFinalization(); 88 } 89 counter()90 public int counter() { 91 return counter; 92 } 93 94 computeResult(String filename)95 protected List<String> computeResult(String filename) throws IOException { 96 97 String outputFilename = relativePath + filename + ".out"; //outputFilename contains path to *.out file. 98 String foo = outputFilename.substring(0, outputFilename.lastIndexOf('/')); 99 100 //If options.opt exist reads parameters from the file and uses them in fzn2jacop program. 101 if (Files.exists(Paths.get(foo + "/options.opt"))) { 102 try (BufferedReader reader = Files.newBufferedReader(Paths.get(foo + "/options.opt"), Charset.defaultCharset())) { 103 String line; 104 List<String> options = new ArrayList<String>(); 105 while ((line = reader.readLine()) != null) { 106 options.add(line); 107 } 108 //fz2jacop compute result with options 109 fz2jacop.main(new String[] {options.get(0), options.get(1), "--outputfile", outputFilename, relativePath + filename}); 110 FloatDomain.setFormat(Double.MAX_VALUE); 111 112 } 113 } else 114 //fz2jacop compute result 115 fz2jacop.main(new String[] {"--outputfile", outputFilename, relativePath + filename}); 116 117 String result = new String(Files.readAllBytes(Paths.get(outputFilename))); 118 119 if (printInfo) { 120 System.out.println(filename + "\n" + result); 121 } 122 123 return Arrays.asList(result.split("\n")); 124 125 } 126 127 expected(String filename)128 protected static List<String> expected(String filename) throws IOException { 129 130 String filePath = new File(relativePath + filename).getAbsolutePath(); 131 return Files.readAllLines(Paths.get(filePath), StandardCharsets.UTF_8); 132 } 133 fileReader(String timeCategory)134 protected static Collection<String> fileReader(String timeCategory) throws IOException { 135 136 System.out.println("timeCategory" + timeCategory); 137 try (FileReader file = new FileReader(relativePath + timeCategory + listFileName); BufferedReader br = new BufferedReader(file)) { 138 139 String line = ""; 140 List<String> list = new ArrayList<String>(); 141 int i = 0; 142 while ((line = br.readLine()) != null) { 143 list.add(i, line); 144 i++; 145 } 146 147 return list; 148 } 149 150 } 151 testExecution(String timeCategory)152 protected void testExecution(String timeCategory) throws IOException { 153 154 System.out.println("Test file: " + timeCategory + testFilename); 155 List<String> result = new ArrayList<String>(); 156 List<String> expectedResult = expected(timeCategory + testFilename + ".out"); //path to file name *.out 157 List<String> res = computeResult(timeCategory + testFilename + ".fzn"); // path to file name *.fzn 158 159 if (expectedResult.get(expectedResult.size() - 1).equals("==========")) { 160 int i; 161 for (i = 0; i < res.size(); i++) { 162 result.add(res.get(i)); 163 } 164 result.add("=========="); 165 } else 166 result = res; 167 168 if (result.size() == 0) 169 fail("\n" + "File path: " + timeCategory + testFilename + ".fzn " + " gave no output to compare against."); 170 171 for (int i = 0, j = 0; i < result.size() || j < expectedResult.size(); ) { 172 if (i < result.size() && result.get(i).trim().isEmpty()) { 173 i++; 174 continue; 175 } 176 if (j < expectedResult.size() && expectedResult.get(j).trim().isEmpty()) { 177 j++; 178 continue; 179 } 180 if (result.size() == i) 181 fail("\n" + "File path: " + timeCategory + testFilename + ".out " 182 + " gave as a result less textlines that was expected. Expected line " + (j + 1) + " not found."); 183 if (expectedResult.size() == j) 184 fail("\n" + "File path: " + timeCategory + testFilename + ".out " 185 + " gave as a result more textlines that was expected. Actual line " + (i + 1) + "not found in expected result"); 186 187 assertEquals( 188 "\n" + "File path: " + timeCategory + testFilename + ".out " + "\nError line number (expected, actual): (" + (j + 1) + "," 189 + (i + 1) + ")\n", expectedResult.get(j).trim(), result.get(i).trim()); 190 i++; 191 j++; 192 } 193 194 } 195 196 197 198 } 199