Skip to content

Commit

Permalink
RW now contains a full TypeExpression (instead of Match)
Browse files Browse the repository at this point in the history
  • Loading branch information
thygrrr committed Nov 8, 2024
1 parent 511d078 commit 54a8140
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 90 deletions.
29 changes: 15 additions & 14 deletions fennecs.tests/Storage/RW.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public void Can_Read()
var entity = world.Spawn();

var x = 1;
var match = default(Match);
var rw = new RW<int>(ref x, ref entity, ref match);
var type = TypeExpression.Of<int>(default);
var rw = new RW<int>(ref x, ref entity, ref type);

Assert.Equal(1, rw.read);
}
Expand All @@ -25,8 +25,8 @@ public void Implicitly_Casts_to_Value()
var entity = world.Spawn();

var x = 1;
var match = default(Match);
var rw = new RW<int>(ref x, ref entity, ref match);
var type = TypeExpression.Of<int>(default);
var rw = new RW<int>(ref x, ref entity, ref type);

Assert.Equal(1, rw);
}
Expand All @@ -39,9 +39,9 @@ public void Can_Write()

var x = 1;

var match = default(Match);
var type = TypeExpression.Of<int>(default);
// ReSharper disable once UseObjectOrCollectionInitializer
var rw = new RW<int>(ref x, ref entity, ref match);
var rw = new RW<int>(ref x, ref entity, ref type);
rw.write = 2; // user usually does not use initializer code

Assert.Equal(2, rw.read);
Expand All @@ -56,8 +56,8 @@ public void Can_Consume()
var x = 77; // TODO: Implement this on the actual component
entity.Add(x);

var match = default(Match);
var rw = new RW<int>(ref x, ref entity, ref match);
var type = TypeExpression.Of<int>(default);
var rw = new RW<int>(ref x, ref entity, ref type);

Assert.Equal(77, rw.consume);
Assert.False(entity.Has<int>());
Expand All @@ -73,13 +73,13 @@ public void Can_Consume_Relation()
var x = 77; // TODO: Implement this on the actual component
entity.Add(x, target);

Match match = target;
Assert.True(entity.Has<int>(match));
var type = TypeExpression.Of<int>(target);
Assert.True(entity.Has<int>(target));

var rw = new RW<int>(ref x, ref entity, ref match);
var rw = new RW<int>(ref x, ref entity, ref type);

Assert.Equal(77, rw.consume);
Assert.False(entity.Has<int>(match));
Assert.False(entity.Has<int>(target));
}

[Fact]
Expand All @@ -93,8 +93,9 @@ public void Can_Remove()

Assert.True(entity.Has<int>());

var plain = Match.Plain;
var rw = new RW<int>(ref x, ref entity, ref plain);
var type = TypeExpression.Of<int>(default);

var rw = new RW<int>(ref x, ref entity, ref type);
rw.Remove();
Assert.False(entity.Has<int>());
}
Expand Down
8 changes: 8 additions & 0 deletions fennecs.tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public void Storage_Can_Be_Created()
Assert.NotNull(new Storage<ReferenceType>());
}

[Fact]
public void Storage_Retains_Expression()
{
Assert.Equal(TypeExpression.Of<ValueType>(default), new Storage<ValueType>(TypeExpression.Of<ValueType>(default)).Expression);
Assert.Equal(TypeExpression.Of<ReferenceType>(default), new Storage<ReferenceType>(TypeExpression.Of<ReferenceType>(default)).Expression);
}

