Beispiel #1
0
        public void AddRemoveRegistryContents()
        {
            NetworkableIdRegistry registry = new NetworkableIdRegistry(typeof(DummyClass1));

            DummyClass1 obj1 = new DummyClass1();
            DummyClass1 obj2 = new DummyClass1();

            // Add a pair of items to registry
            Assert.DoesNotThrow(() => registry.Add(obj1));
            Assert.DoesNotThrow(() => registry.Add(obj2));

            // Once added, the item is assigned an ID, and translations to/from ID match
            int id1 = registry.ToId(obj1);

            Assert.That(registry.FromId(id1), Is.EqualTo(obj1));

            // Adding the same object twice is not allowed
            Assert.Throws <ArgumentException>(() => registry.Add(obj1));

            // Remove item from registry
            registry.Remove(obj1);

            // Once removed, the item is no longer available for lookup
            Assert.Throws <ArgumentException>(() => registry.ToId(obj1));

            // Multiple removal of the same object is not allowed
            Assert.Throws <ArgumentException>(() => registry.Remove(obj1));
        }
        public void CompareClasses()
        {
            var comparerUnderTest = new ClassEqualityComparer();

            var c1d1 = new DummyClass1();
            var c1d2 = new DummyClass1();
            var c2d1 = new DummyClass2();
            var c2d2 = new DummyClass2();
            var c3d1 = new DummyClass3();

            Assert.IsTrue(comparerUnderTest.Equals(c1d1, c1d2));
            Assert.IsTrue(comparerUnderTest.Equals(c2d1, c2d2));
            Assert.IsFalse(comparerUnderTest.Equals(c1d1, c2d2));
            Assert.IsFalse(comparerUnderTest.Equals(c1d1, c2d1));
            Assert.IsFalse(comparerUnderTest.Equals(c1d1, c3d1));
            Assert.IsFalse(comparerUnderTest.Equals(c2d1, c3d1));
            Assert.IsFalse(comparerUnderTest.Equals(c2d2, c3d1));
        }
Beispiel #3
0
        public void AddWithId()
        {
            NetworkableIdRegistry registry = new NetworkableIdRegistry(typeof(DummyClass1));

            DummyClass1 obj1 = new DummyClass1();
            DummyClass1 obj2 = new DummyClass1();

            int id1 = 10;
            int id2 = 20;

            // Add item to registry
            Assert.DoesNotThrow(() => registry.AddWithId(obj1, id1));
            Assert.That(registry.ToId(obj1), Is.EqualTo(id1));

            // Re-adding the same object with a new ID is not allowed
            Assert.Throws <ArgumentException>(() => registry.AddWithId(obj1, id2));

            // Adding a different object with the same ID is not allowed
            Assert.Throws <ArgumentException>(() => registry.AddWithId(obj2, id1));
        }
        public void CompareHashes()
        {
            var comparerUnderTest = new ClassEqualityComparer();

            var c1d1 = new DummyClass1();
            var c1d2 = new DummyClass1();
            var c2d1 = new DummyClass2();
            var c2d2 = new DummyClass2();
            var c3d1 = new DummyClass3();

            var c1d1Hash = comparerUnderTest.GetHashCode(c1d1);
            var c1d2Hash = comparerUnderTest.GetHashCode(c1d2);
            var c2d1Hash = comparerUnderTest.GetHashCode(c2d1);
            var c2d2Hash = comparerUnderTest.GetHashCode(c2d2);
            var c3d1Hash = comparerUnderTest.GetHashCode(c3d1);

            Assert.AreEqual(c1d1Hash, c1d2Hash);
            Assert.AreEqual(c2d1Hash, c2d2Hash);
            Assert.AreNotEqual(c1d1Hash, c2d1Hash);
            Assert.AreNotEqual(c1d2Hash, c2d1Hash);
            Assert.AreNotEqual(c1d1Hash, c3d1Hash);
            Assert.AreNotEqual(c2d1Hash, c3d1Hash);
        }