Пример #1
0
        public void TestCommasAndAndedListOperations()
        {
            List <string> strings = new List <string>(new string[] { "Cat", "Dog", "Horse", "Cow" });

            string[] results = new string[] {
                "Cat",
                "Cat and Dog",
                "",
                "Cat, Dog, Horse and Cow"
            };

            foreach (int count in new int[] { 1, 2, 4 })
            {
                List <string> tmp = new List <string>();
                for (int i = 0; i < count; i++)
                {
                    tmp.Add(strings[i]);
                }
                string result = StringOperations.ToCommasAndAndedList(((IEnumerable <string>)tmp));
                _Debug.Assert(result.Equals(results[count - 1]));
                Console.WriteLine(result);
            }

            foreach (int count in new int[] { 1, 2, 4 })
            {
                ArrayList tmp = new ArrayList();
                for (int i = 0; i < count; i++)
                {
                    tmp.Add(strings[i]);
                }
                string result = StringOperations.ToCommasAndAndedList(tmp);
                _Debug.Assert(result.Equals(results[count - 1]));
                Console.WriteLine(result);
            }

            foreach (int count in new int[] { 1, 2, 4 })
            {
                List <Thingy> tmp = new List <Thingy>();
                for (int i = 0; i < count; i++)
                {
                    tmp.Add(new Thingy(strings[i]));
                }
                string result = StringOperations.ToCommasAndAndedListOfNames(tmp);
                _Debug.Assert(result.Equals(results[count - 1]));
                Console.WriteLine(result);
            }

            foreach (int count in new int[] { 1, 2, 4 })
            {
                List <Thingy> tmp = new List <Thingy>();
                for (int i = 0; i < count; i++)
                {
                    tmp.Add(new Thingy(strings[i]));
                }
                string result = StringOperations.ToCommasAndAndedList(tmp, n => n.Name);
                _Debug.Assert(result.Equals(results[count - 1]));
                Console.WriteLine(result);
            }
        }
Пример #2
0
        public void TestSortedHTOLTemplate()
        {
            IComparer <String> strComp             = Comparer <string> .Default;
            HashtableOfLists <string, string> htol = new HashtableOfLists <string, string>(strComp);

            htol.Add("Dog", "Collie");
            htol.Add("Pig", "Pot-bellied");
            htol.Add("Horse", "Clydesdale");
            htol.Add("Horse", "Arabian");
            htol.Add("Dog", "Chihuahua");

            Console.WriteLine("\r\nSequential dump.");
            foreach (string str in htol)
            {
                Console.WriteLine(str);
            }

            Console.WriteLine("\r\nList dump.");
            foreach (string key in htol.Keys)
            {
                Console.WriteLine(key + " --> " + StringOperations.ToCommasAndAndedList(htol[key]));
            }

            htol.Remove("Horse", "Arabian");
            htol.Remove("Horse", "Clydesdale");

            Console.WriteLine("\r\nSequential dump.");
            foreach (string str in htol)
            {
                Console.WriteLine(str);
            }

            Console.WriteLine("\r\nList dump.");
            foreach (string key in htol.Keys)
            {
                Console.WriteLine(key + " --> " + StringOperations.ToCommasAndAndedList(htol[key]));
            }
        }