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