Caching tests: the reference opens a Python-written typed-leaf store and refuses wrong type names
Vectors py-store/*.cc are written only by cultcache-py (CULTCACHE_WRITE_VECTORS=1). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
100088520e
commit
122894f5f8
8 changed files with 93 additions and 1 deletions
3
.gitattributes
vendored
3
.gitattributes
vendored
|
|
@ -38,3 +38,6 @@ packages/cultmesh-ts/native/MANIFEST.txt text eol=lf
|
|||
# LF-configured or Linux clone builds the same bytes as a Windows one. The index
|
||||
# stays LF; only the checkout changes.
|
||||
packages/cultmath/src/** text eol=crlf
|
||||
|
||||
# Stores the Python typed-leaf tests write for the C# reference to open.
|
||||
tests/vectors/**/*.cc binary
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ namespace GameCult.Caching.Tests
|
|||
|
||||
private static readonly MessagePackSerializerOptions Options = CultDocumentMessagePackSerialization.OptionsFor(typeof(NumericLeafVectorTests).Assembly);
|
||||
|
||||
private static string VectorPath(string name)
|
||||
internal static string VectorPath(string name)
|
||||
{
|
||||
for (var dir = new DirectoryInfo(TestContext.CurrentContext.TestDirectory); dir != null; dir = dir.Parent)
|
||||
{
|
||||
|
|
|
|||
89
tests/GameCult.Caching.Tests/PythonWrittenStoreTests.cs
Normal file
89
tests/GameCult.Caching.Tests/PythonWrittenStoreTests.cs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using GameCult.Caching.MessagePack;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace GameCult.Caching.Tests
|
||||
{
|
||||
// The reference opens a store a Python typed-leaf definition (cultcache-py) wrote. The stores are vectors written only
|
||||
// by cultcache-py's PythonWrittenStoreVectorTests (CULTCACHE_WRITE_VECTORS=1), values authored in Python.
|
||||
public class PythonWrittenStoreTests
|
||||
{
|
||||
private static string Store(string name, string scratch)
|
||||
{
|
||||
var copy = Path.Combine(scratch, name);
|
||||
File.Copy(NumericLeafVectorTests.VectorPath(Path.Combine("py-store", name)), copy, true);
|
||||
return copy;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APythonWrittenTypedLeafStoreOpensWithEverySlotItsCSharpValue()
|
||||
{
|
||||
var scratch = Directory.CreateTempSubdirectory().FullName;
|
||||
try
|
||||
{
|
||||
var cache = new CultCache();
|
||||
cache.AddBackingStore(new SingleFileMessagePackBackingStore(Store("leaf-record.cc", scratch)));
|
||||
await cache.PullAllBackingStoresAsync();
|
||||
var doc = cache.AllEntries.OfType<NumericLeafVectorTests.LeafRecord>().Single();
|
||||
static int Bits(float f) => BitConverter.SingleToInt32Bits(f);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(doc.Id, Is.EqualTo("py"));
|
||||
Assert.That(doc.Flag, Is.False);
|
||||
Assert.That(doc.Blob, Is.EqualTo(new byte[] { 1, 0xff }));
|
||||
Assert.That(doc.I, Is.EqualTo(int.MinValue));
|
||||
Assert.That(doc.L, Is.EqualTo(long.MaxValue));
|
||||
Assert.That(Bits(doc.F), Is.EqualTo(Bits(0.1f)));
|
||||
Assert.That(doc.D, Is.EqualTo(1e-300));
|
||||
Assert.That(doc.MaybeI, Is.Null);
|
||||
Assert.That(doc.Tags, Is.Empty);
|
||||
Assert.That(doc.Names, Is.EqualTo(new[] { "é", "z" }));
|
||||
var any = (IDictionary<object, object>)doc.Any!;
|
||||
Assert.That(Convert.ToInt64(any["n"]), Is.EqualTo(-3));
|
||||
var k = (object[])any["k"];
|
||||
Assert.That(Convert.ToInt64(k[0]), Is.EqualTo(1));
|
||||
Assert.That(k[1], Is.EqualTo(true));
|
||||
Assert.That(k[2], Is.Null);
|
||||
Assert.That(k[3], Is.EqualTo(2.5));
|
||||
Assert.That(k[4], Is.EqualTo("s"));
|
||||
Assert.That(doc.Nested, Is.Null);
|
||||
Assert.That(doc.Items.Select(i => i.N), Is.EqualTo(new[] { "one", "two" }));
|
||||
Assert.That(Bits(doc.Items[0].W), Is.EqualTo(Bits((float)(9007199254740992L + 536870913L))));
|
||||
Assert.That(Bits(doc.Items[1].W), Is.EqualTo(Bits(1e-40f)));
|
||||
Assert.That(Bits(((NumericLeafVectorTests.LeafCircle)doc.Shape!).R), Is.EqualTo(Bits(0.1f)));
|
||||
Assert.That(doc.Floats.Select(Bits), Is.EqualTo(new[] { 0.1f, 3f, -0.0f, float.PositiveInfinity }.Select(Bits)));
|
||||
Assert.That(doc.MaybeF, Is.Null);
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(scratch, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase(3)]
|
||||
[TestCase(5)]
|
||||
[TestCase(7)]
|
||||
[TestCase(14)]
|
||||
public async Task APythonWrittenCatalogNamingAnotherTypeForASlotIsRefused(int slot)
|
||||
{
|
||||
var scratch = Directory.CreateTempSubdirectory().FullName;
|
||||
try
|
||||
{
|
||||
var cache = new CultCache();
|
||||
cache.AddBackingStore(new SingleFileMessagePackBackingStore(Store($"wrong-slot-{slot}.cc", scratch)));
|
||||
var error = Assert.CatchAsync<Exception>(async () => await cache.PullAllBackingStoresAsync());
|
||||
Assert.That(error!.Message, Does.Contain($"slot {slot} changed type"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(scratch, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
tests/vectors/numeric-leaves/py-store/leaf-record.cc
Normal file
BIN
tests/vectors/numeric-leaves/py-store/leaf-record.cc
Normal file
Binary file not shown.
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-14.cc
Normal file
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-14.cc
Normal file
Binary file not shown.
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-3.cc
Normal file
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-3.cc
Normal file
Binary file not shown.
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-5.cc
Normal file
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-5.cc
Normal file
Binary file not shown.
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-7.cc
Normal file
BIN
tests/vectors/numeric-leaves/py-store/wrong-slot-7.cc
Normal file
Binary file not shown.
Loading…
Reference in a new issue