[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.


--- 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 Details: Windows XP SP1
+Status: NEW   
+Priority: Normal
+Component: System
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: cmyers@austin.rr.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+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
+System.Char:CompareTo (object)
+in [0x00063] (at
+System.Collections.Comparer:Compare (object,object)
+in [0x0003a] (at
+--- End of inner exception stack trace ---
+in [0x00084] (at
+in [0x00061] (at
+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.
+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.
+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