fixed tests
[mono.git] / mcs / class / corlib / Test / System / EnumTest.cs
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 // 
7
8 using NUnit.Framework;
9 using System;
10 using System.IO;
11 using System.Reflection;
12
13
14 namespace MonoTests.System
15 {
16
17 public class EnumTest : TestCase
18 {
19         public EnumTest() {}
20
21         protected override void SetUp() 
22         {
23         }
24
25         protected override void TearDown() 
26         {
27         }
28
29         enum TestingEnum {This, Is, A, Test};
30         enum TestingEnum2 {This, Is, A, Test};
31         enum TestingEnum3: ulong {This, Is, A, Test = ulong.MaxValue };
32
33         public void TestCompareTo() {
34                 Enum e1 = new TestingEnum();
35                 Enum e2 = new TestingEnum();
36                 Enum e3 = new TestingEnum2();
37
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));
42
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));
48
49                 {
50                         bool errorThrown = false;
51                         try {
52                                 e1.CompareTo(e3);
53                         } catch (ArgumentException) {
54                                 errorThrown = true;
55                         }
56                         Assert("1) Compare type mismatch not caught.", 
57                                errorThrown);
58                 }
59                 {
60                         bool errorThrown = false;
61                         try {
62                                 ((Enum)e1).CompareTo((Enum)e3);
63                         } catch (ArgumentException) {
64                                 errorThrown = true;
65                         }
66                         Assert("2) Compare type mismatch not caught.", 
67                                errorThrown);
68                 }
69         }
70         
71         public void TestEquals() {
72                 Enum e1 = new TestingEnum();
73                 Enum e2 = new TestingEnum();
74                 Enum e3 = new TestingEnum2();
75
76                 Assert("An enum should equal itself", e1.Equals(e1));
77                 Assert("An enum should equal a copy", e1.Equals(e2));
78
79                 Assert("Shouldn't match", !e1.Equals(e3));
80                 Assert("Shouldn't match null", !e1.Equals(null));
81         }
82
83         public void TestFormat_Args() {
84                 try {
85                         TestingEnum x = TestingEnum.Test;
86                         Enum.Format(null, x, "G");
87                         Fail("#A1: null first arg not caught.");
88                 } catch (ArgumentNullException ex) {
89                         AssertEquals ("#A2", "enumType", ex.ParamName);
90                 } catch (Exception e) {
91                         Fail("#A3: first arg null, wrong exception: " + e.ToString());
92                 }
93
94                 try {
95                         Enum.Format(typeof(TestingEnum), null, "G");
96                         Fail("#B1: null second arg not caught.");
97                 } catch (ArgumentNullException ex) {
98                         AssertEquals ("#B2", "value", ex.ParamName);
99                 } catch (Exception e) {
100                         Fail("#B3: second arg null, wrong exception: " + e.ToString());
101                 }
102
103                 try {
104                         TestingEnum x = TestingEnum.Test;
105                         Enum.Format(x.GetType(), x, null);
106                         Fail("#C1: null third arg not caught.");
107                 } catch (ArgumentNullException ex) {
108                         AssertEquals ("#C2", "format", ex.ParamName);
109                 } catch (Exception e) {
110                         Fail("#C3: third arg null, wrong exception: " + e.ToString());
111                 }
112
113                 try {
114                         TestingEnum x = TestingEnum.Test;
115                         Enum.Format(typeof(string), x, "G");
116                         Fail("#D1: bad type arg not caught.");
117                 } catch (ArgumentException) {
118                         // Type provided must be an Enum
119                 } catch (Exception e) {
120                         Fail("#D2: bad type, wrong exception: " + e.ToString());
121                 }
122
123                 try {
124                         TestingEnum x = TestingEnum.Test;
125                         TestingEnum2 y = TestingEnum2.Test;
126                         Enum.Format(y.GetType(), x, "G");
127                         Fail("#E1: wrong enum type not caught.");
128                 } catch (ArgumentException ex) {
129                         // Object must be the same type as the enum. The type passed in was
130                         // MonoTests.System.EnumTest.TestingEnum2; the enum type was
131                         // MonoTests.System.EnumTest.TestingEnum
132                         AssertNotNull ("#E2", ex.Message);
133                         Assert ("#E3", ex.Message.IndexOf (typeof (TestingEnum2).FullName) != -1);
134                         Assert ("#E4", ex.Message.IndexOf (typeof (TestingEnum).FullName) != -1);
135                 } catch (Exception e) {
136                         Fail("#E5: wrong enum type, wrong exception: " + e.ToString());
137                 }
138
139                 try {
140                         String bad = "huh?";
141                         TestingEnum x = TestingEnum.Test;
142                         Enum.Format(x.GetType(), bad, "G");
143                         Fail("#F1: non-enum object not caught.");
144                 } catch (ArgumentException ex) {
145                         // Enum underlying type and the object must be the same type or
146                         // object. Type passed in was String.String; the enum underlying
147                         // was System.Int32
148                         AssertNotNull ("#F2", ex.Message);
149                         Assert ("#F3", ex.Message.IndexOf (typeof (string).FullName) != -1);
150                         Assert ("#F4", ex.Message.IndexOf (typeof (int).FullName) != -1);
151                 } catch (Exception e) {
152                         Fail("#F5: non-enum object, wrong exception: " + e.ToString());
153                 }
154
155                 string[] codes = {"a", "b", "c", "ad", "e", "af", "ag", "h", 
156                                   "i", "j", "k", "l", "m", "n", "o", "p", 
157                                   "q", "r", "s", "t", "u", "v", "w", "ax", 
158                                   "y", "z"};
159                 foreach (string code in codes) {
160                         try {
161                                 TestingEnum x = TestingEnum.Test;
162                                 Enum.Format(x.GetType(), x, code);
163                                 Fail ("bad format code not caught - " + code);
164                         } catch (FormatException) {
165                                 // do nothing
166                         } catch (Exception e) {
167                                 Fail (String.Format ("bad format code ({0}), wrong exception: {1}", 
168                                                      code, e.ToString()));
169                         }
170                 }
171
172                 TestingEnum test = TestingEnum.Test;
173                 AssertEquals("decimal format wrong",
174                                  "3", Enum.Format (test.GetType (), test, "d"));
175                 AssertEquals("decimal format wrong for ulong enums", 
176                              "18446744073709551615", Enum.Format(typeof(TestingEnum3), TestingEnum3.Test, "d"));
177                 AssertEquals("named format wrong",
178                                  "Test", Enum.Format (test.GetType (), test, "g"));
179                 AssertEquals("hex format wrong",
180                                  "00000003", Enum.Format (test.GetType (), test, "x"));
181                 AssertEquals("bitfield format wrong",
182                                  "Test", Enum.Format (test.GetType (), test, "f"));
183         }
184
185         public void TestFormat_FormatSpecifier ()
186         {
187                 ParameterAttributes pa = 
188                         ParameterAttributes.In | ParameterAttributes.HasDefault;
189                 const string fFormatOutput = "In, HasDefault";
190                 const string xFormatOutput = "00001001";
191                 string fOutput = Enum.Format (pa.GetType(), pa, "f");
192                 AssertEquals ("#F_FS:f", fFormatOutput, fOutput);
193                 string xOutput = Enum.Format (pa.GetType(), pa, "x");
194                 AssertEquals ("#F_FS:x", xFormatOutput, xOutput);
195         }
196
197         public void TestGetHashCode() {
198                 Enum e1 = new TestingEnum();
199                 Enum e2 = new TestingEnum2();
200
201                 AssertEquals("hash code is deterministic", 
202                              e1.GetHashCode(), e1.GetHashCode());
203         }
204         
205         public void GetName() {
206                 {
207                         bool errorThrown = false;
208                         try {
209                                 TestingEnum x = TestingEnum.Test;
210                                 Enum.GetName(null, x);
211                         } catch (ArgumentNullException) {
212                                 errorThrown = true;
213                         }
214                         Assert("null first arg not caught.", 
215                                errorThrown);
216                 }
217                 {
218                         bool errorThrown = false;
219                         try {
220                                 TestingEnum x = TestingEnum.Test;
221                                 Enum.GetName(x.GetType(), null);
222                         } catch (ArgumentNullException) {
223                                 errorThrown = true;
224                         }
225                         Assert("null second arg not caught.", 
226                                errorThrown);
227                 }
228                 {
229                         bool errorThrown = false;
230                         try {
231                                 String bad = "huh?";
232                                 TestingEnum x = TestingEnum.Test;
233                                 Enum.GetName(bad.GetType(), x);
234                         } catch (ArgumentException) {
235                                 errorThrown = true;
236                         }
237                         Assert("non-enum type not caught.", 
238                                errorThrown);
239                 }
240                 {
241                         bool errorThrown = false;
242                         try {
243                                 TestingEnum x = TestingEnum.Test;
244                                 TestingEnum2 y = TestingEnum2.Test;
245                                 Enum.GetName(y.GetType(), x);
246                         } catch (ArgumentException) {
247                                 errorThrown = true;
248                         }
249                         Assert("wrong enum type not caught.", 
250                                errorThrown);
251                 }
252                 {
253                         bool errorThrown = false;
254                         try {
255                                 String bad = "huh?";
256                                 TestingEnum x = TestingEnum.Test;
257                                 Enum.GetName(x.GetType(), bad);
258                         } catch (ArgumentException) {
259                                 errorThrown = true;
260                         }
261                         Assert("non-enum object not caught.", 
262                                errorThrown);
263                 }
264                 {
265                         TestingEnum x = TestingEnum.This;
266                         TestingEnum y = TestingEnum.Is;
267                         TestingEnum z = TestingEnum.A;
268
269                         AssertEquals("first name doesn't match",
270                                      "This", Enum.GetName(x.GetType(), x));
271                         AssertEquals("second name doesn't match",
272                                      "Is", Enum.GetName(y.GetType(), y));
273                         AssertEquals("third name doesn't match",
274                                      "A", Enum.GetName(z.GetType(), z));
275                 }
276         }
277
278         public void TestGetNames() {
279                 {
280                         bool errorThrown = false;
281                         try {
282                                 Enum.GetNames(null);
283                         } catch (ArgumentNullException) {
284                                 errorThrown = true;
285                         }
286                         Assert("null type not caught.", 
287                                errorThrown);
288                 }
289                 {
290                         TestingEnum x = TestingEnum.This;
291                         string[] match = {"This", "Is", "A", "Test"};
292                         string[] names = Enum.GetNames(x.GetType());
293                         AssertNotNull("Got no names", names);
294                         AssertEquals("names wrong size", 
295                                      match.Length, names.Length);
296                         for (int i = 0; i < names.Length; i++) {
297                                 AssertEquals("name mismatch",
298                                              match[i], names[i]);
299                         }
300                 }
301         }
302
303         public void TestGetTypeCode() {
304                 TestingEnum x = TestingEnum.This;
305                 TestingEnum y = new TestingEnum();
306                 AssertEquals("01 bad type code", 
307                              TypeCode.Int32, x.GetTypeCode());
308                 AssertEquals("02 bad type code", 
309                              TypeCode.Int32, y.GetTypeCode());
310         }
311
312         enum TestShortEnum : short { zero, one, two, three, four, five, six};
313         public void TestGetUnderlyingType() {
314                 {
315                         bool errorThrown = false;
316                         try {
317                                 Enum.GetUnderlyingType(null);
318                         } catch (ArgumentNullException) {
319                                 errorThrown = true;
320                         }
321                         Assert("null type not caught.", 
322                                errorThrown);
323                 }
324                 {
325                         bool errorThrown = false;
326                         try {
327                                 String bad = "huh?";
328                                 Enum.GetUnderlyingType(bad.GetType());
329                         } catch (ArgumentException) {
330                                 errorThrown = true;
331                         }
332                         Assert("non-enum type not caught.", 
333                                errorThrown);
334                 }
335                 {
336                         short sh = 5;
337                         int i = 5;
338                         Enum t1 = new TestingEnum();
339                         Enum t2 = new TestShortEnum();
340                         AssertEquals("Wrong default underlying type",
341                                      i.GetType(), 
342                                      Enum.GetUnderlyingType(t1.GetType()));
343                         AssertEquals("Not short underlying type",
344                                      sh.GetType(), 
345                                      Enum.GetUnderlyingType(t2.GetType()));
346                 }
347         }
348
349         public void TestGetValues() {
350                 {
351                         bool errorThrown = false;
352                         try {
353                                 Enum.GetValues(null);
354                         } catch (ArgumentNullException) {
355                                 errorThrown = true;
356                         }
357                         Assert("null type not caught.", 
358                                errorThrown);
359                 }
360                 {
361                         bool errorThrown = false;
362                         try {
363                                 String bad = "huh?";
364                                 Enum.GetValues(bad.GetType());
365                         } catch (ArgumentException) {
366                                 errorThrown = true;
367                         }
368                         Assert("non-enum type not caught.", 
369                                errorThrown);
370                 }
371                 {
372                         Enum t1 = new TestingEnum();
373                         Array a1 = Enum.GetValues(t1.GetType());
374                         for (int i= 0; i < a1.Length; i++) {
375                                 AssertEquals("wrong enum value",
376                                              (TestingEnum)i,
377                                              a1.GetValue(i));
378                         }
379                 }
380                 {
381                         Enum t1 = new TestShortEnum();
382                         Array a1 = Enum.GetValues(t1.GetType());
383                         for (short i= 0; i < a1.Length; i++) {
384                                 AssertEquals("wrong short enum value",
385                                              (TestShortEnum)i,
386                                              a1.GetValue(i));
387                         }
388                 }
389         }
390
391         public void TestIsDefined() {
392                 {
393                         bool errorThrown = false;
394                         try {
395                                 Enum.IsDefined(null, 1);
396                         } catch (ArgumentNullException) {
397                                 errorThrown = true;
398                         }
399                         Assert("null first arg not caught.", 
400                                errorThrown);
401                 }
402                 {
403                         bool errorThrown = false;
404                         try {
405                                 TestingEnum x = TestingEnum.Test;
406                                 Enum.IsDefined(x.GetType(), null);
407                         } catch (ArgumentNullException) {
408                                 errorThrown = true;
409                         }
410                         Assert("null second arg not caught.", 
411                                errorThrown);
412                 }
413                 {
414                         bool errorThrown = false;
415                         try {
416                                 String bad = "huh?";
417                                 int i = 4;
418                                 Enum.IsDefined(bad.GetType(), i);
419                         } catch (ArgumentException) {
420                                 errorThrown = true;
421                         }
422                         Assert("non-enum type not caught.", 
423                                errorThrown);
424                 }
425
426                 try {
427                         TestingEnum x = TestingEnum.Test;
428                         short i = 4;
429                         Enum.IsDefined(x.GetType(), i);
430                         Fail("wrong underlying type not caught.");
431                 } catch (ArgumentException) {
432                 } catch (Exception e) {
433                         Fail("wrong Exception thrown ("+e.ToString()+")for underlying type not caught.");
434                 }
435
436                 // spec says yes, MS impl says no.
437                 //{
438                 //bool errorThrown = false;
439                 //try {
440                 //String bad = "huh?";
441                 //TestingEnum x = TestingEnum.Test;
442                 //Enum.IsDefined(x.GetType(), bad);
443                 //} catch (ExecutionEngineException) {
444                 //errorThrown = true;
445                 //}
446                 //Assert("non-enum object not caught.", 
447                 //errorThrown);
448                 //}
449                 {
450                         Enum t1 = new TestingEnum();
451                         int i = 0;
452                         for (i = 0; 
453                              i < Enum.GetValues(t1.GetType()).Length; i++) {
454                                 Assert("should have value for i=" + i,
455                                        Enum.IsDefined(t1.GetType(), i));
456                         }
457                         Assert("Shouldn't have value",
458                                !Enum.IsDefined(t1.GetType(), i));
459                 }
460         }
461
462         public void TestParse1() {
463                 {
464                         bool errorThrown = false;
465                         try {
466                                 String name = "huh?";
467                                 Enum.Parse(null, name);
468                         } catch (ArgumentNullException) {
469                                 errorThrown = true;
470                         }
471                         Assert("null first arg not caught.", 
472                                errorThrown);
473                 }
474                 {
475                         bool errorThrown = false;
476                         try {
477                                 TestingEnum x = TestingEnum.Test;
478                                 Enum.Parse(x.GetType(), null);
479                         } catch (ArgumentNullException) {
480                                 errorThrown = true;
481                         }
482                         Assert("null second arg not caught.", 
483                                errorThrown);
484                 }
485                 {
486                         bool errorThrown = false;
487                         try {
488                                 String bad = "huh?";
489                                 Enum.Parse(bad.GetType(), bad);
490                         } catch (ArgumentException) {
491                                 errorThrown = true;
492                         }
493                         Assert("non-enum type not caught.", 
494                                errorThrown);
495                 }
496                 {
497                         bool errorThrown = false;
498                         try {
499                                 TestingEnum x = TestingEnum.Test;
500                                 String bad = "";
501                                 Enum.Parse(x.GetType(), bad);
502                         } catch (ArgumentException) {
503                                 errorThrown = true;
504                         }
505                         Assert("empty string not caught.", 
506                                errorThrown);
507                 }
508                 {
509                         bool errorThrown = false;
510                         try {
511                                 TestingEnum x = TestingEnum.Test;
512                                 String bad = " ";
513                                 Enum.Parse(x.GetType(), bad);
514                         } catch (ArgumentException) {
515                                 errorThrown = true;
516                         }
517                         Assert("space-only string not caught.", 
518                                errorThrown);
519                 }
520                 {
521                         bool errorThrown = false;
522                         try {
523                                 String bad = "huh?";
524                                 TestingEnum x = TestingEnum.Test;
525                                 Enum.Parse(x.GetType(), bad);
526                         } catch (ArgumentException) {
527                                 errorThrown = true;
528                         }
529                         Assert("not-in-enum error not caught.", 
530                                errorThrown);
531                 }
532                 {
533                         TestingEnum t1 = new TestingEnum();
534                         AssertEquals("parse first enum",
535                                      TestingEnum.This, 
536                                      Enum.Parse(t1.GetType(), "This"));
537                         AssertEquals("parse second enum",
538                                      TestingEnum.Is, 
539                                      Enum.Parse(t1.GetType(), "Is"));
540                         AssertEquals("parse third enum",
541                                      TestingEnum.A, 
542                                      Enum.Parse(t1.GetType(), "A"));
543                         AssertEquals("parse last enum",
544                                      TestingEnum.Test, 
545                                      Enum.Parse(t1.GetType(), "Test"));
546
547                         AssertEquals("parse last enum with whitespace",
548                                      TestingEnum.Test, 
549                                      Enum.Parse(t1.GetType(), "    \n\nTest\t"));
550
551                         AssertEquals("parse bitwise-or enum",
552                                      TestingEnum.Is, 
553                                      Enum.Parse(t1.GetType(), "This,Is"));
554                         AssertEquals("parse bitwise-or enum",
555                                      TestingEnum.Test, 
556                                      Enum.Parse(t1.GetType(), "This,Test"));
557                         AssertEquals("parse bitwise-or enum",
558                                      TestingEnum.Test, 
559                                      Enum.Parse(t1.GetType(), "This,Is,A"));
560
561                         AssertEquals("parse bitwise-or enum with whitespace",
562                                      TestingEnum.Test, 
563                                      Enum.Parse(t1.GetType(), "   \n\tThis \t\n,    Is,A \n"));
564                 }
565         }
566         public void TestParse2() {
567                 {
568                         bool errorThrown = false;
569                         try {
570                                 String name = "huh?";
571                                 Enum.Parse(null, name, true);
572                         } catch (ArgumentNullException) {
573                                 errorThrown = true;
574                         }
575                         Assert("null first arg not caught.", 
576                                errorThrown);
577                 }
578                 {
579                         bool errorThrown = false;
580                         try {
581                                 TestingEnum x = TestingEnum.Test;
582                                 Enum.Parse(x.GetType(), null, true);
583                         } catch (ArgumentNullException) {
584                                 errorThrown = true;
585                         }
586                         Assert("null second arg not caught.", 
587                                errorThrown);
588                 }
589                 {
590                         bool errorThrown = false;
591                         try {
592                                 String bad = "huh?";
593                                 Enum.Parse(bad.GetType(), bad, true);
594                         } catch (ArgumentException) {
595                                 errorThrown = true;
596                         }
597                         Assert("non-enum type not caught.", 
598                                errorThrown);
599                 }
600                 {
601                         bool errorThrown = false;
602                         try {
603                                 TestingEnum x = TestingEnum.Test;
604                                 String bad = "";
605                                 Enum.Parse(x.GetType(), bad, true);
606                         } catch (ArgumentException) {
607                                 errorThrown = true;
608                         }
609                         Assert("empty string not caught.", 
610                                errorThrown);
611                 }
612                 {
613                         bool errorThrown = false;
614                         try {
615                                 TestingEnum x = TestingEnum.Test;
616                                 String bad = " ";
617                                 Enum.Parse(x.GetType(), bad, true);
618                         } catch (ArgumentException) {
619                                 errorThrown = true;
620                         }
621                         Assert("space-only string not caught.", 
622                                errorThrown);
623                 }
624                 {
625                         bool errorThrown = false;
626                         try {
627                                 String bad = "huh?";
628                                 TestingEnum x = TestingEnum.Test;
629                                 Enum.Parse(x.GetType(), bad, true);
630                         } catch (ArgumentException) {
631                                 errorThrown = true;
632                         }
633                         Assert("not-in-enum error not caught.", 
634                                errorThrown);
635                 }
636                 {
637                         bool errorThrown = false;
638                         try {
639                                 String bad = "test";
640                                 TestingEnum x = TestingEnum.Test;
641                                 Enum.Parse(x.GetType(), bad, false);
642                         } catch (ArgumentException) {
643                                 errorThrown = true;
644                         }
645                         Assert("not-in-enum error not caught.", 
646                                errorThrown);
647                 }
648                 {
649                         TestingEnum t1 = new TestingEnum();
650                         AssertEquals("parse first enum",
651                                      TestingEnum.This, 
652                                      Enum.Parse(t1.GetType(), "this", true));
653                         AssertEquals("parse second enum",
654                                      TestingEnum.Is, 
655                                      Enum.Parse(t1.GetType(), "is", true));
656                         AssertEquals("parse third enum",
657                                      TestingEnum.A, 
658                                      Enum.Parse(t1.GetType(), "a", true));
659                         AssertEquals("parse last enum",
660                                      TestingEnum.Test, 
661                                      Enum.Parse(t1.GetType(), "test", true));
662
663                         AssertEquals("parse last enum with whitespace",
664                                      TestingEnum.Test, 
665                                      Enum.Parse(t1.GetType(), "    \n\ntest\t", true));
666
667                         AssertEquals("parse bitwise-or enum",
668                                      TestingEnum.Is, 
669                                      Enum.Parse(t1.GetType(), "This,is", true));
670                         AssertEquals("parse bitwise-or enum",
671                                      TestingEnum.Test, 
672                                      Enum.Parse(t1.GetType(), "This,test", true));
673                         AssertEquals("parse bitwise-or enum",
674                                      TestingEnum.Test, 
675                                      Enum.Parse(t1.GetType(), "This,is,A", true));
676
677                         AssertEquals("parse bitwise-or enum with whitespace",
678                                      TestingEnum.Test, 
679                                          Enum.Parse(t1.GetType(), "   \n\tThis \t\n,    is,a \n",
680                                                         true));
681                 }
682         }
683
684         [Test]
685         public void ParseValue() {
686                 TestingEnum3 t1 = new TestingEnum3();
687                 AssertEquals ("Parse numeric value", TestingEnum3.Test, Enum.Parse(t1.GetType(), "18446744073709551615", false));
688         }
689
690         public void TestToObject() {
691                 {
692                         bool errorThrown = false;
693                         try {
694                                 Enum.ToObject(null, 1);
695                         } catch (ArgumentNullException) {
696                                 errorThrown = true;
697                         }
698                         Assert("null type not caught.", 
699                                errorThrown);
700                 }
701                 {
702                         bool errorThrown = false;
703                         try {
704                                 Enum.ToObject("huh?".GetType(), 1);
705                         } catch (ArgumentException) {
706                                 errorThrown = true;
707                         }
708                         Assert("null type not caught.", 
709                                errorThrown);
710                 }
711                 {
712                         TestingEnum t1 = new TestingEnum();
713                         AssertEquals("Should get object",
714                                      TestingEnum.This,
715                                      Enum.ToObject(t1.GetType(), 0));
716                 }
717                 // TODO - should probably test all the different underlying types
718         }
719
720         [Flags]
721         enum SomeEnum {a,b,c};
722
723         [Flags]
724         enum SomeByteEnum : byte {a,b,c};
725
726         [Flags]
727         enum SomeInt64Enum : long {a,b,c};
728
729         public void TestToString() {
730                 int i = 0;
731                 try {
732                         i++;
733                         AssertEquals("invalid string", "This", 
734                                      TestingEnum.This.ToString());
735                         i++;
736                         AssertEquals("invalid string", "Is", 
737                                      TestingEnum.Is.ToString());
738                         i++;
739                         AssertEquals("invalid string", "A", 
740                                      TestingEnum.A.ToString());
741                         i++;
742                         AssertEquals("invalid string", "Test", 
743                                      TestingEnum.Test.ToString());
744
745                         Enum is1 = TestingEnum.Is;
746
747                         i++;
748                         AssertEquals("decimal parse wrong", 
749                                      "1", is1.ToString("d"));
750                         i++;
751                         AssertEquals("named format wrong", 
752                                      "Is", is1.ToString("g"));
753                         i++;
754                         AssertEquals("hex format wrong", 
755                                      "00000001", is1.ToString("x"));
756                         i++;
757                         AssertEquals("bitfield format wrong", 
758                                      "Is", is1.ToString("f"));
759
760                         i++;
761                         AssertEquals("bitfield with flags format wrong for Int32 enum", 
762                                      "b, c", ((SomeEnum)3).ToString("f"));
763                         i++;
764                         AssertEquals("bitfield with flags format wrong for Byte enum", 
765                                      "b, c", ((SomeByteEnum)3).ToString("f"));
766                         i++;
767                         AssertEquals("bitfield with flags format wrong for Int64 enum", 
768                                      "b, c", ((SomeInt64Enum)3).ToString("f"));
769
770                         i++;
771                         AssertEquals("bitfield with unknown flags format wrong for Int32 enum",
772                                      "12", ((SomeEnum)12).ToString("f"));
773                         i++;
774                         AssertEquals("bitfield with unknown flags format wrong for Byte enum",
775                                      "12", ((SomeByteEnum)12).ToString("f"));
776                         i++;
777                         AssertEquals("bitfield with unknown flags format wrong for Int64 enum",
778                                      "12", ((SomeInt64Enum)12).ToString("f"));
779
780                 } catch (Exception e) {
781                         Fail ("Unexpected exception at i = " + i + " with e=" + e);
782                 }
783         }
784
785         enum E {
786                 Aa=0,
787                 Bb=1,
788                 Cc=2,
789                 Dd=3,
790         }
791         
792         [Flags]
793         enum E2 {
794                 Aa,
795                 Bb,
796                 Cc,
797                 Dd,
798         }
799         
800         [Test]
801         public void FlagTest ()
802         {
803                 int [] evalues = new int [4] {0,1,2,3};
804                 E [] e = new E [4] {E.Aa, E.Bb, E.Cc, E.Dd};
805                 
806                 for (int i = 0; i < 4; ++i) {
807                         Assertion.AssertEquals ("#" + i,
808                                 e [i].ToString (),
809                                 Enum.Format (typeof (E), evalues [i], "f"));
810                 }
811         }
812
813
814         [Test]
815         public void FlagTest2 () {
816                 int invalidValue = 1000;
817                 
818                 Assertion.AssertEquals ("#01",
819                                 invalidValue.ToString (),
820                                 Enum.Format (typeof (E2), invalidValue, "g"));
821         }
822
823         enum E3 {A=0,B=1,C=2,D=3,}
824         enum UE : ulong {A=1,B=2,C=4,D=8,} 
825         enum EA {A=0, B=2, C=3, D=4}
826         
827         [Test]
828         public void AnotherFormatBugPinned ()
829         {
830                 Assertion.AssertEquals ("#01", "100", Enum.Format (typeof (E3), 100, "f"));
831         }
832         
833         [Test]
834         public void LogicBugPinned ()
835         {
836                 string format=null;
837                 string[] names=new string[] {"A","B","C","D",};
838                 string[] fmtSpl=null;
839                 UE ue = UE.A | UE.B | UE.C | UE.D;
840                 
841                 //all flags must be in format return
842                 format = Enum.Format (typeof (UE), ue, "f");
843                 fmtSpl = format.Split (',');
844                 for( int i=0 ; i<fmtSpl.Length ; ++i )
845                         fmtSpl [i] = fmtSpl[i].Trim ();
846
847                 foreach (string nval in fmtSpl)
848                         Assertion.Assert (nval + " is not a valid enum value name", Array.IndexOf (names, nval) >= 0);
849
850                 foreach (string nval in names)
851                         Assertion.Assert (nval + " is not contained in format return.", Array.IndexOf (fmtSpl, nval) >= 0);
852         }
853         // TODO - ToString with IFormatProviders
854
855         [Flags]
856         enum FlagsNegativeTestEnum {
857                 None = 0,
858                 One = 1,
859                 Two = 2,
860                 Negative = unchecked((int)0xFFFF0000)
861         }
862
863         [Test]
864         public void FlagTest3 () {
865                 FlagsNegativeTestEnum t;
866
867                 t = FlagsNegativeTestEnum.None;
868                 Assertion.AssertEquals("#01", "None", t.ToString());
869                 t = FlagsNegativeTestEnum.One;
870                 Assertion.AssertEquals("#02", "One", t.ToString());
871         }
872 }
873 }