2 import org
.junit
.runner
.RunWith
;
3 import org
.junit
.runners
.Parameterized
;
4 import org
.junit
.runners
.Parameterized
.Parameters
;
6 import java
.util
.Arrays
;
7 import java
.util
.Collection
;
8 import java
.util
.Random
;
9 import java
.util
.TreeSet
;
11 import junit
.framework
.Assert
;
14 * Tests conformance of a Solver to constraints.
15 * May not test whether a solution returns a correct (optimal) answer.
17 @RunWith(Parameterized
.class)
18 public class PetTest
{
21 public PetTest(IPetproblemSolver ps
)
27 public static Collection
<Object
[]> data() {
28 Object
[][] solvers
= new Object
[][] {
29 { //new RandomSolver(),
33 return Arrays
.asList(solvers
);
37 * @param n The number of children.
38 * @param m The number of pets.
41 private static void checkConformance(int n
, int m
, final int[] result
)
43 Assert
.assertEquals("There should not suddenly be more or less children.", n
, result
.length
);
45 TreeSet
<Integer
> pets
= new TreeSet
<Integer
>();
46 for(int i
= 0; i
< result
.length
; i
++)
48 Assert
.assertTrue("Pet should exist, or be a no-pet (-1).", result
[i
] >= -1 && result
[i
] < m
);
49 Assert
.assertTrue("Pet should not be assigned to multiple children.", result
[i
] == -1 || pets
.add(result
[i
]));
53 private static int[][] generateTestMatrix(int n
, int m
, int maxValue
)
55 int[][] result
= new int[n
][m
];
57 Random r
= new Random(n
* (m
+1));
58 for(int n_i
= 0; n_i
< n
; n_i
++)
59 for(int m_i
= 0; m_i
< m
; m_i
++)
60 result
[n_i
][m_i
] = r
.nextInt(maxValue
+1);
65 private static int computeCompatibility(int[][] matrix
, int[] pairing
)
68 for(int n_i
= 0; n_i
< pairing
.length
; n_i
++)
70 int m_i
= pairing
[n_i
];
74 sum
+= matrix
[n_i
][m_i
];
81 * Test whether Solver conforms for a semi-random test set.
82 * Note that these tests do not test whether a solution is optimal.
83 * (If it could do that the test would be a Solver by itself)
85 private void performGeneratedTest(int n
, int m
)
87 int[][] compatibility
= generateTestMatrix(n
, m
, 1);
88 int[] result
= ps
.solve(n
, m
, compatibility
);
89 checkConformance(n
, m
, result
);
92 @Test(timeout
= 10000)
93 public void testExample() {
95 int[][] compatibility
= {
103 int[] result
= ps
.solve(n
, m
, compatibility
);
104 checkConformance(n
, m
, result
);
105 Assert
.assertEquals("Does not give a correct answer", 4, computeCompatibility(compatibility
, result
));
108 @Test(timeout
= 10000)
109 public void testGeneratedSmall() {
110 performGeneratedTest(8, 10);
111 performGeneratedTest(10, 8);
114 @Test(timeout
= 10000)
115 public void testGeneratedMedium() {
116 performGeneratedTest(23, 20);
117 performGeneratedTest(20, 23);
120 @Test(timeout
= 10000)
121 public void testGeneratedLarge() {
122 performGeneratedTest(121, 130);
123 performGeneratedTest(130, 121);
127 * This timeout is just a suggestion,
128 * your solution is not incorrect if it takes longer,
129 * or if it is not capable to finish in a reasonable amount of time.
131 @Test(timeout
= 10000)
132 public void testGeneratedTerrible() {
133 performGeneratedTest(2910, 3102);
134 performGeneratedTest(3102, 2910);