1 // CharTest.cs - NUnit Test Cases for the System.Char struct
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
10 using System
.Globalization
;
12 namespace MonoTests
.System
15 public class CharTest
: TestCase
19 protected override void SetUp()
23 protected override void TearDown()
27 public void TestCompareTo()
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()
43 Assert("Same", c1
.Equals(c1
));
44 Assert("Same value", c2
.Equals(c3
));
45 Assert("Not same", !c1
.Equals(c2
));
48 public void TestGetHashValue()
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()
59 AssertEquals("code 1", -1.0, Char
.GetNumericValue(c1
), 0.1);
60 AssertEquals("code 2", 3.0, Char
.GetNumericValue(c2
), 0.1);
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()
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?
106 AssertEquals("Currency",
107 UnicodeCategory
.CurrencySymbol
,
108 Char
.GetUnicodeCategory(c1
));
112 AssertEquals("Dash Punctuation",
113 UnicodeCategory
.DashPunctuation
,
114 Char
.GetUnicodeCategory(c1
));
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
129 // TODO - InitialQuotePunctuation
130 // TODO - LetterNumber
131 // TODO - LineSeparator (not '\n', that's a control char)
135 AssertEquals("LowercaseLetter",
136 UnicodeCategory
.LowercaseLetter
,
137 Char
.GetUnicodeCategory(c1
));
138 AssertEquals("LowercaseLetter",
139 UnicodeCategory
.LowercaseLetter
,
140 Char
.GetUnicodeCategory(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
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
174 AssertEquals("OtherPunctuation",
175 UnicodeCategory
.OtherPunctuation
,
176 Char
.GetUnicodeCategory(c1
));
178 // TODO - OtherSymbol
179 // TODO - ParagraphSeparator
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
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
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
430 Assert(c1
.Equals(Char
.Parse(s1
)));
433 public void TestToLower()
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()
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()
482 Assert(s1
.Equals(c1
.ToString()));
485 public void TestGetTypeCode()
488 Assert(c1
.GetTypeCode().Equals(TypeCode
.Char
));