1 // CharTest.cs - NUnit Test Cases for the System.Char struct
\r
3 // David Brandt (bucky@keystreams.com)
\r
5 // (C) Ximian, Inc. http://www.ximian.com
\r
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
\r
9 using NUnit.Framework;
\r
11 using System.Globalization;
\r
13 namespace MonoTests.System
\r
16 public class CharTest : TestCase
\r
18 public void TestCompareTo()
\r
23 Assert("Less than", c1.CompareTo(c2) == -1);
\r
24 Assert("Greater than", c2.CompareTo(c1) == 1);
\r
25 Assert("Equal 1", c2.CompareTo(c3) == 0);
\r
26 Assert("Equal 2", c1.CompareTo(c1) == 0);
\r
29 public void TestEquals()
\r
34 Assert("Same", c1.Equals(c1));
\r
35 Assert("Same value", c2.Equals(c3));
\r
36 Assert("Not same", !c1.Equals(c2));
\r
39 public void TestGetHashValue()
\r
42 AssertEquals("deterministic hash code ", c1.GetHashCode(), c1.GetHashCode());
\r
43 // 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.
\r
46 public void TestGetNumericValue()
\r
50 AssertEquals("code 1", -1.0, Char.GetNumericValue(c1), 0.1);
\r
51 AssertEquals("code 2", 3.0, Char.GetNumericValue(c2), 0.1);
\r
54 AssertEquals("space not number", -1.0, Char.GetNumericValue(s1, 0), 0.1);
\r
55 AssertEquals("space not number", 3.0, Char.GetNumericValue(s1, 1), 0.1);
\r
56 AssertEquals("space not number", -1.0, Char.GetNumericValue(s1, 2), 0.1);
\r
59 public void TestGetUnicodeCategory()
\r
65 AssertEquals("Close Punctuation",
\r
66 UnicodeCategory.ClosePunctuation,
\r
67 Char.GetUnicodeCategory(Pe1));
\r
68 AssertEquals("Close Punctuation",
\r
69 UnicodeCategory.ClosePunctuation,
\r
70 Char.GetUnicodeCategory(Pe2));
\r
71 AssertEquals("Close Punctuation",
\r
72 UnicodeCategory.ClosePunctuation,
\r
73 Char.GetUnicodeCategory(Pe3));
\r
75 // TODO - ConnectorPunctuation
\r
77 char c1 = (char)0; // 0000-001F, 007F-009F
\r
78 char c2 = (char)0x001F;
\r
79 char c3 = (char)0x007F;
\r
80 char c4 = (char)0x00F;
\r
81 AssertEquals("Control",
\r
82 UnicodeCategory.Control,
\r
83 Char.GetUnicodeCategory(c1));
\r
84 AssertEquals("Control",
\r
85 UnicodeCategory.Control,
\r
86 Char.GetUnicodeCategory(c2));
\r
87 AssertEquals("Control",
\r
88 UnicodeCategory.Control,
\r
89 Char.GetUnicodeCategory(c3));
\r
90 AssertEquals("Control",
\r
91 UnicodeCategory.Control,
\r
92 Char.GetUnicodeCategory(c4));
\r
95 // TODO - more currencies?
\r
97 AssertEquals("Currency",
\r
98 UnicodeCategory.CurrencySymbol,
\r
99 Char.GetUnicodeCategory(c1));
\r
103 AssertEquals("Dash Punctuation",
\r
104 UnicodeCategory.DashPunctuation,
\r
105 Char.GetUnicodeCategory(c1));
\r
110 AssertEquals("Decimal Digit",
\r
111 UnicodeCategory.DecimalDigitNumber,
\r
112 Char.GetUnicodeCategory(c1));
\r
113 AssertEquals("Decimal Digit",
\r
114 UnicodeCategory.DecimalDigitNumber,
\r
115 Char.GetUnicodeCategory(c2));
\r
117 // TODO - EnclosingMark
\r
118 // TODO - FinalQuotePunctuation
\r
120 // TODO - InitialQuotePunctuation
\r
121 // TODO - LetterNumber
\r
122 // TODO - LineSeparator (not '\n', that's a control char)
\r
126 AssertEquals("LowercaseLetter",
\r
127 UnicodeCategory.LowercaseLetter,
\r
128 Char.GetUnicodeCategory(c1));
\r
129 AssertEquals("LowercaseLetter",
\r
130 UnicodeCategory.LowercaseLetter,
\r
131 Char.GetUnicodeCategory(c2));
\r
136 AssertEquals("MathSymbol",
\r
137 UnicodeCategory.MathSymbol,
\r
138 Char.GetUnicodeCategory(c1));
\r
139 AssertEquals("MathSymbol",
\r
140 UnicodeCategory.MathSymbol,
\r
141 Char.GetUnicodeCategory(c2));
\r
143 // TODO - ModifierSymbol
\r
144 // TODO - NonSpacingMark
\r
145 // TODO - OpenPunctuation
\r
150 AssertEquals("OpenPunctuation",
\r
151 UnicodeCategory.OpenPunctuation,
\r
152 Char.GetUnicodeCategory(c1));
\r
153 AssertEquals("OpenPunctuation",
\r
154 UnicodeCategory.OpenPunctuation,
\r
155 Char.GetUnicodeCategory(c2));
\r
156 AssertEquals("OpenPunctuation",
\r
157 UnicodeCategory.OpenPunctuation,
\r
158 Char.GetUnicodeCategory(c3));
\r
160 // TODO - OtherLetter
\r
161 // TODO - OtherNotAssigned
\r
162 // TODO - OtherNumber
\r
165 AssertEquals("OtherPunctuation",
\r
166 UnicodeCategory.OtherPunctuation,
\r
167 Char.GetUnicodeCategory(c1));
\r
169 // TODO - OtherSymbol
\r
170 // TODO - ParagraphSeparator
\r
171 // TODO - PrivateUse
\r
174 AssertEquals("SpaceSeparator",
\r
175 UnicodeCategory.SpaceSeparator,
\r
176 Char.GetUnicodeCategory(c1));
\r
178 // TODO - SpacingCombiningMark
\r
180 char c1 = (char)0xD800; // D800-DBFF
\r
181 char c2 = (char)0xDBFF; // D800-DBFF
\r
182 char c3 = (char)0xDC01; // DC00-DEFF
\r
183 char c4 = (char)0xDEFF; // DC00-DEFF
\r
184 AssertEquals("High Surrogate",
\r
185 UnicodeCategory.Surrogate,
\r
186 Char.GetUnicodeCategory(c1));
\r
187 AssertEquals("High Surrogate",
\r
188 UnicodeCategory.Surrogate,
\r
189 Char.GetUnicodeCategory(c2));
\r
190 AssertEquals("Low Surrogate",
\r
191 UnicodeCategory.Surrogate,
\r
192 Char.GetUnicodeCategory(c3));
\r
193 AssertEquals("Low Surrogate",
\r
194 UnicodeCategory.Surrogate,
\r
195 Char.GetUnicodeCategory(c4));
\r
197 // TODO - TitlecaseLetter
\r
198 // TODO - UppercaseLetter
\r
202 AssertEquals("UppercaseLetter",
\r
203 UnicodeCategory.UppercaseLetter,
\r
204 Char.GetUnicodeCategory(c1));
\r
205 AssertEquals("UppercaseLetter",
\r
206 UnicodeCategory.UppercaseLetter,
\r
207 Char.GetUnicodeCategory(c2));
\r
211 public void TestIsControl()
\r
213 // control is 0000-001F, 007F-009F
\r
215 char c2 = (char)0x001F;
\r
216 char c3 = (char)0x007F;
\r
217 char c4 = (char)0x009F;
\r
218 Assert("Not control", !Char.IsControl(' '));
\r
219 Assert("control", Char.IsControl(c1));
\r
220 Assert("control", Char.IsControl(c2));
\r
221 Assert("control", Char.IsControl(c3));
\r
222 Assert("control", Char.IsControl(c4));
\r
224 string s1 = " " + c1 + c2 + c3 + c4;
\r
225 Assert("Not control", !Char.IsControl(s1, 0));
\r
226 Assert("control", Char.IsControl(s1, 1));
\r
227 Assert("control", Char.IsControl(s1, 2));
\r
228 Assert("control", Char.IsControl(s1, 3));
\r
229 Assert("control", Char.IsControl(s1, 4));
\r
232 public void TestIsDigit()
\r
236 Assert("Not digit", !Char.IsDigit(' '));
\r
237 Assert("digit", Char.IsDigit(c1));
\r
238 Assert("digit", Char.IsDigit(c2));
\r
240 string s1 = " " + c1 + c2;
\r
241 Assert("Not digit", !Char.IsDigit(s1, 0));
\r
242 Assert("digit", Char.IsDigit(s1, 1));
\r
243 Assert("digit", Char.IsDigit(s1, 2));
\r
246 public void TestIsLetter()
\r
252 Assert("Not letter", !Char.IsLetter(' '));
\r
253 Assert("letter", Char.IsLetter(c1));
\r
254 Assert("letter", Char.IsLetter(c2));
\r
255 Assert("letter", Char.IsLetter(c3));
\r
256 Assert("letter", Char.IsLetter(c4));
\r
258 string s1 = " " + c1 + c2 + c3 + c4;
\r
259 Assert("Not letter", !Char.IsLetter(s1, 0));
\r
260 Assert("letter", Char.IsLetter(s1, 1));
\r
261 Assert("letter", Char.IsLetter(s1, 2));
\r
262 Assert("letter", Char.IsLetter(s1, 3));
\r
263 Assert("letter", Char.IsLetter(s1, 4));
\r
266 public void TestIsLetterOrDigit()
\r
274 Assert("Not letterordigit", !Char.IsLetterOrDigit(' '));
\r
275 Assert("letterordigit", Char.IsLetterOrDigit(c1));
\r
276 Assert("letterordigit", Char.IsLetterOrDigit(c2));
\r
277 Assert("letterordigit", Char.IsLetterOrDigit(c3));
\r
278 Assert("letterordigit", Char.IsLetterOrDigit(c4));
\r
279 Assert("letterordigit", Char.IsLetterOrDigit(c5));
\r
280 Assert("letterordigit", Char.IsLetterOrDigit(c6));
\r
282 string s1 = " " + c1 + c2 + c3 + c4 + c5 + c6;
\r
283 Assert("Not letterordigit", !Char.IsLetterOrDigit(s1, 0));
\r
284 Assert("letterordigit", Char.IsLetterOrDigit(s1, 1));
\r
285 Assert("letterordigit", Char.IsLetterOrDigit(s1, 2));
\r
286 Assert("letterordigit", Char.IsLetterOrDigit(s1, 3));
\r
287 Assert("letterordigit", Char.IsLetterOrDigit(s1, 4));
\r
288 Assert("letterordigit", Char.IsLetterOrDigit(s1, 5));
\r
289 Assert("letterordigit", Char.IsLetterOrDigit(s1, 6));
\r
292 public void TestIsLower()
\r
296 Assert("Not lower", !Char.IsLower(' '));
\r
297 Assert("lower", Char.IsLower(c1));
\r
298 Assert("lower", Char.IsLower(c2));
\r
300 string s1 = " " + c1 + c2;
\r
301 Assert("Not lower", !Char.IsLower(s1, 0));
\r
302 Assert("lower", Char.IsLower(s1, 1));
\r
303 Assert("lower", Char.IsLower(s1, 2));
\r
306 public void TestIsNumber()
\r
310 // TODO - IsNumber of less obvious characters
\r
312 Assert("Not number", !Char.IsNumber(' '));
\r
313 Assert("number", Char.IsNumber(c1));
\r
314 Assert("number", Char.IsNumber(c2));
\r
316 string s1 = " " + c1 + c2;
\r
317 Assert("Not number", !Char.IsNumber(s1, 0));
\r
318 Assert("number", Char.IsNumber(s1, 1));
\r
319 Assert("number", Char.IsNumber(s1, 2));
\r
322 public void TestIsPunctuation()
\r
326 Assert("Not punctuation", !Char.IsPunctuation(' '));
\r
327 Assert("punctuation", Char.IsPunctuation(c1));
\r
328 Assert("punctuation", Char.IsPunctuation(c2));
\r
330 string s1 = " " + c1 + c2;
\r
331 Assert("Not punctuation", !Char.IsPunctuation(s1, 0));
\r
332 Assert("punctuation", Char.IsPunctuation(s1, 1));
\r
333 Assert("punctuation", Char.IsPunctuation(s1, 2));
\r
336 public void TestIsSeparator()
\r
340 Assert("Not separator", !Char.IsSeparator('.'));
\r
341 Assert("separator1", Char.IsSeparator(c1));
\r
343 string s1 = "." + c1;
\r
344 Assert("Not separator", !Char.IsSeparator(s1, 0));
\r
345 Assert("separator1-2", Char.IsSeparator(s1, 1));
\r
348 public void TestIsSurrogate()
\r
350 // high surrogate - D800-DBFF
\r
351 // low surrogate - DC00-DEFF
\r
352 char c1 = (char)0xD800;
\r
353 char c2 = (char)0xDBFF;
\r
354 char c3 = (char)0xDC00;
\r
355 char c4 = (char)0xDEFF;
\r
356 Assert("Not surrogate", !Char.IsSurrogate(' '));
\r
357 Assert("surrogate1", Char.IsSurrogate(c1));
\r
358 Assert("surrogate2", Char.IsSurrogate(c2));
\r
359 Assert("surrogate3", Char.IsSurrogate(c3));
\r
360 Assert("surrogate4", Char.IsSurrogate(c4));
\r
362 string s1 = " " + c1 + c2 + c3 + c4;
\r
363 Assert("Not surrogate", !Char.IsSurrogate(s1, 0));
\r
364 Assert("surrogate1-2", Char.IsSurrogate(s1, 1));
\r
365 Assert("surrogate2-2", Char.IsSurrogate(s1, 2));
\r
366 Assert("surrogate3-2", Char.IsSurrogate(s1, 3));
\r
367 Assert("surrogate4-2", Char.IsSurrogate(s1, 4));
\r
370 public void TestIsSymbol()
\r
374 Assert("Not symbol", !Char.IsSymbol(' '));
\r
375 Assert("symbol", Char.IsSymbol(c1));
\r
376 Assert("symbol", Char.IsSymbol(c2));
\r
378 string s1 = " " + c1 + c2;
\r
379 Assert("Not symbol", !Char.IsSymbol(s1, 0));
\r
380 Assert("symbol", Char.IsSymbol(s1, 1));
\r
381 Assert("symbol", Char.IsSymbol(s1, 2));
\r
384 public void TestIsUpper()
\r
388 Assert("Not upper", !Char.IsUpper('a'));
\r
389 Assert("upper", Char.IsUpper(c1));
\r
390 Assert("upper", Char.IsUpper(c2));
\r
392 string s1 = "a" + c1 + c2;
\r
393 Assert("Not upper", !Char.IsUpper(s1, 0));
\r
394 Assert("upper", Char.IsUpper(s1, 1));
\r
395 Assert("upper", Char.IsUpper(s1, 2));
\r
398 public void TestIsWhiteSpace()
\r
404 Assert("Not whitespace", !Char.IsWhiteSpace('.'));
\r
405 Assert("whitespace1", Char.IsWhiteSpace(c1));
\r
406 Assert("whitespace2", Char.IsWhiteSpace(c2));
\r
407 Assert("whitespace3", Char.IsWhiteSpace(c3));
\r
409 string s1 = "." + c1 + c2 + c3;
\r
410 Assert("Not whitespace", !Char.IsWhiteSpace(s1, 0));
\r
411 Assert("whitespace1-2", Char.IsWhiteSpace(s1, 1));
\r
412 Assert("whitespace2-2", Char.IsWhiteSpace(s1, 2));
\r
413 Assert("whitespace3-2", Char.IsWhiteSpace(s1, 3));
\r
417 public void IsWhiteSpace_Complete ()
\r
419 for (int i=0; i < UInt16.MaxValue; i++) {
\r
420 Char c = Convert.ToChar (i);
\r
450 Assert (i.ToString (), Char.IsWhiteSpace (c));
\r
453 Assert (i.ToString (), !Char.IsWhiteSpace (c));
\r
459 public void TestParse()
\r
463 Assert(c1.Equals(Char.Parse(s1)));
\r
467 public void TestTryParseValid ()
\r
473 AssertEquals ("TryParse1", true, Char.TryParse (s1, out c2));
\r
474 AssertEquals ("TryParse2", c2, c1);
\r
477 public void TestTryParseInvalid ()
\r
481 AssertEquals ("TryParse3", false, Char.TryParse (s, out c));
\r
482 AssertEquals ("TryParse4", '\0', c);
\r
486 public void TestToLower()
\r
500 AssertEquals("char lowered", b1, Char.ToLower(a1));
\r
501 AssertEquals("char lowered", b2, Char.ToLower(a2));
\r
502 AssertEquals("char lowered", b3, Char.ToLower(a3));
\r
503 AssertEquals("char lowered", b4, Char.ToLower(a4));
\r
504 AssertEquals("char lowered", b5, Char.ToLower(a5));
\r
505 AssertEquals("char lowered", b6, Char.ToLower(a6));
\r
508 public void TestToUpper()
\r
522 AssertEquals("char uppered", b1, Char.ToUpper(a1));
\r
523 AssertEquals("char uppered", b2, Char.ToUpper(a2));
\r
524 AssertEquals("char uppered", b3, Char.ToUpper(a3));
\r
525 AssertEquals("char uppered", b4, Char.ToUpper(a4));
\r
526 AssertEquals("char uppered", b5, Char.ToUpper(a5));
\r
527 AssertEquals("char uppered", b6, Char.ToUpper(a6));
\r
531 public void TestToString()
\r
535 Assert(s1.Equals(c1.ToString()));
\r
538 public void TestGetTypeCode()
\r
541 Assert(c1.GetTypeCode().Equals(TypeCode.Char));
\r
545 public void TestConvertFromUtf32 ()
\r
547 AssertEquals ("#1", "A", Char.ConvertFromUtf32 (0x41));
\r
548 AssertEquals ("#2", "\uD800\uDC00", Char.ConvertFromUtf32 (0x10000));
\r
551 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
552 public void TestConvertFromUtf32Fail1 ()
\r
554 Char.ConvertFromUtf32 (-1);
\r
557 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
558 public void TestConvertFromUtf32Fail2 ()
\r
560 Char.ConvertFromUtf32 (0x110001);
\r
563 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
564 public void TestConvertFromUtf32Fail3 ()
\r
566 Char.ConvertFromUtf32 (0xD800);
\r
569 public void TestConvertToUtf32 ()
\r
571 AssertEquals ("#1", 0x10000, Char.ConvertToUtf32 ('\uD800', '\uDC00'));
\r
572 AssertEquals ("#2", 0x10FFFF, Char.ConvertToUtf32 ('\uDBFF', '\uDFFF'));
\r
575 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
576 public void TestConvertToUtf32Fail1 ()
\r
578 Char.ConvertToUtf32 ('A', '\uDC00');
\r
581 [ExpectedException (typeof (ArgumentOutOfRangeException))]
\r
582 public void TestConvertUtf32Fail2 ()
\r
584 Char.ConvertToUtf32 ('\uD800', '\uD800');
\r