Exemplo n.º 1
0
        public void T_GetNext_SimpleLongChain()
        {
            ChainGenerator chainGenerator = TestUtils.Create2WordGen(_simpleSentence);
            WordGenerator  wordGenerator  = InitSimpleWordGen();

            wordGenerator.ResetSubchains(new ChainCondition(ck => ck.Words[0] == null && ck.Words[1] == null), null);

            chainGenerator.GenerateChains();
            ChainMap origChains = chainGenerator.Chains;

            StringBuilder sb = new StringBuilder();

            sb.Append(wordGenerator.CurrentWord);
            for (int i = 0; i < 200; i++)
            {
                sb.Append(" ");
                sb.Append(wordGenerator.GetNextWord());
            }

            chainGenerator = TestUtils.Create2WordGen(sb.ToString());
            chainGenerator.GenerateChains();
            ChainMap resultChains = chainGenerator.Chains;

            //When we get to the end we should start from the beginning
            origChains.AddToChain(new ChainKey(new string[] { "two", "?" }), "hello");
            origChains.AddToChain(new ChainKey(new string[] { "?", "hello" }), "world");

            TestUtils.CompareChainTablesNoDuplicates(origChains, resultChains);
        }
Exemplo n.º 2
0
        public void Put_inserts_a_key_value_pair()
        {
            var map = new ChainMap <string, int>();

            map["hello"] = 1;
            Assert.False(map.Empty);
        }
Exemplo n.º 3
0
        public void T_GetSubChain()
        {
            WordGenerator gen = InitSimpleWordGen();

            //Should give me both "hello world one" and "hello world two"
            ChainMap tempMap    = gen.GetSubChain(new ChainCondition(cw => cw.Words[0] == "hello", w => w.Length == 3));
            ChainMap correctMap = new ChainMap();

            correctMap.AddToChain(new ChainKey(new string[] { "hello", "world" }), "one");
            correctMap.AddToChain(new ChainKey(new string[] { "hello", "world" }), "two");
            TestUtils.CompareChainTables(correctMap, tempMap);

            //Should give me just "hello world one"
            tempMap    = gen.GetSubChain(new ChainCondition(cw => cw.Words[0] == "hello", w => w.StartsWith("o")));
            correctMap = new ChainMap();
            correctMap.AddToChain(new ChainKey(new string[] { "hello", "world" }), "one");
            TestUtils.CompareChainTables(correctMap, tempMap);

            //If passed in condition is null nothing should change
            tempMap = gen.GetSubChain(new ChainCondition(null, null));
            TestUtils.CompareChainTables(gen.Chains, tempMap);

            //If impossible condition is passed in exception should be thrown
            Assert.Throws(typeof(Exceptions.NoPossibleElements), delegate
                          { gen.GetSubChain(new ChainCondition(null, w => w == "Does not exist")); });
        }
Exemplo n.º 4
0
        public void T_GetRandomKey_WithParams()
        {
            WordGenerator gen    = InitSimpleWordGen();
            ChainMap      subMap = new ChainMap();
            ChainKey      key1   = new ChainKey(new string[] { "key1", "key2", "key3" });
            ChainKey      key2   = new ChainKey(new string[] { "key1", "key2", "key4" });

            subMap.AddToChain(key1, "val1");
            subMap.AddToChain(key2, "val2");
            bool key1Found, key2Found;

            key1Found = key2Found = false;

            for (int i = 0; i < 10; i++)
            {
                ChainKey currKey = gen.GetRandomKey(subMap);
                if (key1.Equals(currKey))
                {
                    key1Found = true;
                }
                else if (key2.Equals(currKey))
                {
                    key2Found = true;
                }
                else
                {
                    Assert.Fail("Invalid key returned");
                }
            }
            if (!(key1Found && key2Found))
            {
                Assert.Fail("GetRandomKey is not random");
            }
        }
Exemplo n.º 5
0
        public void ContainsValue_returns_expected(string needle, bool expected)
        {
            var map = new ChainMap <string, string> {
                ["one"] = "hello", ["two"] = "world"
            };

            Assert.Equal(expected, map.ContainsValue(needle));
        }
Exemplo n.º 6
0
        public void Get_returns_expected_value()
        {
            var map = new ChainMap <string, string> {
                ["hello"] = "world"
            };

            Assert.Equal("world", map["hello"]);
        }
Exemplo n.º 7
0
        public void TryGet_returns_false_on_nonexisting_key()
        {
            var map = new ChainMap <string, string>();
            var ok  = map.TryGet("hello", out var value);

            Assert.False(ok);
            Assert.Equal(default(string), value);
        }
Exemplo n.º 8
0
        public void Settings_twice_overrides()
        {
            var map = new ChainMap <string, string>();

            map["hello"] = "world";
            map["hello"] = "heaven";
            Assert.Equal("heaven", map["hello"]);
        }
Exemplo n.º 9
0
        public void KeySet_returns_set_of_keys()
        {
            var map = new ChainMap <string, string> {
                ["a"] = "A", ["b"] = "B"
            };
            var keySet = map.KeySet();

            Assert.True(keySet.OrderBy(k => k).SequenceEqual(new [] { "a", "b" }));
        }
