**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System / CharTest.cs
blob4cfbfa7aad303e4c805364b75934a8ece1af45ab
1 // CharTest.cs - NUnit Test Cases for the System.Char struct
2 //
3 // David Brandt (bucky@keystreams.com)
4 //
5 // (C) Ximian, Inc. http://www.ximian.com
6 //
8 using NUnit.Framework;
9 using System;
10 using System.Globalization;
12 namespace MonoTests.System
15 public class CharTest : TestCase
17 public CharTest() {}
19 protected override void SetUp()
23 protected override void TearDown()
27 public void TestCompareTo()
29 Char c1 = 'a';
30 Char c2 = 'b';
31 Char c3 = 'b';
32 Assert("Less than", c1.CompareTo(c2) == -1);
33 Assert("Greater than", c2.CompareTo(c1) == 1);
34 Assert("Equal 1", c2.CompareTo(c3) == 0);
35 Assert("Equal 2", c1.CompareTo(c1) == 0);
38 public void TestEquals()
40 Char c1 = 'a';
41 Char c2 = 'b';
42 Char c3 = 'b';
43 Assert("Same", c1.Equals(c1));
44 Assert("Same value", c2.Equals(c3));
45 Assert("Not same", !c1.Equals(c2));
48 public void TestGetHashValue()
50 Char c1 = ' ';
51 AssertEquals("deterministic hash code ", c1.GetHashCode(), c1.GetHashCode());
52 // TODO - the spec doesn't say what algorithm is used to get hash codes. So far, just a weak test for determinism and mostly-uniqueness.
55 public void TestGetNumericValue()
57 Char c1 = ' ';
58 Char c2 = '3';
59 AssertEquals("code 1", -1.0, Char.GetNumericValue(c1), 0.1);
60 AssertEquals("code 2", 3.0, Char.GetNumericValue(c2), 0.1);
62 string s1 = " 3 ";
63 AssertEquals("space not number", -1.0, Char.GetNumericValue(s1, 0), 0.1);
64 AssertEquals("space not number", 3.0, Char.GetNumericValue(s1, 1), 0.1);
65 AssertEquals("space not number", -1.0, Char.GetNumericValue(s1, 2), 0.1);
68 public void TestGetUnicodeCategory()
71 char Pe1 = ']';
72 char Pe2 = '}';
73 char Pe3 = ')';
74 AssertEquals("Close Punctuation",
75 UnicodeCategory.ClosePunctuation,
76 Char.GetUnicodeCategory(Pe1));
77 AssertEquals("Close Punctuation",
78 UnicodeCategory.ClosePunctuation,
79 Char.GetUnicodeCategory(Pe2));
80 AssertEquals("Close Punctuation",
81 UnicodeCategory.ClosePunctuation,
82 Char.GetUnicodeCategory(Pe3));
84 // TODO - ConnectorPunctuation
86 char c1 = (char)0; // 0000-001F, 007F-009F
87 char c2 = (char)0x001F;
88 char c3 = (char)0x007F;
89 char c4 = (char)0x00F;
90 AssertEquals("Control",
91 UnicodeCategory.Control,
92 Char.GetUnicodeCategory(c1));
93 AssertEquals("Control",
94 UnicodeCategory.Control,
95 Char.GetUnicodeCategory(c2));
96 AssertEquals("Control",
97 UnicodeCategory.Control,
98 Char.GetUnicodeCategory(c3));
99 AssertEquals("Control",
100 UnicodeCategory.Control,
101 Char.GetUnicodeCategory(c4));
104 // TODO - more currencies?
105 char c1 = '$';
106 AssertEquals("Currency",
107 UnicodeCategory.CurrencySymbol,
108 Char.GetUnicodeCategory(c1));
111 char c1 = '-';
112 AssertEquals("Dash Punctuation",
113 UnicodeCategory.DashPunctuation,
114 Char.GetUnicodeCategory(c1));
117 char c1 = '2';
118 char c2 = '7';
119 AssertEquals("Decimal Digit",
120 UnicodeCategory.DecimalDigitNumber,
121 Char.GetUnicodeCategory(c1));
122 AssertEquals("Decimal Digit",
123 UnicodeCategory.DecimalDigitNumber,
124 Char.GetUnicodeCategory(c2));
126 // TODO - EnclosingMark
127 // TODO - FinalQuotePunctuation
128 // TODO - Format
129 // TODO - InitialQuotePunctuation
130 // TODO - LetterNumber
131 // TODO - LineSeparator (not '\n', that's a control char)
133 char c1 = 'a';
134 char c2 = 'z';
135 AssertEquals("LowercaseLetter",
136 UnicodeCategory.LowercaseLetter,
137 Char.GetUnicodeCategory(c1));
138 AssertEquals("LowercaseLetter",
139 UnicodeCategory.LowercaseLetter,
140 Char.GetUnicodeCategory(c2));
143 char c1 = '+';
144 char c2 = '=';
145 AssertEquals("MathSymbol",
146 UnicodeCategory.MathSymbol,
147 Char.GetUnicodeCategory(c1));
148 AssertEquals("MathSymbol",
149 UnicodeCategory.MathSymbol,
150 Char.GetUnicodeCategory(c2));
152 // TODO - ModifierSymbol
153 // TODO - NonSpacingMark
154 // TODO - OpenPunctuation
156 char c1 = '[';
157 char c2 = '{';
158 char c3 = '(';
159 AssertEquals("OpenPunctuation",
160 UnicodeCategory.OpenPunctuation,
161 Char.GetUnicodeCategory(c1));
162 AssertEquals("OpenPunctuation",
163 UnicodeCategory.OpenPunctuation,
164 Char.GetUnicodeCategory(c2));
165 AssertEquals("OpenPunctuation",
166 UnicodeCategory.OpenPunctuation,
167 Char.GetUnicodeCategory(c3));
169 // TODO - OtherLetter
170 // TODO - OtherNotAssigned
171 // TODO - OtherNumber
173 char c1 = '/';
174 AssertEquals("OtherPunctuation",
175 UnicodeCategory.OtherPunctuation,
176 Char.GetUnicodeCategory(c1));
178 // TODO - OtherSymbol
179 // TODO - ParagraphSeparator
180 // TODO - PrivateUse
182 char c1 = ' ';
183 AssertEquals("SpaceSeparator",
184 UnicodeCategory.SpaceSeparator,
185 Char.GetUnicodeCategory(c1));
187 // TODO - SpacingCombiningMark
189 char c1 = (char)0xD800; // D800-DBFF
190 char c2 = (char)0xDBFF; // D800-DBFF
191 char c3 = (char)0xDC01; // DC00-DEFF
192 char c4 = (char)0xDEFF; // DC00-DEFF
193 AssertEquals("High Surrogate",
194 UnicodeCategory.Surrogate,
195 Char.GetUnicodeCategory(c1));
196 AssertEquals("High Surrogate",
197 UnicodeCategory.Surrogate,
198 Char.GetUnicodeCategory(c2));
199 AssertEquals("Low Surrogate",
200 UnicodeCategory.Surrogate,
201 Char.GetUnicodeCategory(c3));
202 AssertEquals("Low Surrogate",
203 UnicodeCategory.Surrogate,
204 Char.GetUnicodeCategory(c4));
206 // TODO - TitlecaseLetter
207 // TODO - UppercaseLetter
209 char c1 = 'A';
210 char c2 = 'Z';
211 AssertEquals("UppercaseLetter",
212 UnicodeCategory.UppercaseLetter,
213 Char.GetUnicodeCategory(c1));
214 AssertEquals("UppercaseLetter",
215 UnicodeCategory.UppercaseLetter,
216 Char.GetUnicodeCategory(c2));
220 public void TestIsControl()
222 // control is 0000-001F, 007F-009F
223 char c1 = (char)0;
224 char c2 = (char)0x001F;
225 char c3 = (char)0x007F;
226 char c4 = (char)0x009F;
227 Assert("Not control", !Char.IsControl(' '));
228 Assert("control", Char.IsControl(c1));
229 Assert("control", Char.IsControl(c2));
230 Assert("control", Char.IsControl(c3));
231 Assert("control", Char.IsControl(c4));
233 string s1 = " " + c1 + c2 + c3 + c4;
234 Assert("Not control", !Char.IsControl(s1, 0));
235 Assert("control", Char.IsControl(s1, 1));
236 Assert("control", Char.IsControl(s1, 2));
237 Assert("control", Char.IsControl(s1, 3));
238 Assert("control", Char.IsControl(s1, 4));
241 public void TestIsDigit()
243 char c1 = '0';
244 char c2 = '9';
245 Assert("Not digit", !Char.IsDigit(' '));
246 Assert("digit", Char.IsDigit(c1));
247 Assert("digit", Char.IsDigit(c2));
249 string s1 = " " + c1 + c2;
250 Assert("Not digit", !Char.IsDigit(s1, 0));
251 Assert("digit", Char.IsDigit(s1, 1));
252 Assert("digit", Char.IsDigit(s1, 2));
255 public void TestIsLetter()
257 char c1 = 'a';
258 char c2 = 'z';
259 char c3 = 'A';
260 char c4 = 'Z';
261 Assert("Not letter", !Char.IsLetter(' '));
262 Assert("letter", Char.IsLetter(c1));
263 Assert("letter", Char.IsLetter(c2));
264 Assert("letter", Char.IsLetter(c3));
265 Assert("letter", Char.IsLetter(c4));
267 string s1 = " " + c1 + c2 + c3 + c4;
268 Assert("Not letter", !Char.IsLetter(s1, 0));
269 Assert("letter", Char.IsLetter(s1, 1));
270 Assert("letter", Char.IsLetter(s1, 2));
271 Assert("letter", Char.IsLetter(s1, 3));
272 Assert("letter", Char.IsLetter(s1, 4));
275 public void TestIsLetterOrDigit()
277 char c1 = 'a';
278 char c2 = 'z';
279 char c3 = 'A';
280 char c4 = 'Z';
281 char c5 = '0';
282 char c6 = '9';
283 Assert("Not letterordigit", !Char.IsLetterOrDigit(' '));
284 Assert("letterordigit", Char.IsLetterOrDigit(c1));
285 Assert("letterordigit", Char.IsLetterOrDigit(c2));
286 Assert("letterordigit", Char.IsLetterOrDigit(c3));
287 Assert("letterordigit", Char.IsLetterOrDigit(c4));
288 Assert("letterordigit", Char.IsLetterOrDigit(c5));
289 Assert("letterordigit", Char.IsLetterOrDigit(c6));
291 string s1 = " " + c1 + c2 + c3 + c4 + c5 + c6;
292 Assert("Not letterordigit", !Char.IsLetterOrDigit(s1, 0));
293 Assert("letterordigit", Char.IsLetterOrDigit(s1, 1));
294 Assert("letterordigit", Char.IsLetterOrDigit(s1, 2));
295 Assert("letterordigit", Char.IsLetterOrDigit(s1, 3));
296 Assert("letterordigit", Char.IsLetterOrDigit(s1, 4));
297 Assert("letterordigit", Char.IsLetterOrDigit(s1, 5));
298 Assert("letterordigit", Char.IsLetterOrDigit(s1, 6));
301 public void TestIsLower()
303 char c1 = 'a';
304 char c2 = 'z';
305 Assert("Not lower", !Char.IsLower(' '));
306 Assert("lower", Char.IsLower(c1));
307 Assert("lower", Char.IsLower(c2));
309 string s1 = " " + c1 + c2;
310 Assert("Not lower", !Char.IsLower(s1, 0));
311 Assert("lower", Char.IsLower(s1, 1));
312 Assert("lower", Char.IsLower(s1, 2));
315 public void TestIsNumber()
317 char c1 = '0';
318 char c2 = '9';
319 // TODO - IsNumber of less obvious characters
321 Assert("Not number", !Char.IsNumber(' '));
322 Assert("number", Char.IsNumber(c1));
323 Assert("number", Char.IsNumber(c2));
325 string s1 = " " + c1 + c2;
326 Assert("Not number", !Char.IsNumber(s1, 0));
327 Assert("number", Char.IsNumber(s1, 1));
328 Assert("number", Char.IsNumber(s1, 2));
331 public void TestIsPunctuation()
333 char c1 = '.';
334 char c2 = '?';
335 Assert("Not punctuation", !Char.IsPunctuation(' '));
336 Assert("punctuation", Char.IsPunctuation(c1));
337 Assert("punctuation", Char.IsPunctuation(c2));
339 string s1 = " " + c1 + c2;
340 Assert("Not punctuation", !Char.IsPunctuation(s1, 0));
341 Assert("punctuation", Char.IsPunctuation(s1, 1));
342 Assert("punctuation", Char.IsPunctuation(s1, 2));
345 public void TestIsSeparator()
347 char c1 = ' ';
349 Assert("Not separator", !Char.IsSeparator('.'));
350 Assert("separator1", Char.IsSeparator(c1));
352 string s1 = "." + c1;
353 Assert("Not separator", !Char.IsSeparator(s1, 0));
354 Assert("separator1-2", Char.IsSeparator(s1, 1));
357 public void TestIsSurrogate()
359 // high surrogate - D800-DBFF
360 // low surrogate - DC00-DEFF
361 char c1 = (char)0xD800;
362 char c2 = (char)0xDBFF;
363 char c3 = (char)0xDC00;
364 char c4 = (char)0xDEFF;
365 Assert("Not surrogate", !Char.IsSurrogate(' '));
366 Assert("surrogate1", Char.IsSurrogate(c1));
367 Assert("surrogate2", Char.IsSurrogate(c2));
368 Assert("surrogate3", Char.IsSurrogate(c3));
369 Assert("surrogate4", Char.IsSurrogate(c4));
371 string s1 = " " + c1 + c2 + c3 + c4;
372 Assert("Not surrogate", !Char.IsSurrogate(s1, 0));
373 Assert("surrogate1-2", Char.IsSurrogate(s1, 1));
374 Assert("surrogate2-2", Char.IsSurrogate(s1, 2));
375 Assert("surrogate3-2", Char.IsSurrogate(s1, 3));
376 Assert("surrogate4-2", Char.IsSurrogate(s1, 4));
379 public void TestIsSymbol()
381 char c1 = '+';
382 char c2 = '=';
383 Assert("Not symbol", !Char.IsSymbol(' '));
384 Assert("symbol", Char.IsSymbol(c1));
385 Assert("symbol", Char.IsSymbol(c2));
387 string s1 = " " + c1 + c2;
388 Assert("Not symbol", !Char.IsSymbol(s1, 0));
389 Assert("symbol", Char.IsSymbol(s1, 1));
390 Assert("symbol", Char.IsSymbol(s1, 2));
393 public void TestIsUpper()
395 char c1 = 'A';
396 char c2 = 'Z';
397 Assert("Not upper", !Char.IsUpper('a'));
398 Assert("upper", Char.IsUpper(c1));
399 Assert("upper", Char.IsUpper(c2));
401 string s1 = "a" + c1 + c2;
402 Assert("Not upper", !Char.IsUpper(s1, 0));
403 Assert("upper", Char.IsUpper(s1, 1));
404 Assert("upper", Char.IsUpper(s1, 2));
407 public void TestIsWhiteSpace()
409 char c1 = ' ';
410 char c2 = '\n';
411 char c3 = '\t';
413 Assert("Not whitespace", !Char.IsWhiteSpace('.'));
414 Assert("whitespace1", Char.IsWhiteSpace(c1));
415 Assert("whitespace2", Char.IsWhiteSpace(c2));
416 Assert("whitespace3", Char.IsWhiteSpace(c3));
418 string s1 = "." + c1 + c2 + c3;
419 Assert("Not whitespace", !Char.IsWhiteSpace(s1, 0));
420 Assert("whitespace1-2", Char.IsWhiteSpace(s1, 1));
421 Assert("whitespace2-2", Char.IsWhiteSpace(s1, 2));
422 Assert("whitespace3-2", Char.IsWhiteSpace(s1, 3));
426 public void TestParse()
428 char c1 = 'a';
429 string s1 = "a";
430 Assert(c1.Equals(Char.Parse(s1)));
433 public void TestToLower()
435 char a1 = 'a';
436 char a2 = 'A';
437 char a3 = 'z';
438 char a4 = 'Z';
439 char a5 = ' ';
440 char a6 = '+';
441 char b1 = 'a';
442 char b2 = 'a';
443 char b3 = 'z';
444 char b4 = 'z';
445 char b5 = ' ';
446 char b6 = '+';
447 AssertEquals("char lowered", b1, Char.ToLower(a1));
448 AssertEquals("char lowered", b2, Char.ToLower(a2));
449 AssertEquals("char lowered", b3, Char.ToLower(a3));
450 AssertEquals("char lowered", b4, Char.ToLower(a4));
451 AssertEquals("char lowered", b5, Char.ToLower(a5));
452 AssertEquals("char lowered", b6, Char.ToLower(a6));
455 public void TestToUpper()
457 char a1 = 'a';
458 char a2 = 'A';
459 char a3 = 'z';
460 char a4 = 'Z';
461 char a5 = ' ';
462 char a6 = '+';
463 char b1 = 'A';
464 char b2 = 'A';
465 char b3 = 'Z';
466 char b4 = 'Z';
467 char b5 = ' ';
468 char b6 = '+';
469 AssertEquals("char uppered", b1, Char.ToUpper(a1));
470 AssertEquals("char uppered", b2, Char.ToUpper(a2));
471 AssertEquals("char uppered", b3, Char.ToUpper(a3));
472 AssertEquals("char uppered", b4, Char.ToUpper(a4));
473 AssertEquals("char uppered", b5, Char.ToUpper(a5));
474 AssertEquals("char uppered", b6, Char.ToUpper(a6));
478 public void TestToString()
480 char c1 = 'a';
481 string s1 = "a";
482 Assert(s1.Equals(c1.ToString()));
485 public void TestGetTypeCode()
487 char c1 = 'a';
488 Assert(c1.GetTypeCode().Equals(TypeCode.Char));