1 // EnumTest.cs - NUnit Test Cases for the System.Enum class
3 // David Brandt (bucky@keystreams.com)
5 // (C) Ximian, Inc. http://www.ximian.com
11 using System
.Reflection
;
14 namespace MonoTests
.System
17 public class EnumTest
: TestCase
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",
40 AssertEquals("An enum should equal a copy",
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;
53 } catch (ArgumentException
) {
56 Assert("1) Compare type mismatch not caught.",
60 bool errorThrown
= false;
62 ((Enum
)e1
).CompareTo((Enum
)e3
);
63 } catch (ArgumentException
) {
66 Assert("2) Compare type mismatch not caught.",
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() {
85 TestingEnum x
= TestingEnum
.Test
;
86 Enum
.Format(null, x
, "G");
87 Fail("null first arg not caught.");
88 } catch (ArgumentNullException
) {
90 } catch (Exception e
) {
91 Fail("first arg null, wrong exception: " + e
.ToString());
95 Enum
.Format(typeof(TestingEnum
), null, "G");
96 Fail("null second arg not caught.");
97 } catch (ArgumentNullException
) {
99 } catch (Exception e
) {
100 Fail("second arg null, wrong exception: " + e
.ToString());
104 TestingEnum x
= TestingEnum
.Test
;
105 Enum
.Format(x
.GetType(), x
, null);
106 Fail("null third arg not caught.");
107 } catch (ArgumentNullException
) {
109 } catch (Exception e
) {
110 Fail("third arg null, wrong exception: " + e
.ToString());
114 TestingEnum x
= TestingEnum
.Test
;
115 Enum
.Format(typeof(string), x
, "G");
116 Fail("bad type arg not caught.");
117 } catch (ArgumentException
) {
119 } catch (Exception e
) {
120 Fail("bad type, wrong exception: " + e
.ToString());
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
) {
130 } catch (Exception e
) {
131 Fail("wrong enum type, wrong exception: " + e
.ToString());
136 TestingEnum x
= TestingEnum
.Test
;
137 Enum
.Format(x
.GetType(), bad
, "G");
138 Fail("non-enum object not caught.");
139 } catch (ArgumentException
) {
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",
149 foreach (string code
in codes
) {
151 TestingEnum x
= TestingEnum
.Test
;
152 Enum
.Format(x
.GetType(), x
, code
);
153 Fail ("bad format code not caught - " + code
);
154 } catch (FormatException
) {
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;
199 TestingEnum x
= TestingEnum
.Test
;
200 Enum
.GetName(null, x
);
201 } catch (ArgumentNullException
) {
204 Assert("null first arg not caught.",
208 bool errorThrown
= false;
210 TestingEnum x
= TestingEnum
.Test
;
211 Enum
.GetName(x
.GetType(), null);
212 } catch (ArgumentNullException
) {
215 Assert("null second arg not caught.",
219 bool errorThrown
= false;
222 TestingEnum x
= TestingEnum
.Test
;
223 Enum
.GetName(bad
.GetType(), x
);
224 } catch (ArgumentException
) {
227 Assert("non-enum type not caught.",
231 bool errorThrown
= false;
233 TestingEnum x
= TestingEnum
.Test
;
234 TestingEnum2 y
= TestingEnum2
.Test
;
235 Enum
.GetName(y
.GetType(), x
);
236 } catch (ArgumentException
) {
239 Assert("wrong enum type not caught.",
243 bool errorThrown
= false;
246 TestingEnum x
= TestingEnum
.Test
;
247 Enum
.GetName(x
.GetType(), bad
);
248 } catch (ArgumentException
) {
251 Assert("non-enum object not caught.",
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;
273 } catch (ArgumentNullException
) {
276 Assert("null type not caught.",
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",
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;
307 Enum
.GetUnderlyingType(null);
308 } catch (ArgumentNullException
) {
311 Assert("null type not caught.",
315 bool errorThrown
= false;
318 Enum
.GetUnderlyingType(bad
.GetType());
319 } catch (ArgumentException
) {
322 Assert("non-enum type not caught.",
328 Enum t1
= new TestingEnum();
329 Enum t2
= new TestShortEnum();
330 AssertEquals("Wrong default underlying type",
332 Enum
.GetUnderlyingType(t1
.GetType()));
333 AssertEquals("Not short underlying type",
335 Enum
.GetUnderlyingType(t2
.GetType()));
339 public void TestGetValues() {
341 bool errorThrown
= false;
343 Enum
.GetValues(null);
344 } catch (ArgumentNullException
) {
347 Assert("null type not caught.",
351 bool errorThrown
= false;
354 Enum
.GetValues(bad
.GetType());
355 } catch (ArgumentException
) {
358 Assert("non-enum type not caught.",
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",
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",
381 public void TestIsDefined() {
383 bool errorThrown
= false;
385 Enum
.IsDefined(null, 1);
386 } catch (ArgumentNullException
) {
389 Assert("null first arg not caught.",
393 bool errorThrown
= false;
395 TestingEnum x
= TestingEnum
.Test
;
396 Enum
.IsDefined(x
.GetType(), null);
397 } catch (ArgumentNullException
) {
400 Assert("null second arg not caught.",
404 bool errorThrown
= false;
408 Enum
.IsDefined(bad
.GetType(), i
);
409 } catch (ArgumentException
) {
412 Assert("non-enum type not caught.",
417 TestingEnum x
= TestingEnum
.Test
;
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;
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.",
440 Enum t1
= new TestingEnum();
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;
456 String name
= "huh?";
457 Enum
.Parse(null, name
);
458 } catch (ArgumentNullException
) {
461 Assert("null first arg not caught.",
465 bool errorThrown
= false;
467 TestingEnum x
= TestingEnum
.Test
;
468 Enum
.Parse(x
.GetType(), null);
469 } catch (ArgumentNullException
) {
472 Assert("null second arg not caught.",
476 bool errorThrown
= false;
479 Enum
.Parse(bad
.GetType(), bad
);
480 } catch (ArgumentException
) {
483 Assert("non-enum type not caught.",
487 bool errorThrown
= false;
489 TestingEnum x
= TestingEnum
.Test
;
491 Enum
.Parse(x
.GetType(), bad
);
492 } catch (ArgumentException
) {
495 Assert("empty string not caught.",
499 bool errorThrown
= false;
501 TestingEnum x
= TestingEnum
.Test
;
503 Enum
.Parse(x
.GetType(), bad
);
504 } catch (ArgumentException
) {
507 Assert("space-only string not caught.",
511 bool errorThrown
= false;
514 TestingEnum x
= TestingEnum
.Test
;
515 Enum
.Parse(x
.GetType(), bad
);
516 } catch (ArgumentException
) {
519 Assert("not-in-enum error not caught.",
523 TestingEnum t1
= new TestingEnum();
524 AssertEquals("parse first enum",
526 Enum
.Parse(t1
.GetType(), "This"));
527 AssertEquals("parse second enum",
529 Enum
.Parse(t1
.GetType(), "Is"));
530 AssertEquals("parse third enum",
532 Enum
.Parse(t1
.GetType(), "A"));
533 AssertEquals("parse last enum",
535 Enum
.Parse(t1
.GetType(), "Test"));
537 AssertEquals("parse last enum with whitespace",
539 Enum
.Parse(t1
.GetType(), " \n\nTest\t"));
541 AssertEquals("parse bitwise-or enum",
543 Enum
.Parse(t1
.GetType(), "This,Is"));
544 AssertEquals("parse bitwise-or enum",
546 Enum
.Parse(t1
.GetType(), "This,Test"));
547 AssertEquals("parse bitwise-or enum",
549 Enum
.Parse(t1
.GetType(), "This,Is,A"));
551 AssertEquals("parse bitwise-or enum with whitespace",
553 Enum
.Parse(t1
.GetType(), " \n\tThis \t\n, Is,A \n"));
556 public void TestParse2() {
558 bool errorThrown
= true;
560 String name
= "huh?";
561 Enum
.Parse(null, name
, true);
562 } catch (ArgumentNullException
) {
565 Assert("null first arg not caught.",
569 bool errorThrown
= true;
571 TestingEnum x
= TestingEnum
.Test
;
572 Enum
.Parse(x
.GetType(), null, true);
573 } catch (ArgumentNullException
) {
576 Assert("null second arg not caught.",
580 bool errorThrown
= true;
583 Enum
.Parse(bad
.GetType(), bad
, true);
584 } catch (ArgumentException
) {
587 Assert("non-enum type not caught.",
591 bool errorThrown
= true;
593 TestingEnum x
= TestingEnum
.Test
;
595 Enum
.Parse(x
.GetType(), bad
, true);
596 } catch (ArgumentException
) {
599 Assert("empty string not caught.",
603 bool errorThrown
= true;
605 TestingEnum x
= TestingEnum
.Test
;
607 Enum
.Parse(x
.GetType(), bad
, true);
608 } catch (ArgumentException
) {
611 Assert("space-only string not caught.",
615 bool errorThrown
= true;
618 TestingEnum x
= TestingEnum
.Test
;
619 Enum
.Parse(x
.GetType(), bad
, true);
620 } catch (ArgumentException
) {
623 Assert("not-in-enum error not caught.",
627 bool errorThrown
= true;
630 TestingEnum x
= TestingEnum
.Test
;
631 Enum
.Parse(x
.GetType(), bad
, false);
632 } catch (ArgumentException
) {
635 Assert("not-in-enum error not caught.",
639 TestingEnum t1
= new TestingEnum();
640 AssertEquals("parse first enum",
642 Enum
.Parse(t1
.GetType(), "this", true));
643 AssertEquals("parse second enum",
645 Enum
.Parse(t1
.GetType(), "is", true));
646 AssertEquals("parse third enum",
648 Enum
.Parse(t1
.GetType(), "a", true));
649 AssertEquals("parse last enum",
651 Enum
.Parse(t1
.GetType(), "test", true));
653 AssertEquals("parse last enum with whitespace",
655 Enum
.Parse(t1
.GetType(), " \n\ntest\t", true));
657 AssertEquals("parse bitwise-or enum",
659 Enum
.Parse(t1
.GetType(), "This,is", true));
660 AssertEquals("parse bitwise-or enum",
662 Enum
.Parse(t1
.GetType(), "This,test", true));
663 AssertEquals("parse bitwise-or enum",
665 Enum
.Parse(t1
.GetType(), "This,is,A", true));
667 AssertEquals("parse bitwise-or enum with whitespace",
669 Enum
.Parse(t1
.GetType(), " \n\tThis \t\n, is,a \n",
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;
684 Enum
.ToObject(null, 1);
685 } catch (ArgumentNullException
) {
688 Assert("null type not caught.",
692 bool errorThrown
= false;
694 Enum
.ToObject("huh?".GetType(), 1);
695 } catch (ArgumentException
) {
698 Assert("null type not caught.",
702 TestingEnum t1
= new TestingEnum();
703 AssertEquals("Should get object",
705 Enum
.ToObject(t1
.GetType(), 0));
707 // TODO - should probably test all the different underlying types
711 enum SomeEnum {a,b,c}
;
714 enum SomeByteEnum
: byte {a,b,c}
;
717 enum SomeInt64Enum
: long {a,b,c}
;
719 public void TestToString() {
723 AssertEquals("invalid string", "This",
724 TestingEnum
.This
.ToString());
726 AssertEquals("invalid string", "Is",
727 TestingEnum
.Is
.ToString());
729 AssertEquals("invalid string", "A",
730 TestingEnum
.A
.ToString());
732 AssertEquals("invalid string", "Test",
733 TestingEnum
.Test
.ToString());
735 Enum is1
= TestingEnum
.Is
;
738 AssertEquals("decimal parse wrong",
739 "1", is1
.ToString("d"));
741 AssertEquals("named format wrong",
742 "Is", is1
.ToString("g"));
744 AssertEquals("hex format wrong",
745 "00000001", is1
.ToString("x"));
747 AssertEquals("bitfield format wrong",
748 "Is", is1
.ToString("f"));
751 AssertEquals("bitfield with flags format wrong for Int32 enum",
752 "b, c", ((SomeEnum
)3).ToString("f"));
754 AssertEquals("bitfield with flags format wrong for Byte enum",
755 "b, c", ((SomeByteEnum
)3).ToString("f"));
757 AssertEquals("bitfield with flags format wrong for Int64 enum",
758 "b, c", ((SomeInt64Enum
)3).ToString("f"));
761 AssertEquals("bitfield with unknown flags format wrong for Int32 enum",
762 "12", ((SomeEnum
)12).ToString("f"));
764 AssertEquals("bitfield with unknown flags format wrong for Byte enum",
765 "12", ((SomeByteEnum
)12).ToString("f"));
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
);
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
,
799 Enum
.Format (typeof (E
), evalues
[i
], "f"));
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}
818 public void AnotherFormatBugPinned ()
820 Assertion
.AssertEquals ("#01", "100", Enum
.Format (typeof (E3
), 100, "f"));
824 public void LogicBugPinned ()
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