コード例 #1
0
ファイル: InventoryStore.cs プロジェクト: AztecPyramid/Labs
        static InventoryStore()
        {
            _inventory = new TransactionalDictionary<string, AnimalDetails>();

            _inventory.Add("Elephant", new AnimalDetails(3, 200));
            _inventory.Add("Mouse", new AnimalDetails(100, 50));
        }
コード例 #2
0
        public void Snapshot_Full()
        {
            var d = new TransactionalDictionary <int, char>
            {
                { 0, '0' },
                { 1, 'a' },
                { 2, 'b' },
                { 4, '4' },
                { 6, 'c' },
            };

            d[1] = '1';

            d.Remove(2);
            d[2] = '2';

            d.Add(5, '5');

            d.Remove(6);
            d.Add(6, 'd');

            d.Remove(6);
            d[6] = '6';

            d.Add(3, '3');

            var s = d.CreateSnapshot(differential: false);

            var res = new Dictionary <int, char>();

            var v = new DictionarySnapshotVisitor <int, char>(res);

            s.Accept(v);

            var e = new Dictionary <int, char>
            {
                { 0, '0' },
                { 1, '1' },
                { 2, '2' },
                { 3, '3' },
                { 4, '4' },
                { 5, '5' },
                { 6, '6' },
            };

            AssertEqual(e, d);
            AssertEqual(res, d);
        }
コード例 #3
0
        public void Basics_Remove()
        {
            var d = new TransactionalDictionary <int, int>
            {
                { 42, 43 } // Add
            };

            Assert.IsTrue(d.Remove(42));

            Assert.IsFalse(d.ContainsKey(42));

            Assert.IsFalse(d.TryGetValue(42, out var _));

            Assert.ThrowsException <KeyNotFoundException>(() => _ = d[42]);

            Assert.IsFalse(d.Any());
        }
コード例 #4
0
        private static void AssertEqual <K, V>(Dictionary <K, V> expected, TransactionalDictionary <K, V> actual)
        {
            Assert.IsTrue(expected.OrderBy(kv => kv.Key).SequenceEqual(actual.OrderBy(kv => kv.Key)));

            foreach (var kv in expected)
            {
                var key   = kv.Key;
                var value = kv.Value;

                Assert.IsTrue(actual.ContainsKey(key));

                Assert.IsTrue(actual.TryGetValue(key, out var v));
                Assert.AreEqual(value, v);

                Assert.AreEqual(value, actual[key]);
            }
        }
コード例 #5
0
        public void Basics_Assign()
        {
            var d = new TransactionalDictionary <int, int>
            {
                [42] = 43 // indexer assignment
            };

            Assert.ThrowsException <InvalidOperationException>(() => d.Add(42, -1));

            Assert.IsTrue(d.ContainsKey(42));

            Assert.IsTrue(d.TryGetValue(42, out var x));
            Assert.AreEqual(43, x);

            Assert.AreEqual(43, d[42]);

            Assert.IsTrue(d.SequenceEqual(new[] { new KeyValuePair <int, int>(42, 43) }));
        }
コード例 #6
0
        public void Basics_Empty()
        {
            var d = new TransactionalDictionary <int, int>();

            Assert.IsFalse(d.ContainsKey(0));
            Assert.IsFalse(d.ContainsKey(1));
            Assert.IsFalse(d.ContainsKey(2));

            Assert.IsFalse(d.TryGetValue(0, out _));
            Assert.IsFalse(d.TryGetValue(1, out _));
            Assert.IsFalse(d.TryGetValue(2, out _));

            Assert.ThrowsException <KeyNotFoundException>(() => _ = d[0]);
            Assert.ThrowsException <KeyNotFoundException>(() => _ = d[1]);
            Assert.ThrowsException <KeyNotFoundException>(() => _ = d[2]);

            Assert.IsFalse(d.Any());

            Assert.IsFalse(d.Remove(0));
            Assert.IsFalse(d.Remove(1));
            Assert.IsFalse(d.Remove(2));
        }
コード例 #7
0
        public void PerformTransactionsTest()
        {
            var collection = new TransactionalDictionary <UserType, string, int>();

            collection.Add(new UserType(DateTime.Now), null, 10);

            int expectedRecordsCount = 1;
            int actualRecordCount    = collection.Count;

            Assert.AreNotEqual(expectedRecordsCount, actualRecordCount);

            var key1 = new UserType(DateTime.Today);
            var key2 = "Analytics";

            collection.Add(key1, key2, 20);
            actualRecordCount = collection.Count;
            Assert.AreEqual(expectedRecordsCount, actualRecordCount);

            collection.Remove(key1, "Managers");
            actualRecordCount    = collection.Count;
            expectedRecordsCount = 1;
            Assert.AreEqual(expectedRecordsCount, actualRecordCount);
        }
コード例 #8
0
        public void Snapshot_Delta()
        {
            var res = new Dictionary <int, char>();
            var v   = new DictionarySnapshotVisitor <int, char>(res);

            var d = new TransactionalDictionary <int, char>
            {
                { 0, '0' },
                { 1, '1' },
                { 2, '2' },
                { 3, '3' },
                { 4, '4' },
                { 6, '6' },
            };

            CheckpointAndAssert(delta: false);

            d.Add(5, '5');

            CheckpointAndAssert(delta: true);

            d.Remove(2);

            CheckpointAndAssert(delta: true);

            d[0] = '-';

            CheckpointAndAssert(delta: true);

            d.Remove(5);
            d.Add(5, 'x');

            CheckpointAndAssert(delta: true);

            d.Remove(4);
            d.Add(4, 'x');
            d.Remove(4);

            CheckpointAndAssert(delta: true);

            d.Remove(3);
            d.Add(3, 'x');
            d.Remove(3);
            d.Add(3, 'y');

            CheckpointAndAssert(delta: true);

            d.Add(7, 'a');
            d.Remove(7);

            CheckpointAndAssert(delta: true);

            d.Add(8, 'b');
            d.Remove(8);
            d.Add(8, 'c');

            CheckpointAndAssert(delta: true);

            d.Add(9, 'd');
            d.Remove(9);
            d.Add(9, 'e');
            d.Remove(9);

            CheckpointAndAssert(delta: true);

            var e = new Dictionary <int, char>
            {
                { 0, '-' },
                { 1, '1' },
                { 3, 'y' },
                { 5, 'x' },
                { 6, '6' },
                { 8, 'c' },
            };

            AssertEqual(e, d);

            void CheckpointAndAssert(bool delta)
            {
                var s1 = d.CreateSnapshot(delta);

                s1.Accept(v);
                s1.OnCommitted();

                AssertEqual(res, d);
            }
        }