Esempio n. 1
0
        public void DeepCopyTest()
        {
            object[] arr = new object[2] {
                1, "str"
            };
            var obj1 = arr.ShallowCopy();
            var p    = Obj <object[]> .ShallowCopy(arr);

            DeepTestClass t = Obj <DeepTestClass> .New();

            t.Linked        = new DeepTestClass();
            t.Linked.Linked = t;
            t.Str           = str + str;
            var arr2 = arr.DeepCopy();
            K   s;

            s.Field = 1;
            s.t     = new TestClass();
            var t1 = Obj.DeepCopy(t);
            var s1 = Obj.DeepCopy(s);
            Dictionary <object, object> d = new Dictionary <object, object>()
            {
                { t, t }
            };

            //Obj<Dictionary<object, object>>.SetDeepCopyFn(CloneDict);
            var d2 = d.DeepCopy();
            var k  = d2.Keys.First();
            var k1 = d2[k];
            HashSet <DeepTestClass> hs = new HashSet <DeepTestClass>();

            hs.Add(t);
            hs.Add(t1);
            Hashtable ht = new Hashtable();

            ht[t]  = t;
            ht[t1] = t1;
            var htc = ht.DeepCopy();
            var hsc = hs.DeepCopy();

            Assert.IsTrue(k == k1);
            Assert.IsNotNull(t);
            Assert.IsNotNull(t1);
            Assert.IsInstanceOfType(t1, typeof(DeepTestClass));
            Assert.IsInstanceOfType(s1, typeof(K));
            Assert.AreEqual(t1.Str, t.Str);
            Assert.AreEqual(s1.Field, s.Field);
            Assert.AreEqual(hs.Count, hsc.Count);
            Assert.IsFalse(Object.ReferenceEquals(hs.ElementAt(0), hsc.ElementAt(0)));
            Assert.AreEqual(ht.Count, htc.Count);
            Assert.IsFalse(Object.ReferenceEquals(ht, htc));
            foreach (var h in htc.Keys)
            {
                Assert.IsTrue(Object.ReferenceEquals(htc[h], h));
            }
            Assert.IsFalse(Assert.ReferenceEquals(t, t1));
            Assert.IsFalse(Assert.ReferenceEquals(s.t, s1.t));
        }
Esempio n. 2
0
        public void LinkedDeepCopyTest()
        {
            DeepTestClass t = Obj <DeepTestClass> .New();

            t.Linked        = new DeepTestClass();
            t.Linked.Linked = t;
            t.Str           = str + str;
            var t1 = Obj.DeepCopy(t);

            Assert.IsTrue(Object.ReferenceEquals(t1, t1.Linked.Linked));
        }
Esempio n. 3
0
        public void NewVsDeepCopyPerformanceTest()
        {
            const int count     = 100000;
            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < count; ++i)
            {
                var obj = new DeepTestClass();
            }
            stopwatch.Stop();
            Console.WriteLine("new object: {0} ms", stopwatch.ElapsedMilliseconds);
            var co = new DeepTestClass();
            var p  = co.DeepCopy();

            stopwatch.Restart();
            for (int i = 0; i < count; ++i)
            {
                var obj = (object)Obj.DeepCopy((object)co);
            }
            stopwatch.Stop();
            Console.WriteLine("Obj.DeepCopy : {0} ms", stopwatch.ElapsedMilliseconds);
        }