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 ConvertToInt16Tests : ConvertTestBase<Int16> 10 { 11 [Fact] FromBoolean()12 public void FromBoolean() 13 { 14 Boolean[] testValues = { true, false }; 15 Int16[] expectedValues = { 1, 0 }; 16 Verify(Convert.ToInt16, testValues, expectedValues); 17 } 18 19 [Fact] FromByte()20 public void FromByte() 21 { 22 Byte[] testValues = { Byte.MaxValue, Byte.MinValue }; 23 Int16[] expectedValues = { 255, 0 }; 24 Verify(Convert.ToInt16, testValues, expectedValues); 25 } 26 27 [Fact] FromChar()28 public void FromChar() 29 { 30 Char[] testValues = { 'A', Char.MinValue, }; 31 Int16[] expectedValues = { 65, (Int16)Char.MinValue }; 32 Verify(Convert.ToInt16, testValues, expectedValues); 33 } 34 35 [Fact] FromDecimal()36 public void FromDecimal() 37 { 38 Decimal[] testValues = { 100m, -100m, 0m }; 39 Int16[] expectedValues = { 100, -100, 0 }; 40 Verify(Convert.ToInt16, testValues, expectedValues); 41 42 Decimal[] overflowValues = { Decimal.MaxValue, Decimal.MinValue }; 43 VerifyThrows<OverflowException, Decimal>(Convert.ToInt16, overflowValues); 44 } 45 46 [Fact] FromDouble()47 public void FromDouble() 48 { 49 Double[] testValues = { 100.0, -100.0, 0 }; 50 Int16[] expectedValues = { 100, -100, 0 }; 51 Verify(Convert.ToInt16, testValues, expectedValues); 52 53 Double[] overflowValues = { Double.MaxValue, Double.MinValue }; 54 VerifyThrows<OverflowException, Double>(Convert.ToInt16, overflowValues); 55 } 56 57 [Fact] FromInt16()58 public void FromInt16() 59 { 60 Int16[] testValues = { Int16.MaxValue, Int16.MinValue, 0 }; 61 Int16[] expectedValues = { Int16.MaxValue, Int16.MinValue, 0 }; 62 Verify(Convert.ToInt16, testValues, expectedValues); 63 } 64 65 [Fact] FromInt32()66 public void FromInt32() 67 { 68 Int32[] testValues = { 100, -100, 0 }; 69 Int16[] expectedValues = { 100, -100, 0 }; 70 Verify(Convert.ToInt16, testValues, expectedValues); 71 72 Int32[] overflowValues = { Int32.MaxValue, Int32.MinValue }; 73 VerifyThrows<OverflowException, Int32>(Convert.ToInt16, overflowValues); 74 } 75 76 [Fact] FromInt64()77 public void FromInt64() 78 { 79 Int64[] testValues = { 100, -100, 0 }; 80 Int16[] expectedValues = { 100, -100, 0 }; 81 Verify(Convert.ToInt16, testValues, expectedValues); 82 83 Int64[] overflowValues = { Int64.MaxValue, Int64.MinValue }; 84 VerifyThrows<OverflowException, Int64>(Convert.ToInt16, overflowValues); 85 } 86 87 [Fact] FromObject()88 public void FromObject() 89 { 90 Object[] testValues = { null }; 91 Int16[] expectedValues = { 0 }; 92 VerifyFromObject(Convert.ToInt16, Convert.ToInt16, testValues, expectedValues); 93 94 Object[] invalidValues = { new Object(), DateTime.Now }; 95 VerifyFromObjectThrows<InvalidCastException>(Convert.ToInt16, Convert.ToInt16, invalidValues); 96 } 97 98 [Fact] FromSByte()99 public void FromSByte() 100 { 101 SByte[] testValues = { 100, -100, 0 }; 102 Int16[] expectedValues = { 100, -100, 0 }; 103 Verify(Convert.ToInt16, testValues, expectedValues); 104 } 105 106 [Fact] FromSingle()107 public void FromSingle() 108 { 109 Single[] testValues = { 100.0f, -100.0f, 0.0f, }; 110 Int16[] expectedValues = { 100, -100, 0, }; 111 Verify(Convert.ToInt16, testValues, expectedValues); 112 113 Single[] overflowValues = { Single.MaxValue, Single.MinValue }; 114 VerifyThrows<OverflowException, Single>(Convert.ToInt16, overflowValues); 115 } 116 117 [Fact] FromString()118 public void FromString() 119 { 120 String[] testValues = { "100", "-100", "0", Int16.MinValue.ToString(), Int16.MaxValue.ToString(), null }; 121 Int16[] expectedValues = { 100, -100, 0, Int16.MinValue, Int16.MaxValue, 0 }; 122 VerifyFromString(Convert.ToInt16, Convert.ToInt16, testValues, expectedValues); 123 124 String[] overflowValues = { Int32.MinValue.ToString(), Int32.MaxValue.ToString() }; 125 VerifyFromStringThrows<OverflowException>(Convert.ToInt16, Convert.ToInt16, overflowValues); 126 127 String[] formatExceptionValues = { "abba" }; 128 VerifyFromStringThrows<FormatException>(Convert.ToInt16, Convert.ToInt16, formatExceptionValues); 129 } 130 131 [Fact] FromStringWithBase()132 public void FromStringWithBase() 133 { 134 String[] testValues = { null, null, null, null, "7fff", "32767", "77777", "111111111111111", "8000", "-32768", "100000", "1000000000000000" }; 135 Int32[] testBases = { 10, 2, 8, 16, 16, 10, 8, 2, 16, 10, 8, 2 }; 136 Int16[] expectedValues = { 0, 0, 0, 0, Int16.MaxValue, Int16.MaxValue, Int16.MaxValue, Int16.MaxValue, Int16.MinValue, Int16.MinValue, Int16.MinValue, Int16.MinValue }; 137 VerifyFromStringWithBase(Convert.ToInt16, testValues, testBases, expectedValues); 138 139 String[] overflowValues = { "32768", "-32769", "11111111111111111", "1FFFF", "777777" }; 140 Int32[] overflowBases = { 10, 10, 2, 16, 8 }; 141 VerifyFromStringWithBaseThrows<OverflowException>(Convert.ToInt16, overflowValues, overflowBases); 142 143 String[] formatExceptionValues = { "12", "ffffffffffffffffffff" }; 144 Int32[] formatExceptionBases = { 2, 8 }; 145 VerifyFromStringWithBaseThrows<FormatException>(Convert.ToInt16, formatExceptionValues, formatExceptionBases); 146 147 String[] argumentExceptionValues = { "10", "11", "abba", "-ab" }; 148 Int32[] argumentExceptionBases = { -1, 3, 0, 16 }; 149 VerifyFromStringWithBaseThrows<ArgumentException>(Convert.ToInt16, argumentExceptionValues, argumentExceptionBases); 150 } 151 152 [Fact] FromUInt16()153 public void FromUInt16() 154 { 155 UInt16[] testValues = { 100, 0 }; 156 Int16[] expectedValues = { 100, 0 }; 157 Verify(Convert.ToInt16, testValues, expectedValues); 158 159 UInt16[] overflowValues = { UInt16.MaxValue }; 160 VerifyThrows<OverflowException, UInt16>(Convert.ToInt16, overflowValues); 161 } 162 163 [Fact] FromUInt32()164 public void FromUInt32() 165 { 166 UInt32[] testValues = { 100, 0 }; 167 Int16[] expectedValues = { 100, 0 }; 168 Verify(Convert.ToInt16, testValues, expectedValues); 169 170 UInt32[] overflowValues = { UInt32.MaxValue }; 171 VerifyThrows<OverflowException, UInt32>(Convert.ToInt16, overflowValues); 172 } 173 174 [Fact] FromUInt64()175 public void FromUInt64() 176 { 177 UInt64[] testValues = { 100, 0 }; 178 Int16[] expectedValues = { 100, 0 }; 179 Verify(Convert.ToInt16, testValues, expectedValues); 180 181 UInt64[] overflowValues = { UInt64.MaxValue }; 182 VerifyThrows<OverflowException, UInt64>(Convert.ToInt16, overflowValues); 183 } 184 } 185 } 186