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