|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.File; |
| 3 | +import java.io.FileReader; |
| 4 | +import java.io.IOException; |
| 5 | + |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author medany |
| 11 | + */ |
| 12 | + |
| 13 | +public class DynamicArrayTest { |
| 14 | + |
| 15 | + private DynamicArray alg = new DynamicArray(); |
| 16 | + Integer[] actual = null, expected = null; |
| 17 | + |
| 18 | + @Test |
| 19 | + public void Test_1() { |
| 20 | + |
| 21 | + BufferedReader ir = null, or = null; |
| 22 | + FileReader i = null, o = null; |
| 23 | + |
| 24 | + try { |
| 25 | + i = new FileReader( |
| 26 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_input_1").getPath())); |
| 27 | + o = new FileReader( |
| 28 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_output_1").getPath())); |
| 29 | + ir = new BufferedReader(i); |
| 30 | + or = new BufferedReader(o); |
| 31 | + int n = 0; |
| 32 | + String[] queries; |
| 33 | + String line = ir.readLine(); |
| 34 | + |
| 35 | + n = Integer.parseInt(line.split(" ")[0]); |
| 36 | + queries = new String[Integer.parseInt(line.split(" ")[1])]; |
| 37 | + |
| 38 | + int q = 0, r = 0; |
| 39 | + while ((line = ir.readLine()) != null && q < queries.length) { |
| 40 | + if (line.charAt(0) == '2') |
| 41 | + r++; |
| 42 | + queries[q] = line; |
| 43 | + q++; |
| 44 | + } |
| 45 | + |
| 46 | + actual = alg.solve(n, queries); |
| 47 | + expected = new Integer[r]; |
| 48 | + |
| 49 | + int e = 0; |
| 50 | + while ((line = or.readLine()) != null && e < expected.length) { |
| 51 | + expected[e] = Integer.parseInt(line); |
| 52 | + e++; |
| 53 | + } |
| 54 | + |
| 55 | + Assert.assertArrayEquals(expected, actual); |
| 56 | + |
| 57 | + } catch (IOException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } finally { |
| 60 | + try { |
| 61 | + if (ir != null) |
| 62 | + ir.close(); |
| 63 | + if (or != null) |
| 64 | + or.close(); |
| 65 | + if (i != null) |
| 66 | + i.close(); |
| 67 | + if (o != null) |
| 68 | + o.close(); |
| 69 | + } catch (IOException ex) { |
| 70 | + ex.printStackTrace(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void Test_2() { |
| 78 | + |
| 79 | + BufferedReader ir = null, or = null; |
| 80 | + FileReader i = null, o = null; |
| 81 | + |
| 82 | + try { |
| 83 | + i = new FileReader( |
| 84 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_input_2").getPath())); |
| 85 | + o = new FileReader( |
| 86 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_output_2").getPath())); |
| 87 | + ir = new BufferedReader(i); |
| 88 | + or = new BufferedReader(o); |
| 89 | + int n = 0; |
| 90 | + String[] queries; |
| 91 | + String line = ir.readLine(); |
| 92 | + |
| 93 | + n = Integer.parseInt(line.split(" ")[0]); |
| 94 | + queries = new String[Integer.parseInt(line.split(" ")[1])]; |
| 95 | + |
| 96 | + int q = 0, r = 0; |
| 97 | + while ((line = ir.readLine()) != null && q < queries.length) { |
| 98 | + if (line.charAt(0) == '2') |
| 99 | + r++; |
| 100 | + queries[q] = line; |
| 101 | + q++; |
| 102 | + } |
| 103 | + |
| 104 | + actual = alg.solve(n, queries); |
| 105 | + expected = new Integer[r]; |
| 106 | + |
| 107 | + int e = 0; |
| 108 | + while ((line = or.readLine()) != null && e < expected.length) { |
| 109 | + expected[e] = Integer.parseInt(line); |
| 110 | + e++; |
| 111 | + } |
| 112 | + |
| 113 | + Assert.assertArrayEquals(expected, actual); |
| 114 | + |
| 115 | + } catch (IOException e) { |
| 116 | + e.printStackTrace(); |
| 117 | + } finally { |
| 118 | + try { |
| 119 | + if (ir != null) |
| 120 | + ir.close(); |
| 121 | + if (or != null) |
| 122 | + or.close(); |
| 123 | + if (i != null) |
| 124 | + i.close(); |
| 125 | + if (o != null) |
| 126 | + o.close(); |
| 127 | + } catch (IOException ex) { |
| 128 | + ex.printStackTrace(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void Test_3() { |
| 136 | + |
| 137 | + BufferedReader ir = null, or = null; |
| 138 | + FileReader i = null, o = null; |
| 139 | + |
| 140 | + try { |
| 141 | + i = new FileReader( |
| 142 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_input_3").getPath())); |
| 143 | + o = new FileReader( |
| 144 | + new File(this.getClass().getClassLoader().getResource("dynamic_array_output_3").getPath())); |
| 145 | + ir = new BufferedReader(i); |
| 146 | + or = new BufferedReader(o); |
| 147 | + int n = 0; |
| 148 | + String[] queries; |
| 149 | + String line = ir.readLine(); |
| 150 | + |
| 151 | + n = Integer.parseInt(line.split(" ")[0]); |
| 152 | + queries = new String[Integer.parseInt(line.split(" ")[1])]; |
| 153 | + |
| 154 | + int q = 0, r = 0; |
| 155 | + while ((line = ir.readLine()) != null && q < queries.length) { |
| 156 | + if (line.charAt(0) == '2') |
| 157 | + r++; |
| 158 | + queries[q] = line; |
| 159 | + q++; |
| 160 | + } |
| 161 | + |
| 162 | + actual = alg.solve(n, queries); |
| 163 | + expected = new Integer[r]; |
| 164 | + |
| 165 | + int e = 0; |
| 166 | + while ((line = or.readLine()) != null && e < expected.length) { |
| 167 | + expected[e] = Integer.parseInt(line); |
| 168 | + e++; |
| 169 | + } |
| 170 | + |
| 171 | + Assert.assertArrayEquals(expected, actual); |
| 172 | + |
| 173 | + } catch (IOException e) { |
| 174 | + e.printStackTrace(); |
| 175 | + } finally { |
| 176 | + try { |
| 177 | + if (ir != null) |
| 178 | + ir.close(); |
| 179 | + if (or != null) |
| 180 | + or.close(); |
| 181 | + if (i != null) |
| 182 | + i.close(); |
| 183 | + if (o != null) |
| 184 | + o.close(); |
| 185 | + } catch (IOException ex) { |
| 186 | + ex.printStackTrace(); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + } |
| 191 | +} |
0 commit comments