public void TestSelect() { RWList <int> one = new RWList <int>(); one.Add(3); RWList <int> two = one.Clone(); two.Add(2); RWList <int> thr = two.Clone(); thr.Add(1); ExpectList(thr, 3, 2, 1); ExpectList(one.Select(delegate(int i) { return(i + 1); }), 4); ExpectList(two.Select(delegate(int i) { return(i + 1); }), 4, 3); ExpectList(thr.Select(delegate(int i) { return(i + 1); }), 4, 3, 2); ExpectList(two.Select(delegate(int i) { return(i == 3 ? 3 : 0); }), 3, 0); ExpectList(thr.Select(delegate(int i) { return(i == 3 ? 3 : 0); }), 3, 0, 0); ExpectList(thr.Select(delegate(int i) { return(i == 1 ? 0 : i); }), 3, 2, 0); Assert.That(one.SmartSelect(delegate(int i) { return(i); }).ToRVList() == one.ToRVList()); Assert.That(two.SmartSelect(delegate(int i) { return(i); }).ToRVList() == two.ToRVList()); Assert.That(thr.SmartSelect(delegate(int i) { return(i); }).ToRVList() == thr.ToRVList()); ExpectList(one.SmartSelect(delegate(int i) { return(i + 1); }), 4); ExpectList(two.SmartSelect(delegate(int i) { return(i + 1); }), 4, 3); ExpectList(thr.SmartSelect(delegate(int i) { return(i + 1); }), 4, 3, 2); ExpectList(two.SmartSelect(delegate(int i) { return(i == 3 ? 3 : 0); }), 3, 0); ExpectList(thr.SmartSelect(delegate(int i) { return(i == 3 ? 3 : 0); }), 3, 0, 0); ExpectList(thr.SmartSelect(delegate(int i) { return(i == 1 ? 0 : i); }), 3, 2, 0); Assert.That(thr.SmartSelect(delegate(int i) { return(i == 1 ? 0 : i); }).WithoutLast(1) == thr.WithoutLast(1)); }
public void TestFork() { RWList <int> A = new RWList <int>(); A.AddRange(new int[] { 1, 2, 3 }); RWList <int> B = A.Clone(); A.Push(4); ExpectList(B, 1, 2, 3); ExpectList(A, 1, 2, 3, 4); B.Push(-4); ExpectList(B, 1, 2, 3, -4); Assert.That(A.WithoutLast(2) == B.WithoutLast(2)); }
public void TestWhere() { RWList <int> one = new RWList <int>(); one.Add(3); RWList <int> two = one.Clone(); two.Add(2); RWList <int> thr = two.Clone(); thr.Add(1); ExpectList(one.Where(delegate(int i) { return(false); })); ExpectList(two.Where(delegate(int i) { return(false); })); ExpectList(thr.Where(delegate(int i) { return(false); })); Assert.That(one.Where(delegate(int i) { return(true); }).ToRVList() == one.ToRVList()); Assert.That(two.Where(delegate(int i) { return(true); }).ToRVList() == two.ToRVList()); Assert.That(thr.Where(delegate(int i) { return(true); }).ToRVList() == thr.ToRVList()); Assert.That(two.Where(delegate(int i) { return(i == 3); }).ToRVList() == two.WithoutLast(1)); Assert.That(thr.Where(delegate(int i) { return(i == 3); }).ToRVList() == thr.WithoutLast(2)); Assert.That(thr.Where(delegate(int i) { return(i > 1); }).ToRVList() == thr.WithoutLast(1)); ExpectList(two.Where(delegate(int i) { return(i == 2); }), 2); ExpectList(thr.Where(delegate(int i) { return(i == 2); }), 2); }
public void TestFalseOwnership() { // This test tries to make sure a RWList doesn't get confused about what // blocks it owns. It's possible for a RWList to share a partially-mutable // block that contains mutable items with another RWList, but only one // RWList owns the items. // Case 1: two WLists point to the same block but only one owns it: // // block 0 // owned by A // |____3| block 1 // |____2| unowned // A,B--->|Imm_1|--->|Imm_1| // |____0| |____0| // // (The location of "Imm" in each block denotes the highest immutable // item; this diagram shows there are two immutable items in each // block) RWList <int> A = new RWList <int>(); A.Resize(4); for (int i = 0; i < 4; i++) { A[i] = i; } RWList <int> B = A.Clone(); // B can't add to the second block because it's not the owner, so a // third block is created when we Add(1). B.Add(4); A.Add(-4); ExpectList(A, 0, 1, 2, 3, -4); ExpectList(B, 0, 1, 2, 3, 4); Assert.AreEqual(2, A.BlockChainLength); Assert.AreEqual(3, B.BlockChainLength); // Case 2: two WLists point to different blocks but they share a common // tail, where one list owns part of the tail and the other does not: // // block 0 // owned by B // |____8| // |____7| // |____6| // |____5| block 1 // |____4| owned by A // |____3| A |____3| block 2 // |____2| | |____2| unowned // |____1|---+---->|Imm_1|---->|Imm_1| // B--->|____0| |____0| |____0| // mutable // // Actually the previous test puts us in just this state. // // I can't think of a test that uses the public interface to detect bugs // in this case. The most important thing is that B._block.PriorIsOwned // returns false. Assert.That(B.IsOwner && !B.Block.PriorIsOwned); Assert.That(A.IsOwner); Assert.That(B.Block.Prior.ToRVList() == A.WithoutLast(1)); }