public bool PosTest2() { bool retVal = true; Array array; int length; MyStruct element; int index; int newIndex; MyStructComparer msc = new MyStructComparer(); TestLibrary.TestFramework.BeginScenario("PosTest2: Array.BinarySearch(Array, int, int, object, IComparer) non-primitive type (not derived from object)"); try { // creat the array length = (TestLibrary.Generator.GetInt32(-55) % (c_MAX_SIZE - c_MIN_SIZE)) + c_MIN_SIZE; array = Array.CreateInstance(typeof(MyStruct), length); element = new MyStruct(TestLibrary.Generator.GetSingle(-55)); // fill the array for (int i = 0; i < array.Length; i++) { do { array.SetValue(new MyStruct(TestLibrary.Generator.GetSingle(-55)), i); }while(element.f == ((MyStruct)array.GetValue(i)).f); } // set the lucky index index = TestLibrary.Generator.GetInt32(-55) % length; // set the value array.SetValue(element, index); Array.Sort(array); newIndex = Array.BinarySearch(array, 0, array.Length, (object)element, msc); if (element.f != ((MyStruct)array.GetValue(newIndex)).f) { TestLibrary.TestFramework.LogError("002", "Unexpected value: Expected(" + element.f + ") Actual(" + ((MyStruct)array.GetValue(newIndex)).f + ")"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; Array array; int length; MyStruct element; int index; int newIndex; MyStructComparer msc = new MyStructComparer(); TestLibrary.TestFramework.BeginScenario("PosTest2: Array.BinarySearch(Array, int, int, object, IComparer) non-primitive type (not derived from object)"); try { // creat the array length = (TestLibrary.Generator.GetInt32(-55) % (c_MAX_SIZE-c_MIN_SIZE)) + c_MIN_SIZE; array = Array.CreateInstance(typeof(MyStruct), length); element = new MyStruct(TestLibrary.Generator.GetSingle(-55)); // fill the array for (int i=0; i<array.Length; i++) { do { array.SetValue(new MyStruct(TestLibrary.Generator.GetSingle(-55)), i); } while(element.f == ((MyStruct)array.GetValue(i)).f); } // set the lucky index index = TestLibrary.Generator.GetInt32(-55) % length; // set the value array.SetValue( element, index); Array.Sort(array); newIndex = Array.BinarySearch(array, 0, array.Length, (object)element, msc); if (element.f != ((MyStruct)array.GetValue(newIndex)).f) { TestLibrary.TestFramework.LogError("002", "Unexpected value: Expected(" + element.f + ") Actual(" + ((MyStruct)array.GetValue(newIndex)).f + ")"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e); retVal = false; } return retVal; }