예제 #1
0
        public void PersistentVectorTests()
        {
            IPersistentVector <int> target = new PersistentVector <int>();

            target = target.Cons(1);
            target = target.Cons(5);
            target = target.Cons(10);
            target = target.Cons(20);

            Assert.AreEqual(4, target.Count);
            Assert.AreEqual(4, target.Length());
            Assert.AreEqual(20, target.Peek());
            Assert.AreEqual(5, target.Nth(1));
            Assert.AreEqual(5, target.ValAt(1));
            Assert.AreEqual(5, target.ValAt(1, 666));
            Assert.AreEqual(666, target.ValAt(5, 666));
            Assert.AreEqual(5, target.EntryAt(1).Value);
            Assert.IsTrue(target.ContainsKey(2));


            target = target.Pop();

            Assert.AreEqual(3, target.Count);
            Assert.AreEqual(10, target.Peek());

            target = target.AssocN(1, 9);
            Assert.AreEqual(3, target.Count);
            Assert.AreEqual(9, target.Nth(1));

            try
            {
                target = target.AssocN(15, 8);
                Assert.Fail();
            }
            catch (Exception)
            {
                //Expected exception
            }

            IPersistentVector <int> target2 = new PersistentVector <int>(new[] { 3, 56, 55, 8 });
            IPersistentVector <int> target3 = new PersistentVector <int>(new[] { 3, 56, 55, 8 });

            Assert.AreEqual(target2, target3);
            Assert.AreEqual(target2, target3);
            Assert.AreEqual(target2.GetHashCode(), target3.GetHashCode());

            target = target.Cons(-9);
            target = target.SubVec(2, 4);
            Assert.AreEqual(2, target.Count);
            Assert.AreEqual(10, target.Nth(0));
            Assert.AreEqual(-9, target.Nth(1));

            target = target.Without(10);
            Assert.AreEqual(1, target.Count);
            Assert.AreEqual(-9, target.Peek());

            target = target.Empty();

            Assert.AreEqual(0, target.Count);
        }
        public static void Samples()
        {
            // ------------------------------------------------------------
            // Creating PersistentVectors
            // ------------------------------------------------------------

            // [create-vector]
            // Create an empty PersistentVector and add some elements
            PersistentVector <string> vector =
                PersistentVector <string> .Empty()
                .Conj("hello")
                .Conj("world")
                .Conj("!");

            Console.WriteLine(vector[0]); // hello
            Console.WriteLine(vector[2]); // !

            // Check no. of elements in the PersistentVector
            Console.WriteLine(vector.Length);  // 3

            // [/create-vector]

            // [modify-vector]
            PersistentVector <string> vector2 = vector.Conj("!!!").Update(0, "hi");

            Console.WriteLine(vector2[0]);     // hi
            Console.WriteLine(vector[0]);      // hello
            Console.WriteLine(vector2[3]);     // !!!

            Console.WriteLine(vector.Length);  // 3
            Console.WriteLine(vector2.Length); // 4

            // remove the last element from a PersistentVector
            PersistentVector <string> vector3 = vector2.Initial;

            Console.WriteLine(vector3.Length); // 3

            // [/modify-vector]
        }