Skip to content

Commit 67be0fa

Browse files
test: make QuadPrecompute tests indictment aware
1 parent 1d5852e commit 67be0fa

1 file changed

Lines changed: 46 additions & 18 deletions

File tree

core/src/test/java/ai/timefold/solver/core/impl/score/stream/common/quad/AbstractQuadConstraintStreamPrecomputeTest.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ai.timefold.solver.core.impl.score.stream.common.quad;
22

3+
import java.util.ArrayList;
4+
import java.util.Arrays;
35
import java.util.List;
46
import java.util.function.Function;
57

@@ -186,9 +188,27 @@ public void filter_3_changed() {
186188
(entity, value, entityGroup, valueGroup) -> new Quadruple<>(value, entityGroup, valueGroup, entity));
187189
}
188190

191+
record ExpectedQuad<A, B, C, D>(A a, B b, C c, D d, Object... indicted) {
192+
ExpectedQuad<A, B, C, D> withIndictedObject(Object indictedObject) {
193+
for (var obj : indicted) {
194+
if (obj == indictedObject) {
195+
return this;
196+
}
197+
}
198+
var newIndicted = Arrays.copyOf(indicted, indicted.length + 1);
199+
newIndicted[indicted.length] = indictedObject;
200+
return new ExpectedQuad<>(a, b, c, d, newIndicted);
201+
}
202+
}
203+
204+
<A, B, C, D> ExpectedQuad<A, B, C, D> expected(A a, B b, C c, D d, Object... indicted) {
205+
return new ExpectedQuad<>(a, b, c, d, indicted);
206+
}
207+
189208
private <A, B, C, D> void assertPrecompute(TestdataLavishSolution solution,
190-
List<Quadruple<A, B, C, D>> expectedValues,
209+
List<ExpectedQuad<A, B, C, D>> expectedValues,
191210
Function<PrecomputeFactory, QuadConstraintStream<A, B, C, D>> entityStreamSupplier) {
211+
expectedValues = new ArrayList<>(expectedValues);
192212
var scoreDirector =
193213
buildScoreDirector(factory -> factory.precompute(entityStreamSupplier)
194214
.ifExists(TestdataLavishEntity.class)
@@ -203,11 +223,16 @@ private <A, B, C, D> void assertPrecompute(TestdataLavishSolution solution,
203223
scoreDirector.beforeVariableChanged(entity, "value");
204224
entity.setValue(solution.getFirstValue());
205225
scoreDirector.afterVariableChanged(entity, "value");
226+
var listIterator = expectedValues.listIterator();
227+
while (listIterator.hasNext()) {
228+
var expected = listIterator.next();
229+
listIterator.set(expected.withIndictedObject(entity));
230+
}
206231
}
207232

208233
assertScore(scoreDirector, expectedValues.stream()
209-
.map(quad -> new Object[] { quad.a(), quad.b(), quad.c(), quad.d() })
210-
.map(AbstractConstraintStreamTest::assertMatch)
234+
.map(expected -> assertMatch(expected.a, expected.b, expected.c, expected.d)
235+
.withIndictedObjects(expected.indicted))
211236
.toArray(AssertableMatch[]::new));
212237
}
213238

@@ -224,7 +249,7 @@ public void ifExists() {
224249
var value = new TestdataLavishValue();
225250
solution.getValueList().add(value);
226251

227-
assertPrecompute(solution, List.of(new Quadruple<>(entityWithGroup, value, value, value)),
252+
assertPrecompute(solution, List.of(expected(entityWithGroup, value, value, value, entityWithGroup, value, entityGroup)),
228253
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
229254
.join(TestdataLavishValue.class)
230255
.join(TestdataLavishValue.class)
@@ -247,7 +272,7 @@ public void ifNotExists() {
247272
var value = new TestdataLavishValue();
248273
solution.getValueList().add(value);
249274

250-
assertPrecompute(solution, List.of(new Quadruple<>(entityWithoutGroup, value, value, value)),
275+
assertPrecompute(solution, List.of(expected(entityWithoutGroup, value, value, value, entityWithoutGroup, value)),
251276
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
252277
.join(TestdataLavishValue.class)
253278
.join(TestdataLavishValue.class)
@@ -270,7 +295,7 @@ public void groupBy() {
270295
var value = new TestdataLavishValue();
271296
solution.getValueList().add(value);
272297

273-
assertPrecompute(solution, List.of(new Quadruple<>(entityGroup, 1L, 1L, 1L)),
298+
assertPrecompute(solution, List.of(expected(entityGroup, 1L, 1L, 1L)),
274299
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
275300
.filter(entity -> entity.getEntityGroup() != null)
276301
.groupBy(TestdataLavishEntity::getEntityGroup,
@@ -292,8 +317,9 @@ public void flattenLast() {
292317
var value = new TestdataLavishValue();
293318
solution.getValueList().add(value);
294319

295-
assertPrecompute(solution, List.of(new Quadruple<>(entityWithoutGroup, value, value, value),
296-
new Quadruple<>(entityWithGroup, value, value, value)),
320+
assertPrecompute(solution, List.of(
321+
expected(entityWithoutGroup, value, value, value, entityWithoutGroup, value),
322+
expected(entityWithGroup, value, value, value, entityWithGroup, value)),
297323
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
298324
.groupBy(ConstraintCollectors.toList())
299325
.flattenLast(entityList -> entityList)
@@ -318,8 +344,9 @@ record ValueHolder(int value) {
318344
var value = new TestdataLavishValue();
319345
solution.getValueList().add(value);
320346

321-
assertPrecompute(solution, List.of(new Quadruple<>(new ValueHolder(1), value, value, value),
322-
new Quadruple<>(new ValueHolder(2), value, value, value)),
347+
assertPrecompute(solution, List.of(
348+
expected(new ValueHolder(1), value, value, value, entity1, value),
349+
expected(new ValueHolder(2), value, value, value, entity2, value)),
323350
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
324351
.groupBy(ConstraintCollectors.toList())
325352
.flattenLast(entityList -> entityList
@@ -346,8 +373,8 @@ public void map() {
346373
var value = new TestdataLavishValue();
347374
solution.getValueList().add(value);
348375

349-
assertPrecompute(solution, List.of(new Quadruple<>(entityGroup, value, value, value),
350-
new Quadruple<>(entityGroup, value, value, value)),
376+
assertPrecompute(solution, List.of(expected(entityGroup, value, value, value, entityWithGroup1, value),
377+
expected(entityGroup, value, value, value, entityWithGroup2, value)),
351378
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
352379
.join(TestdataLavishValue.class)
353380
.join(TestdataLavishValue.class)
@@ -373,8 +400,8 @@ public void concat() {
373400
solution.getValueList().add(value);
374401

375402
assertPrecompute(solution,
376-
List.of(new Quadruple<>(entityWithoutGroup, value, value, value),
377-
new Quadruple<>(entityWithGroup, value, value, value)),
403+
List.of(expected(entityWithoutGroup, value, value, value, entityWithoutGroup, value),
404+
expected(entityWithGroup, value, value, value, entityWithGroup, value)),
378405
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
379406
.join(TestdataLavishValue.class)
380407
.join(TestdataLavishValue.class)
@@ -403,7 +430,8 @@ public void distinct() {
403430
var value = new TestdataLavishValue();
404431
solution.getValueList().add(value);
405432

406-
assertPrecompute(solution, List.of(new Quadruple<>(entityGroup, value, value, value)),
433+
assertPrecompute(solution,
434+
List.of(expected(entityGroup, value, value, value, entityWithGroup1, entityWithGroup2, value)),
407435
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
408436
.join(TestdataLavishValue.class)
409437
.join(TestdataLavishValue.class)
@@ -432,9 +460,9 @@ public void complement() {
432460
solution.getValueList().add(value);
433461

434462
assertPrecompute(solution, List.of(
435-
new Quadruple<>(entityWithGroup1, value, value, value),
436-
new Quadruple<>(entityWithGroup2, value, value, value),
437-
new Quadruple<>(entityWithoutGroup, null, null, null)),
463+
expected(entityWithGroup1, value, value, value, entityWithGroup1, value),
464+
expected(entityWithGroup2, value, value, value, entityWithGroup2, value),
465+
expected(entityWithoutGroup, null, null, null, entityWithoutGroup)),
438466
pf -> pf.forEachUnfiltered(TestdataLavishEntity.class)
439467
.join(TestdataLavishValue.class)
440468
.join(TestdataLavishValue.class)

0 commit comments

Comments
 (0)