-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
IntegerTypeTests.cs
223 lines (194 loc) · 8.82 KB
/
IntegerTypeTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.VisualBasic.Tests;
using System;
using System.Collections.Generic;
using Xunit;
namespace Microsoft.VisualBasic.CompilerServices.Tests
{
public class IntegerTypeTests
{
[Theory]
[MemberData(nameof(FromObject_TestData))]
[MemberData(nameof(FromString_TestData))]
public void FromObject(object value, int expected)
{
Assert.Equal(expected, IntegerType.FromObject(value));
}
// The following conversions are not supported.
[Theory]
[MemberData(nameof(FromObject_NotSupported_TestData))]
public void FromObject_NotSupported(object value)
{
Assert.Throws<InvalidCastException>(() => IntegerType.FromObject(value));
}
[Theory]
[MemberData(nameof(FromObject_Invalid_TestData))]
public void FromObject_ThrowsInvalidCastException(object value)
{
Assert.Throws<InvalidCastException>(() => IntegerType.FromObject(value));
}
[Theory]
[MemberData(nameof(FromObject_Overflow_TestData))]
public void FromObject_ThrowsOverflowException(object value)
{
Assert.Throws<OverflowException>(() => IntegerType.FromObject(value));
}
[Theory]
[MemberData(nameof(FromString_TestData))]
public void FromString(string value, int expected)
{
Assert.Equal(expected, IntegerType.FromString(value));
}
[Theory]
[MemberData(nameof(FromString_Invalid_TestData))]
public void FromString_ThrowsInvalidCastException(string value)
{
Assert.Throws<InvalidCastException>(() => IntegerType.FromString(value));
}
[Theory]
[MemberData(nameof(FromString_Overflow_TestData))]
public void FromString_ThrowsOverflowException(string value)
{
Assert.Throws<OverflowException>(() => IntegerType.FromString(value));
}
public static IEnumerable<object[]> FromObject_TestData()
{
// byte.
yield return new object[] { byte.MinValue, 0 };
yield return new object[] { (byte)1, 1 };
yield return new object[] { byte.MaxValue, 255 };
yield return new object[] { (ByteEnum)byte.MinValue, 0 };
yield return new object[] { (ByteEnum)1, 1 };
yield return new object[] { (ByteEnum)byte.MaxValue, 255 };
// short.
yield return new object[] { short.MinValue, -32768 };
yield return new object[] { (short)(-1), -1 };
yield return new object[] { (short)0, 0 };
yield return new object[] { (short)1, 1 };
yield return new object[] { short.MaxValue, 32767 };
yield return new object[] { (ShortEnum)short.MinValue, -32768 };
yield return new object[] { (ShortEnum)(-1), -1 };
yield return new object[] { (ShortEnum)0, 0 };
yield return new object[] { (ShortEnum)1, 1 };
yield return new object[] { (ShortEnum)short.MaxValue, 32767 };
// int.
yield return new object[] { int.MinValue, int.MinValue };
yield return new object[] { -1, -1 };
yield return new object[] { 0, 0 };
yield return new object[] { 1, 1 };
yield return new object[] { int.MaxValue, int.MaxValue };
yield return new object[] { (IntEnum)int.MinValue, int.MinValue };
yield return new object[] { (IntEnum)(-1), -1 };
yield return new object[] { (IntEnum)0, 0 };
yield return new object[] { (IntEnum)1, 1 };
yield return new object[] { (IntEnum)int.MaxValue, int.MaxValue };
// long.
yield return new object[] { (long)(-1), -1 };
yield return new object[] { (long)0, 0 };
yield return new object[] { (long)1, 1 };
yield return new object[] { (LongEnum)(-1), -1 };
yield return new object[] { (LongEnum)0, 0 };
yield return new object[] { (LongEnum)1, 1 };
// float.
yield return new object[] { (float)(-1), -1 };
yield return new object[] { (float)0, 0 };
yield return new object[] { (float)1, 1 };
// double.
yield return new object[] { (double)(-1), -1 };
yield return new object[] { (double)0, 0 };
yield return new object[] { (double)1, 1 };
// decimal.
yield return new object[] { (decimal)(-1), -1 };
yield return new object[] { (decimal)0, 0 };
yield return new object[] { (decimal)1, 1 };
// bool.
yield return new object[] { true, -1 };
yield return new object[] { false, 0 };
// null.
yield return new object[] { null, 0 };
}
public static IEnumerable<object[]> FromObject_NotSupported_TestData()
{
// sbyte.
yield return new object[] { sbyte.MinValue };
yield return new object[] { (sbyte)(-1) };
yield return new object[] { (sbyte)0 };
yield return new object[] { (sbyte)1 };
yield return new object[] { sbyte.MaxValue };
yield return new object[] { (SByteEnum)sbyte.MinValue };
yield return new object[] { (SByteEnum)(-1) };
yield return new object[] { (SByteEnum)0 };
yield return new object[] { (SByteEnum)1 };
yield return new object[] { (SByteEnum)sbyte.MaxValue };
// ushort.
yield return new object[] { ushort.MinValue };
yield return new object[] { (ushort)1 };
yield return new object[] { ushort.MaxValue };
yield return new object[] { (UShortEnum)ushort.MinValue };
yield return new object[] { (UShortEnum)1 };
yield return new object[] { (UShortEnum)ushort.MaxValue };
// uint.
yield return new object[] { uint.MinValue };
yield return new object[] { (uint)1 };
yield return new object[] { (UIntEnum)uint.MinValue };
yield return new object[] { (UIntEnum)1 };
// ulong.
yield return new object[] { ulong.MinValue };
yield return new object[] { (ulong)1 };
yield return new object[] { (ULongEnum)ulong.MinValue };
yield return new object[] { (ULongEnum)1 };
}
public static IEnumerable<object[]> FromObject_Invalid_TestData()
{
// char.
yield return new object[] { char.MinValue };
yield return new object[] { (char)1 };
yield return new object[] { char.MaxValue };
// DateTime.
yield return new object[] { new DateTime(10) };
// object.
yield return new object[] { new object() };
// uint.
yield return new object[] { uint.MaxValue };
yield return new object[] { (UIntEnum)uint.MaxValue };
}
public static IEnumerable<object[]> FromObject_Overflow_TestData()
{
yield return new object[] { long.MinValue };
yield return new object[] { long.MaxValue };
yield return new object[] { (LongEnum)long.MinValue };
yield return new object[] { (LongEnum)long.MaxValue };
}
public static IEnumerable<object[]> FromString_TestData()
{
yield return new object[] { null, 0 };
yield return new object[] { "-1", -1 };
yield return new object[] { "0", 0 };
yield return new object[] { "1", 1 };
yield return new object[] { "&h5", 5 };
yield return new object[] { "&h0", 0 };
yield return new object[] { "&o5", 5 };
yield return new object[] { " &o5", 5 };
yield return new object[] { "&o0", 0 };
yield return new object[] { 1.1.ToString(), 1 };
}
public static IEnumerable<object[]> FromString_Invalid_TestData()
{
yield return new object[] { "" };
yield return new object[] { "&" };
yield return new object[] { "&a" };
yield return new object[] { "&a0" };
yield return new object[] { "true" };
yield return new object[] { "false" };
yield return new object[] { "invalid" };
}
public static IEnumerable<object[]> FromString_Overflow_TestData()
{
yield return new object[] { "2147483648" };
yield return new object[] { double.PositiveInfinity.ToString() };
yield return new object[] { double.NegativeInfinity.ToString() };
yield return new object[] { double.NaN.ToString() };
}
}
}