[Fact]
public void Storage_Stores_Values()
{
Expand Down Expand Up @@ -280,6 +287,7 @@ public void AsMemory_Default_Is_Entire_Size()
var memory1 = storage.AsMemory();
Assert.Equal(0, memory1.Length);

// ReSharper disable once RedundantArgumentDefaultValue
storage.Append(1, 1);
var memory2 = storage.AsMemory();
Assert.Equal(1, memory2.Length);
Expand Down
111 changes: 71 additions & 40 deletions fennecs.tests/Stream/Stream.2.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ [Fact] public void Can_Use_RW_Inferred()

var stream = world.Stream<int, float>();

stream.For(static (a, b) => {
stream.For(static (a, b) =>
{
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
b.write = 456f;
});
}

[Fact] public void Can_Use_WR_Inferred()
{
using var world = new World();
Expand All @@ -29,12 +30,13 @@ [Fact] public void Can_Use_WR_Inferred()

var stream = world.Stream<int, float>();

stream.For(static (a, b) => {
stream.For(static (a, b) =>
{
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
});
}

[Fact] public void Can_Use_WW_Inferred()
{
using var world = new World();
Expand All @@ -43,7 +45,7 @@ [Fact] public void Can_Use_WW_Inferred()

var stream = world.Stream<int, float>();

stream.For(static (a, b) =>
stream.For(static (a, b) =>
{
Assert.Equal(123, a.read);
Assert.Equal(890f, b.read);
Expand All @@ -67,21 +69,49 @@ [Fact] public void Can_Use_RR_Inferred()
});
}

[Fact] public void Can_Use_WW_With_Different_Types()
{
using var world = new World();
var entity = world.Spawn();
var target = world.Spawn();
entity.Add(123, target).Add(456);

var stream = world.Stream<int, int>(target, default);

stream.For((a, b) =>
{
Assert.Equal(123, a.write);
Assert.Equal(456, b.write);
Assert.Equal(a.Match, target);
Assert.Equal(b.Match, default);
});

var swapped = world.Stream<int, int>(default,target);

swapped.For((b, a) =>
{
Assert.Equal(123, a.write);
Assert.Equal(456, b.write);
Assert.Equal(a.Match, target);
Assert.Equal(b.Match, default);
});
}

[Fact]
public void Can_Enumerate_Stream()
{
using var world = new World();
var arnold = world.Spawn().Add("Arnold").Add(1);
var dolph = world.Spawn().Add("Dolph").Add(2);

List<(Entity, string, int)> list = [(arnold, "Arnold", 1), (dolph, "Dolph", 2)];

var stream = world.Stream<string, int>();
foreach (var row in stream)
{
Assert.True(list.Remove(row));
}

Assert.Empty(list);
}

Expand All @@ -91,27 +121,28 @@ public void Can_Enumerate_Boxed_Inherited()
using var world = new World();
var arnold = world.Spawn().Add("Arnold").Add(1);
var dolph = world.Spawn().Add("Dolph").Add(2);

List<object> list = [(arnold, "Arnold", 1), (dolph, "Dolph", 2)];

IEnumerable stream = world.Stream<string, int>();
foreach (var row in stream)
{
Assert.True(list.Remove(row));
}

Assert.Empty(list);
}


[Fact]
public void Cannot_Structural_Change_While_Enumerating()
{
using var world = new World();
world.Spawn().Add("Arnold").Add(1);
world.Spawn().Add("Dolph").Add(2);

var stream = world.Stream<string, int>();

Assert.Throws<InvalidOperationException>(() =>
{
foreach (var row in stream)
Expand Down Expand Up @@ -176,20 +207,20 @@ private void All_Runners_Applicable(int count, bool createEmptyTable)
});

query.Job(6,
(int uniform, ref int index, ref string str) =>
{
Assert.Equal(index, index);
Assert.Equal("five", str);
str = uniform.ToString();
});
(int uniform, ref int index, ref string str) =>
{
Assert.Equal(index, index);
Assert.Equal("five", str);
str = uniform.ToString();
});


query.For(7,
(int uniform, ref int _, ref string str) =>
{
Assert.Equal(6.ToString(), str);
str = uniform.ToString();
});
(int uniform, ref int _, ref string str) =>
{
Assert.Equal(6.ToString(), str);
str = uniform.ToString();
});

query.Raw(8, (uniform, _, strings) =>
{
Expand Down Expand Up @@ -560,19 +591,19 @@ private static void Raw_Visits_All_Entities(int count, bool createEmptyTable)
}
});
}

[Fact]
private void Can_Loop_With_Entity()
{
using var world = new World();

var e1 = world.Spawn().Add(123).Add<string>("123");
var e2 = world.Spawn().Add(555).Add<string>("ralf");

var query = world.Query<int, string>().Stream();

var found = new List<Entity>();

query.For((in Entity e, ref int _, ref string _) =>
{
found.Add(e);
Expand All @@ -583,20 +614,20 @@ private void Can_Loop_With_Entity()
Assert.Contains(e2, found);
}


[Fact]
private void Can_Loop_With_Entity_and_Uniform()
{
using var world = new World();

var e1 = world.Spawn().Add(123).Add<string>("123");
var e2 = world.Spawn().Add(555).Add<string>("ralf");

var query = world.Query<int, string>().Stream();

var found = new List<Entity>();
query.For( 3.1415f, (float uniform, in Entity e, ref int _, ref string _) =>

query.For(3.1415f, (float uniform, in Entity e, ref int _, ref string _) =>
{
found.Add(e);
Assert.Equal(3.1415f, uniform);
Expand All @@ -606,40 +637,40 @@ private void Can_Loop_With_Entity_and_Uniform()
Assert.Contains(e1, found);
Assert.Contains(e2, found);
}

[Fact]
private void Can_Warmup()
{
using var world = new World();
var stream = world.Query<int, byte>().Stream();
stream.Query.Warmup();
}

[Fact]
public void Cannot_Run_Job_on_Wildcard_Query()
{
using var world = new World();
world.Spawn().Add("jason").Add(123);

var stream = world.Query<string, int>(Match.Any).Stream();
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str);}));
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str); }));

stream = world.Query<string, int>(Match.Entity).Stream();
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str);}));
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str); }));

stream = world.Query<string, int>(Match.Target).Stream();
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str);}));
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str); }));

stream = world.Query<string, int>(Match.Object).Stream();
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str);}));
Assert.Throws<InvalidOperationException>(() => stream.Job((ref string str) => { output.WriteLine(str); }));

stream = world.Query<string, int>(Match.Plain).Stream();
var ran = false;
stream.Job((ref string str, ref int _) =>
{
output.WriteLine(str);
{
output.WriteLine(str);
ran = true;
});
Assert.True(ran);
}
}
}
Loading

0 comments on commit 54a8140

Please sign in to comment.