public static void Samples() { // ------------------------------------------------------------ // Creating PersistentVectors // ------------------------------------------------------------ // [create-hashmap] // Create an empty PersistentHashMap and add some elements PersistentHashMap <int, string> map = PersistentHashMap <int, string> .Empty() .Add(42, "hello") .Add(99, "world"); Console.WriteLine(map[42]); // hello Console.WriteLine(map[99]); // world // Check no. of elements in the PersistentHashMap Console.WriteLine(map.Length); // 2 // [/create-hashmap] // [modify-hashmap] PersistentHashMap <int, string> map2 = map .Add(104, "!") .Add(42, "hi"); // replace existing value Console.WriteLine(map2[42]); // hi Console.WriteLine(map[42]); // hello Console.WriteLine(map.Length); // 2 Console.WriteLine(map2.Length); // 3 // remove the last element from a PersistentHashMap PersistentHashMap <int, string> map3 = map2.Remove(104); Console.WriteLine(map3.Length); // 2 // [/modify-hashmap] }
public void PersistentMapTests() { IPersistentMap <String, String> target = new PersistentHashMap <String, String>(); target = target.Assoc("k1", "v1"); target = target.Assoc("k2", "v2"); target = target.Assoc("k3", "v3"); target = target.Assoc("k4", "v4"); Assert.AreEqual(4, target.Count); Assert.IsTrue(target.ContainsKey("k3")); Assert.IsFalse(target.ContainsKey("x")); Assert.AreEqual("k3", target.EntryAt("k3").Key); Assert.AreEqual("v3", target.EntryAt("k3").Value); Assert.AreEqual("v3", target.ValAt("k3")); Assert.AreEqual("nf", target.ValAt("x", "nf")); target = target.Assoc("k2", "vx"); Assert.AreEqual(4, target.Count); Assert.AreEqual("vx", target.ValAt("k2")); try { target = target.AssocEx("k2", "vy"); Assert.Fail(); } catch (Exception) { //Expected exception } Assert.AreEqual(4, target.Count); Assert.AreEqual("vx", target.ValAt("k2")); target = target.Without("k3"); Assert.AreEqual(3, target.Count); Assert.AreEqual("v4", target.ValAt("k4")); target = target.Cons(new KeyValuePair <string, string>("k5", "v5")); Assert.AreEqual(4, target.Count); Assert.AreEqual("v5", target.ValAt("k5")); target = target.Assoc(null, "null"); target = target.Assoc("null", null); Assert.AreEqual(6, target.Count); Assert.AreEqual("null", target.ValAt(null)); Assert.IsNull(target.ValAt("null")); foreach (KeyValuePair <string, string> mapEntry in target) { if (mapEntry.Key != null && mapEntry.Key.Equals("null")) { Assert.IsNull(mapEntry.Value); } } target = target.Empty(); Assert.AreEqual(0, target.Count); target = target.Assoc("x", "y"); target = target.Assoc("z", "a"); IPersistentMap <String, String> target2 = new PersistentHashMap <String, String>(); target2 = target2.Assoc("x", "y"); target2 = target2.Assoc("z", "a"); Assert.AreEqual(target, target2); Assert.AreEqual(target, target2); Assert.AreEqual(target.GetHashCode(), target2.GetHashCode()); target2 = target2.Without("z"); target2 = target2.Without("b"); Assert.AreNotEqual(target, target2); Assert.IsFalse(target == target2); Assert.AreNotEqual(target, target2); }