2009-07-14 Gonzalo Paniagua Javier <gonzalo@novell.com>
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Wed, 15 Jul 2009 01:05:07 +0000 (01:05 -0000)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Wed, 15 Jul 2009 01:05:07 +0000 (01:05 -0000)
* SortedListTest.cs: new tests for bug #521750 provided by
Kevin Fitzgerald.

svn path=/trunk/mcs/; revision=137916

mcs/class/System/Test/System.Collections.Generic/ChangeLog
mcs/class/System/Test/System.Collections.Generic/SortedListTest.cs

index fe162abaf29d55b20fd703f668253ff73cb9e399..9901e47c71bf970070adc53cef7c509cd421ece6 100644 (file)
@@ -1,3 +1,8 @@
+2009-07-14 Gonzalo Paniagua Javier <gonzalo@novell.com>
+
+       * SortedListTest.cs: new tests for bug #521750 provided by
+       Kevin Fitzgerald.
+
 2009-05-10  AndrĂ©s G. Aragoneses  <aaragoneses@novell.com>
 
        * LinkedListTest.cs: Added test for #481621,
index 7b90220a0c747d170e885b432558822317ab770e..ced822e6ed1c47abe73ccbeedb7dbac30372b3da 100644 (file)
@@ -325,6 +325,144 @@ namespace MonoTests.System.Collections.Generic
                        list.RemoveAt (0);
                        en.MoveNext ();
                }
