Exemplo n.º 1
0
        private static void RunTrackableDictionary()
        {
            Log.WriteLine("***** TrackableDictionary (Json) *****");

            var dict = new TrackableDictionary<int, string>();
            dict.SetDefaultTracker();

            dict.Add(1, "One");
            dict.Add(2, "Two");
            dict.Add(3, "Three");

            var json = JsonConvert.SerializeObject(dict.Tracker, JsonSerializerSettings);
            Log.WriteLine(json);
            dict.Tracker.Clear();

            dict.Remove(1);
            dict[2] = "TwoTwo";
            dict.Add(4, "Four");

            var json2 = JsonConvert.SerializeObject(dict.Tracker, JsonSerializerSettings);
            Log.WriteLine(json2);
            dict.Tracker.Clear();

            Log.WriteLine();
        }
        public void TestClear(bool track)
        {
            var events = 0;
            var acc    = new Accumulator("test");
            var dic    = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };

            dic.CollectionChanged += (s, e) =>
            {
                events++;
                Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Reset);
            };
            dic.Clear();
            Assert.AreEqual(0, dic.Count);
            Assert.AreEqual(1, events);
            if (track)
            {
                Assert.AreEqual(3, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
Exemplo n.º 3
0
        public static bool TryAchieved(this TrackableDictionary <int, UserAchievement> dict,
                                       AchievementKey key)
        {
            UserAchievement ach;

            if (dict.TryGetValue((int)key, out ach))
            {
                if (ach.AchieveTime.HasValue)
                {
                    return(false);
                }

                ach = new UserAchievement {
                    AchieveTime = DateTime.UtcNow, Value = ach.Value
                };
                dict[(int)key] = ach;
            }
            else
            {
                ach = new UserAchievement {
                    AchieveTime = DateTime.UtcNow
                };
                dict.Add((int)key, ach);
            }
            return(true);
        }
        public void TestIndexing(bool track)
        {
            var events = 0;
            var acc    = new Accumulator("test");
            var dic    = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };

            dic.CollectionChanged += (s, e) =>
            {
                events++;
                Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Replace);
            };
            dic["one"] = 3;
            Assert.AreEqual(3, dic["one"]);
            dic["four"] = 4;
            Assert.AreEqual(4, dic["four"]);
            Assert.AreEqual(2, events);
            if (track)
            {
                Assert.AreEqual(4, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
        public void TestSafeRemove(bool track)
        {
            var events = 0;
            var acc    = new Accumulator("test");
            var dic    = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };

            dic.CollectionChanged += (s, e) =>
            {
                events++;
                Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Remove);
                Assert.AreEqual(e.NewItems[0], new KeyValuePair <string, int>("one", 1));
            };
            Assert.IsTrue(dic.SafeRemove("one"));
            Assert.IsFalse(dic.SafeRemove("one"));
            Assert.AreEqual(1, events);
            if (track)
            {
                Assert.AreEqual(3, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
Exemplo n.º 6
0
        public static int?TryProgress(this TrackableDictionary <int, UserAchievement> dict,
                                      AchievementKey key, int increment)
        {
            UserAchievement ach;

            if (dict.TryGetValue((int)key, out ach))
            {
                if (ach.AchieveTime.HasValue)
                {
                    return(null);
                }

                ach = new UserAchievement {
                    Value = ach.Value + increment
                };
                dict[(int)key] = ach;
            }
            else
            {
                ach = new UserAchievement {
                    Value = increment
                };
                dict.Add((int)key, ach);
            }
            return(ach.Value);
        }
Exemplo n.º 7
0
        public static bool IsAchieved(this TrackableDictionary <int, UserAchievement> dict,
                                      AchievementKey key)
        {
            UserAchievement ach;

            return(dict.TryGetValue((int)key, out ach) && ach.AchieveTime.HasValue);
        }
Exemplo n.º 8
0
        private static void RunTrackableDictionary()
        {
            Console.WriteLine("***** TrackableDictionary (Protobuf) *****");

            var dict = new TrackableDictionary<int, string>();
            dict.SetDefaultTracker();

            dict.Add(1, "One");
            dict.Add(2, "Two");
            dict.Add(3, "Three");

            var buf = PrintBytes(Serialize(dict.Tracker));
            Console.WriteLine(Deserialize<TrackableDictionaryTracker<int, string>>(buf));
            dict.Tracker.Clear();

            dict.Remove(1);
            dict[2] = "TwoTwo";
            dict.Add(4, "Four");

            var buf2 = PrintBytes(Serialize(dict.Tracker));
            Console.WriteLine(Deserialize<TrackableDictionaryTracker<int, string>>(buf2));
            dict.Tracker.Clear();

            Console.WriteLine();
        }
        public void TestAddRange(bool track)
        {
            var events = 0;
            var acc    = new Accumulator("test");
            var dic    = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };
            var other = new TrackableDictionary <string, int>()
            {
                { "three", 3 }, { "four", 4 }
            };

            dic.CollectionChanged += (s, e) =>
            {
                events++;
                Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Reset);
            };
            dic.AddRange(other);
            Assert.IsTrue(dic.ContainsKey("three"));
            Assert.IsTrue(dic.ContainsKey("four"));
            Assert.AreEqual(dic["three"], 3);
            Assert.AreEqual(dic["four"], 4);
            Assert.AreEqual(1, events);
            if (track)
            {
                Assert.AreEqual(4, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
Exemplo n.º 10
0
        private static void RunTrackableDictionary()
        {
            Log.WriteLine("***** TrackableDictionary (Json) *****");

            var dict = new TrackableDictionary <int, string>();

            dict.SetDefaultTracker();

            dict.Add(1, "One");
            dict.Add(2, "Two");
            dict.Add(3, "Three");

            var json = JsonConvert.SerializeObject(dict.Tracker, JsonSerializerSettings);

            Log.WriteLine(json);
            dict.Tracker.Clear();

            dict.Remove(1);
            dict[2] = "TwoTwo";
            dict.Add(4, "Four");

            var json2 = JsonConvert.SerializeObject(dict.Tracker, JsonSerializerSettings);

            Log.WriteLine(json2);
            dict.Tracker.Clear();

            Log.WriteLine();
        }
        public TrackableDictionary <TKey, TValue> ConvertToTrackableDictionary(BsonDocument doc)
        {
            var dictionary = new TrackableDictionary <TKey, TValue>();

            ConvertToDictionary(doc, dictionary);
            return(dictionary);
        }
Exemplo n.º 12
0
 public void TestDictionary_Update_Detect_Value_Equal_Ok()
 {
     var dict = new TrackableDictionary<int, int>();
     dict[1] = 100;
     var a = "!";
     var b = a.Clone();
     dict.Update(1, (key, value) => value);
 }
Exemplo n.º 13
0
        private TrackableDictionary <TKey, string> CreateTestDictionary()
        {
            var dict = new TrackableDictionary <TKey, string>();

            dict.Add(CreateKey(1), "One");
            dict.Add(CreateKey(2), "Two");
            dict.Add(CreateKey(3), "Three");
            return(dict);
        }
Exemplo n.º 14
0
 public void TestDictionary_Update_Detect_Reference_Equal_Error()
 {
     var dict = new TrackableDictionary<int, string>();
     dict[1] = "one";
     Assert.Throws<InvalidOperationException>(() =>
     {
         dict.Update(1, (key, value) => value);
     });
 }
        public async Task <TrackableDictionary <TKey, TValue> > LoadAsync(DbDataReader reader)
        {
            var dictionary = new TrackableDictionary <TKey, TValue>();

            while (await reader.ReadAsync())
            {
                dictionary.Add(ConvertToKeyAndValue(reader));
            }
            return(dictionary);
        }
Exemplo n.º 16
0
 private void AssertEqual(TrackableDictionary <TKey, ItemData> a, TrackableDictionary <TKey, ItemData> b)
 {
     Assert.Equal(a.Count, b.Count);
     foreach (var item in a)
     {
         var a_v = item.Value;
         var b_v = b[item.Key];
         Assert.Equal(a_v.Kind, b_v.Kind);
         Assert.Equal(a_v.Count, b_v.Count);
         Assert.Equal(a_v.Note, b_v.Note);
     }
 }
Exemplo n.º 17
0
        public static bool TryProgress(this TrackableDictionary <int, UserAchievement> dict,
                                       AchievementKey key, int increment, int goal)
        {
            var value = TryProgress(dict, key, increment);

            if (value == null || value < goal)
            {
                return(false);
            }

            return(TryAchieved(dict, key));
        }
        public TrackableDictionary <TKey, TValue> ConvertToTrackableDictionary(BsonDocument doc,
                                                                               params object[] partialKeys)
        {
            var partialDoc = DocumentHelper.QueryValue(doc, partialKeys);

            if (partialDoc == null)
            {
                return(null);
            }

            var dictionary = new TrackableDictionary <TKey, TValue>();

            ConvertToDictionary(partialDoc.AsBsonDocument, dictionary);
            return(dictionary);
        }
Exemplo n.º 19
0
        private TrackableDictionary <TKey, ItemData> CreateTestDictionary()
        {
            var dict = new TrackableDictionary <TKey, ItemData>();

            var value1 = new ItemData();

            value1.Kind  = 101;
            value1.Count = 1;
            value1.Note  = "Handmade Sword";
            dict.Add(CreateKey(1), value1);

            var value2 = new ItemData();

            value2.Kind  = 102;
            value2.Count = 3;
            value2.Note  = "Lord of Ring";
            dict.Add(CreateKey(2), value2);

            return(dict);
        }
        public void TestTryGetValue(bool track)
        {
            var acc = new Accumulator("test");
            var dic = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };

            Assert.IsTrue(dic.TryGetValue("one", out var val));
            Assert.AreEqual(1, val);
            Assert.IsTrue(dic.ContainsKey("one"));

            Assert.IsFalse(dic.TryGetValue("four", out val));

            if (track)
            {
                Assert.AreEqual(2, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
Exemplo n.º 21
0
        private static void RunTrackableDictionary()
        {
            Console.WriteLine("***** TrackableDictionary *****");

            var dict = new TrackableDictionary<int, string>();
            dict.SetDefaultTracker();

            dict.Add(1, "One");
            dict.Add(2, "Two");
            dict.Add(3, "Three");

            Console.WriteLine(dict.Tracker);
            dict.Tracker.Clear();

            dict.Remove(1);
            dict[2] = "TwoTwo";
            dict.Add(4, "Four");

            Console.WriteLine(dict.Tracker);
            dict.Tracker.Clear();

            Console.WriteLine();
        }
Exemplo n.º 22
0
        private static void RunTrackableDictionary()
        {
            Console.WriteLine("***** TrackableDictionary *****");

            var dict = new TrackableDictionary <int, string>();

            dict.SetDefaultTracker();

            dict.Add(1, "One");
            dict.Add(2, "Two");
            dict.Add(3, "Three");

            Console.WriteLine(dict.Tracker);
            dict.Tracker.Clear();

            dict.Remove(1);
            dict[2] = "TwoTwo";
            dict.Add(4, "Four");

            Console.WriteLine(dict.Tracker);
            dict.Tracker.Clear();

            Console.WriteLine();
        }
        public void TestReplace(bool track)
        {
            var events = 0;
            var acc    = new Accumulator("test");
            var dic    = new TrackableDictionary <string, int>(acc, track)
            {
                { "one", 1 }, { "two", 2 }
            };
            var other = new TrackableDictionary <string, int>()
            {
                { "three", 3 }, { "four", 4 }
            };

            dic.CollectionChanged += (s, e) =>
            {
                events++;
                Assert.AreEqual(e.Action, NotifyCollectionChangedAction.Replace);
            };

            dic.Replace(other);
            Assert.AreEqual(1, events);
            Assert.AreEqual(2, other.Count);
            foreach (var o in other)
            {
                Assert.IsTrue(dic.Contains(o));
            }

            if (track)
            {
                Assert.AreEqual(3, acc.Records.Count);
            }
            else
            {
                Assert.AreEqual(0, acc.Records.Count);
            }
        }
Exemplo n.º 24
0
 public void TestDictionary_Update_Detect_Value_Changed_Ok()
 {
     var dict = new TrackableDictionary<int, int>();
     dict[1] = 100;
     dict.Update(1, (key, value) => value + 1);
 }
Exemplo n.º 25
0
 protected override Task SaveAsync(TrackableDictionary <int, ItemData> dictionary)
 {
     return(_mapper.SaveAsync(_db.Connection, dictionary.Tracker));
 }
Exemplo n.º 26
0
 public void TestDictionary_Update_Detect_Reference_Cloned_Ok()
 {
     var dict = new TrackableDictionary<int, string>();
     dict[1] = "one";
     dict.Update(1, (key, value) => value + "!");
 }
Exemplo n.º 27
0
 protected override Task SaveAsync(TrackableDictionary <int, ItemData> dictionary)
 {
     return(_mapper.SaveAsync(_collection, dictionary.Tracker, _testId));
 }
        public async Task <TrackableDictionary <TKey, TValue> > LoadAsync(IDatabase db, RedisKey key)
        {
            var dictionary = new TrackableDictionary <TKey, TValue>();

            return(await LoadAsync(db, dictionary, key).ConfigureAwait(false) ? dictionary : null);
        }
Exemplo n.º 29
0
        private TTrackableContainer CreateTestContainer(bool withTracker)
        {
            dynamic container = new TTrackableContainer();

            dynamic person = new TTrackablePerson();

            container.Person = person;
            var missions = new TrackableDictionary <int, MissionData>();

            container.Missions = missions;

            var tags = new TrackableList <TagData>();

            if (_useList)
            {
                container.Tags = tags;
            }

            var aliases = new TrackableSet <string>();

            if (_useSet)
            {
                container.Aliases = aliases;
            }

            if (withTracker)
            {
                ((ITrackable)container).SetDefaultTracker();
            }

            // Person
            {
                person.Name = "Testor";
                person.Age  = 10;
            }

            // Missions
            {
                var value1 = new MissionData();
                value1.Kind  = 101;
                value1.Count = 1;
                value1.Note  = "Handmade Sword";
                missions.Add(1, value1);
                var value2 = new MissionData();
                value2.Kind  = 102;
                value2.Count = 3;
                value2.Note  = "Lord of Ring";
                missions.Add(2, value2);
            }

            // Tags
            if (_useList)
            {
                tags.Add(new TagData {
                    Text = "Hello", Priority = 1
                });
                tags.Add(new TagData {
                    Text = "World", Priority = 2
                });
            }

            // Aliases
            if (_useSet)
            {
                aliases.Add("alpha");
                aliases.Add("beta");
                aliases.Add("gamma");
            }

            return(container);
        }
Exemplo n.º 30
0
 protected override Task SaveAsync(TrackableDictionary <int, string> dictionary)
 {
     return(_mapper.SaveAsync(_db, dictionary.Tracker, _testId));
 }
        public void TestGetEnuemrator()
        {
            var dic = new TrackableDictionary <string, int>();

            Assert.IsNotNull(dic.GetEnumerator());
        }
Exemplo n.º 32
0
 protected abstract Task SaveAsync(TrackableDictionary <TKey, string> dictionary);
Exemplo n.º 33
0
 private void AssertEqual(TrackableDictionary <TKey, string> a, TrackableDictionary <TKey, string> b)
 {
     Assert.Equal(a.OrderBy(x => x.Key), b.OrderBy(x => x.Key));
 }
Exemplo n.º 34
0
 protected abstract Task SaveAsync(TrackableDictionary <TKey, ItemData> dictionary);
		protected override void Add(TrackableDictionary<TKey, TValue> collection, int index, TKey key, TValue value, MessagePackSerializerOptions options)
		{
			collection.Add(key, value);
		}