Example #1
0
        public void RollForwardPastLastValue()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
                storage.StoreCurrent(new TimeMarker((ulong)(i + 1)));
            }

            storage.RollBackTo(new TimeMarker(5));
            storage.RollForwardTo(new TimeMarker((ulong)(maximumValue + 1)));

            Assert.AreEqual(maximumValue, storage.Count);

            int index = 0;

            foreach (var pair in storage)
            {
                Assert.AreEqual(index, pair.Key);
                Assert.AreSame(objects[new HistoryId(index)], pair.Value);

                index++;
            }
        }
Example #2
0
        public void RollForwardThroughClear()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
            }

            storage.StoreCurrent(new TimeMarker(1));

            storage.Clear();

            var otherObj = new MockHistoryObject(maximumValue + 1);

            objects.Add(otherObj.HistoryId, otherObj);
            storage.Add(maximumValue + 1, otherObj);

            storage.StoreCurrent(new TimeMarker(2));

            storage.RollBackToStart();
            storage.RollForwardTo(new TimeMarker(2));

            Assert.AreEqual(1, storage.Count);
            Assert.IsTrue(storage.ContainsKey(maximumValue + 1));
        }
Example #3
0
        public void RollBackMultipleTimes()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
                storage.StoreCurrent(new TimeMarker((ulong)(i + 1)));
            }

            for (int i = maximumValue; i > 0; i--)
            {
                storage.RollBackTo(new TimeMarker((ulong)i));
                Assert.AreEqual(i, storage.Count);
                for (int j = 1; j <= i; j++)
                {
                    Assert.IsTrue(storage.ContainsKey(j - 1));
                }
            }
        }
Example #4
0
        public void RollBackToLastSnapshot()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 30;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
                storage.StoreCurrent(new TimeMarker((ulong)(i + 1)));
            }

            // The first snapshot is at 1, the next one is 20 further so it is at 21
            storage.RollBackTo(new TimeMarker(21));
            Assert.AreEqual(21, storage.Count);

            int index = 0;

            foreach (var pair in storage)
            {
                Assert.AreEqual(index, pair.Key);
                Assert.AreSame(objects[new HistoryId(index)], pair.Value);

                index++;
            }
        }
Example #5
0
        public void RollForwardWithLocalChange()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
                storage.StoreCurrent(new TimeMarker((ulong)(i + 1)));
            }

            storage.RollBackToStart();

            var newObj = new MockHistoryObject(maximumValue + 1);

            objects.Add(newObj.HistoryId, newObj);
            storage.Add(maximumValue + 1, newObj);

            for (int i = 1; i < maximumValue; i++)
            {
                storage.RollForwardTo(new TimeMarker((ulong)i));
                Assert.AreEqual(i, storage.Count);
                for (int j = 1; j <= i; j++)
                {
                    Assert.IsTrue(storage.ContainsKey(j - 1));
                }
            }
        }
Example #6
0
        public void RollBackToStartWithNoValues()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            Assert.DoesNotThrow(() => storage.RollBackToStart());
        }
Example #7
0
        public void AddWithNullKey()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <string, MockHistoryObject>(lookupFunc);

            Assert.Throws <ArgumentNullException>(() => storage.Add(null, new MockHistoryObject(0)));
        }
Example #8
0
        public void TryGetValueWithNullKey()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <string, MockHistoryObject>(lookupFunc);

            MockHistoryObject result;
            var success = storage.TryGetValue(null, out result);

            Assert.IsFalse(success);
            Assert.AreEqual(null, result);
        }
