11package ai .timefold .solver .core .impl .score .stream .common .tri ;
22
3+ import java .util .ArrayList ;
4+ import java .util .Arrays ;
35import java .util .List ;
46import java .util .function .Function ;
57
@@ -162,9 +164,27 @@ public void filter_2_changed() {
162164 (entity , value , entityGroup ) -> new Triple <>(value , entityGroup , entity ));
163165 }
164166
167+ record ExpectedTri <A , B , C >(A a , B b , C c , Object ... indicted ) {
168+ ExpectedTri <A , B , C > withIndictedObject (Object indictedObject ) {
169+ for (var obj : indicted ) {
170+ if (obj == indictedObject ) {
171+ return this ;
172+ }
173+ }
174+ var newIndicted = Arrays .copyOf (indicted , indicted .length + 1 );
175+ newIndicted [indicted .length ] = indictedObject ;
176+ return new ExpectedTri <>(a , b , c , newIndicted );
177+ }
178+ }
179+
180+ <A , B , C > ExpectedTri <A , B , C > expected (A a , B b , C c , Object ... indicted ) {
181+ return new ExpectedTri <>(a , b , c , indicted );
182+ }
183+
165184 private <A , B , C > void assertPrecompute (TestdataLavishSolution solution ,
166- List <Triple <A , B , C >> expectedValues ,
185+ List <ExpectedTri <A , B , C >> expectedValues ,
167186 Function <PrecomputeFactory , TriConstraintStream <A , B , C >> entityStreamSupplier ) {
187+ expectedValues = new ArrayList <>(expectedValues );
168188 var scoreDirector =
169189 buildScoreDirector (factory -> factory .precompute (entityStreamSupplier )
170190 .ifExists (TestdataLavishEntity .class )
@@ -179,11 +199,16 @@ private <A, B, C> void assertPrecompute(TestdataLavishSolution solution,
179199 scoreDirector .beforeVariableChanged (entity , "value" );
180200 entity .setValue (solution .getFirstValue ());
181201 scoreDirector .afterVariableChanged (entity , "value" );
202+ var listIterator = expectedValues .listIterator ();
203+ while (listIterator .hasNext ()) {
204+ var expected = listIterator .next ();
205+ listIterator .set (expected .withIndictedObject (entity ));
206+ }
182207 }
183208
184209 assertScore (scoreDirector , expectedValues .stream ()
185- .map (triple -> new Object [] { triple . a (), triple . b (), triple . c () } )
186- . map ( AbstractConstraintStreamTest :: assertMatch )
210+ .map (expected -> assertMatch ( expected . a , expected . b , expected . c )
211+ . withIndictedObjects ( expected . indicted ) )
187212 .toArray (AssertableMatch []::new ));
188213 }
189214
@@ -200,7 +225,7 @@ public void ifExists() {
200225 var value = new TestdataLavishValue ();
201226 solution .getValueList ().add (value );
202227
203- assertPrecompute (solution , List .of (new Triple <> (entityWithGroup , value , value )),
228+ assertPrecompute (solution , List .of (expected (entityWithGroup , value , value , entityWithGroup , value , entityGroup )),
204229 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
205230 .join (TestdataLavishValue .class )
206231 .join (TestdataLavishValue .class )
@@ -222,7 +247,7 @@ public void ifNotExists() {
222247 var value = new TestdataLavishValue ();
223248 solution .getValueList ().add (value );
224249
225- assertPrecompute (solution , List .of (new Triple <> (entityWithoutGroup , value , value )),
250+ assertPrecompute (solution , List .of (expected (entityWithoutGroup , value , value , entityWithoutGroup , value )),
226251 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
227252 .join (TestdataLavishValue .class )
228253 .join (TestdataLavishValue .class )
@@ -244,7 +269,7 @@ public void groupBy() {
244269 var value = new TestdataLavishValue ();
245270 solution .getValueList ().add (value );
246271
247- assertPrecompute (solution , List .of (new Triple <> (entityGroup , 1L , 1L )),
272+ assertPrecompute (solution , List .of (expected (entityGroup , 1L , 1L )),
248273 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
249274 .filter (entity -> entity .getEntityGroup () != null )
250275 .groupBy (TestdataLavishEntity ::getEntityGroup ,
@@ -265,8 +290,9 @@ public void flatten() {
265290 var value = new TestdataLavishValue ();
266291 solution .getValueList ().add (value );
267292
268- assertPrecompute (solution , List .of (new Triple <>(entityWithoutGroup , entityWithoutGroup , value ),
269- new Triple <>(entityWithGroup , entityWithoutGroup , value )),
293+ assertPrecompute (solution , List .of (
294+ expected (entityWithoutGroup , entityWithoutGroup , value , entityWithoutGroup , value ),
295+ expected (entityWithGroup , entityWithoutGroup , value , entityWithGroup , value )),
270296 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
271297 .flatten (List ::of )
272298 .join (TestdataLavishValue .class ));
@@ -288,8 +314,9 @@ record ValueHolder(int value) {
288314 var value = new TestdataLavishValue ();
289315 solution .getValueList ().add (value );
290316
291- assertPrecompute (solution , List .of (new Triple <>(entity1 , new ValueHolder (entity1 .getIntegerProperty ()), value ),
292- new Triple <>(entity2 , new ValueHolder (entity2 .getIntegerProperty ()), value )),
317+ assertPrecompute (solution , List .of (
318+ expected (entity1 , new ValueHolder (entity1 .getIntegerProperty ()), value , entity1 , value ),
319+ expected (entity2 , new ValueHolder (entity2 .getIntegerProperty ()), value , entity2 , value )),
293320 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
294321 .flatten (entity -> List .of (new ValueHolder (entity .getIntegerProperty ())))
295322 .join (TestdataLavishValue .class ));
@@ -308,8 +335,9 @@ public void flattenLast() {
308335 var value = new TestdataLavishValue ();
309336 solution .getValueList ().add (value );
310337
311- assertPrecompute (solution , List .of (new Triple <>(entityWithoutGroup , value , value ),
312- new Triple <>(entityWithGroup , value , value )),
338+ assertPrecompute (solution , List .of (
339+ expected (entityWithoutGroup , value , value , entityWithoutGroup , value ),
340+ expected (entityWithGroup , value , value , entityWithGroup , value )),
313341 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
314342 .groupBy (ConstraintCollectors .toList ())
315343 .flattenLast (entityList -> entityList )
@@ -333,8 +361,9 @@ record ValueHolder(int value) {
333361 var value = new TestdataLavishValue ();
334362 solution .getValueList ().add (value );
335363
336- assertPrecompute (solution , List .of (new Triple <>(new ValueHolder (1 ), value , value ),
337- new Triple <>(new ValueHolder (2 ), value , value )),
364+ assertPrecompute (solution , List .of (
365+ expected (new ValueHolder (1 ), value , value , entity1 , value ),
366+ expected (new ValueHolder (2 ), value , value , entity2 , value )),
338367 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
339368 .groupBy (ConstraintCollectors .toList ())
340369 .flattenLast (entityList -> entityList
@@ -360,8 +389,9 @@ public void map() {
360389 var value = new TestdataLavishValue ();
361390 solution .getValueList ().add (value );
362391
363- assertPrecompute (solution , List .of (new Triple <>(entityGroup , value , value ),
364- new Triple <>(entityGroup , value , value )),
392+ assertPrecompute (solution , List .of (
393+ expected (entityGroup , value , value , entityWithGroup1 , value ),
394+ expected (entityGroup , value , value , entityWithGroup2 , value )),
365395 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
366396 .join (TestdataLavishValue .class )
367397 .join (TestdataLavishValue .class )
@@ -385,7 +415,8 @@ public void concat() {
385415 solution .getValueList ().add (value );
386416
387417 assertPrecompute (solution ,
388- List .of (new Triple <>(entityWithoutGroup , value , value ), new Triple <>(entityWithGroup , value , value )),
418+ List .of (expected (entityWithoutGroup , value , value , entityWithoutGroup , value ),
419+ expected (entityWithGroup , value , value , entityWithGroup , value )),
389420 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
390421 .join (TestdataLavishValue .class )
391422 .join (TestdataLavishValue .class )
@@ -411,7 +442,8 @@ public void distinct() {
411442 var value = new TestdataLavishValue ();
412443 solution .getValueList ().add (value );
413444
414- assertPrecompute (solution , List .of (new Triple <>(entityGroup , value , value )),
445+ assertPrecompute (solution , List .of (
446+ expected (entityGroup , value , value , entityWithGroup1 , entityWithGroup2 , value )),
415447 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
416448 .join (TestdataLavishValue .class )
417449 .join (TestdataLavishValue .class )
@@ -438,9 +470,9 @@ public void complement() {
438470 solution .getValueList ().add (value );
439471
440472 assertPrecompute (solution , List .of (
441- new Triple <> (entityWithGroup1 , value , value ),
442- new Triple <> (entityWithGroup2 , value , value ),
443- new Triple <> (entityWithoutGroup , null , null )),
473+ expected (entityWithGroup1 , value , value , entityWithGroup1 , value ),
474+ expected (entityWithGroup2 , value , value , entityWithGroup2 , value ),
475+ expected (entityWithoutGroup , null , null , entityWithoutGroup )),
444476 pf -> pf .forEachUnfiltered (TestdataLavishEntity .class )
445477 .join (TestdataLavishValue .class )
446478 .join (TestdataLavishValue .class )
0 commit comments