Exemplo n.º 1
0
        static InventoryStore()
        {
            _inventory = new TransactionalDictionary<string, AnimalDetails>();

            _inventory.Add("Elephant", new AnimalDetails(3, 200));
            _inventory.Add("Mouse", new AnimalDetails(100, 50));
        }
        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);
        }
Exemplo n.º 3
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);
        }
        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) }));
        }
        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);
            }
        }