public void TestTryRoutines() { var data = new LurchTableTest <int, string>(); Assert.IsTrue(data.TryAdd(1, "a")); Assert.IsFalse(data.TryAdd(1, "a")); Assert.IsTrue(data.TryUpdate(1, "a")); Assert.IsTrue(data.TryUpdate(1, "c")); Assert.IsTrue(data.TryUpdate(1, "d", "c")); Assert.IsFalse(data.TryUpdate(1, "f", "c")); Assert.AreEqual("d", data[1]); Assert.IsTrue(data.TryUpdate(1, "a", data[1])); Assert.AreEqual("a", data[1]); Assert.IsFalse(data.TryUpdate(2, "b")); string val; Assert.IsTrue(data.TryRemove(1, out val) && val == "a"); Assert.IsFalse(data.TryRemove(2, out val)); Assert.AreNotEqual(val, "a"); Assert.IsFalse(data.TryUpdate(1, (k, x) => x.ToUpper())); data[1] = "a"; data[1] = "b"; Assert.IsTrue(data.TryUpdate(1, (k, x) => x.ToUpper())); Assert.AreEqual("B", data[1]); }
public void TestAtomicAdd() { var data = new LurchTableTest <int, string>(); int[] counter = new int[] { -1 }; for (int i = 0; i < 100; i++) { Assert.IsTrue(data.TryAdd(i, (k) => (++counter[0]).ToString())); } Assert.AreEqual(100, data.Count); Assert.AreEqual(100, counter[0] + 1); //Inserts of existing keys will not call method Assert.IsFalse(data.TryAdd(50, (k) => { throw new InvalidOperationException(); })); Assert.AreEqual(100, data.Count); }