Example #9
0
        public void AddVoidsForwardStack()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
            }

            storage.StoreCurrent(new TimeMarker(1));

            var otherObj = new MockHistoryObject(maximumValue);

            objects.Add(otherObj.HistoryId, otherObj);
            storage[5] = otherObj;
            storage.StoreCurrent(new TimeMarker(2));

            storage.RollBackTo(new TimeMarker(1));

            var yetAnotherObj = new MockHistoryObject(maximumValue + 1);

            objects.Add(yetAnotherObj.HistoryId, yetAnotherObj);
            storage.Add(11, yetAnotherObj);
            storage.StoreCurrent(new TimeMarker(3));

            storage.RollForwardTo(new TimeMarker(2));

            Assert.AreEqual(maximumValue + 1, storage.Count);

            int index = 0;

            foreach (var pair in storage)
            {
                if (index < maximumValue)
                {
                    Assert.AreEqual(index, pair.Key);
                    Assert.AreSame(objects[new HistoryId(index)], pair.Value);
                }
                else
                {
                    Assert.AreEqual(index + 1, pair.Key);
                    Assert.AreSame(objects[new HistoryId(index + 1)], pair.Value);
                }

                index++;
            }
        }
Example #10
0
        public void RemoveWithUnknownKey()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int key   = 5;
            var value = new MockHistoryObject(10);

            objects.Add(value.HistoryId, value);
            storage.Add(key, value);

            Assert.IsFalse(storage.Remove(6));
        }
Example #11
0
        public void AddWithDuplicateKey()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int key   = 5;
            var value = new MockHistoryObject(10);

            objects.Add(value.HistoryId, value);
            storage.Add(key, value);

            Assert.Throws <ArgumentException>(() => storage.Add(key, value));
        }
Example #12
0
        public void RemoveWithNullKey()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <string, MockHistoryObject>(lookupFunc);

            bool result = true;

            Assert.DoesNotThrow(
                () =>
            {
                result = storage.Remove(null);
            });
            Assert.IsFalse(result);
        }
Example #13
0
        public void Add()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int key   = 5;
            var value = new MockHistoryObject(10);

            objects.Add(value.HistoryId, value);
            storage.Add(key, value);

            Assert.IsTrue(storage.ContainsKey(key));
            Assert.AreEqual(1, storage.Count);
            Assert.AreSame(value, storage[key]);
        }
Example #14
0
        public void Remove()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int key   = 5;
            var value = new MockHistoryObject(10);

            objects.Add(value.HistoryId, value);
            storage.Add(key, value);

            var result = storage.Remove(key);

            Assert.IsTrue(result);
            Assert.AreEqual(0, storage.Count);
            Assert.IsFalse(storage.ContainsKey(key));
        }
Example #15
0
        public void TryGetValueWithUnknownValue()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int key   = 5;
            var value = new MockHistoryObject(10);

            objects.Add(value.HistoryId, value);
            storage.Add(key, value);

            MockHistoryObject result;
            var success = storage.TryGetValue(6, out result);

            Assert.IsFalse(success);
            Assert.AreEqual(null, result);
        }
Example #16
0
        public void RollForwardThroughRemove()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
            }

            storage.StoreCurrent(new TimeMarker(1));

            storage.Remove(5);
            storage.StoreCurrent(new TimeMarker(2));

            storage.RollBackToStart();
            storage.RollForwardTo(new TimeMarker(2));

            Assert.AreEqual(maximumValue - 1, storage.Count);

            int index = 0;

            foreach (var pair in storage)
            {
                if (index < 5)
                {
                    Assert.AreEqual(index, pair.Key);
                    Assert.AreSame(objects[new HistoryId(index)], pair.Value);
                }
                else
                {
                    Assert.AreEqual(index + 1, pair.Key);
                    Assert.AreSame(objects[new HistoryId(index + 1)], pair.Value);
                }

                index++;
            }
        }
Example #17
0
        public void RollBackToStart()
        {
            var objects = new Dictionary <HistoryId, MockHistoryObject>();
            Func <HistoryId, MockHistoryObject> lookupFunc = id => objects[id];
            var storage = new HistoryObjectDictionaryHistory <int, MockHistoryObject>(lookupFunc);

            int maximumValue = 10;

            for (int i = 0; i < maximumValue; i++)
            {
                var obj = new MockHistoryObject(i);
                objects.Add(obj.HistoryId, obj);
                storage.Add(i, obj);
                storage.StoreCurrent(new TimeMarker((ulong)(i + 1)));
            }

            storage.RollBackToStart();
            Assert.AreEqual(0, storage.Count);
        }