+
+               sealed class StartsWithComparator : IComparer<string> {
+                       private readonly static Comparer<string> _stringComparer = Comparer<string>.Default;
+                       public static readonly StartsWithComparator Instance = new StartsWithComparator();
+
+                       public int Compare(string part, string whole)
+                       {
+                               // let the default string comparer deal with null or when part is not smaller then whole
+                               if (part == null || whole == null || part.Length >= whole.Length)
+                                       return _stringComparer.Compare(part, whole);
+
+                               // loop through all characters that part and whole have in common
+                               int pos = 0;
+                               bool match;
+                               do {
+                                       match = (part[pos] == whole[pos]);
+                               } while (match && ++pos < part.Length);
+
+                               // return result of last comparison
+                               return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
+                       }
+               }
+
+               sealed class StartsWithComparatorPartWholeCheck : IComparer<string>
+               {
+                       private readonly static Comparer<string> _stringComparer = Comparer<string>.Default;
+
+                       public static readonly StartsWithComparator Instance = new StartsWithComparator();
+
+                       public int Compare(string part, string whole)
+                       {
+                               Assert.IsTrue(part == "Part", "#PWC0");
+                               Assert.IsTrue(whole == "Whole", "#PWC1");
+
+                               // let the default string comparer deal with null or when part is not smaller then whole
+                               if (part == null || whole == null || part.Length >= whole.Length)
+                                       return _stringComparer.Compare(part, whole);
+
+                               // loop through all characters that part and whole have in common
+                               int pos = 0;
+                               bool match;
+                               do {
+                                       match = (part[pos] == whole[pos]);
+                               } while (match && ++pos < part.Length);
+
+                               // return result of last comparison
+                               return match ? 0 : (part[pos] < whole[pos] ? -1 : 1);
+                       }
+               }
+
+               [Test]
+               public void ComparatorUsageTest()
+               {
+                       SortedList<string, string> sl = new SortedList<string, string>(StartsWithComparator.Instance);
+
+                       sl.Add("Apples", "Value-Apples");
+                       sl.Add("Bananas", "Value-Bananas");
+                       sl.Add("Oranges", "Value-Oranges");
+
+                       // Ensure 3 objects exist in the collection
+                       Assert.IsTrue(sl.Count == 3, "Count");
+
+                       // Complete Match Test Set
+                       Assert.IsTrue(sl.ContainsKey("Apples"), "#A0");
+                       Assert.IsTrue(sl.ContainsKey("Bananas"), "#A1");
+                       Assert.IsTrue(sl.ContainsKey("Oranges"), "#A2");
+
+                       // Partial Match Test Set
+                       Assert.IsTrue(sl.ContainsKey("Apples are great fruit!"), "#B0");
+                       Assert.IsTrue(sl.ContainsKey("Bananas are better fruit."), "#B1");
+                       Assert.IsTrue(sl.ContainsKey("Oranges are fun to peel."), "#B2");
+
+                       // Reversed Match Test Set
+                       Assert.IsFalse(sl.ContainsKey("Value"), "#C0");
+
+                       // No match tests
+                       Assert.IsFalse(sl.ContainsKey("I forgot to bring my bananas."), "#D0");
+                       Assert.IsFalse(sl.ContainsKey("My apples are on vacation."), "#D0");
+                       Assert.IsFalse(sl.ContainsKey("The oranges are not ripe yet."), "#D0");
+
+               }
+
+               [Test]
+               public void ComparatorPartWholeCheck()
+               {
+                       SortedList<string, string> sl = new SortedList<string, string>(StartsWithComparatorPartWholeCheck.Instance);
+                       sl.Add("Part", "Value-Part");
+                       Assert.IsFalse(sl.ContainsKey("Whole"), "#PWC2");
+               }
+
+               [Test]
+               public void NonComparatorStringCheck()
+               {
+                       SortedList<string, string> sl = new SortedList<string, string>();
+
+                       sl.Add("Oranges", "Value-Oranges");
+                       sl.Add("Apples", "Value-Apples");
+                       sl.Add("Bananas", "Value-Bananas");
+
+                       int i = 0;
+                       Assert.IsTrue(sl.Count == 3, "NCSC #A0");
+
+                       Assert.IsTrue(sl.ContainsKey("Apples"), "NCSC #B1");
+                       Assert.IsTrue(sl.ContainsKey("Bananas"), "NCSC #B2");
+                       Assert.IsTrue(sl.ContainsKey("Oranges"), "NCSC #B3");
+
+                       Assert.IsFalse(sl.ContainsKey("XApples"), "NCSC #C1");
+                       Assert.IsFalse(sl.ContainsKey("XBananas"), "NCSC #C2");
+                       Assert.IsFalse(sl.ContainsKey("XOranges"), "NCSC #C3");
+
+                       Assert.IsTrue(sl.Keys[0] == "Apples", "NCSC #D1");
+                       Assert.IsTrue(sl.Keys[1] == "Bananas", "NCSC #D2");
+                       Assert.IsTrue(sl.Keys[2] == "Oranges", "NCSC #D3");
+               }
+
+               [Test]
+               public void NonComparatorIntCheck()
+               {
+                       SortedList<int, string> sl = new SortedList<int, string>();
+
+                       sl.Add(3, "Value-Oranges");
+                       sl.Add(2, "Value-Bananas");
+                       sl.Add(1, "Value-Apples");
+
+                       Assert.IsTrue(sl.Count == 3, "NCIC #A0");
+
+                       Assert.IsTrue(sl.ContainsKey(1), "NCIC #B1");
+                       Assert.IsTrue(sl.ContainsKey(2), "NCIC #B2");
+                       Assert.IsTrue(sl.ContainsKey(3), "NCIC #B3");
+
+                       Assert.IsFalse(sl.ContainsKey(11), "NCIC #C1");
+                       Assert.IsFalse(sl.ContainsKey(22), "NCIC #C2");
+                       Assert.IsFalse(sl.ContainsKey(33), "NCIC #C3");
+
+                       Assert.IsTrue(sl.Keys[0] == 1, "NCIC #D1");
+                       Assert.IsTrue(sl.Keys[1] == 2, "NCIC #D2");
+                       Assert.IsTrue(sl.Keys[2] == 3, "NCIC #D3");
+               }
        }
 }