Exemplo n.º 10
0
        public void ChainMap_can_be_enumerated()
        {
            var map = new ChainMap <string, string>
            {
                ["hello"]   = "world",
                ["goodbye"] = "heaven"
            };

            var expected = new[] { ("hello", "world"), ("goodbye", "heaven") }.OrderBy(t => t);
Exemplo n.º 11
0
        private ChainMap <string, int> CreateMapWithKeys(params string[] keys)
        {
            var map = new ChainMap <string, int>();

            foreach (var key in keys)
            {
                map[key] = 0;
            }
            return(map);
        }
Exemplo n.º 12
0
        public void TryGet_sets_out_param()
        {
            var map = new ChainMap <string, string> {
                ["hello"] = "world"
            };
            var ok = map.TryGet("hello", out var value);

            Assert.True(ok);
            Assert.Equal("world", value);
        }
Exemplo n.º 13
0
        private ChainMap InitSimpleChainMapp()
        {
            ChainMap cm = new ChainMap();

            cm.AddToChain(new ChainKey(new string[] { null, "word1" }), "valnull");
            cm.AddToChain(new ChainKey(new string[] { "word1", "word2" }), "val1");
            cm.AddToChain(new ChainKey(new string[] { "word2", "word3" }), "val2");
            cm.AddToChain(new ChainKey(new string[] { "word1", "word2" }), "val3");
            return(cm);
        }
Exemplo n.º 14
0
        public static Dictionary <ChainKey, List <string> > GetUnderlyingDictionary(ChainMap inputChainMap)
        {
            Dictionary <ChainKey, List <string> > retValues = new Dictionary <ChainKey, List <string> >();
            List <ChainKey> keys = inputChainMap.GetAllKeys();

            foreach (ChainKey key in keys)
            {
                retValues.Add(key, inputChainMap.GetValues(key));
            }
            return(retValues);
        }
Exemplo n.º 15
0
        public void T_GetAllKeys()
        {
            ChainMap        cm          = InitSimpleChainMapp();
            List <ChainKey> allKeys     = cm.GetAllKeys();
            List <ChainKey> correctKeys = new List <ChainKey>();

            correctKeys.Add(new ChainKey(new string[] { null, "word1" }));
            correctKeys.Add(new ChainKey(new string[] { "word1", "word2" }));
            correctKeys.Add(new ChainKey(new string[] { "word2", "word3" }));
            CollectionAssert.AreEquivalent(allKeys, correctKeys);
        }
Exemplo n.º 16
0
        public void Values_returns_list_of_values()
        {
            var map = new ChainMap <string, string>
            {
                ["hello"]   = "world",
                ["goodbye"] = "world",
                ["hola"]    = "mundo",
            };
            var expected = new[] { "mundo", "world", "world" };

            Assert.True(map.Values().OrderBy(v => v).SequenceEqual(expected));
        }
Exemplo n.º 17
0
        public void Remove_removes_key_and_value_and_decrements_count_and_returns_value_associated_with_key()
        {
            var map = new ChainMap <string, string>();

            map["hello"]   = "world";
            map["goodbye"] = "sky";

            var val = map.Remove("hello");

            Assert.Equal(1, map.Count);
            Assert.False(map.ContainsKey("hello"));
            Assert.False(map.ContainsValue("world"));
            Assert.Equal("world", val);
        }
Exemplo n.º 18
0
        public void T_AddToChain()
        {
            ChainMap cm = InitSimpleChainMapp();
            Dictionary <ChainKey, List <string> > correctMap = new Dictionary <ChainKey, List <string> >();

            correctMap.Add(new ChainKey(new string[] { null, "word1" }), new List <string> {
                "valnull"
            });
            correctMap.Add(new ChainKey(new string[] { "word1", "word2" }), new List <string> {
                "val1", "val3"
            });
            correctMap.Add(new ChainKey(new string[] { "word2", "word3" }), new List <string> {
                "val2"
            });
            TestUtils.CompareChainTables(cm, correctMap);
        }
Exemplo n.º 19
0
        public void T_GetValues()
        {
            ChainMap cm           = InitSimpleChainMapp();
            ChainKey keyNull      = new ChainKey(new string[] { null, "word1" });
            ChainKey key1         = new ChainKey(new string[] { "word1", "word2" });
            ChainKey key2         = new ChainKey(new string[] { "word2", "word3" });
            ChainKey keyIncorrect = new ChainKey(new string[] { "word2", "word1" });

            CollectionAssert.AreEquivalent(cm.GetValues(keyNull), new List <string> {
                "valnull"
            });
            CollectionAssert.AreEquivalent(cm.GetValues(key1), new List <string> {
                "val1", "val3"
            });
            CollectionAssert.AreEquivalent(cm.GetValues(key2), new List <string> {
                "val2"
            });
            Assert.Throws(typeof(Exceptions.InvalidKey),
                          delegate { cm.GetValues(keyIncorrect); });
        }
Exemplo n.º 20
0
 /// <summary>
 /// Compare chain tables ignoring duplicates
 /// </summary>
 public static void CompareChainTablesNoDuplicates(ChainMap inputChainMap1,
                                                   ChainMap inputChainMap2)
 {
     CompareChainTables(GetUnderlyingDictionary(inputChainMap1),
                        GetUnderlyingDictionary(inputChainMap2), true);
 }
Exemplo n.º 21
0
 public static void CompareChainTables(ChainMap inputChainMap,
                                       Dictionary <ChainKey, List <string> > inputChains)
 {
     CompareChainTables(GetUnderlyingDictionary(inputChainMap), inputChains);
 }
Exemplo n.º 22
0
 public SettingsPage()
 {
     DataContext   = this;
     settings_copy = ConfigManager.Settings.Clone();
     InitializeComponent();
 }