Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UInt32Test.cs « System « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: da64ebd7087a758208ce059165b0834be69e0802 (plain)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
// UInt32Test.cs - NUnit Test Cases for the System.UInt32 struct
//
// Mario Martinez (mariom925@home.om)
//
// (C) Ximian, Inc.  http://www.ximian.com
// 

using NUnit.Framework;
using System;
using System.Globalization;
using System.Threading;

namespace MonoTests.System
{

public class UInt32Test : TestCase
{
	private const UInt32 MyUInt32_1 = 42;
	private const UInt32 MyUInt32_2 = 0;
	private const UInt32 MyUInt32_3 = 4294967295;
	private const string MyString1 = "42";
	private const string MyString2 = "0";
	private const string MyString3 = "4294967295";
	private string[] Formats1 = {"c", "d", "e", "f", "g", "n", "p", "x" };
	private string[] Formats2 = {"c5", "d5", "e5", "f5", "g5", "n5", "p5", "x5" };
	private string[] Results1 = {"",
					"0", "0.000000e+000", "0.00",
					"0", "0.00", "0.00 %", "0"};
	private string[] ResultsNfi1 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"0.00",
					"0", "0.000000e+000", "0.00",
					"0", "0.00", "0.00 %", "0"};
	private string[] Results2 = {"",
					"4294967295", "4.29497e+009", "4294967295.00000",
					"4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
	private string[] ResultsNfi2 = {NumberFormatInfo.InvariantInfo.CurrencySymbol+"4,294,967,295.00000",
					"4294967295", "4.29497e+009", "4294967295.00000",
					"4.295e+09", "4,294,967,295.00000", "429,496,729,500.00000 %", "ffffffff"};
	private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
	
	public UInt32Test() : base ("MonoTests.System.UInt32Test testcase") {}
	public UInt32Test(string name) : base(name) {}

	private CultureInfo old_culture;

	protected override void SetUp() 
	{
		old_culture = Thread.CurrentThread.CurrentCulture;

		// Set culture to en-US and don't let the user override.
		Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);

		// We can't initialize this until we set the culture.
		Results1 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"0.00";
		Results2 [0] = NumberFormatInfo.CurrentInfo.CurrencySymbol+"4,294,967,295.00000";
	}

	protected override void TearDown()
	{
		Thread.CurrentThread.CurrentCulture = old_culture;
	}

	public static ITest Suite {
		get { 
			return new TestSuite(typeof(UInt32Test)); 
		}
	}
    
	public void TestMinMax()
	{
		
		AssertEquals(UInt32.MinValue, MyUInt32_2);
		AssertEquals(UInt32.MaxValue, MyUInt32_3);
	}
	
	public void TestCompareTo()
	{
		Assert(MyUInt32_3.CompareTo(MyUInt32_2) > 0);
		Assert(MyUInt32_2.CompareTo(MyUInt32_2) == 0);
		Assert(MyUInt32_1.CompareTo((UInt32)(42)) == 0);
		Assert(MyUInt32_2.CompareTo(MyUInt32_3) < 0);
		Assert (1 == UInt32.Parse ("1"));
		Assert (1 == UInt32.Parse (" 1"));
		Assert (1 == UInt32.Parse ("     1"));
		Assert (1 == UInt32.Parse ("1    "));
		Assert (1 == UInt32.Parse ("+1"));

		try {
			UInt32.Parse (" + 1 ");
			Fail ("Should raise FormatException1");
		} catch (Exception e){
			Assert (typeof (FormatException) == e.GetType ());
		}

		try {
			UInt32.Parse (" + ");
			Fail ("Should raise FormatException");
		} catch (Exception e){
			Assert (typeof (FormatException) == e.GetType ());
		}
		try {
			MyUInt32_2.CompareTo((Int16)100);
			Fail("Should raise a System.ArgumentException");
		}
		catch (Exception e) {
			Assert(typeof(ArgumentException) == e.GetType());
		}
	}

	public void TestEquals()
	{
		Assert(MyUInt32_1.Equals(MyUInt32_1));
		Assert(MyUInt32_1.Equals((UInt32)(42)));
		Assert(MyUInt32_1.Equals((SByte)(42)) == false);
		Assert(MyUInt32_1.Equals(MyUInt32_2) == false);
	}
	
