Insert() public method

public Insert ( int index, object key, object value ) : void
index int
key object
value object
return void
示例#1
0
		private void Common (OrderedDictionary od)
		{
			Assert.IsNotNull (od.GetEnumerator (), "GetEnumerator");
			Assert.AreEqual (0, od.Count, "Count-0");
			Assert.IsFalse (od.IsReadOnly, "IsReadOnly");
			od.Add ("a", "1");
			Assert.AreEqual (1, od.Count, "Count-1");
			od["a"] = "11";
			Assert.AreEqual ("11", od["a"], "this[string]");
			od[0] = "111";
			Assert.AreEqual ("111", od[0], "this[int]");

			DictionaryEntry[] array = new DictionaryEntry[2];
			od.CopyTo (array, 1);

			Assert.AreEqual ("111", ((DictionaryEntry)array[1]).Value, "CopyTo");
			Assert.AreEqual (1, od.Keys.Count, "Keys");
			Assert.AreEqual (1, od.Values.Count, "Values");
			Assert.IsTrue (od.Contains ("a"), "Contains(a)");
			Assert.IsFalse (od.Contains ("111"), "Contains(111)");

			od.Insert (0, "b", "2");
			Assert.AreEqual (2, od.Count, "Count-2");
			od.Add ("c", "3");
			Assert.AreEqual (3, od.Count, "Count-3");

			OrderedDictionary ro = od.AsReadOnly ();

			od.RemoveAt (2);
			Assert.AreEqual (2, od.Count, "Count-4");
			Assert.IsFalse (od.Contains ("c"), "Contains(c)");

			od.Remove ("b");
			Assert.AreEqual (1, od.Count, "Count-5");
			Assert.IsFalse (od.Contains ("b"), "Contains(b)");

			od.Clear ();
			Assert.AreEqual (0, od.Count, "Count-6");

			Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2");
			// it's a read-only reference
			Assert.AreEqual (0, od.Count, "Count-7");
		}
        private static OrderedDictionary BuildPortalAliasesRegexDictionary()
        {
            IDictionary<string, PortalAliasInfo> aliases = TestablePortalAliasController.Instance.GetPortalAliases();
            //create a new OrderedDictionary.  We use this because we
            //want to key by the correct regex pattern and return the
            //portalAlias that matches, and we want to preserve the
            //order of the items, such that the item with the most path separators (/)
            //is at the front of the list.  
            var regexList = new OrderedDictionary(aliases.Count);
            //this regex pattern, when formatted with the httpAlias, will match a request 
            //for this portalAlias
            const string aliasRegexPattern = @"(?:^(?<http>http[s]{0,1}://){0,1})(?:(?<alias>_ALIAS_)(?<path>$|\?[\w]*|/[\w]*))";
            var pathLengths = new List<int>();
            foreach (string aliasKey in aliases.Keys)
            {
                PortalAliasInfo alias = aliases[aliasKey];
                //regex escape the portal alias for inclusion into a regex pattern
                string plainAlias = alias.HTTPAlias;
                string escapedAlias = Regex.Escape(plainAlias);
                var aliasesToAdd = new List<string> { escapedAlias };
                //check for existence of www. version of domain, if it doesn't have a www.
                if (plainAlias.ToLower().StartsWith("www."))
                {
                    if (plainAlias.Length > 4)
                    {
                        string noWWWVersion = plainAlias.Substring(4);
                        if (!aliases.ContainsKey(noWWWVersion))
                        {
                            //there is no no-www version of the alias
                            aliasesToAdd.Add(Regex.Escape(noWWWVersion));
                        }
                    }
                }
                else
                {
                    string wwwVersion = "www." + plainAlias;
                    if (!aliases.ContainsKey(wwwVersion))
                    {
                        aliasesToAdd.Add(Regex.Escape(wwwVersion));
                    }
                }
                int count = 0;
                foreach (string aliasToAdd in aliasesToAdd)
                {
                    //set flag on object to know whether to redirect or not
                    count++;
                    var aliasObject = new PortalAliasInfo(alias) { Redirect = count != 1 };

                    //format up the regex pattern by replacing the alias portion with the portal alias name
                    string regexPattern = aliasRegexPattern.Replace("_ALIAS_", aliasToAdd);
                    //work out how many path separators there are in the portalAlias (ie myalias/mychild = 1 path)
                    int pathLength = plainAlias.Split('/').GetUpperBound(0);
                    //now work out where in the list we should put this portalAlias regex pattern
                    //the list is to be sorted so that those aliases with the most paths 
                    //are at the front of the list : ie, they are tested first
                    int insertPoint = pathLengths.Count - 1;
                    //walk through the existing list of path lengths,
                    //and ascertain where in the list this one falls
                    //if they are all the same path length, then place them in portal alias order
                    for (int i = 0; i < pathLengths.Count; i++)
                    {
                        insertPoint = i;
                        if (pathLength > pathLengths[i])
                        {
                            //larger than this position, insert at this value
                            break;
                        }
                        insertPoint++; //next one along (if at end, means add)
                    }
                    if (pathLengths.Count > 0 && insertPoint <= pathLengths.Count - 1)
                    {
                        //put the new regex pattern into the correct position
                        regexList.Insert(insertPoint, regexPattern, aliasObject);
                        pathLengths.Insert(insertPoint, pathLength);
                    }
                    else
                    {
                        //put the new regex pattern on the end of the list
                        regexList.Add(regexPattern, aliasObject);
                        pathLengths.Add(pathLength);
                    }
                }
            }
            return regexList;
        }
示例#3
0
        public void InsertTests()
        {
            var d = new OrderedDictionary();
            Assert.Throws<ArgumentOutOfRangeException>(() => d.Insert(-1, "foo", "bar"));
            Assert.Throws<ArgumentNullException>(() => d.Insert(0, null, "bar"));
            Assert.Throws<ArgumentOutOfRangeException>(() => d.Insert(1, "foo", "bar"));

            d.Insert(0, "foo", "bar");
            Assert.Equal("bar", d["foo"]);
            Assert.Equal("bar", d[0]);
            Assert.Throws<ArgumentException>(() => d.Insert(0, "foo", "bar"));

            d.Insert(0, "aaa", "bbb");
            Assert.Equal("bbb", d["aaa"]);
            Assert.Equal("bbb", d[0]);

            d.Insert(0, "zzz", "ccc");
            Assert.Equal("ccc", d["zzz"]);
            Assert.Equal("ccc", d[0]);

            d.Insert(3, "13", "37");
            Assert.Equal("37", d["13"]);
            Assert.Equal("37", d[3]);
        }
示例#4
0
		public void ReadOnly_Insert ()
		{
			OrderedDictionary od = new OrderedDictionary ().AsReadOnly ();
			od.Insert (0, "a", "1");
		}
        public void AsReadOnly_AttemptingToModifyDictionary_Throws()
        {
            OrderedDictionary orderedDictionary = new OrderedDictionary().AsReadOnly();
            Assert.Throws<NotSupportedException>(() => orderedDictionary[0] = "value");
            Assert.Throws<NotSupportedException>(() => orderedDictionary["key"] = "value");
 
            Assert.Throws<NotSupportedException>(() => orderedDictionary.Add("key", "value"));
            Assert.Throws<NotSupportedException>(() => orderedDictionary.Insert(0, "key", "value"));
 
            Assert.Throws<NotSupportedException>(() => orderedDictionary.Remove("key"));
            Assert.Throws<NotSupportedException>(() => orderedDictionary.RemoveAt(0));
            Assert.Throws<NotSupportedException>(() => orderedDictionary.Clear());
        }