(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / Test / System / EnumTest.cs
bloba720df625d79204855fb0710bca53a0d60c4bba6
1 // EnumTest.cs - NUnit Test Cases for the System.Enum class
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.IO;
11 using System.Reflection;
14 namespace MonoTests.System
17 public class EnumTest : TestCase
19 public EnumTest() {}
21 protected override void SetUp()
25 protected override void TearDown()
29 enum TestingEnum {This, Is, A, Test};
30 enum TestingEnum2 {This, Is, A, Test};
31 enum TestingEnum3: ulong {This, Is, A, Test = ulong.MaxValue };
33 public void TestCompareTo() {
34 Enum e1 = new TestingEnum();
35 Enum e2 = new TestingEnum();
36 Enum e3 = new TestingEnum2();
38 AssertEquals("An enum should equal itself",
39 0, e1.CompareTo(e1));
40 AssertEquals("An enum should equal a copy",
41 0, e1.CompareTo(e2));
43 TestingEnum x = TestingEnum.This;
44 TestingEnum y = TestingEnum.Is;
45 AssertEquals("should equal", 0, x.CompareTo(x));
46 AssertEquals("less than", -1, x.CompareTo(y));
47 AssertEquals("greater than", 1, y.CompareTo(x));
50 bool errorThrown = false;
51 try {
52 e1.CompareTo(e3);
53 } catch (ArgumentException) {
54 errorThrown = true;
56 Assert("1) Compare type mismatch not caught.",
57 errorThrown);
60 bool errorThrown = false;
61 try {
62 ((Enum)e1).CompareTo((Enum)e3);
63 } catch (ArgumentException) {
64 errorThrown = true;
66 Assert("2) Compare type mismatch not caught.",
67 errorThrown);
71 public void TestEquals() {
72 Enum e1 = new TestingEnum();
73 Enum e2 = new TestingEnum();
74 Enum e3 = new TestingEnum2();
76 Assert("An enum should equal itself", e1.Equals(e1));
77 Assert("An enum should equal a copy", e1.Equals(e2));
79 Assert("Shouldn't match", !e1.Equals(e3));
80 Assert("Shouldn't match null", !e1.Equals(null));
83 public void TestFormat_Args() {
84 try {
85 TestingEnum x = TestingEnum.Test;
86 Enum.Format(null, x, "G");
87 Fail("null first arg not caught.");
88 } catch (ArgumentNullException) {
89 // do nothing
90 } catch (Exception e) {
91 Fail("first arg null, wrong exception: " + e.ToString());
94 try {
95 Enum.Format(typeof(TestingEnum), null, "G");
96 Fail("null second arg not caught.");
97 } catch (ArgumentNullException) {
98 // do nothing
99 } catch (Exception e) {
100 Fail("second arg null, wrong exception: " + e.ToString());
103 try {
104 TestingEnum x = TestingEnum.Test;
105 Enum.Format(x.GetType(), x, null);
106 Fail("null third arg not caught.");
107 } catch (ArgumentNullException) {
108 // do nothing
109 } catch (Exception e) {
110 Fail("third arg null, wrong exception: " + e.ToString());
113 try {
114 TestingEnum x = TestingEnum.Test;
115 Enum.Format(typeof(string), x, "G");
116 Fail("bad type arg not caught.");
117 } catch (ArgumentException) {
118 // do nothing
119 } catch (Exception e) {
120 Fail("bad type, wrong exception: " + e.ToString());
123 try {
124 TestingEnum x = TestingEnum.Test;
125 TestingEnum2 y = TestingEnum2.Test;
126 Enum.Format(y.GetType(), x, "G");
127 Fail("wrong enum type not caught.");
128 } catch (ArgumentException) {
129 // do nothing
130 } catch (Exception e) {
131 Fail("wrong enum type, wrong exception: " + e.ToString());
134 try {
135 String bad = "huh?";
136 TestingEnum x = TestingEnum.Test;
137 Enum.Format(x.GetType(), bad, "G");
138 Fail("non-enum object not caught.");
139 } catch (ArgumentException) {
140 // do nothing
141 } catch (Exception e) {
142 Fail("non-enum object, wrong exception: " + e.ToString());
145 string[] codes = {"a", "b", "c", "ad", "e", "af", "ag", "h",
146 "i", "j", "k", "l", "m", "n", "o", "p",
147 "q", "r", "s", "t", "u", "v", "w", "ax",
148 "y", "z"};
149 foreach (string code in codes) {
150 try {
151 TestingEnum x = TestingEnum.Test;
152 Enum.Format(x.GetType(), x, code);
153 Fail ("bad format code not caught - " + code);
154 } catch (FormatException) {
155 // do nothing
156 } catch (Exception e) {
157 Fail (String.Format ("bad format code ({0}), wrong exception: {1}",
158 code, e.ToString()));
162 TestingEnum ex = TestingEnum.Test;
163 AssertEquals("decimal format wrong",
164 "3", Enum.Format(ex.GetType(), ex, "d"));
165 AssertEquals("decimal format wrong for ulong enums",
166 "18446744073709551615", Enum.Format(typeof(TestingEnum3), TestingEnum3.Test, "d"));
167 AssertEquals("named format wrong",
168 "Test", Enum.Format(ex.GetType(), ex, "g"));
169 AssertEquals("hex format wrong",
170 "00000003", Enum.Format(ex.GetType(), ex, "x"));
171 AssertEquals("bitfield format wrong",
172 "Test", Enum.Format(ex.GetType(), ex, "f"));
175 public void TestFormat_FormatSpecifier ()
177 ParameterAttributes pa =
178 ParameterAttributes.In | ParameterAttributes.HasDefault;
179 const string fFormatOutput = "In, HasDefault";
180 const string xFormatOutput = "00001001";
181 string fOutput = Enum.Format (pa.GetType(), pa, "f");
182 AssertEquals ("#F_FS:f", fFormatOutput, fOutput);
183 string xOutput = Enum.Format (pa.GetType(), pa, "x");
184 AssertEquals ("#F_FS:x", xFormatOutput, xOutput);
187 public void TestGetHashCode() {
188 Enum e1 = new TestingEnum();
189 Enum e2 = new TestingEnum2();
191 AssertEquals("hash code is deterministic",
192 e1.GetHashCode(), e1.GetHashCode());
195 public void GetName() {
197 bool errorThrown = false;
198 try {
199 TestingEnum x = TestingEnum.Test;
200 Enum.GetName(null, x);
201 } catch (ArgumentNullException) {
202 errorThrown = true;
204 Assert("null first arg not caught.",
205 errorThrown);
208 bool errorThrown = false;
209 try {
210 TestingEnum x = TestingEnum.Test;
211 Enum.GetName(x.GetType(), null);
212 } catch (ArgumentNullException) {
213 errorThrown = true;
215 Assert("null second arg not caught.",
216 errorThrown);
219 bool errorThrown = false;
220 try {
221 String bad = "huh?";
222 TestingEnum x = TestingEnum.Test;
223 Enum.GetName(bad.GetType(), x);
224 } catch (ArgumentException) {
225 errorThrown = true;
227 Assert("non-enum type not caught.",
228 errorThrown);
231 bool errorThrown = false;
232 try {
233 TestingEnum x = TestingEnum.Test;
234 TestingEnum2 y = TestingEnum2.Test;
235 Enum.GetName(y.GetType(), x);
236 } catch (ArgumentException) {
237 errorThrown = true;
239 Assert("wrong enum type not caught.",
240 errorThrown);
243 bool errorThrown = false;
244 try {
245 String bad = "huh?";
246 TestingEnum x = TestingEnum.Test;
247 Enum.GetName(x.GetType(), bad);
248 } catch (ArgumentException) {
249 errorThrown = true;
251 Assert("non-enum object not caught.",
252 errorThrown);
255 TestingEnum x = TestingEnum.This;
256 TestingEnum y = TestingEnum.Is;
257 TestingEnum z = TestingEnum.A;
259 AssertEquals("first name doesn't match",
260 "This", Enum.GetName(x.GetType(), x));
261 AssertEquals("second name doesn't match",
262 "Is", Enum.GetName(y.GetType(), y));
263 AssertEquals("third name doesn't match",
264 "A", Enum.GetName(z.GetType(), z));
268 public void TestGetNames() {
270 bool errorThrown = false;
271 try {
272 Enum.GetNames(null);
273 } catch (ArgumentNullException) {
274 errorThrown = true;
276 Assert("null type not caught.",
277 errorThrown);
280 TestingEnum x = TestingEnum.This;
281 string[] match = {"This", "Is", "A", "Test"};
282 string[] names = Enum.GetNames(x.GetType());
283 AssertNotNull("Got no names", names);
284 AssertEquals("names wrong size",
285 match.Length, names.Length);
286 for (int i = 0; i < names.Length; i++) {
287 AssertEquals("name mismatch",
288 match[i], names[i]);
293 public void TestGetTypeCode() {
294 TestingEnum x = TestingEnum.This;
295 TestingEnum y = new TestingEnum();
296 AssertEquals("01 bad type code",
297 TypeCode.Int32, x.GetTypeCode());
298 AssertEquals("02 bad type code",
299 TypeCode.Int32, y.GetTypeCode());
302 enum TestShortEnum : short { zero, one, two, three, four, five, six};
303 public void TestGetUnderlyingType() {
305 bool errorThrown = false;
306 try {
307 Enum.GetUnderlyingType(null);
308 } catch (ArgumentNullException) {
309 errorThrown = true;
311 Assert("null type not caught.",
312 errorThrown);
315 bool errorThrown = false;
316 try {
317 String bad = "huh?";
318 Enum.GetUnderlyingType(bad.GetType());
319 } catch (ArgumentException) {
320 errorThrown = true;
322 Assert("non-enum type not caught.",
323 errorThrown);
326 short sh = 5;
327 int i = 5;
328 Enum t1 = new TestingEnum();
329 Enum t2 = new TestShortEnum();
330 AssertEquals("Wrong default underlying type",
331 i.GetType(),
332 Enum.GetUnderlyingType(t1.GetType()));
333 AssertEquals("Not short underlying type",
334 sh.GetType(),
335 Enum.GetUnderlyingType(t2.GetType()));
339 public void TestGetValues() {
341 bool errorThrown = false;
342 try {
343 Enum.GetValues(null);
344 } catch (ArgumentNullException) {
345 errorThrown = true;
347 Assert("null type not caught.",
348 errorThrown);
351 bool errorThrown = false;
352 try {
353 String bad = "huh?";
354 Enum.GetValues(bad.GetType());
355 } catch (ArgumentException) {
356 errorThrown = true;
358 Assert("non-enum type not caught.",
359 errorThrown);
362 Enum t1 = new TestingEnum();
363 Array a1 = Enum.GetValues(t1.GetType());
364 for (int i= 0; i < a1.Length; i++) {
365 AssertEquals("wrong enum value",
366 (TestingEnum)i,
367 a1.GetValue(i));
371 Enum t1 = new TestShortEnum();
372 Array a1 = Enum.GetValues(t1.GetType());
373 for (short i= 0; i < a1.Length; i++) {
374 AssertEquals("wrong short enum value",
375 (TestShortEnum)i,
376 a1.GetValue(i));
381 public void TestIsDefined() {
383 bool errorThrown = false;
384 try {
385 Enum.IsDefined(null, 1);
386 } catch (ArgumentNullException) {
387 errorThrown = true;
389 Assert("null first arg not caught.",
390 errorThrown);
393 bool errorThrown = false;
394 try {
395 TestingEnum x = TestingEnum.Test;
396 Enum.IsDefined(x.GetType(), null);
397 } catch (ArgumentNullException) {
398 errorThrown = true;
400 Assert("null second arg not caught.",
401 errorThrown);
404 bool errorThrown = false;
405 try {
406 String bad = "huh?";
407 int i = 4;
408 Enum.IsDefined(bad.GetType(), i);
409 } catch (ArgumentException) {
410 errorThrown = true;
412 Assert("non-enum type not caught.",
413 errorThrown);
416 try {
417 TestingEnum x = TestingEnum.Test;
418 short i = 4;
419 Enum.IsDefined(x.GetType(), i);
420 Fail("wrong underlying type not caught.");
421 } catch (ArgumentException) {
422 } catch (Exception e) {
423 Fail("wrong Exception thrown ("+e.ToString()+")for underlying type not caught.");
426 // spec says yes, MS impl says no.
428 //bool errorThrown = false;
429 //try {
430 //String bad = "huh?";
431 //TestingEnum x = TestingEnum.Test;
432 //Enum.IsDefined(x.GetType(), bad);
433 //} catch (ExecutionEngineException) {
434 //errorThrown = true;
436 //Assert("non-enum object not caught.",
437 //errorThrown);
440 Enum t1 = new TestingEnum();
441 int i = 0;
442 for (i = 0;
443 i < Enum.GetValues(t1.GetType()).Length; i++) {
444 Assert("should have value for i=" + i,
445 Enum.IsDefined(t1.GetType(), i));
447 Assert("Shouldn't have value",
448 !Enum.IsDefined(t1.GetType(), i));
452 public void TestParse1() {
454 bool errorThrown = false;
455 try {
456 String name = "huh?";
457 Enum.Parse(null, name);
458 } catch (ArgumentNullException) {
459 errorThrown = true;
461 Assert("null first arg not caught.",
462 errorThrown);
465 bool errorThrown = false;
466 try {
467 TestingEnum x = TestingEnum.Test;
468 Enum.Parse(x.GetType(), null);
469 } catch (ArgumentNullException) {
470 errorThrown = true;
472 Assert("null second arg not caught.",
473 errorThrown);
476 bool errorThrown = false;
477 try {
478 String bad = "huh?";
479 Enum.Parse(bad.GetType(), bad);
480 } catch (ArgumentException) {
481 errorThrown = true;
483 Assert("non-enum type not caught.",
484 errorThrown);
487 bool errorThrown = false;
488 try {
489 TestingEnum x = TestingEnum.Test;
490 String bad = "";
491 Enum.Parse(x.GetType(), bad);
492 } catch (ArgumentException) {
493 errorThrown = true;
495 Assert("empty string not caught.",
496 errorThrown);
499 bool errorThrown = false;
500 try {
501 TestingEnum x = TestingEnum.Test;
502 String bad = " ";
503 Enum.Parse(x.GetType(), bad);
504 } catch (ArgumentException) {
505 errorThrown = true;
507 Assert("space-only string not caught.",
508 errorThrown);
511 bool errorThrown = false;
512 try {
513 String bad = "huh?";
514 TestingEnum x = TestingEnum.Test;
515 Enum.Parse(x.GetType(), bad);
516 } catch (ArgumentException) {
517 errorThrown = true;
519 Assert("not-in-enum error not caught.",
520 errorThrown);
523 TestingEnum t1 = new TestingEnum();
524 AssertEquals("parse first enum",
525 TestingEnum.This,
526 Enum.Parse(t1.GetType(), "This"));
527 AssertEquals("parse second enum",
528 TestingEnum.Is,
529 Enum.Parse(t1.GetType(), "Is"));
530 AssertEquals("parse third enum",
531 TestingEnum.A,
532 Enum.Parse(t1.GetType(), "A"));
533 AssertEquals("parse last enum",
534 TestingEnum.Test,
535 Enum.Parse(t1.GetType(), "Test"));
537 AssertEquals("parse last enum with whitespace",
538 TestingEnum.Test,
539 Enum.Parse(t1.GetType(), " \n\nTest\t"));
541 AssertEquals("parse bitwise-or enum",
542 TestingEnum.Is,
543 Enum.Parse(t1.GetType(), "This,Is"));
544 AssertEquals("parse bitwise-or enum",
545 TestingEnum.Test,
546 Enum.Parse(t1.GetType(), "This,Test"));
547 AssertEquals("parse bitwise-or enum",
548 TestingEnum.Test,
549 Enum.Parse(t1.GetType(), "This,Is,A"));
551 AssertEquals("parse bitwise-or enum with whitespace",
552 TestingEnum.Test,
553 Enum.Parse(t1.GetType(), " \n\tThis \t\n, Is,A \n"));
556 public void TestParse2() {
558 bool errorThrown = true;
559 try {
560 String name = "huh?";
561 Enum.Parse(null, name, true);
562 } catch (ArgumentNullException) {
563 errorThrown = true;
565 Assert("null first arg not caught.",
566 errorThrown);
569 bool errorThrown = true;
570 try {
571 TestingEnum x = TestingEnum.Test;
572 Enum.Parse(x.GetType(), null, true);
573 } catch (ArgumentNullException) {
574 errorThrown = true;
576 Assert("null second arg not caught.",
577 errorThrown);
580 bool errorThrown = true;
581 try {
582 String bad = "huh?";
583 Enum.Parse(bad.GetType(), bad, true);
584 } catch (ArgumentException) {
585 errorThrown = true;
587 Assert("non-enum type not caught.",
588 errorThrown);
591 bool errorThrown = true;
592 try {
593 TestingEnum x = TestingEnum.Test;
594 String bad = "";
595 Enum.Parse(x.GetType(), bad, true);
596 } catch (ArgumentException) {
597 errorThrown = true;
599 Assert("empty string not caught.",
600 errorThrown);
603 bool errorThrown = true;
604 try {
605 TestingEnum x = TestingEnum.Test;
606 String bad = " ";
607 Enum.Parse(x.GetType(), bad, true);
608 } catch (ArgumentException) {
609 errorThrown = true;
611 Assert("space-only string not caught.",
612 errorThrown);
615 bool errorThrown = true;
616 try {
617 String bad = "huh?";
618 TestingEnum x = TestingEnum.Test;
619 Enum.Parse(x.GetType(), bad, true);
620 } catch (ArgumentException) {
621 errorThrown = true;
623 Assert("not-in-enum error not caught.",
624 errorThrown);
627 bool errorThrown = true;
628 try {
629 String bad = "test";
630 TestingEnum x = TestingEnum.Test;
631 Enum.Parse(x.GetType(), bad, false);
632 } catch (ArgumentException) {
633 errorThrown = true;
635 Assert("not-in-enum error not caught.",
636 errorThrown);
639 TestingEnum t1 = new TestingEnum();
640 AssertEquals("parse first enum",
641 TestingEnum.This,
642 Enum.Parse(t1.GetType(), "this", true));
643 AssertEquals("parse second enum",
644 TestingEnum.Is,
645 Enum.Parse(t1.GetType(), "is", true));
646 AssertEquals("parse third enum",
647 TestingEnum.A,
648 Enum.Parse(t1.GetType(), "a", true));
649 AssertEquals("parse last enum",
650 TestingEnum.Test,
651 Enum.Parse(t1.GetType(), "test", true));
653 AssertEquals("parse last enum with whitespace",
654 TestingEnum.Test,
655 Enum.Parse(t1.GetType(), " \n\ntest\t", true));
657 AssertEquals("parse bitwise-or enum",
658 TestingEnum.Is,
659 Enum.Parse(t1.GetType(), "This,is", true));
660 AssertEquals("parse bitwise-or enum",
661 TestingEnum.Test,
662 Enum.Parse(t1.GetType(), "This,test", true));
663 AssertEquals("parse bitwise-or enum",
664 TestingEnum.Test,
665 Enum.Parse(t1.GetType(), "This,is,A", true));
667 AssertEquals("parse bitwise-or enum with whitespace",
668 TestingEnum.Test,
669 Enum.Parse(t1.GetType(), " \n\tThis \t\n, is,a \n",
670 true));
674 [Test]
675 public void ParseValue() {
676 TestingEnum3 t1 = new TestingEnum3();
677 AssertEquals ("Parse numeric value", TestingEnum3.Test, Enum.Parse(t1.GetType(), "18446744073709551615", false));
680 public void TestToObject() {
682 bool errorThrown = false;
683 try {
684 Enum.ToObject(null, 1);
685 } catch (ArgumentNullException) {
686 errorThrown = true;
688 Assert("null type not caught.",
689 errorThrown);
692 bool errorThrown = false;
693 try {
694 Enum.ToObject("huh?".GetType(), 1);
695 } catch (ArgumentException) {
696 errorThrown = true;
698 Assert("null type not caught.",
699 errorThrown);
702 TestingEnum t1 = new TestingEnum();
703 AssertEquals("Should get object",
704 TestingEnum.This,
705 Enum.ToObject(t1.GetType(), 0));
707 // TODO - should probably test all the different underlying types
710 [Flags]
711 enum SomeEnum {a,b,c};
713 [Flags]
714 enum SomeByteEnum : byte {a,b,c};
716 [Flags]
717 enum SomeInt64Enum : long {a,b,c};
719 public void TestToString() {
720 int i = 0;
721 try {
722 i++;
723 AssertEquals("invalid string", "This",
724 TestingEnum.This.ToString());
725 i++;
726 AssertEquals("invalid string", "Is",
727 TestingEnum.Is.ToString());
728 i++;
729 AssertEquals("invalid string", "A",
730 TestingEnum.A.ToString());
731 i++;
732 AssertEquals("invalid string", "Test",
733 TestingEnum.Test.ToString());
735 Enum is1 = TestingEnum.Is;
737 i++;
738 AssertEquals("decimal parse wrong",
739 "1", is1.ToString("d"));
740 i++;
741 AssertEquals("named format wrong",
742 "Is", is1.ToString("g"));
743 i++;
744 AssertEquals("hex format wrong",
745 "00000001", is1.ToString("x"));
746 i++;
747 AssertEquals("bitfield format wrong",
748 "Is", is1.ToString("f"));
750 i++;
751 AssertEquals("bitfield with flags format wrong for Int32 enum",
752 "b, c", ((SomeEnum)3).ToString("f"));
753 i++;
754 AssertEquals("bitfield with flags format wrong for Byte enum",
755 "b, c", ((SomeByteEnum)3).ToString("f"));
756 i++;
757 AssertEquals("bitfield with flags format wrong for Int64 enum",
758 "b, c", ((SomeInt64Enum)3).ToString("f"));
760 i++;
761 AssertEquals("bitfield with unknown flags format wrong for Int32 enum",
762 "12", ((SomeEnum)12).ToString("f"));
763 i++;
764 AssertEquals("bitfield with unknown flags format wrong for Byte enum",
765 "12", ((SomeByteEnum)12).ToString("f"));
766 i++;
767 AssertEquals("bitfield with unknown flags format wrong for Int64 enum",
768 "12", ((SomeInt64Enum)12).ToString("f"));
770 } catch (Exception e) {
771 Fail ("Unexpected exception at i = " + i + " with e=" + e);
775 enum E {
776 Aa=0,
777 Bb=1,
778 Cc=2,
779 Dd=3,
782 [Flags]
783 enum E2 {
790 [Test]
791 public void FlagTest ()
793 int [] evalues = new int [4] {0,1,2,3};
794 E [] e = new E [4] {E.Aa, E.Bb, E.Cc, E.Dd};
796 for (int i = 0; i < 4; ++i) {
797 Assertion.AssertEquals ("#" + i,
798 e [i].ToString (),
799 Enum.Format (typeof (E), evalues [i], "f"));
804 [Test]
805 public void FlagTest2 () {
806 int invalidValue = 1000;
808 Assertion.AssertEquals ("#01",
809 invalidValue.ToString (),
810 Enum.Format (typeof (E2), invalidValue, "g"));
813 enum E3 {A=0,B=1,C=2,D=3,}
814 enum UE : ulong {A=1,B=2,C=4,D=8,}
815 enum EA {A=0, B=2, C=3, D=4}
817 [Test]
818 public void AnotherFormatBugPinned ()
820 Assertion.AssertEquals ("#01", "100", Enum.Format (typeof (E3), 100, "f"));
823 [Test]
824 public void LogicBugPinned ()
826 string format=null;
827 string[] names=new string[] {"A","B","C","D",};
828 string[] fmtSpl=null;
829 UE ue = UE.A | UE.B | UE.C | UE.D;
831 //all flags must be in format return
832 format = Enum.Format (typeof (UE), ue, "f");
833 fmtSpl = format.Split (',');
834 for( int i=0 ; i<fmtSpl.Length ; ++i )
835 fmtSpl [i] = fmtSpl[i].Trim ();
837 foreach (string nval in fmtSpl)
838 Assertion.Assert (nval + " is not a valid enum value name", Array.IndexOf (names, nval) >= 0);
840 foreach (string nval in names)
841 Assertion.Assert (nval + " is not contained in format return.", Array.IndexOf (fmtSpl, nval) >= 0);
843 // TODO - ToString with IFormatProviders