@@ -557,6 +557,45 @@ public static void Test_Relations_outdated_DeleteEntity_source()
557557 } ) ;
558558 AreEqual ( "Relations<AttackRelation> outdated. Added / Removed relations after calling GetRelations<AttackRelation>()." , e1 ! . Message ) ;
559559 }
560+
561+ [ Test ]
562+ // Regression test for issue in Relations<> indexer (missed to use start)
563+ // Issue detected at: [Relations<TRelation> [index] operator sometimes returns the wrong TRelation]
564+ // https://github.com/friflo/Friflo.Engine.ECS/issues/70#issuecomment-2896790850
565+ public static void Test_Relations_indexer ( )
566+ {
567+ var store = new EntityStore ( ) ;
568+
569+ var entity1 = store . CreateEntity ( ) ;
570+ entity1 . AddRelation ( new IntRelation { value = 10 } ) ;
571+ entity1 . AddRelation ( new IntRelation { value = 11 } ) ;
572+ entity1 . AddRelation ( new IntRelation { value = 12 } ) ;
573+ entity1 . AddRelation ( new IntRelation { value = 13 } ) ;
574+
575+ var entity2 = store . CreateEntity ( ) ;
576+ entity2 . AddRelation ( new IntRelation { value = 20 } ) ;
577+ entity2 . AddRelation ( new IntRelation { value = 21 } ) ;
578+ entity2 . AddRelation ( new IntRelation { value = 22 } ) ;
579+ entity2 . AddRelation ( new IntRelation { value = 23 } ) ;
580+
581+ var relations1 = entity1 . GetRelations < IntRelation > ( ) ;
582+ var relations2 = entity2 . GetRelations < IntRelation > ( ) ;
583+
584+ // Both relations must have same length for this test.
585+ // So they are using the same Relations<TRelation>.positions with different Relations<TRelation>.start values
586+ AreEqual ( relations1 . Length , relations2 . Length ) ;
587+ {
588+ int index = 0 ;
589+ foreach ( var relation in relations1 ) {
590+ AreEqual ( relations1 [ index ++ ] . value , relation . value ) ;
591+ }
592+ } {
593+ int index = 0 ;
594+ foreach ( var relation in relations2 ) {
595+ AreEqual ( relations2 [ index ++ ] . value , relation . value ) ;
596+ }
597+ }
598+ }
560599}
561600
562601}
0 commit comments