Skip to content

Commit

Permalink
Engine - Index: full coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jun 20, 2024
1 parent 725e167 commit 1b4a4c3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions Engine/src/ECS/Lab/Indexes/ValueInRangeIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Friflo.Engine.ECS.Index;

internal sealed class ValueInRangeIndex<TValue> : ComponentIndex<TValue>
{
internal int Count => map.Count;
private readonly SortedList<TValue, IdArray> map = new();
private readonly IdArrayHeap arrayHeap = new();

Expand Down
2 changes: 1 addition & 1 deletion Engine/src/ECS/Lab/Utils/DictionaryUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static void AddComponentValue<TValue>(int id, in TValue value, Dictiona
#endif
var idSpan = ids.GetIdSpan(arrayHeap);
if (idSpan.IndexOf(id) != -1) {
return;
return; // unexpected. Better safe than sorry. Used belts with suspenders :)
}
ids.AddId(id, arrayHeap);
MapUtils.Set(map, value, ids);
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/ECS/Lab/Utils/SortedListUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static void AddComponentValue<TValue>(int id, in TValue value, SortedLi
map.TryGetValue(value, out var ids);
var idSpan = ids.GetIdSpan(arrayHeap);
if (idSpan.IndexOf(id) != -1) {
return;
return; // unexpected. Better safe than sorry. Used belts with suspenders :)
}
ids.AddId(id, arrayHeap);
map[value] = ids;
Expand Down
15 changes: 14 additions & 1 deletion Engine/src/Tests-internal/ECS/Test_Index_Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static void Test_Index_Range_Update_Remove()
entity2.AddComponent(new IndexedIntRange { value = 200 });
entity3.AddComponent(new IndexedIntRange { value = 200 });
entity4.AddComponent(new IndexedIntRange { value = 300 });
entity4.AddComponent(new IndexedIntRange { value = 300 }); // cover add same component again
entity4.AddComponent(new IndexedIntRange { value = 300 }); // cover add same component value again

var query1 = store.Query<IndexedIntRange, Position>().ValueInRange<IndexedIntRange, int>(100, 300);
var query2 = store.Query<IndexedIntRange, Position>().ValueInRange<IndexedIntRange, int>(200, 400);
Expand All @@ -132,6 +132,19 @@ public static void Test_Index_Range_coverage() {
_ = new ComponentIndexAttribute(null);
}

[Test]
public static void Test_Index_Range_already_added()
{
var world = new EntityStore();
var entity = world.CreateEntity(1);

entity.AddComponent(new IndexedIntRange { value = 456 });

var index = (ValueInRangeIndex<int>)world.extension.componentIndexes[StructInfo<IndexedIntRange>.Index];
index.Add(1, new IndexedIntRange { value = 456 });
AreEqual(1, index.Count);
}

[Test]
public static void Test_Index_Range_already_removed()
{
Expand Down

0 comments on commit 1b4a4c3

Please sign in to comment.