zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

blob e230032d (21034B) - Raw


      1 const builtin = @import("builtin");
      2 const std = @import("std");
      3 const expect = std.testing.expect;
      4 const mem = std.mem;
      5 const Tag = std.meta.Tag;
      6 
      7 const Number = enum { Zero, One, Two, Three, Four };
      8 
      9 fn shouldEqual(n: Number, expected: u3) !void {
     10     try expect(@enumToInt(n) == expected);
     11 }
     12 
     13 test "enum to int" {
     14     try shouldEqual(Number.Zero, 0);
     15     try shouldEqual(Number.One, 1);
     16     try shouldEqual(Number.Two, 2);
     17     try shouldEqual(Number.Three, 3);
     18     try shouldEqual(Number.Four, 4);
     19 }
     20 
     21 fn testIntToEnumEval(x: i32) !void {
     22     try expect(@intToEnum(IntToEnumNumber, x) == IntToEnumNumber.Three);
     23 }
     24 const IntToEnumNumber = enum { Zero, One, Two, Three, Four };
     25 
     26 test "int to enum" {
     27     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
     28     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
     29 
     30     try testIntToEnumEval(3);
     31 }
     32 
     33 const ValueCount1 = enum {
     34     I0,
     35 };
     36 const ValueCount2 = enum {
     37     I0,
     38     I1,
     39 };
     40 const ValueCount256 = enum {
     41     I0,
     42     I1,
     43     I2,
     44     I3,
     45     I4,
     46     I5,
     47     I6,
     48     I7,
     49     I8,
     50     I9,
     51     I10,
     52     I11,
     53     I12,
     54     I13,
     55     I14,
     56     I15,
     57     I16,
     58     I17,
     59     I18,
     60     I19,
     61     I20,
     62     I21,
     63     I22,
     64     I23,
     65     I24,
     66     I25,
     67     I26,
     68     I27,
     69     I28,
     70     I29,
     71     I30,
     72     I31,
     73     I32,
     74     I33,
     75     I34,
     76     I35,
     77     I36,
     78     I37,
     79     I38,
     80     I39,
     81     I40,
     82     I41,
     83     I42,
     84     I43,
     85     I44,
     86     I45,
     87     I46,
     88     I47,
     89     I48,
     90     I49,
     91     I50,
     92     I51,
     93     I52,
     94     I53,
     95     I54,
     96     I55,
     97     I56,
     98     I57,
     99     I58,
    100     I59,
    101     I60,
    102     I61,
    103     I62,
    104     I63,
    105     I64,
    106     I65,
    107     I66,
    108     I67,
    109     I68,
    110     I69,
    111     I70,
    112     I71,
    113     I72,
    114     I73,
    115     I74,
    116     I75,
    117     I76,
    118     I77,
    119     I78,
    120     I79,
    121     I80,
    122     I81,
    123     I82,
    124     I83,
    125     I84,
    126     I85,
    127     I86,
    128     I87,
    129     I88,
    130     I89,
    131     I90,
    132     I91,
    133     I92,
    134     I93,
    135     I94,
    136     I95,
    137     I96,
    138     I97,
    139     I98,
    140     I99,
    141     I100,
    142     I101,
    143     I102,
    144     I103,
    145     I104,
    146     I105,
    147     I106,
    148     I107,
    149     I108,
    150     I109,
    151     I110,
    152     I111,
    153     I112,
    154     I113,
    155     I114,
    156     I115,
    157     I116,
    158     I117,
    159     I118,
    160     I119,
    161     I120,
    162     I121,
    163     I122,
    164     I123,
    165     I124,
    166     I125,
    167     I126,
    168     I127,
    169     I128,
    170     I129,
    171     I130,
    172     I131,
    173     I132,
    174     I133,
    175     I134,
    176     I135,
    177     I136,
    178     I137,
    179     I138,
    180     I139,
    181     I140,
    182     I141,
    183     I142,
    184     I143,
    185     I144,
    186     I145,
    187     I146,
    188     I147,
    189     I148,
    190     I149,
    191     I150,
    192     I151,
    193     I152,
    194     I153,
    195     I154,
    196     I155,
    197     I156,
    198     I157,
    199     I158,
    200     I159,
    201     I160,
    202     I161,
    203     I162,
    204     I163,
    205     I164,
    206     I165,
    207     I166,
    208     I167,
    209     I168,
    210     I169,
    211     I170,
    212     I171,
    213     I172,
    214     I173,
    215     I174,
    216     I175,
    217     I176,
    218     I177,
    219     I178,
    220     I179,
    221     I180,
    222     I181,
    223     I182,
    224     I183,
    225     I184,
    226     I185,
    227     I186,
    228     I187,
    229     I188,
    230     I189,
    231     I190,
    232     I191,
    233     I192,
    234     I193,
    235     I194,
    236     I195,
    237     I196,
    238     I197,
    239     I198,
    240     I199,
    241     I200,
    242     I201,
    243     I202,
    244     I203,
    245     I204,
    246     I205,
    247     I206,
    248     I207,
    249     I208,
    250     I209,
    251     I210,
    252     I211,
    253     I212,
    254     I213,
    255     I214,
    256     I215,
    257     I216,
    258     I217,
    259     I218,
    260     I219,
    261     I220,
    262     I221,
    263     I222,
    264     I223,
    265     I224,
    266     I225,
    267     I226,
    268     I227,
    269     I228,
    270     I229,
    271     I230,
    272     I231,
    273     I232,
    274     I233,
    275     I234,
    276     I235,
    277     I236,
    278     I237,
    279     I238,
    280     I239,
    281     I240,
    282     I241,
    283     I242,
    284     I243,
    285     I244,
    286     I245,
    287     I246,
    288     I247,
    289     I248,
    290     I249,
    291     I250,
    292     I251,
    293     I252,
    294     I253,
    295     I254,
    296     I255,
    297 };
    298 const ValueCount257 = enum {
    299     I0,
    300     I1,
    301     I2,
    302     I3,
    303     I4,
    304     I5,
    305     I6,
    306     I7,
    307     I8,
    308     I9,
    309     I10,
    310     I11,
    311     I12,
    312     I13,
    313     I14,
    314     I15,
    315     I16,
    316     I17,
    317     I18,
    318     I19,
    319     I20,
    320     I21,
    321     I22,
    322     I23,
    323     I24,
    324     I25,
    325     I26,
    326     I27,
    327     I28,
    328     I29,
    329     I30,
    330     I31,
    331     I32,
    332     I33,
    333     I34,
    334     I35,
    335     I36,
    336     I37,
    337     I38,
    338     I39,
    339     I40,
    340     I41,
    341     I42,
    342     I43,
    343     I44,
    344     I45,
    345     I46,
    346     I47,
    347     I48,
    348     I49,
    349     I50,
    350     I51,
    351     I52,
    352     I53,
    353     I54,
    354     I55,
    355     I56,
    356     I57,
    357     I58,
    358     I59,
    359     I60,
    360     I61,
    361     I62,
    362     I63,
    363     I64,
    364     I65,
    365     I66,
    366     I67,
    367     I68,
    368     I69,
    369     I70,
    370     I71,
    371     I72,
    372     I73,
    373     I74,
    374     I75,
    375     I76,
    376     I77,
    377     I78,
    378     I79,
    379     I80,
    380     I81,
    381     I82,
    382     I83,
    383     I84,
    384     I85,
    385     I86,
    386     I87,
    387     I88,
    388     I89,
    389     I90,
    390     I91,
    391     I92,
    392     I93,
    393     I94,
    394     I95,
    395     I96,
    396     I97,
    397     I98,
    398     I99,
    399     I100,
    400     I101,
    401     I102,
    402     I103,
    403     I104,
    404     I105,
    405     I106,
    406     I107,
    407     I108,
    408     I109,
    409     I110,
    410     I111,
    411     I112,
    412     I113,
    413     I114,
    414     I115,
    415     I116,
    416     I117,
    417     I118,
    418     I119,
    419     I120,
    420     I121,
    421     I122,
    422     I123,
    423     I124,
    424     I125,
    425     I126,
    426     I127,
    427     I128,
    428     I129,
    429     I130,
    430     I131,
    431     I132,
    432     I133,
    433     I134,
    434     I135,
    435     I136,
    436     I137,
    437     I138,
    438     I139,
    439     I140,
    440     I141,
    441     I142,
    442     I143,
    443     I144,
    444     I145,
    445     I146,
    446     I147,
    447     I148,
    448     I149,
    449     I150,
    450     I151,
    451     I152,
    452     I153,
    453     I154,
    454     I155,
    455     I156,
    456     I157,
    457     I158,
    458     I159,
    459     I160,
    460     I161,
    461     I162,
    462     I163,
    463     I164,
    464     I165,
    465     I166,
    466     I167,
    467     I168,
    468     I169,
    469     I170,
    470     I171,
    471     I172,
    472     I173,
    473     I174,
    474     I175,
    475     I176,
    476     I177,
    477     I178,
    478     I179,
    479     I180,
    480     I181,
    481     I182,
    482     I183,
    483     I184,
    484     I185,
    485     I186,
    486     I187,
    487     I188,
    488     I189,
    489     I190,
    490     I191,
    491     I192,
    492     I193,
    493     I194,
    494     I195,
    495     I196,
    496     I197,
    497     I198,
    498     I199,
    499     I200,
    500     I201,
    501     I202,
    502     I203,
    503     I204,
    504     I205,
    505     I206,
    506     I207,
    507     I208,
    508     I209,
    509     I210,
    510     I211,
    511     I212,
    512     I213,
    513     I214,
    514     I215,
    515     I216,
    516     I217,
    517     I218,
    518     I219,
    519     I220,
    520     I221,
    521     I222,
    522     I223,
    523     I224,
    524     I225,
    525     I226,
    526     I227,
    527     I228,
    528     I229,
    529     I230,
    530     I231,
    531     I232,
    532     I233,
    533     I234,
    534     I235,
    535     I236,
    536     I237,
    537     I238,
    538     I239,
    539     I240,
    540     I241,
    541     I242,
    542     I243,
    543     I244,
    544     I245,
    545     I246,
    546     I247,
    547     I248,
    548     I249,
    549     I250,
    550     I251,
    551     I252,
    552     I253,
    553     I254,
    554     I255,
    555     I256,
    556 };
    557 
    558 test "enum sizes" {
    559     comptime {
    560         try expect(@sizeOf(ValueCount1) == 0);
    561         try expect(@sizeOf(ValueCount2) == 1);
    562         try expect(@sizeOf(ValueCount256) == 1);
    563         try expect(@sizeOf(ValueCount257) == 2);
    564     }
    565 }
    566 
    567 test "enum literal equality" {
    568     const x = .hi;
    569     const y = .ok;
    570     const z = .hi;
    571 
    572     try expect(x != y);
    573     try expect(x == z);
    574 }
    575 
    576 test "enum literal cast to enum" {
    577     const Color = enum { Auto, Off, On };
    578 
    579     var color1: Color = .Auto;
    580     var color2 = Color.Auto;
    581     try expect(color1 == color2);
    582 }
    583 
    584 test "peer type resolution with enum literal" {
    585     const Items = enum { one, two };
    586 
    587     try expect(Items.two == .two);
    588     try expect(.two == Items.two);
    589 }
    590 
    591 const MultipleChoice = enum(u32) {
    592     A = 20,
    593     B = 40,
    594     C = 60,
    595     D = 1000,
    596 };
    597 
    598 fn testEnumWithSpecifiedTagValues(x: MultipleChoice) !void {
    599     try expect(@enumToInt(x) == 60);
    600     try expect(1234 == switch (x) {
    601         MultipleChoice.A => 1,
    602         MultipleChoice.B => 2,
    603         MultipleChoice.C => @as(u32, 1234),
    604         MultipleChoice.D => 4,
    605     });
    606 }
    607 
    608 test "enum with specified tag values" {
    609     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    610     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    611 
    612     try testEnumWithSpecifiedTagValues(MultipleChoice.C);
    613     comptime try testEnumWithSpecifiedTagValues(MultipleChoice.C);
    614 }
    615 
    616 test "non-exhaustive enum" {
    617     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    618     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    619 
    620     const S = struct {
    621         const E = enum(u8) { a, b, _ };
    622 
    623         fn doTheTest(y: u8) !void {
    624             var e: E = .b;
    625             try expect(switch (e) {
    626                 .a => false,
    627                 .b => true,
    628                 _ => false,
    629             });
    630             e = @intToEnum(E, 12);
    631             try expect(switch (e) {
    632                 .a => false,
    633                 .b => false,
    634                 _ => true,
    635             });
    636 
    637             try expect(switch (e) {
    638                 .a => false,
    639                 .b => false,
    640                 else => true,
    641             });
    642             e = .b;
    643             try expect(switch (e) {
    644                 .a => false,
    645                 else => true,
    646             });
    647 
    648             try expect(@typeInfo(E).Enum.fields.len == 2);
    649             e = @intToEnum(E, 12);
    650             try expect(@enumToInt(e) == 12);
    651             e = @intToEnum(E, y);
    652             try expect(@enumToInt(e) == 52);
    653             try expect(@typeInfo(E).Enum.is_exhaustive == false);
    654         }
    655     };
    656     try S.doTheTest(52);
    657     comptime try S.doTheTest(52);
    658 }
    659 
    660 test "empty non-exhaustive enum" {
    661     const S = struct {
    662         const E = enum(u8) { _ };
    663 
    664         fn doTheTest(y: u8) !void {
    665             var e = @intToEnum(E, y);
    666             try expect(switch (e) {
    667                 _ => true,
    668             });
    669             try expect(@enumToInt(e) == y);
    670 
    671             try expect(@typeInfo(E).Enum.fields.len == 0);
    672             try expect(@typeInfo(E).Enum.is_exhaustive == false);
    673         }
    674     };
    675     try S.doTheTest(42);
    676     comptime try S.doTheTest(42);
    677 }
    678 
    679 test "single field non-exhaustive enum" {
    680     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    681     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    682 
    683     const S = struct {
    684         const E = enum(u8) { a, _ };
    685         fn doTheTest(y: u8) !void {
    686             var e: E = .a;
    687             try expect(switch (e) {
    688                 .a => true,
    689                 _ => false,
    690             });
    691             e = @intToEnum(E, 12);
    692             try expect(switch (e) {
    693                 .a => false,
    694                 _ => true,
    695             });
    696 
    697             try expect(switch (e) {
    698                 .a => false,
    699                 else => true,
    700             });
    701             e = .a;
    702             try expect(switch (e) {
    703                 .a => true,
    704                 else => false,
    705             });
    706 
    707             try expect(@enumToInt(@intToEnum(E, y)) == y);
    708             try expect(@typeInfo(E).Enum.fields.len == 1);
    709             try expect(@typeInfo(E).Enum.is_exhaustive == false);
    710         }
    711     };
    712     try S.doTheTest(23);
    713     comptime try S.doTheTest(23);
    714 }
    715 
    716 const EnumWithTagValues = enum(u4) {
    717     A = 1 << 0,
    718     B = 1 << 1,
    719     C = 1 << 2,
    720     D = 1 << 3,
    721 };
    722 test "enum with tag values don't require parens" {
    723     try expect(@enumToInt(EnumWithTagValues.C) == 0b0100);
    724 }
    725 
    726 const MultipleChoice2 = enum(u32) {
    727     Unspecified1,
    728     A = 20,
    729     Unspecified2,
    730     B = 40,
    731     Unspecified3,
    732     C = 60,
    733     Unspecified4,
    734     D = 1000,
    735     Unspecified5,
    736 };
    737 
    738 test "cast integer literal to enum" {
    739     try expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1);
    740     try expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B);
    741 }
    742 
    743 test "enum with specified and unspecified tag values" {
    744     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    745     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    746 
    747     try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
    748     comptime try testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
    749 }
    750 
    751 fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
    752     try expect(@enumToInt(x) == 1000);
    753     try expect(1234 == switch (x) {
    754         MultipleChoice2.A => 1,
    755         MultipleChoice2.B => 2,
    756         MultipleChoice2.C => 3,
    757         MultipleChoice2.D => @as(u32, 1234),
    758         MultipleChoice2.Unspecified1 => 5,
    759         MultipleChoice2.Unspecified2 => 6,
    760         MultipleChoice2.Unspecified3 => 7,
    761         MultipleChoice2.Unspecified4 => 8,
    762         MultipleChoice2.Unspecified5 => 9,
    763     });
    764 }
    765 
    766 const Small2 = enum(u2) { One, Two };
    767 const Small = enum(u2) { One, Two, Three, Four };
    768 
    769 test "set enum tag type" {
    770     {
    771         var x = Small.One;
    772         x = Small.Two;
    773         comptime try expect(Tag(Small) == u2);
    774     }
    775     {
    776         var x = Small2.One;
    777         x = Small2.Two;
    778         comptime try expect(Tag(Small2) == u2);
    779     }
    780 }
    781 
    782 test "casting enum to its tag type" {
    783     try testCastEnumTag(Small2.Two);
    784     comptime try testCastEnumTag(Small2.Two);
    785 }
    786 
    787 fn testCastEnumTag(value: Small2) !void {
    788     try expect(@enumToInt(value) == 1);
    789 }
    790 
    791 test "enum with 1 field but explicit tag type should still have the tag type" {
    792     const Enum = enum(u8) {
    793         B = 2,
    794     };
    795     comptime try expect(@sizeOf(Enum) == @sizeOf(u8));
    796 }
    797 
    798 test "signed integer as enum tag" {
    799     const SignedEnum = enum(i2) {
    800         A0 = -1,
    801         A1 = 0,
    802         A2 = 1,
    803     };
    804 
    805     try expect(@enumToInt(SignedEnum.A0) == -1);
    806     try expect(@enumToInt(SignedEnum.A1) == 0);
    807     try expect(@enumToInt(SignedEnum.A2) == 1);
    808 }
    809 
    810 test "enum with one member and custom tag type" {
    811     const E = enum(u2) {
    812         One,
    813     };
    814     try expect(@enumToInt(E.One) == 0);
    815     const E2 = enum(u2) {
    816         One = 2,
    817     };
    818     try expect(@enumToInt(E2.One) == 2);
    819 }
    820 
    821 test "enum with one member and u1 tag type @enumToInt" {
    822     const Enum = enum(u1) {
    823         Test,
    824     };
    825     try expect(@enumToInt(Enum.Test) == 0);
    826 }
    827 
    828 test "enum with comptime_int tag type" {
    829     const Enum = enum(comptime_int) {
    830         One = 3,
    831         Two = 2,
    832         Three = 1,
    833     };
    834     comptime try expect(Tag(Enum) == comptime_int);
    835 }
    836 
    837 test "enum with one member default to u0 tag type" {
    838     const E0 = enum { X };
    839     comptime try expect(Tag(E0) == u0);
    840 }
    841 
    842 const EnumWithOneMember = enum { Eof };
    843 
    844 fn doALoopThing(id: EnumWithOneMember) void {
    845     while (true) {
    846         if (id == EnumWithOneMember.Eof) {
    847             break;
    848         }
    849         @compileError("above if condition should be comptime");
    850     }
    851 }
    852 
    853 test "comparison operator on enum with one member is comptime known" {
    854     doALoopThing(EnumWithOneMember.Eof);
    855 }
    856 
    857 const State = enum { Start };
    858 test "switch on enum with one member is comptime known" {
    859     var state = State.Start;
    860     switch (state) {
    861         State.Start => return,
    862     }
    863     @compileError("analysis should not reach here");
    864 }
    865 
    866 test "method call on an enum" {
    867     const S = struct {
    868         const E = enum {
    869             one,
    870             two,
    871 
    872             fn method(self: *E) bool {
    873                 return self.* == .two;
    874             }
    875 
    876             fn generic_method(self: *E, foo: anytype) bool {
    877                 return self.* == .two and foo == bool;
    878             }
    879         };
    880         fn doTheTest() !void {
    881             var e = E.two;
    882             try expect(e.method());
    883             try expect(e.generic_method(bool));
    884         }
    885     };
    886     try S.doTheTest();
    887     comptime try S.doTheTest();
    888 }
    889 
    890 test "enum value allocation" {
    891     const LargeEnum = enum(u32) {
    892         A0 = 0x80000000,
    893         A1,
    894         A2,
    895     };
    896 
    897     try expect(@enumToInt(LargeEnum.A0) == 0x80000000);
    898     try expect(@enumToInt(LargeEnum.A1) == 0x80000001);
    899     try expect(@enumToInt(LargeEnum.A2) == 0x80000002);
    900 }
    901 
    902 test "enum literal casting to tagged union" {
    903     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    904     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    905 
    906     const Arch = union(enum) {
    907         x86_64,
    908         arm: Arm32,
    909 
    910         const Arm32 = enum {
    911             v8_5a,
    912             v8_4a,
    913         };
    914     };
    915 
    916     var t = true;
    917     var x: Arch = .x86_64;
    918     var y = if (t) x else .x86_64;
    919     switch (y) {
    920         .x86_64 => {},
    921         else => @panic("fail"),
    922     }
    923 }
    924 
    925 const Bar = enum { A, B, C, D };
    926 
    927 test "enum literal casting to error union with payload enum" {
    928     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    929     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    930 
    931     var bar: error{B}!Bar = undefined;
    932     bar = .B; // should never cast to the error set
    933 
    934     try expect((try bar) == Bar.B);
    935 }
    936 
    937 test "constant enum initialization with differing sizes" {
    938     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
    939     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    940     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    941 
    942     try test3_1(test3_foo);
    943     try test3_2(test3_bar);
    944 }
    945 const Test3Foo = union(enum) {
    946     One: void,
    947     Two: f32,
    948     Three: Test3Point,
    949 };
    950 const Test3Point = struct {
    951     x: i32,
    952     y: i32,
    953 };
    954 const test3_foo = Test3Foo{
    955     .Three = Test3Point{
    956         .x = 3,
    957         .y = 4,
    958     },
    959 };
    960 const test3_bar = Test3Foo{ .Two = 13 };
    961 fn test3_1(f: Test3Foo) !void {
    962     switch (f) {
    963         Test3Foo.Three => |pt| {
    964             try expect(pt.x == 3);
    965             try expect(pt.y == 4);
    966         },
    967         else => unreachable,
    968     }
    969 }
    970 fn test3_2(f: Test3Foo) !void {
    971     switch (f) {
    972         Test3Foo.Two => |x| {
    973             try expect(x == 13);
    974         },
    975         else => unreachable,
    976     }
    977 }
    978 
    979 test "@tagName" {
    980     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
    981     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
    982     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
    983     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
    984     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
    985 
    986     try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
    987     comptime try expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
    988 }
    989 
    990 fn testEnumTagNameBare(n: anytype) []const u8 {
    991     return @tagName(n);
    992 }
    993 
    994 const BareNumber = enum { One, Two, Three };
    995 
    996 test "@tagName non-exhaustive enum" {
    997     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
    998     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
    999     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
   1000     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
   1001     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1002 
   1003     try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
   1004     comptime try expect(mem.eql(u8, testEnumTagNameBare(NonExhaustive.B), "B"));
   1005 }
   1006 const NonExhaustive = enum(u8) { A, B, _ };
   1007 
   1008 test "@tagName is null-terminated" {
   1009     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
   1010     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
   1011     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
   1012     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
   1013     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1014 
   1015     const S = struct {
   1016         fn doTheTest(n: BareNumber) !void {
   1017             try expect(@tagName(n)[3] == 0);
   1018         }
   1019     };
   1020     try S.doTheTest(.Two);
   1021     try comptime S.doTheTest(.Two);
   1022 }
   1023 
   1024 test "tag name with assigned enum values" {
   1025     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
   1026     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
   1027     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
   1028     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
   1029     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1030 
   1031     const LocalFoo = enum(u8) {
   1032         A = 1,
   1033         B = 0,
   1034     };
   1035     var b = LocalFoo.B;
   1036     try expect(mem.eql(u8, @tagName(b), "B"));
   1037 }
   1038 
   1039 test "@tagName on enum literals" {
   1040     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1041 
   1042     try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
   1043     comptime try expect(mem.eql(u8, @tagName(.FooBar), "FooBar"));
   1044 }
   1045 
   1046 test "enum literal casting to optional" {
   1047     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
   1048     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1049 
   1050     var bar: ?Bar = undefined;
   1051     bar = .B;
   1052 
   1053     try expect(bar.? == Bar.B);
   1054 }
   1055 
   1056 const A = enum(u3) { One, Two, Three, Four, One2, Two2, Three2, Four2 };
   1057 const B = enum(u3) { One3, Two3, Three3, Four3, One23, Two23, Three23, Four23 };
   1058 const C = enum(u2) { One4, Two4, Three4, Four4 };
   1059 
   1060 const BitFieldOfEnums = packed struct {
   1061     a: A,
   1062     b: B,
   1063     c: C,
   1064 };
   1065 
   1066 const bit_field_1 = BitFieldOfEnums{
   1067     .a = A.Two,
   1068     .b = B.Three3,
   1069     .c = C.Four4,
   1070 };
   1071 
   1072 test "bit field access with enum fields" {
   1073     if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
   1074     if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
   1075     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
   1076     if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
   1077 
   1078     var data = bit_field_1;
   1079     try expect(getA(&data) == A.Two);
   1080     try expect(getB(&data) == B.Three3);
   1081     try expect(getC(&data) == C.Four4);
   1082     comptime try expect(@sizeOf(BitFieldOfEnums) == 1);
   1083 
   1084     data.b = B.Four3;
   1085     try expect(data.b == B.Four3);
   1086 
   1087     data.a = A.Three;
   1088     try expect(data.a == A.Three);
   1089     try expect(data.b == B.Four3);
   1090 }
   1091 
   1092 fn getA(data: *const BitFieldOfEnums) A {
   1093     return data.a;
   1094 }
   1095 
   1096 fn getB(data: *const BitFieldOfEnums) B {
   1097     return data.b;
   1098 }
   1099 
   1100 fn getC(data: *const BitFieldOfEnums) C {
   1101     return data.c;
   1102 }
   1103 
   1104 test "enum literal in array literal" {
   1105     const Items = enum { one, two };
   1106     const array = [_]Items{ .one, .two };
   1107 
   1108     try expect(array[0] == .one);
   1109     try expect(array[1] == .two);
   1110 }