* roottypes.cs: Rename from tree.cs.
[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("null first arg not caught.");
88                 } catch (ArgumentNullException) {
89                         // do nothing
90                 } catch (Exception e) {
91                         Fail("first arg null, wrong exception: " + e.ToString());
92                 }
93
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());
101                 }
102
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());
111                 }
112
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());
121                 }
122
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());
132                 }
133
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());
143                 }
144
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()));
159                         }
160                 }
161
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"));
173         }
174
175         public void TestFormat_FormatSpecifier ()
176         {
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);
185         }
186
187         public void TestGetHashCode() {
188                 Enum e1 = new TestingEnum();
189                 Enum e2 = new TestingEnum2();
190
191                 AssertEquals("hash code is deterministic", 
192                              e1.GetHashCode(), e1.GetHashCode());
193         }
194         
195         public void GetName() {
196                 {
197                         bool errorThrown = false;
198                         try {
199                                 TestingEnum x = TestingEnum.Test;
200                                 Enum.GetName(null, x);
201                         } catch (ArgumentNullException) {
202                                 errorThrown = true;
203                         }
204                         Assert("null first arg not caught.", 
205                                errorThrown);
206                 }
207                 {
208                         bool errorThrown = false;
209                         try {
210                                 TestingEnum x = TestingEnum.Test;
211                                 Enum.GetName(x.GetType(), null);
212                         } catch (ArgumentNullException) {
213                                 errorThrown = true;
214                         }
215                         Assert("null second arg not caught.", 
216                                errorThrown);
217                 }
218                 {
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;
226                         }
227                         Assert("non-enum type not caught.", 
228                                errorThrown);
229                 }
230                 {
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;
238                         }
239                         Assert("wrong enum type not caught.", 
240                                errorThrown);
241                 }
242                 {
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;
250                         }
251                         Assert("non-enum object not caught.", 
252                                errorThrown);
253                 }
254                 {
255                         TestingEnum x = TestingEnum.This;
256                         TestingEnum y = TestingEnum.Is;
257                         TestingEnum z = TestingEnum.A;
258
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));
265                 }
266         }
267
268         public void TestGetNames() {
269                 {
270                         bool errorThrown = false;
271                         try {
272                                 Enum.GetNames(null);
273                         } catch (ArgumentNullException) {
274                                 errorThrown = true;
275                         }
276                         Assert("null type not caught.", 
277                                errorThrown);
278                 }
279                 {
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]);
289                         }
290                 }
291         }
292
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());
300         }
301
302         enum TestShortEnum : short { zero, one, two, three, four, five, six};
303         public void TestGetUnderlyingType() {
304                 {
305                         bool errorThrown = false;
306                         try {
307                                 Enum.GetUnderlyingType(null);
308                         } catch (ArgumentNullException) {
309                                 errorThrown = true;
310                         }
311                         Assert("null type not caught.", 
312                                errorThrown);
313                 }
314                 {
315                         bool errorThrown = false;
316                         try {
317                                 String bad = "huh?";
318                                 Enum.GetUnderlyingType(bad.GetType());
319                         } catch (ArgumentException) {
320                                 errorThrown = true;
321                         }
322                         Assert("non-enum type not caught.", 
323                                errorThrown);
324                 }
325                 {
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()));
336                 }
337         }
338
339         public void TestGetValues() {
340                 {
341                         bool errorThrown = false;
342                         try {
343                                 Enum.GetValues(null);
344                         } catch (ArgumentNullException) {
345                                 errorThrown = true;
346                         }
347                         Assert("null type not caught.", 
348                                errorThrown);
349                 }
350                 {
351                         bool errorThrown = false;
352                         try {
353                                 String bad = "huh?";
354                                 Enum.GetValues(bad.GetType());
355                         } catch (ArgumentException) {
356                                 errorThrown = true;
357                         }
358                         Assert("non-enum type not caught.", 
359                                errorThrown);
360                 }
361                 {
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));
368                         }
369                 }
370                 {
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));
377                         }
378                 }
379         }
380
381         public void TestIsDefined() {
382                 {
383                         bool errorThrown = false;
384                         try {
385                                 Enum.IsDefined(null, 1);
386                         } catch (ArgumentNullException) {
387                                 errorThrown = true;
388                         }
389                         Assert("null first arg not caught.", 
390                                errorThrown);
391                 }
392                 {
393                         bool errorThrown = false;
394                         try {
395                                 TestingEnum x = TestingEnum.Test;
396                                 Enum.IsDefined(x.GetType(), null);
397                         } catch (ArgumentNullException) {
398                                 errorThrown = true;
399                         }
400                         Assert("null second arg not caught.", 
401                                errorThrown);
402                 }
403                 {
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;
411                         }
412                         Assert("non-enum type not caught.", 
413                                errorThrown);
414                 }
415
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.");
424                 }
425
426                 // spec says yes, MS impl says no.
427                 //{
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;
435                 //}
436                 //Assert("non-enum object not caught.", 
437                 //errorThrown);
438                 //}
439                 {
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));
446                         }
447                         Assert("Shouldn't have value",
448                                !Enum.IsDefined(t1.GetType(), i));
449                 }
450         }
451
452         public void TestParse1() {
453                 {
454                         bool errorThrown = false;
455                         try {
456                                 String name = "huh?";
457                                 Enum.Parse(null, name);
458                         } catch (ArgumentNullException) {
459                                 errorThrown = true;
460                         }
461                         Assert("null first arg not caught.", 
462                                errorThrown);
463                 }
464                 {
465                         bool errorThrown = false;
466                         try {
467                                 TestingEnum x = TestingEnum.Test;
468                                 Enum.Parse(x.GetType(), null);
469                         } catch (ArgumentNullException) {
470                                 errorThrown = true;
471                         }
472                         Assert("null second arg not caught.", 
473                                errorThrown);
474                 }
475                 {
476                         bool errorThrown = false;
477                         try {
478                                 String bad = "huh?";
479                                 Enum.Parse(bad.GetType(), bad);
480                         } catch (ArgumentException) {
481                                 errorThrown = true;
482                         }
483                         Assert("non-enum type not caught.", 
484                                errorThrown);
485                 }
486                 {
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;
494                         }
495                         Assert("empty string not caught.", 
496                                errorThrown);
497                 }
498                 {
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;
506                         }
507                         Assert("space-only string not caught.", 
508                                errorThrown);
509                 }
510                 {
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;
518                         }
519                         Assert("not-in-enum error not caught.", 
520                                errorThrown);
521                 }
522                 {
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"));
536
537                         AssertEquals("parse last enum with whitespace",
538                                      TestingEnum.Test, 
539                                      Enum.Parse(t1.GetType(), "    \n\nTest\t"));
540
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"));
550
551                         AssertEquals("parse bitwise-or enum with whitespace",
552                                      TestingEnum.Test, 
553                                      Enum.Parse(t1.GetType(), "   \n\tThis \t\n,    Is,A \n"));
554                 }
555         }
556         public void TestParse2() {
557                 {
558                         bool errorThrown = false;
559                         try {
560                                 String name = "huh?";
561                                 Enum.Parse(null, name, true);
562                         } catch (ArgumentNullException) {
563                                 errorThrown = true;
564                         }
565                         Assert("null first arg not caught.", 
566                                errorThrown);
567                 }
568                 {
569                         bool errorThrown = false;
570                         try {
571                                 TestingEnum x = TestingEnum.Test;
572                                 Enum.Parse(x.GetType(), null, true);
573                         } catch (ArgumentNullException) {
574                                 errorThrown = true;
575                         }
576                         Assert("null second arg not caught.", 
577                                errorThrown);
578                 }
579                 {
580                         bool errorThrown = false;
581                         try {
582                                 String bad = "huh?";
583                                 Enum.Parse(bad.GetType(), bad, true);
584                         } catch (ArgumentException) {
585                                 errorThrown = true;
586                         }
587                         Assert("non-enum type not caught.", 
588                                errorThrown);
589                 }
590                 {
591                         bool errorThrown = false;
592                         try {
593                                 TestingEnum x = TestingEnum.Test;
594                                 String bad = "";
595                                 Enum.Parse(x.GetType(), bad, true);
596                         } catch (ArgumentException) {
597                                 errorThrown = true;
598                         }
599                         Assert("empty string not caught.", 
600                                errorThrown);
601                 }
602                 {
603                         bool errorThrown = false;
604                         try {
605                                 TestingEnum x = TestingEnum.Test;
606                                 String bad = " ";
607                                 Enum.Parse(x.GetType(), bad, true);
608                         } catch (ArgumentException) {
609                                 errorThrown = true;
610                         }
611                         Assert("space-only string not caught.", 
612                                errorThrown);
613                 }
614                 {
615                         bool errorThrown = false;
616                         try {
617                                 String bad = "huh?";
618                                 TestingEnum x = TestingEnum.Test;
619                                 Enum.Parse(x.GetType(), bad, true);
620                         } catch (ArgumentException) {
621                                 errorThrown = true;
622                         }
623                         Assert("not-in-enum error not caught.", 
624                                errorThrown);
625                 }
626                 {
627                         bool errorThrown = false;
628                         try {
629                                 String bad = "test";
630                                 TestingEnum x = TestingEnum.Test;
631                                 Enum.Parse(x.GetType(), bad, false);
632                         } catch (ArgumentException) {
633                                 errorThrown = true;
634                         }
635                         Assert("not-in-enum error not caught.", 
636                                errorThrown);
637                 }
638                 {
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));
652
653                         AssertEquals("parse last enum with whitespace",
654                                      TestingEnum.Test, 
655                                      Enum.Parse(t1.GetType(), "    \n\ntest\t", true));
656
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));
666
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));
671                 }
672         }
673
674         [Test]
675         public void ParseValue() {
676                 TestingEnum3 t1 = new TestingEnum3();
677                 AssertEquals ("Parse numeric value", TestingEnum3.Test, Enum.Parse(t1.GetType(), "18446744073709551615", false));
678         }
679
680         public void TestToObject() {
681                 {
682                         bool errorThrown = false;
683                         try {
684                                 Enum.ToObject(null, 1);
685                         } catch (ArgumentNullException) {
686                                 errorThrown = true;
687                         }
688                         Assert("null type not caught.", 
689                                errorThrown);
690                 }
691                 {
692                         bool errorThrown = false;
693                         try {
694                                 Enum.ToObject("huh?".GetType(), 1);
695                         } catch (ArgumentException) {
696                                 errorThrown = true;
697                         }
698                         Assert("null type not caught.", 
699                                errorThrown);
700                 }
701                 {
702                         TestingEnum t1 = new TestingEnum();
703                         AssertEquals("Should get object",
704                                      TestingEnum.This,
705                                      Enum.ToObject(t1.GetType(), 0));
706                 }
707                 // TODO - should probably test all the different underlying types
708         }
709
710         [Flags]
711         enum SomeEnum {a,b,c};
712
713         [Flags]
714         enum SomeByteEnum : byte {a,b,c};
715
716         [Flags]
717         enum SomeInt64Enum : long {a,b,c};
718
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());
734
735                         Enum is1 = TestingEnum.Is;
736
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"));
749
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"));
759
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"));
769
770                 } catch (Exception e) {
771                         Fail ("Unexpected exception at i = " + i + " with e=" + e);
772                 }
773         }
774
775         enum E {
776                 Aa=0,
777                 Bb=1,
778                 Cc=2,
779                 Dd=3,
780         }
781         
782         [Flags]
783         enum E2 {
784                 Aa,
785                 Bb,
786                 Cc,
787                 Dd,
788         }
789         
790         [Test]
791         public void FlagTest ()
792         {
793                 int [] evalues = new int [4] {0,1,2,3};
794                 E [] e = new E [4] {E.Aa, E.Bb, E.Cc, E.Dd};
795                 
796                 for (int i = 0; i < 4; ++i) {
797                         Assertion.AssertEquals ("#" + i,
798                                 e [i].ToString (),
799                                 Enum.Format (typeof (E), evalues [i], "f"));
800                 }
801         }
802
803
804         [Test]
805         public void FlagTest2 () {
806                 int invalidValue = 1000;
807                 
808                 Assertion.AssertEquals ("#01",
809                                 invalidValue.ToString (),
810                                 Enum.Format (typeof (E2), invalidValue, "g"));
811         }
812
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}
816         
817         [Test]
818         public void AnotherFormatBugPinned ()
819         {
820                 Assertion.AssertEquals ("#01", "100", Enum.Format (typeof (E3), 100, "f"));
821         }
822         
823         [Test]
824         public void LogicBugPinned ()
825         {
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;
830                 
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 ();
836
837                 foreach (string nval in fmtSpl)
838                         Assertion.Assert (nval + " is not a valid enum value name", Array.IndexOf (names, nval) >= 0);
839
840                 foreach (string nval in names)
841                         Assertion.Assert (nval + " is not contained in format return.", Array.IndexOf (fmtSpl, nval) >= 0);
842         }
843         // TODO - ToString with IFormatProviders
844
845         [Flags]
846         enum FlagsNegativeTestEnum {
847                 None = 0,
848                 One = 1,
849                 Two = 2,
850                 Negative = unchecked((int)0xFFFF0000)
851         }
852
853         [Test]
854         public void FlagTest3 () {
855                 FlagsNegativeTestEnum t;
856
857                 t = FlagsNegativeTestEnum.None;
858                 Assertion.AssertEquals("#01", "None", t.ToString());
859                 t = FlagsNegativeTestEnum.One;
860                 Assertion.AssertEquals("#02", "One", t.ToString());
861         }
862 }
863 }