	public void TestGetHashCode()
	{
		try {
			MyUInt32_1.GetHashCode();
			MyUInt32_2.GetHashCode();
			MyUInt32_3.GetHashCode();
		}
		catch {
			Fail("GetHashCode should not raise an exception here");
		}
	}
	
	public void TestParse()
	{
		//test Parse(string s)
		Assert("Parse problem on \""+MyString1+"\"", MyUInt32_1 == UInt32.Parse(MyString1));
		Assert("Parse problem on \""+MyString2+"\"", MyUInt32_2 == UInt32.Parse(MyString2));
		Assert("Parse problem on \""+MyString3+"\"", MyUInt32_3 == UInt32.Parse(MyString3));
		try {
			UInt32.Parse(null);
			Fail("Should raise a System.ArgumentNullException");
		}
		catch (Exception e) {
			Assert("Did not get ArgumentNullException type", typeof(ArgumentNullException) == e.GetType());
		}
		try {
			UInt32.Parse("not-a-number");
			Fail("Should raise a System.FormatException");
		}
		catch (Exception e) {
			Assert("Did not get FormatException type", typeof(FormatException) == e.GetType());
		}
		try {
			// TODO: Use this after ToString() is completed. For now, hard code string that generates
			// exception.
			//double OverInt = (double)UInt32.MaxValue + 1;
			//UInt32.Parse(OverInt.ToString());
			UInt32.Parse("4294967296");
			Fail("Should raise a System.OverflowException");
		}
		catch (Exception e) {
			Assert("Did not get OverflowException type on '"+"4294967296"+"'. Instead, got: '"+e.GetType()+"'", typeof(OverflowException) == e.GetType());
		}
		//test Parse(string s, NumberStyles style)
		Assert(42 == UInt32.Parse(" "+NumberFormatInfo.CurrentInfo.CurrencySymbol+"42 ", NumberStyles.Currency));
		try {
			UInt32.Parse("$42", NumberStyles.Integer);
			Fail("Should raise a System.FormatException");
		}
		catch (Exception e) {
			Assert(typeof(FormatException) == e.GetType());
		}
		//test Parse(string s, IFormatProvider provider)
		Assert(42 == UInt32.Parse(" 42 ", Nfi));
		try {
			UInt32.Parse("%42", Nfi);
			Fail("Should raise a System.FormatException");
		}
		catch (Exception e) {
			Assert(typeof(FormatException) == e.GetType());
		}
		//test Parse(string s, NumberStyles style, IFormatProvider provider)
		Assert(16 == UInt32.Parse(" 10 ", NumberStyles.HexNumber, Nfi));
		try {
			UInt32.Parse("$42", NumberStyles.Integer, Nfi);
			Fail("Should raise a System.FormatException");
		}
		catch (Exception e) {
			Assert(typeof(FormatException) == e.GetType());
		}
	}
	
	public void TestToString()
	{
		int TestNumber = 1;
		try {
			//test ToString()
			AssertEquals(MyString1, MyUInt32_1.ToString());
			TestNumber++;
			AssertEquals(MyString2, MyUInt32_2.ToString());
			TestNumber++;
			AssertEquals(MyString3, MyUInt32_3.ToString());
		} catch (Exception e) {
			Fail("TestToString: Failed on TestNumber=" + TestNumber 
				+ " with exception: " + e.ToString());
		}

		//test ToString(string format)
		for (int i=0; i < Formats1.Length; i++) {
			try {
				AssertEquals(Results1[i], MyUInt32_2.ToString(Formats1[i]));
			} catch (Exception e) {
				Fail("TestToString: MyUInt32_2.ToString(Formats1[i]) i=" + i 
					+ ". e = " + e.ToString());
			}

			try {
				AssertEquals(Results2[i], MyUInt32_3.ToString(Formats2[i]));
			} catch (Exception e) {
				Fail("TestToString: MyUInt32_3.ToString(Formats2[i]) i=" + i
					+ ". e = " + e.ToString());
			}
		}
		//test ToString(string format, IFormatProvider provider);
		for (int i=0; i < Formats1.Length; i++) {
			AssertEquals(ResultsNfi1[i], MyUInt32_2.ToString(Formats1[i], Nfi));
			AssertEquals(ResultsNfi2[i], MyUInt32_3.ToString(Formats2[i], Nfi));
		}
		try {
			MyUInt32_1.ToString("z");
			Fail("Should raise a System.FormatException");
		}
		catch (Exception e) {
			Assert(typeof(FormatException) == e.GetType());
		}
	}
}


}