aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-12-02 22:31:42 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-12-02 22:32:39 -0500
commit137c8f5e8a6023db24f90555e968b592a4b843e4 (patch)
tree822547a6857ad24dad7ed8c7d26720d17b7bcce7 /test
parent98237f7c0ba62099e85a8caf8fc09039845b224e (diff)
downloadzig-137c8f5e8a6023db24f90555e968b592a4b843e4.tar.gz
zig-137c8f5e8a6023db24f90555e968b592a4b843e4.zip
ability to set tag values of enums
also remove support for enums with 0 values closes #305
Diffstat (limited to 'test')
-rw-r--r--test/cases/enum.zig56
-rw-r--r--test/compile_errors.zig55
2 files changed, 103 insertions, 8 deletions
diff --git a/test/cases/enum.zig b/test/cases/enum.zig
index 6df858a48f..eda3cf6376 100644
--- a/test/cases/enum.zig
+++ b/test/cases/enum.zig
@@ -137,7 +137,6 @@ const AlignTestEnum = enum {
B: u64,
};
-const ValueCount0 = enum {};
const ValueCount1 = enum { I0 };
const ValueCount2 = enum { I0, I1 };
const ValueCount256 = enum {
@@ -183,7 +182,6 @@ const ValueCount257 = enum {
test "enum sizes" {
comptime {
- assert(@sizeOf(ValueCount0) == 0);
assert(@sizeOf(ValueCount1) == 0);
assert(@sizeOf(ValueCount2) == 1);
assert(@sizeOf(ValueCount256) == 1);
@@ -292,3 +290,57 @@ test "casting enum to its tag type" {
fn testCastEnumToTagType(value: Small2) {
assert(u2(value) == 1);
}
+
+const MultipleChoice = enum(u32) {
+ A = 20,
+ B = 40,
+ C = 60,
+ D = 1000,
+};
+
+test "enum with specified tag values" {
+ testEnumWithSpecifiedTagValues(MultipleChoice.C);
+ comptime testEnumWithSpecifiedTagValues(MultipleChoice.C);
+}
+
+fn testEnumWithSpecifiedTagValues(x: MultipleChoice) {
+ assert(u32(x) == 60);
+ assert(1234 == switch (x) {
+ MultipleChoice.A => 1,
+ MultipleChoice.B => 2,
+ MultipleChoice.C => u32(1234),
+ MultipleChoice.D => 4,
+ });
+}
+
+const MultipleChoice2 = enum(u32) {
+ Unspecified1,
+ A = 20,
+ Unspecified2,
+ B = 40,
+ Unspecified3,
+ C = 60,
+ Unspecified4,
+ D = 1000,
+ Unspecified5,
+};
+
+test "enum with specified and unspecified tag values" {
+ testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
+ comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2.D);
+}
+
+fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) {
+ assert(u32(x) == 1000);
+ assert(1234 == switch (x) {
+ MultipleChoice2.A => 1,
+ MultipleChoice2.B => 2,
+ MultipleChoice2.C => 3,
+ MultipleChoice2.D => u32(1234),
+ MultipleChoice2.Unspecified1 => 5,
+ MultipleChoice2.Unspecified2 => 6,
+ MultipleChoice2.Unspecified3 => 7,
+ MultipleChoice2.Unspecified4 => 8,
+ MultipleChoice2.Unspecified5 => 9,
+ });
+}
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 367dec08b3..e1de167ac5 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2307,11 +2307,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("@memberType enum out of bounds",
\\comptime {
- \\ _ = @memberType(Foo, 0);
+ \\ _ = @memberType(Foo, 1);
\\}
- \\const Foo = enum {};
+ \\const Foo = enum {A,};
,
- ".tmp_source.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members");
+ ".tmp_source.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members");
cases.add("@memberName on unsupported type",
\\comptime {
@@ -2330,11 +2330,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("@memberName enum out of bounds",
\\comptime {
- \\ _ = @memberName(Foo, 0);
+ \\ _ = @memberName(Foo, 1);
\\}
- \\const Foo = enum {};
+ \\const Foo = enum {A,};
,
- ".tmp_source.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members");
+ ".tmp_source.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members");
cases.add("calling var args extern function, passing array instead of pointer",
\\export fn entry() {
@@ -2447,4 +2447,47 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
\\}
,
".tmp_source.zig:1:19: error: expected unsigned integer, found 'i2'");
+
+ cases.add("struct fields with value assignments",
+ \\const MultipleChoice = struct {
+ \\ A: i32 = 20,
+ \\};
+ \\export fn entry() {
+ \\ var x: MultipleChoice = undefined;
+ \\}
+ ,
+ ".tmp_source.zig:2:14: error: enums, not structs, support field assignment");
+
+ cases.add("union fields with value assignments",
+ \\const MultipleChoice = union {
+ \\ A: i32 = 20,
+ \\};
+ \\export fn entry() {
+ \\ var x: MultipleChoice = undefined;
+ \\}
+ ,
+ ".tmp_source.zig:2:14: error: enums, not unions, support field assignment");
+
+ cases.add("enum with 0 fields",
+ \\const Foo = enum {};
+ \\export fn entry() -> usize {
+ \\ return @sizeOf(Foo);
+ \\}
+ ,
+ ".tmp_source.zig:1:13: error: enums must have 1 or more fields");
+
+ cases.add("enum value already taken",
+ \\const MultipleChoice = enum(u32) {
+ \\ A = 20,
+ \\ B = 40,
+ \\ C = 60,
+ \\ D = 1000,
+ \\ E = 60,
+ \\};
+ \\export fn entry() {
+ \\ var x = MultipleChoice.C;
+ \\}
+ ,
+ ".tmp_source.zig:6:9: error: enum tag value 60 already taken",
+ ".tmp_source.zig:4:9: note: other occurrence here");
}