[Mono-bugs] [Bug 70725][Nor] New - Array.BinarySearch does not respect IComparable implementation
bugzilla-daemon@bugzilla.ximian.com
bugzilla-daemon@bugzilla.ximian.com
Tue, 21 Dec 2004 11:36:22 -0500 (EST)
Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.
Changed by cmyers@austin.rr.com.
http://bugzilla.ximian.com/show_bug.cgi?id=70725
--- shadow/70725 2004-12-21 11:36:22.000000000 -0500
+++ shadow/70725.tmp.9577 2004-12-21 11:36:22.000000000 -0500
@@ -0,0 +1,121 @@
+Bug#: 70725
+Product: Mono: Class Libraries
+Version: 1.0
+OS:
+OS Details: Windows XP SP1
+Status: NEW
+Resolution:
+Severity:
+Priority: Normal
+Component: System
+AssignedTo: mono-bugs@ximian.com
+ReportedBy: cmyers@austin.rr.com
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL:
+Cc:
+Summary: Array.BinarySearch does not respect IComparable implementation
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+
+Array.BinarySearch does not appear to respect that the items in the array
+implement IComparable.
+
+Steps to reproduce the problem:
+1. Create a struct which implements IComparable to compare against, for
+example, a char.
+2. Create an array of these structs
+3. Call Array.BinarySearch using that array and a char ('a' for example).
+NOTE: See below for code example
+
+Actual Results:
+System.InvalidOperationException: Comparer threw an exception. --->
+System.ArgumentException: Value is not a System.Char
+
+in [0x00022] (at
+C:\cygwin\download\mono-1.0.4\mcs\class\corlib\System\Char.cs:87)
+System.Char:CompareTo (object)
+in [0x00063] (at
+C:\cygwin\download\mono-1.0.4\mcs\class\corlib\System.Collections\Comparer.cs:86)
+System.Collections.Comparer:Compare (object,object)
+in [0x0003a] (at
+C:\cygwin\download\mono-1.0.4\mcs\class\corlib\System\Array.cs:608)
+System.Array:DoBinarySearch
+(System.Array,int,int,object,System.Collections.IComparer)
+--- End of inner exception stack trace ---
+
+in [0x00084] (at
+C:\cygwin\download\mono-1.0.4\mcs\class\corlib\System\Array.cs:618)
+System.Array:DoBinarySearch
+(System.Array,int,int,object,System.Collections.IComparer)
+in [0x00061] (at
+C:\cygwin\download\mono-1.0.4\mcs\class\corlib\System\Array.cs:521)
+System.Array:BinarySearch (System.Array,object)
+
+Expected Results:
+It should search the array, calling the correct IComparable.CompareTo()
+method on the objects in the array and return:
+
+The index of the specified value in the specified array, if value is found.
+
+-or-
+
+A negative number, which is the bitwise complement of the index of the
+first element that is larger than value, if value is not found and value is
+less than one or more elements in array.
+
+-or-
+
+A negative number, which is the bitwise complement of (the index of the
+last element + 1), if value is not found and value is greater than any of
+the elements in array.
+
+
+How often does this happen?
+Every time.
+
+Additional Information:
+Here's an example struct:
+public struct CharTouple : IComparable {
+ public char baseChar, mappedChar;
+
+ public CharTouple( char inBaseChar, char inMappedChar ){
+ this.baseChar = inBaseChar;
+ this.mappedChar = inMappedChar;
+ }
+
+ public int CompareTo(object obj){
+ if( obj is CharTouple )
+ return baseChar.CompareTo( ((CharTouple)obj).baseChar );
+ else
+ return baseChar.CompareTo( obj );
+ }
+}
+
+Here's an example test using the above struct:
+
+public class MonoTest{
+ public static void Main(string[] args){
+ try{
+ CharTouple[] touples = new CharTouple[]{
+ new CharTouple('a', 'z'), new CharTouple('b', 'y'), new CharTouple('c',
+'x') };
+
+ int idx = Array.BinarySearch( touples, 'b' );
+
+ Console.WriteLine("Found char at index {0}", idx);
+ }
+ catch( Exception ex )
+ {
+ Console.WriteLine(ex);
+ }
+ }
+}
+
+It should print:
+
+"Found char at index 1"
+
+But instead it throws an exception