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