public void TestBlock02()
{
IntPtr number;
int value;
int[] values = new int[256];
// Create random values
for (int i = 0; i < values.Length; i++)
{
values[i] = new Random().Next(1, 65535);
}
// Create a mutable array
IntPtr array = objc_sendMsg_IntPtr(this.cls_NSMutableArray, this.sel_alloc);
Assert.AreNotEqual(IntPtr.Zero, array, "NSMutableArray allocation failed");
array = objc_sendMsg_IntPtr(array, this.sel_init);
Assert.AreNotEqual(IntPtr.Zero, array, "NSMutableArray initialization failed");
// Add elements
for (int i = 0; i < values.Length; i++)
{
number = objc_sendMsg_IntPtr_int(this.cls_NSNumber, this.sel_numberWithInt, values[i]);
Assert.AreNotEqual(IntPtr.Zero, number, "NSNumber '" + values[i] + "' initialization failed");
objc_sendMsg_void_IntPtr(array, this.sel_addObject, number);
}
// Checking the count
uint count;
if (ObjectiveCRuntime.Is64Bits)
{
count = (uint)objc_sendMsg_ulong(array, this.sel_count);
}
else
{
count = objc_sendMsg_uint(array, this.sel_count);
}
Assert.AreEqual(values.Length, count, "Array's count is wrong");
// Checking elements
for (int i = 0; i < values.Length; i++)
{
number = objc_sendMsg_IntPtr_uint(array, this.sel_objectAtIndex, (uint)i);
Assert.AreNotEqual(IntPtr.Zero, number, "Object at index " + i + " is nil");
value = objc_sendMsg_int(number, this.sel_intValue);
Assert.AreEqual(values[i], value, "Object at index " + i + " is wrong");
}
// Sort with a block: NSComparisonResult (^)(id, id)
IntPtr sorted;
Block block = this.GetBlock((Func<IntPtr, IntPtr, int>)delegate(IntPtr ptr1, IntPtr ptr2)
{
return objc_sendMsg_int_IntPtr(ptr1, this.sel_compare, ptr2);
});
{
Assert.AreNotEqual(IntPtr.Zero, block.NativePointer, "Block native structure is nil");
sorted = objc_sendMsg_IntPtr_IntPtr(array, this.sel_sortedArrayUsingComparator, block.NativePointer);
Assert.AreNotEqual(IntPtr.Zero, sorted, "Method call failed");
// Retain the result for the check
objc_sendMsg_IntPtr(sorted, this.sel_retain);
}
block.Dispose();
// Checking the count
if (ObjectiveCRuntime.Is64Bits)
{
count = (uint)objc_sendMsg_ulong(array, this.sel_count);
}
else
{
count = objc_sendMsg_uint(array, this.sel_count);
}
Assert.AreEqual(values.Length, count, "Array's count is wrong");
// Checking elements
Array.Sort(values);
for (int i = 0; i < values.Length; i++)
{
number = objc_sendMsg_IntPtr_uint(sorted, this.sel_objectAtIndex, (uint)i);
Assert.AreNotEqual(IntPtr.Zero, number, "Object at index " + i + " is nil");
value = objc_sendMsg_int(number, this.sel_intValue);
Assert.AreEqual(values[i], value, "Object at index " + i + " is wrong");
}
// Release the sorted array
objc_sendMsg_void(sorted, this.sel_release);
// Release the array
objc_sendMsg_void(array, this.sel_release);
}