First
This commit is contained in:
commit
79b5ab98aa
13 changed files with 2016 additions and 0 deletions
61
TestProject1/PersistenceTests.cs
Normal file
61
TestProject1/PersistenceTests.cs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
namespace TestProject1;
|
||||
using PersistentMap;
|
||||
public class PersistenceTests
|
||||
{
|
||||
private readonly UnicodeStrategy _strategy = new UnicodeStrategy();
|
||||
|
||||
[Fact]
|
||||
public void PersistentMap_IsTrulyImmutable()
|
||||
{
|
||||
var v0 = BaseOrderedMap<string, int, UnicodeStrategy>.Create(_strategy);
|
||||
var v1 = v0.Set("A", 1);
|
||||
var v2 = v1.Set("B", 2);
|
||||
|
||||
// v0 should be empty
|
||||
Assert.False(v0.ContainsKey("A"));
|
||||
|
||||
// v1 should have A but not B
|
||||
Assert.True(v1.ContainsKey("A"));
|
||||
Assert.False(v1.ContainsKey("B"));
|
||||
|
||||
// v2 should have both
|
||||
Assert.True(v2.ContainsKey("A"));
|
||||
Assert.True(v2.ContainsKey("B"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TransientToPersistent_CreatesSnapshot()
|
||||
{
|
||||
var tMap = BaseOrderedMap<string, int, UnicodeStrategy>.CreateTransient(_strategy);
|
||||
tMap.Set("A", 1);
|
||||
|
||||
// Create Snapshot
|
||||
var pMap = tMap.ToPersistent();
|
||||
|
||||
// Mutate Transient Map further
|
||||
tMap.Set("B", 2);
|
||||
tMap.Remove("A");
|
||||
|
||||
// Assert Transient State
|
||||
Assert.False(tMap.ContainsKey("A"));
|
||||
Assert.True(tMap.ContainsKey("B"));
|
||||
|
||||
// Assert Persistent Snapshot (Should be isolated)
|
||||
Assert.True(pMap.ContainsKey("A")); // A should still be here
|
||||
Assert.False(pMap.ContainsKey("B")); // B should not exist
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PersistentToTransient_DoesNotCorruptSource()
|
||||
{
|
||||
var pMap = BaseOrderedMap<string, int, UnicodeStrategy>.Create(_strategy);
|
||||
pMap = pMap.Set("Fixed", 1);
|
||||
|
||||
var tMap = pMap.ToTransient();
|
||||
tMap.Set("Fixed", 999); // Modify shared key
|
||||
|
||||
// pMap should remain 1
|
||||
Assert.True(pMap.TryGetValue("Fixed", out int val));
|
||||
Assert.Equal(1, val);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue