1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Xunit; 6 7 namespace System.Tests 8 { 9 public class ConvertToSByteTests : ConvertTestBase<SByte> 10 { 11 [Fact] FromBoolean()12 public void FromBoolean() 13 { 14 Boolean[] testValues = { true, false }; 15 SByte[] expectedValues = { 1, 0 }; 16 Verify(Convert.ToSByte, testValues, expectedValues); 17 } 18 19 [Fact] FromByte()20 public void FromByte() 21 { 22 Byte[] testValues = { 100, 0 }; 23 SByte[] expectedValues = { 100, 0 }; 24 Verify(Convert.ToSByte, testValues, expectedValues); 25 26 Byte[] overflowValues = { Byte.MaxValue }; 27 VerifyThrows<OverflowException, Byte>(Convert.ToSByte, overflowValues); 28 } 29 30 [Fact] FromChar()31 public void FromChar() 32 { 33 Char[] testValues = { 'A', Char.MinValue, }; 34 SByte[] expectedValues = { 65, (SByte)Char.MinValue }; 35 Verify(Convert.ToSByte, testValues, expectedValues); 36 } 37 38 [Fact] FromDecimal()39 public void FromDecimal() 40 { 41 Decimal[] testValues = { 100m, -100m, 0m }; 42 SByte[] expectedValues = { 100, -100, 0 }; 43 Verify(Convert.ToSByte, testValues, expectedValues); 44 45 Decimal[] overflowValues = { Decimal.MaxValue, Decimal.MinValue }; 46 VerifyThrows<OverflowException, Decimal>(Convert.ToSByte, overflowValues); 47 } 48 49 [Fact] FromDouble()50 public void FromDouble() 51 { 52 Double[] testValues = { 100.0, -100.0, 0 }; 53 SByte[] expectedValues = { 100, -100, 0 }; 54 Verify(Convert.ToSByte, testValues, expectedValues); 55 56 Double[] overflowValues = { Double.MaxValue, Double.MinValue }; 57 VerifyThrows<OverflowException, Double>(Convert.ToSByte, overflowValues); 58 } 59 60 [Fact] FromInt16()61 public void FromInt16() 62 { 63 Int16[] testValues = { 100, -100, 0 }; 64 SByte[] expectedValues = { 100, -100, 0 }; 65 Verify(Convert.ToSByte, testValues, expectedValues); 66 67 Int64[] overflowValues = { Int64.MaxValue, Int64.MinValue }; 68 VerifyThrows<OverflowException, Int64>(Convert.ToSByte, overflowValues); 69 } 70 71 [Fact] FromInt32()72 public void FromInt32() 73 { 74 Int32[] testValues = { 100, -100, 0 }; 75 SByte[] expectedValues = { 100, -100, 0 }; 76 Verify(Convert.ToSByte, testValues, expectedValues); 77 78 Int32[] overflowValues = { Int32.MaxValue, Int32.MinValue }; 79 VerifyThrows<OverflowException, Int32>(Convert.ToSByte, overflowValues); 80 } 81 82 [Fact] FromInt64()83 public void FromInt64() 84 { 85 Int64[] testValues = { 100, -100, 0 }; 86 SByte[] expectedValues = { 100, -100, 0 }; 87 Verify(Convert.ToSByte, testValues, expectedValues); 88 89 Int64[] overflowValues = { Int64.MaxValue, Int64.MinValue }; 90 VerifyThrows<OverflowException, Int64>(Convert.ToSByte, overflowValues); 91 } 92 93 [Fact] FromObject()94 public void FromObject() 95 { 96 Object[] testValues = { null }; 97 SByte[] expectedValues = { 0 }; 98 VerifyFromObject(Convert.ToSByte, Convert.ToSByte, testValues, expectedValues); 99 100 Object[] invalidValues = { new Object(), DateTime.Now }; 101 VerifyFromObjectThrows<InvalidCastException>(Convert.ToSByte, Convert.ToSByte, invalidValues); 102 } 103 104 [Fact] FromSByte()105 public void FromSByte() 106 { 107 SByte[] testValues = { SByte.MaxValue, SByte.MinValue }; 108 SByte[] expectedValues = { SByte.MaxValue, SByte.MinValue }; 109 Verify(Convert.ToSByte, testValues, expectedValues); 110 } 111 112 [Fact] FromSingle()113 public void FromSingle() 114 { 115 Single[] testValues = { 100.0f, -100.0f, 0.0f, }; 116 SByte[] expectedValues = { 100, -100, 0, }; 117 Verify(Convert.ToSByte, testValues, expectedValues); 118 119 Single[] overflowValues = { Single.MaxValue, Single.MinValue }; 120 VerifyThrows<OverflowException, Single>(Convert.ToSByte, overflowValues); 121 } 122 123 [Fact] FromString()124 public void FromString() 125 { 126 String[] testValues = { "100", "-100", "0", SByte.MinValue.ToString(), SByte.MaxValue.ToString() }; 127 SByte[] expectedValues = { 100, -100, 0, SByte.MinValue, SByte.MaxValue }; 128 VerifyFromString(Convert.ToSByte, Convert.ToSByte, testValues, expectedValues); 129 130 String[] overflowValues = { Int16.MinValue.ToString(), Int16.MaxValue.ToString() }; 131 VerifyFromStringThrows<OverflowException>(Convert.ToSByte, Convert.ToSByte, overflowValues); 132 133 String[] formatExceptionValues = { "abba" }; 134 VerifyFromStringThrows<FormatException>(Convert.ToSByte, Convert.ToSByte, formatExceptionValues); 135 136 // Note: Only the Convert.ToSByte(String, IFormatProvider) overload throws an ArgumentNullException. 137 // This is inconsistent with the other numeric conversions, but fixing this behavior is not worth making 138 // a breaking change which will affect the desktop CLR. 139 Assert.Throws<ArgumentNullException>(() => Convert.ToSByte((String)null, TestFormatProvider.s_instance)); 140 } 141 142 [Fact] FromStringWithBase()143 public void FromStringWithBase() 144 { 145 String[] testValues = { null, null, null, null, "7f", "127", "177", "1111111", "80", "-128", "200", "10000000" }; 146 Int32[] testBases = { 10, 2, 8, 16, 16, 10, 8, 2, 16, 10, 8, 2 }; 147 SByte[] expectedValues = { 0, 0, 0, 0, SByte.MaxValue, SByte.MaxValue, SByte.MaxValue, SByte.MaxValue, SByte.MinValue, SByte.MinValue, SByte.MinValue, SByte.MinValue }; 148 VerifyFromStringWithBase(Convert.ToSByte, testValues, testBases, expectedValues); 149 150 String[] overflowValues = { "128", "-129", "111111111", "1FF", "777" }; 151 Int32[] overflowBases = { 10, 10, 2, 16, 8 }; 152 VerifyFromStringWithBaseThrows<OverflowException>(Convert.ToSByte, overflowValues, overflowBases); 153 154 String[] formatExceptionValues = { "12", "ffffffffffffffffffff" }; 155 Int32[] formatExceptionBases = { 2, 8 }; 156 VerifyFromStringWithBaseThrows<FormatException>(Convert.ToSByte, formatExceptionValues, formatExceptionBases); 157 158 String[] argumentExceptionValues = { "10", "11", "abba", "-ab" }; 159 Int32[] argumentExceptionBases = { -1, 3, 0, 16 }; 160 VerifyFromStringWithBaseThrows<ArgumentException>(Convert.ToSByte, argumentExceptionValues, argumentExceptionBases); 161 } 162 163 [Fact] FromUInt16()164 public void FromUInt16() 165 { 166 UInt16[] testValues = { 100, 0 }; 167 SByte[] expectedValues = { 100, 0 }; 168 Verify(Convert.ToSByte, testValues, expectedValues); 169 170 UInt16[] overflowValues = { UInt16.MaxValue }; 171 VerifyThrows<OverflowException, UInt16>(Convert.ToSByte, overflowValues); 172 } 173 174 [Fact] FromUInt32()175 public void FromUInt32() 176 { 177 UInt32[] testValues = { 100, 0 }; 178 SByte[] expectedValues = { 100, 0 }; 179 Verify(Convert.ToSByte, testValues, expectedValues); 180 181 UInt32[] overflowValues = { UInt32.MaxValue }; 182 VerifyThrows<OverflowException, UInt32>(Convert.ToSByte, overflowValues); 183 } 184 185 [Fact] FromUInt64()186 public void FromUInt64() 187 { 188 UInt64[] testValues = { 100, 0 }; 189 SByte[] expectedValues = { 100, 0 }; 190 Verify(Convert.ToSByte, testValues, expectedValues); 191 192 UInt64[] overflowValues = { UInt64.MaxValue }; 193 VerifyThrows<OverflowException, UInt64>(Convert.ToSByte, overflowValues); 194 } 195 } 196 } 197