# -*- coding: utf-8 -*-"""Test support for managed arrays."""import clrimport Python.Test as Testimport Systemimport pytestfrom collections import UserListfrom System import Single as float32def test_public_array():"""Test public arrays."""ob = Test.PublicArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4items[0] = 8assert items[0] == 8items[4] = 9assert items[4] == 9items[-4] = 0assert items[-4] == 0items[-1] = 4assert items[-1] == 4def test_protected_array():"""Test protected arrays."""ob = Test.ProtectedArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4items[0] = 8assert items[0] == 8items[4] = 9assert items[4] == 9items[-4] = 0assert items[-4] == 0items[-1] = 4assert items[-1] == 4def test_internal_array():"""Test internal arrays."""with pytest.raises(AttributeError):ob = Test.InternalArrayTest()_ = ob.itemsdef test_private_array():"""Test private arrays."""with pytest.raises(AttributeError):ob = Test.PrivateArrayTest()_ = ob.itemsdef test_array_bounds_checking():"""Test array bounds checking."""ob = Test.Int32ArrayTest()items = ob.itemsassert items[0] == 0assert items[1] == 1assert items[2] == 2assert items[3] == 3assert items[4] == 4assert items[-5] == 0assert items[-4] == 1assert items[-3] == 2assert items[-2] == 3assert items[-1] == 4with pytest.raises(IndexError):ob = Test.Int32ArrayTest()_ = ob.items[5]with pytest.raises(IndexError):ob = Test.Int32ArrayTest()ob.items[5] = 0with pytest.raises(IndexError):ob = Test.Int32ArrayTest()items[-6]with pytest.raises(IndexError):ob = Test.Int32ArrayTest()items[-6] = 0def test_array_contains():"""Test array support for __contains__."""ob = Test.Int32ArrayTest()items = ob.itemsassert 0 in itemsassert 1 in itemsassert 2 in itemsassert 3 in itemsassert 4 in itemsassert not (5 in items) # "H:\Python27\Lib\unittest\case.py", line 592, in deprecated_func,assert not (-1 in items) # TypeError: int() argument must be a string or a number, not 'NoneType'assert not (None in items) # which threw ^ here which is a little odd.# But when run from runtests.py. Not when this module ran by itself.def test_boolean_array():"""Test boolean arrays."""ob = Test.BooleanArrayTest()items = ob.itemsassert len(items) == 5assert items[0] is Trueassert items[1] is Falseassert items[2] is Trueassert items[3] is Falseassert items[4] is Trueitems[0] = Falseassert items[0] is Falseitems[0] = Trueassert items[0] is Truewith pytest.raises(TypeError):ob = Test.ByteArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.ByteArrayTest()ob[0] = "wrong"def test_byte_array():"""Test byte arrays."""ob = Test.ByteArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 255min_ = 0items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.ByteArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.ByteArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.ByteArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.ByteArrayTest()ob[0] = "wrong"def test_sbyte_array():"""Test sbyte arrays."""ob = Test.SByteArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 127min_ = -128items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.SByteArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.SByteArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.SByteArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.SByteArrayTest()ob[0] = "wrong"def test_char_array():"""Test char arrays."""ob = Test.CharArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 'a'assert items[4] == 'e'max_ = chr(65535)min_ = chr(0)items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(TypeError):ob = Test.CharArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.CharArrayTest()ob[0] = "wrong"def test_int16_array():"""Test Int16 arrays."""ob = Test.Int16ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 32767min_ = -32768items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.Int16ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.Int16ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.Int16ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.Int16ArrayTest()ob[0] = "wrong"def test_int32_array():"""Test Int32 arrays."""ob = Test.Int32ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 2147483647min_ = -2147483648items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.Int32ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.Int32ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.Int32ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.Int32ArrayTest()ob[0] = "wrong"def test_int64_array():"""Test Int64 arrays."""ob = Test.Int64ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 9223372036854775807min_ = -9223372036854775808items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.Int64ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.Int64ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.Int64ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.Int64ArrayTest()ob[0] = "wrong"def test_uint16_array():"""Test UInt16 arrays."""ob = Test.UInt16ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 65535min_ = 0items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.UInt16ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.UInt16ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.UInt16ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.UInt16ArrayTest()ob[0] = "wrong"def test_uint32_array():"""Test UInt32 arrays."""ob = Test.UInt32ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 4294967295min_ = 0items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.UInt32ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.UInt32ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.UInt32ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.UInt32ArrayTest()ob[0] = "wrong"def test_uint64_array():"""Test UInt64 arrays."""ob = Test.UInt64ArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0assert items[4] == 4max_ = 18446744073709551615min_ = 0items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(OverflowError):ob = Test.UInt64ArrayTest()ob.items[0] = max_ + 1with pytest.raises(OverflowError):ob = Test.UInt64ArrayTest()ob.items[0] = min_ - 1with pytest.raises(TypeError):ob = Test.UInt64ArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.UInt64ArrayTest()ob[0] = "wrong"def test_single_array():"""Test Single arrays."""ob = Test.SingleArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0.0assert items[4] == 4.0max_ = float32(3.402823e38)min_ = float32(-3.402823e38)items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(TypeError):ob = Test.SingleArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.SingleArrayTest()ob[0] = "wrong"def test_double_array():"""Test Double arrays."""ob = Test.DoubleArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == 0.0assert items[4] == 4.0max_ = 1.7976931348623157e308min_ = -1.7976931348623157e308items[0] = max_assert items[0] == max_items[0] = min_assert items[0] == min_items[-4] = max_assert items[-4] == max_items[-1] = min_assert items[-1] == min_with pytest.raises(TypeError):ob = Test.DoubleArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.DoubleArrayTest()ob[0] = "wrong"def test_decimal_array():"""Test Decimal arrays."""ob = Test.DecimalArrayTest()items = ob.itemsfrom System import Decimalmax_d = Decimal.Parse("79228162514264337593543950335")min_d = Decimal.Parse("-79228162514264337593543950335")assert len(items) == 5assert items[0] == Decimal(0)assert items[4] == Decimal(4)items[0] = max_dassert items[0] == max_ditems[0] = min_dassert items[0] == min_ditems[-4] = max_dassert items[-4] == max_ditems[-1] = min_dassert items[-1] == min_dwith pytest.raises(TypeError):ob = Test.DecimalArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.DecimalArrayTest()ob[0] = "wrong"def test_string_array():"""Test String arrays."""ob = Test.StringArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == '0'assert items[4] == '4'items[0] = "spam"assert items[0] == "spam"items[0] = "eggs"assert items[0] == "eggs"items[-4] = "spam"assert items[-4] == "spam"items[-1] = "eggs"assert items[-1] == "eggs"with pytest.raises(TypeError):ob = Test.StringArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.Int64ArrayTest()ob[0] = 0def test_enum_array():"""Test enum arrays."""from Python.Test import ShortEnumob = Test.EnumArrayTest()items = ob.itemsassert len(items) == 5assert items[0] == ShortEnum.Zeroassert items[4] == ShortEnum.Fouritems[0] = ShortEnum.Fourassert items[0] == ShortEnum.Fouritems[0] = ShortEnum.Zeroassert items[0] == ShortEnum.Zeroitems[-4] = ShortEnum.Fourassert items[-4] == ShortEnum.Fouritems[-1] = ShortEnum.Zeroassert items[-1] == ShortEnum.Zerowith pytest.raises(TypeError):ob = Test.EnumArrayTest()ob.items[0] = 99with pytest.raises(TypeError):ob = Test.EnumArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.EnumArrayTest()ob[0] = "wrong"def test_object_array():"""Test ob arrays."""from Python.Test import Spamob = Test.ObjectArrayTest()items = ob.itemsassert len(items) == 5assert items[0].GetValue() == "0"assert items[4].GetValue() == "4"items[0] = Spam("4")assert items[0].GetValue() == "4"items[0] = Spam("0")assert items[0].GetValue() == "0"items[-4] = Spam("4")assert items[-4].GetValue() == "4"items[-1] = Spam("0")assert items[-1].GetValue() == "0"items[0] = 99assert items[0] == 99items[0] = Noneassert items[0] is Nonewith pytest.raises(TypeError):ob = Test.ObjectArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.ObjectArrayTest()ob.items["wrong"] = "wrong"def test_null_array():"""Test null arrays."""ob = Test.NullArrayTest()items = ob.itemsassert len(items) == 5assert items[0] is Noneassert items[4] is Noneitems[0] = "spam"assert items[0] == "spam"items[0] = Noneassert items[0] is Noneitems[-4] = "spam"assert items[-4] == "spam"items[-1] = Noneassert items[-1] is Noneempty = ob.emptyassert len(empty) == 0with pytest.raises(TypeError):ob = Test.NullArrayTest()_ = ob.items["wrong"]def test_interface_array():"""Test interface arrays."""from Python.Test import Spamob = Test.InterfaceArrayTest()items = ob.itemsassert len(items) == 5assert items[0].GetValue() == "0"assert items[4].GetValue() == "4"items[0] = Spam("4")assert items[0].GetValue() == "4"items[0] = Spam("0")assert items[0].GetValue() == "0"items[-4] = Spam("4")assert items[-4].GetValue() == "4"items[-1] = Spam("0")assert items[-1].GetValue() == "0"items[0] = Noneassert items[0] is Nonewith pytest.raises(TypeError):ob = Test.InterfaceArrayTest()ob.items[0] = 99with pytest.raises(TypeError):ob = Test.InterfaceArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.InterfaceArrayTest()ob.items["wrong"] = "wrong"def test_typed_array():"""Test typed arrays."""from Python.Test import Spamob = Test.TypedArrayTest()items = ob.itemsassert len(items) == 5assert items[0].GetValue() == "0"assert items[4].GetValue() == "4"items[0] = Spam("4")assert items[0].GetValue() == "4"items[0] = Spam("0")assert items[0].GetValue() == "0"items[-4] = Spam("4")assert items[-4].GetValue() == "4"items[-1] = Spam("0")assert items[-1].GetValue() == "0"items[0] = Noneassert items[0] is Nonewith pytest.raises(TypeError):ob = Test.TypedArrayTest()ob.items[0] = 99with pytest.raises(TypeError):ob = Test.TypedArrayTest()_ = ob.items["wrong"]with pytest.raises(TypeError):ob = Test.TypedArrayTest()ob.items["wrong"] = Spam("0")with pytest.raises(TypeError):ob = Test.TypedArrayTest()_ = ob.items[0.5]with pytest.raises(TypeError):ob = Test.TypedArrayTest()ob.items[0.5] = Spam("0")def test_multi_dimensional_array():"""Test multi-dimensional arrays."""ob = Test.MultiDimensionalArrayTest()items = ob.itemsassert len(items) == 25assert items[0, 0] == 0assert items[0, 1] == 1assert items[0, 2] == 2assert items[0, 3] == 3assert items[0, 4] == 4assert items[1, 0] == 5assert items[1, 1] == 6assert items[1, 2] == 7assert items[1, 3] == 8assert items[1, 4] == 9assert items[2, 0] == 10assert items[2, 1] == 11assert items[2, 2] == 12assert items[2, 3] == 13assert items[2, 4] == 14assert items[3, 0] == 15assert items[3, 1] == 16assert items[3, 2] == 17assert items[3, 3] == 18assert items[3, 4] == 19assert items[4, 0] == 20assert items[4, 1] == 21assert items[4, 2] == 22assert items[4, 3] == 23assert items[4, 4] == 24max_ = 2147483647min_ = -2147483648items[0, 0] = max_assert items[0, 0] == max_items[0, 0] = min_assert items[0, 0] == min_items[-4, 0] = max_assert items[-4, 0] == max_items[-1, -1] = min_assert items[-1, -1] == min_with pytest.raises(OverflowError):ob = Test.MultiDimensionalArrayTest()ob.items[0, 0] = max_ + 1with pytest.raises(OverflowError):ob = Test.MultiDimensionalArrayTest()ob.items[0, 0] = min_ - 1with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()_ = ob.items["wrong", 0]with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()ob.items[0, 0] = "wrong"with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()ob["0", 0] = 0with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()ob.items["0", 0] = 0with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()_ = ob.items[0.5, 0]with pytest.raises(TypeError):ob = Test.MultiDimensionalArrayTest()ob.items[0.5, 0] = 0def test_array_iteration():"""Test array iteration."""items = Test.Int32ArrayTest().itemsfor i in items:assert (i > -1) and (i < 5)items = Test.NullArrayTest().itemsfor i in items:assert i is Noneempty = Test.NullArrayTest().emptyfor i in empty:raise TypeError('iteration over empty array')def test_tuple_array_conversion():"""Test conversion of tuples to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = []for i in range(10):items.append(Spam(str(i)))items = tuple(items)result = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert len(result) == 10def test_tuple_nested_array_conversion():"""Test conversion of tuples to array-of-array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = []for i in range(10):subs = []for _ in range(10):subs.append(Spam(str(i)))items.append(tuple(subs))items = tuple(items)result = ArrayConversionTest.EchoRangeAA(items)assert len(result) == 10assert len(result[0]) == 10assert result[0][0].__class__ == Spamdef test_list_array_conversion():"""Test conversion of lists to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = []for i in range(10):items.append(Spam(str(i)))result = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert len(result) == 10def test_list_nested_array_conversion():"""Test conversion of lists to array-of-array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = []for i in range(10):subs = []for _ in range(10):subs.append(Spam(str(i)))items.append(subs)result = ArrayConversionTest.EchoRangeAA(items)assert len(result) == 10assert len(result[0]) == 10assert result[0][0].__class__ == Spamdef test_sequence_array_conversion():"""Test conversion of sequence-like obs to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = UserList()for i in range(10):items.append(Spam(str(i)))result = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert len(result) == 10def test_sequence_nested_array_conversion():"""Test conversion of sequences to array-of-array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamitems = UserList()for i in range(10):subs = UserList()for _ in range(10):subs.append(Spam(str(i)))items.append(subs)result = ArrayConversionTest.EchoRangeAA(items)assert len(result) == 10assert len(result[0]) == 10assert result[0][0].__class__ == Spamdef test_tuple_array_conversion_type_checking():"""Test error handling for tuple conversion to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spam# This should work, because null / None is a valid value in an# array of reference types.items = []for i in range(10):items.append(Spam(str(i)))items[1] = Noneitems = tuple(items)result = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert result[1] is Noneassert len(result) == 10with pytest.raises(TypeError):temp = list(items)temp[1] = 1_ = ArrayConversionTest.EchoRange(tuple(temp))with pytest.raises(TypeError):temp = list(items)temp[1] = "spam"_ = ArrayConversionTest.EchoRange(tuple(temp))def test_list_array_conversion_type_checking():"""Test error handling for list conversion to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spam# This should work, because null / None is a valid value in an# array of reference types.items = []for i in range(10):items.append(Spam(str(i)))items[1] = Noneresult = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert result[1] is Noneassert len(result) == 10with pytest.raises(TypeError):items[1] = 1_ = ArrayConversionTest.EchoRange(items)with pytest.raises(TypeError):items[1] = "spam"_ = ArrayConversionTest.EchoRange(items)def test_sequence_array_conversion_type_checking():"""Test error handling for sequence conversion to array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spam# This should work, because null / None is a valid value in an# array of reference types.items = UserList()for i in range(10):items.append(Spam(str(i)))items[1] = Noneresult = ArrayConversionTest.EchoRange(items)assert result[0].__class__ == Spamassert result[1] is Noneassert len(result) == 10with pytest.raises(TypeError):items[1] = 1_ = ArrayConversionTest.EchoRange(items)with pytest.raises(TypeError):items[1] = "spam"_ = ArrayConversionTest.EchoRange(items)def test_md_array_conversion():"""Test passing of multi-dimensional array arguments."""from Python.Test import ArrayConversionTestfrom Python.Test import Spamfrom System import Array# Currently, the runtime does not support automagic conversion of# Python sequences to true multi-dimensional arrays (though it# does support arrays-of-arrays). This test exists mostly as an# example of how a multi-dimensional array can be created and used# with managed code from Python.items = Array.CreateInstance(Spam, 5, 5)for i in range(5):for n in range(5):items.SetValue(Spam(str((i, n))), (i, n))result = ArrayConversionTest.EchoRangeMD(items)assert len(result) == 25assert result[0, 0].__class__ == Spamassert result[0, 0].__class__ == Spamdef test_boxed_value_type_mutation_result():"""Test behavior of boxed value types."""# This test actually exists mostly as documentation of an important# concern when dealing with value types. Python does not have any# value type semantics that can be mapped to the CLR, so it is easy# to accidentally write code like the following which is not really# mutating value types in-place but changing boxed copies.from System import Arrayfrom Python.Test import Pointitems = Array.CreateInstance(Point, 5)for i in range(5):items[i] = Point(i, i)for i in range(5):# Boxed items, so set_attr will not change the array member.assert items[i].X == iassert items[i].Y == iitems[i].X = i + 1items[i].Y = i + 1assert items[i].X == iassert items[i].Y == ifor i in range(5):# Demonstrates the workaround that will change the members.assert items[i].X == iassert items[i].Y == iitem = items[i]item.X = i + 1item.Y = i + 1items[i] = itemassert items[i].X == i + 1assert items[i].Y == i + 1def test_create_array_from_shape():from System import Arrayvalue = Array[int](3)assert value[1] == 0assert value.Length == 3value = Array[int](3, 4)assert value[1, 1] == 0assert value.GetLength(0) == 3assert value.GetLength(1) == 4with pytest.raises(ValueError):Array[int](-1)with pytest.raises(TypeError):Array[int]('1')with pytest.raises(ValueError):Array[int](-1, -1)with pytest.raises(TypeError):Array[int]('1', '1')def test_special_array_creation():"""Test using the Array[<type>] syntax for creating arrays."""from Python.Test import ISayHello1, InterfaceTest, ShortEnumfrom System import Arrayinst = InterfaceTest()value = Array[System.Boolean]([True, True])assert value[0] is Trueassert value[1] is Trueassert value.Length == 2value = Array[bool]([True, True])assert value[0] is Trueassert value[1] is Trueassert value.Length == 2value = Array[System.Byte]([0, 255])assert value[0] == 0assert value[1] == 255assert value.Length == 2value = Array[System.SByte]([0, 127])assert value[0] == 0assert value[1] == 127assert value.Length == 2value = Array[System.Char]([u'A', u'Z'])assert value[0] == u'A'assert value[1] == u'Z'assert value.Length == 2value = Array[System.Char]([0, 65535])assert value[0] == chr(0)assert value[1] == chr(65535)assert value.Length == 2value = Array[System.Int16]([0, 32767])assert value[0] == 0assert value[1] == 32767assert value.Length == 2value = Array[System.Int32]([0, 2147483647])assert value[0] == 0assert value[1] == 2147483647assert value.Length == 2value = Array[int]([0, 2147483647])assert value[0] == 0assert value[1] == 2147483647assert value.Length == 2value = Array[System.Int64]([0, 9223372036854775807])assert value[0] == 0assert value[1] == 9223372036854775807assert value.Length == 2value = Array[System.UInt16]([0, 65000])assert value[0] == 0assert value[1] == 65000assert value.Length == 2value = Array[System.UInt32]([0, 4294967295])assert value[0] == 0assert value[1] == 4294967295assert value.Length == 2value = Array[System.UInt64]([0, 18446744073709551615])assert value[0] == 0assert value[1] == 18446744073709551615assert value.Length == 2value = Array[System.Single]([0.0, 3.402823e38])assert value[0] == 0.0assert value[1] == System.Single(3.402823e38)assert value.Length == 2value = Array[System.Double]([0.0, 1.7976931348623157e308])assert value[0] == 0.0assert value[1] == 1.7976931348623157e308assert value.Length == 2value = Array[float]([0.0, 1.7976931348623157e308])assert value[0] == 0.0assert value[1] == 1.7976931348623157e308assert value.Length == 2value = Array[System.Decimal]([System.Decimal.Zero, System.Decimal.One])assert value[0] == System.Decimal.Zeroassert value[1] == System.Decimal.Oneassert value.Length == 2value = Array[System.String](["one", "two"])assert value[0] == "one"assert value[1] == "two"assert value.Length == 2value = Array[str](["one", "two"])assert value[0] == "one"assert value[1] == "two"assert value.Length == 2value = Array[ShortEnum]([ShortEnum.Zero, ShortEnum.One])assert value[0] == ShortEnum.Zeroassert value[1] == ShortEnum.Oneassert value.Length == 2value = Array[System.Object]([inst, inst])assert value[0].__class__ == inst.__class__assert value[1].__class__ == inst.__class__assert value.Length == 2value = Array[InterfaceTest]([inst, inst])assert value[0].__class__ == inst.__class__assert value[1].__class__ == inst.__class__assert value.Length == 2iface_class = ISayHello1(inst).__class__value = Array[ISayHello1]([inst, inst])assert value[0].__class__ == iface_classassert value[1].__class__ == iface_classassert value.Length == 2inst = System.Exception("badness")value = Array[System.Exception]([inst, inst])assert value[0].__class__ == inst.__class__assert value[1].__class__ == inst.__class__assert value.Length == 2def test_array_abuse():"""Test array abuse."""_class = Test.PublicArrayTestob = Test.PublicArrayTest()with pytest.raises(AttributeError):del _class.__getitem__with pytest.raises(AttributeError):del ob.__getitem__with pytest.raises(AttributeError):del _class.__setitem__with pytest.raises(AttributeError):del ob.__setitem__with pytest.raises(TypeError):Test.PublicArrayTest.__getitem__(0, 0)with pytest.raises(AttributeError):Test.PublicArrayTest.__setitem__(0, 0, 0)with pytest.raises(KeyError):Test.PublicArrayTest.__dict__['__getitem__']with pytest.raises(KeyError):Test.PublicArrayTest.__dict__['__setitem__']def test_iterator_to_array():from System import Array, Stringd = {"a": 1, "b": 2, "c": 3}keys_iterator = iter(d.keys())arr = Array[String](keys_iterator)Array.Sort(arr)assert arr[0] == "a"assert arr[1] == "b"assert arr[2] == "c"def test_dict_keys_to_array():from System import Array, Stringd = {"a": 1, "b": 2, "c": 3}d_keys = d.keys()arr = Array[String](d_keys)Array.Sort(arr)assert arr[0] == "a"assert arr[1] == "b"assert arr[2] == "c"
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。