Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDecompositionTest.java
More file actions
Latest commit
252 lines (219 loc) · 7.06 KB
/
Copy pathDecompositionTest.java
File metadata and controls
252 lines (219 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
importorg.junit.Assert;
importorg.junit.Ignore;
importorg.junit.Test;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.PrintStream;
importjava.nio.charset.Charset;
importjava.nio.file.Files;
importjava.nio.file.Paths;
publicclassDecompositionTestextendsAssert {
privatestaticStringreadFile(Stringpath) throwsIOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
returnnewString(encoded, Charset.forName("UTF-8")).trim();
}
@Test
publicvoidtest1() {
Decompositiont = newDecomposition(1);
assertEquals(1, t.N);
// Заменяю System.out на свой объект, который будет печатать в строчку
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
assertEquals(String.format("1 = 1%n"), os.toString());
}
@Test
publicvoidtest2() {
Decompositiont = newDecomposition(2);
assertEquals(2, t.N);
// Заменяю System.out на свой объект, который будет печатать в строчку
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
assertEquals(String.format("2 = 2%n2 = 1 + 1%n"), os.toString());
}
@Test
publicvoidtestPrint() {
Decompositiont = newDecomposition(2);
// Заменяю System.out на свой объект, который будет печатать в строчку
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
int[] A = {1, 1};
t.print(A, A.length);
assertEquals(String.format("2 = 1 + 1%n"), os.toString());
}
@Test
publicvoidtest3() throwsIOException {
checkFile(3);
}
@Test
publicvoidtest4() throwsIOException {
checkFile(4);
}
@Test
publicvoidtest5() throwsIOException {
checkFile(5);
}
@Test
publicvoidtest6() throwsIOException {
checkFile(6);
}
@Test
publicvoidtest7() throwsIOException {
checkFile(7);
}
privatevoidcheckFile(intn) throwsIOException {
Decompositiont = newDecomposition(n);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
assertEquals(readFile(String.format("%02d.a", n)), os.toString().trim());
}
@Test
@Ignore// Тест производительности (слишком долгий)
publicvoidtest40() {
Decompositiont = newDecomposition(40);
t.gen();
}
// Additional edge case tests for 100% coverage
/**
* Test constructor stores N and creates correct sized array
*/
@Test
publicvoidtestConstructorInitialization() {
Decompositiont = newDecomposition(5);
assertEquals(5, t.N);
assertNotNull(t.A);
assertEquals(5, t.A.length);
}
/**
* Test print method with single element
*/
@Test
publicvoidtestPrintSingleElement() {
Decompositiont = newDecomposition(3);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
int[] A = {3};
t.print(A, 1);
assertEquals(String.format("3 = 3%n"), os.toString());
}
/**
* Test print method with three elements
*/
@Test
publicvoidtestPrintThreeElements() {
Decompositiont = newDecomposition(4);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
int[] A = {2, 1, 1};
t.print(A, 3);
assertEquals(String.format("4 = 2 + 1 + 1%n"), os.toString());
}
/**
* Test print method with all ones
*/
@Test
publicvoidtestPrintAllOnes() {
Decompositiont = newDecomposition(4);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
int[] A = {1, 1, 1, 1};
t.print(A, 4);
assertEquals(String.format("4 = 1 + 1 + 1 + 1%n"), os.toString());
}
/**
* Test that array is correctly sized for N
*/
@Test
publicvoidtestArraySize() {
Decompositiont = newDecomposition(10);
assertEquals(10, t.A.length);
}
/**
* Test gen method generates correct number of decompositions for N=8
*/
@Test
publicvoidtest8() throwsIOException {
checkFile(8);
}
/**
* Test gen method generates correct number of decompositions for N=9
*/
@Test
publicvoidtest9() throwsIOException {
checkFile(9);
}
/**
* Test gen method generates correct number of decompositions for N=10
*/
@Test
publicvoidtest10() throwsIOException {
checkFile(10);
}
/**
* Test multiple decomposition instances are independent
*/
@Test
publicvoidtestMultipleInstances() {
Decompositiont1 = newDecomposition(3);
Decompositiont2 = newDecomposition(4);
assertEquals(3, t1.N);
assertEquals(4, t2.N);
assertNotSame(t1.A, t2.A);
}
/**
* Test gen can be called multiple times
*/
@Test
publicvoidtestMultipleGenCalls() {
Decompositiont = newDecomposition(2);
ByteArrayOutputStreamos1 = newByteArrayOutputStream();
System.setOut(newPrintStream(os1));
t.gen();
Stringresult1 = os1.toString();
ByteArrayOutputStreamos2 = newByteArrayOutputStream();
System.setOut(newPrintStream(os2));
t.gen();
Stringresult2 = os2.toString();
assertEquals(result1, result2);
}
/**
* Test decomposition contains all expected values
*/
@Test
publicvoidtestDecompositionContents3() {
Decompositiont = newDecomposition(3);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
Stringoutput = os.toString();
assertTrue(output.contains("3 = 3"));
assertTrue(output.contains("3 = 2 + 1"));
assertTrue(output.contains("3 = 1 + 1 + 1"));
}
/**
* Test that first decomposition is N itself
*/
@Test
publicvoidtestFirstDecomposition() {
Decompositiont = newDecomposition(5);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
Stringoutput = os.toString();
assertTrue(output.startsWith(String.format("5 = 5%n")));
}
/**
* Test that last decomposition is all ones
*/
@Test
publicvoidtestLastDecomposition() {
Decompositiont = newDecomposition(4);
ByteArrayOutputStreamos = newByteArrayOutputStream();
System.setOut(newPrintStream(os));
t.gen();
Stringoutput = os.toString();
assertTrue(output.endsWith(String.format("4 = 1 + 1 + 1 + 1%n")));
}
}