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 System;
6 using System.Globalization;
7 using Xunit;
8 
9 namespace System.Tests
10 {
11     public class IsDBNullTests
12     {
13         [Fact]
ChangeTypeTest()14         public static void ChangeTypeTest()
15         {
16             var testValue = 0;
17             bool expectedValue = false;
18             Assert.Equal(expectedValue, Convert.ChangeType(testValue, TypeCode.Boolean));
19         }
20 
21         [Fact]
ToBase64CharArrayTest()22         public static void ToBase64CharArrayTest()
23         {
24             byte[] barray = new byte[256];
25             char[] carray = new char[352];
26             int length = Convert.ToBase64CharArray(barray, 0, barray.Length, carray, 0, Base64FormattingOptions.InsertLineBreaks);
27             int length2 = Convert.ToBase64CharArray(barray, 0, barray.Length, carray, 0, Base64FormattingOptions.None);
28             Assert.Equal(length, 352);
29             Assert.Equal(length, 352);
30         }
31 
32         [Fact]
ToBase64StringTest()33         public static void ToBase64StringTest()
34         {
35             byte[] barray = new byte[] { 1, 2, 3 };
36             byte[] subset = new byte[] { 2, 3 };
37             string s1 = Convert.ToBase64String(barray, Base64FormattingOptions.InsertLineBreaks);
38             string s2 = Convert.ToBase64String(barray, Base64FormattingOptions.None);
39             string s3 = Convert.ToBase64String(barray, 1, 2, Base64FormattingOptions.None);
40             Assert.Equal(barray, Convert.FromBase64String(s1));
41             Assert.True(!s2.Contains("\n"));
42             Assert.Equal(barray, Convert.FromBase64String(s2));
43             Assert.Equal(subset, Convert.FromBase64String(s3));
44         }
45 
46         [Fact]
ToBooleanTests()47         public void ToBooleanTests()
48         {
49             char testValue = char.MinValue;
50             Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(testValue));
51             DateTime testValue2 = DateTime.MinValue;
52             Assert.Throws<InvalidCastException>(() => Convert.ToBoolean(testValue2));
53         }
54 
55         [Fact]
ToByteTest()56         public void ToByteTest()
57         {
58             DateTime testValue = DateTime.MaxValue;
59             Assert.Throws<InvalidCastException>(() => Convert.ToByte(testValue));
60         }
61 
62         [Fact]
ToCharTests()63         public void ToCharTests()
64         {
65             char testValue = char.MinValue;
66             Assert.Equal(testValue, Convert.ToChar(testValue));
67             DateTime testValue2 = DateTime.MinValue;
68             Assert.Throws<InvalidCastException>(() => Convert.ToChar(testValue2));
69         }
70 
71         [Fact]
ToDateTimeTests()72         public void ToDateTimeTests()
73         {
74             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(byte.MinValue));
75             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(sbyte.MinValue));
76             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(float.MinValue));
77             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(ushort.MinValue));
78             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(uint.MinValue));
79             Assert.Throws<InvalidCastException>(() => Convert.ToDateTime(ulong.MinValue));
80         }
81 
82         [Fact]
ToDecimalTests()83         public void ToDecimalTests()
84         {
85             Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(char.MinValue));
86             Assert.Throws<InvalidCastException>(() => Convert.ToDecimal(DateTime.MinValue));
87         }
88 
89         [Fact]
ToDoubleTests()90         public void ToDoubleTests()
91         {
92             Assert.Throws<InvalidCastException>(() => Convert.ToDouble(char.MinValue));
93             Assert.Throws<InvalidCastException>(() => Convert.ToDouble(DateTime.MinValue));
94         }
95 
96         [Fact]
ToInt16Test()97         public void ToInt16Test()
98         {
99             Assert.Throws<InvalidCastException>(() => Convert.ToInt16(DateTime.MaxValue));
100         }
101 
102         [Fact]
ToInt32Test()103         public void ToInt32Test()
104         {
105             Assert.Throws<InvalidCastException>(() => Convert.ToInt32(DateTime.MaxValue));
106         }
107 
108         [Fact]
ToInt64Test()109         public void ToInt64Test()
110         {
111             Assert.Throws<InvalidCastException>(() => Convert.ToInt64(DateTime.MaxValue));
112         }
113 
114         [Fact]
IsDBNullTest()115         public static void IsDBNullTest()
116         {
117             Assert.True(Convert.IsDBNull(Convert.DBNull));
118             Assert.False(Convert.IsDBNull(4));
119             Assert.False(Convert.IsDBNull(true));
120             Assert.False(Convert.IsDBNull('x'));
121             Assert.False(Convert.IsDBNull(1.1));
122             Assert.False(Convert.IsDBNull(null));
123         }
124 
125         [Fact]
ToSByteTest()126         public void ToSByteTest()
127         {
128             Assert.Throws<InvalidCastException>(() => Convert.ToSByte(DateTime.MaxValue));
129         }
130 
131         [Fact]
ToSingleTests()132         public void ToSingleTests()
133         {
134             Assert.Throws<InvalidCastException>(() => Convert.ToSingle(DateTime.MaxValue));
135             Assert.Throws<InvalidCastException>(() => Convert.ToSingle(char.MinValue));
136         }
137 
138         [Fact]
ToStringTests()139         public static void ToStringTests()
140         {
141             string testValue = "Hello World!";
142             Assert.Equal(testValue, Convert.ToString(testValue));
143             Assert.Equal(testValue, Convert.ToString(testValue, NumberFormatInfo.CurrentInfo));
144         }
145 
146         [Fact]
ToUInt16Test()147         public void ToUInt16Test()
148         {
149             Assert.Throws<InvalidCastException>(() => Convert.ToUInt16(DateTime.MaxValue));
150         }
151 
152         [Fact]
ToUInt32Test()153         public void ToUInt32Test()
154         {
155             Assert.Throws<InvalidCastException>(() => Convert.ToUInt32(DateTime.MaxValue));
156         }
157 
158         [Fact]
ToUInt64Test()159         public void ToUInt64Test()
160         {
161             Assert.Throws<InvalidCastException>(() => Convert.ToUInt64(DateTime.MaxValue));
162         }
163     }
164 }
165