]> git.lizzy.rs Git - rust.git/commitdiff
Add tests for enum discriminant sizing.
authorJed Davis <jld@panix.com>
Sun, 29 Sep 2013 21:00:34 +0000 (14:00 -0700)
committerJed Davis <jld@panix.com>
Tue, 29 Oct 2013 16:09:20 +0000 (09:09 -0700)
src/test/compile-fail/enum-discrim-too-small.rs [new file with mode: 0644]
src/test/compile-fail/lint-ctypes-enum.rs [new file with mode: 0644]
src/test/run-pass/enum-discrim-autosizing.rs [new file with mode: 0644]
src/test/run-pass/enum-discrim-manual-sizing.rs [new file with mode: 0644]
src/test/run-pass/enum-discrim-width-stuff.rs

diff --git a/src/test/compile-fail/enum-discrim-too-small.rs b/src/test/compile-fail/enum-discrim-too-small.rs
new file mode 100644 (file)
index 0000000..2de50ad
--- /dev/null
@@ -0,0 +1,58 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[repr(u8)] //~ NOTE discriminant type specified here
+enum Eu8 {
+    Au8 = 23,
+    Bu8 = 223,
+    Cu8 = -23, //~ ERROR discriminant value outside specified type
+}
+
+#[repr(i8)] //~ NOTE discriminant type specified here
+enum Ei8 {
+    Ai8 = 23,
+    Bi8 = -23,
+    Ci8 = 223, //~ ERROR discriminant value outside specified type
+}
+
+#[repr(u16)] //~ NOTE discriminant type specified here
+enum Eu16 {
+    Au16 = 23,
+    Bu16 = 55555,
+    Cu16 = -22333, //~ ERROR discriminant value outside specified type
+}
+
+#[repr(i16)] //~ NOTE discriminant type specified here
+enum Ei16 {
+    Ai16 = 23,
+    Bi16 = -22333,
+    Ci16 = 55555, //~ ERROR discriminant value outside specified type
+}
+
+#[repr(u32)] //~ NOTE discriminant type specified here
+enum Eu32 {
+    Au32 = 23,
+    Bu32 = 3_000_000_000,
+    Cu32 = -2_000_000_000, //~ ERROR discriminant value outside specified type
+}
+
+#[repr(i32)] //~ NOTE discriminant type specified here
+enum Ei32 {
+    Ai32 = 23,
+    Bi32 = -2_000_000_000,
+    Ci32 = 3_000_000_000, //~ ERROR discriminant value outside specified type
+}
+
+// u64 currently allows negative numbers, and i64 allows numbers greater than `1<<63`.  This is a
+// little counterintuitive, but since the discriminant can store all the bits, and extracting it
+// with a cast requires specifying the signedness, there is no loss of information in those cases.
+// This also applies to int and uint on 64-bit targets.
+
+pub fn main() { }
diff --git a/src/test/compile-fail/lint-ctypes-enum.rs b/src/test/compile-fail/lint-ctypes-enum.rs
new file mode 100644 (file)
index 0000000..857e3bb
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[deny(ctypes)];
+
+enum Z { }
+enum U { A }
+enum B { C, D }
+enum T { E, F, G }
+
+extern {
+   fn zf(x: Z);
+   fn uf(x: U);
+   fn bf(x: B); //~ ERROR found enum type without foreign-function-safe
+   fn tf(x: T); //~ ERROR found enum type without foreign-function-safe
+}
+
+pub fn main() { }
diff --git a/src/test/run-pass/enum-discrim-autosizing.rs b/src/test/run-pass/enum-discrim-autosizing.rs
new file mode 100644 (file)
index 0000000..ef34115
--- /dev/null
@@ -0,0 +1,62 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::mem::size_of;
+
+enum Ei8 {
+    Ai8 = -1,
+    Bi8 = 0
+}
+
+enum Eu8 {
+    Au8 = 0,
+    Bu8 = 0x80
+}
+
+enum Ei16 {
+    Ai16 = -1,
+    Bi16 = 0x80
+}
+
+enum Eu16 {
+    Au16 = 0,
+    Bu16 = 0x8000
+}
+
+enum Ei32 {
+    Ai32 = -1,
+    Bi32 = 0x8000
+}
+
+enum Eu32 {
+    Au32 = 0,
+    Bu32 = 0x8000_0000
+}
+
+enum Ei64 {
+    Ai64 = -1,
+    Bi64 = 0x8000_0000
+}
+
+enum Eu64 {
+    Au64 = 0,
+    Bu64 = 0x8000_0000_0000_0000
+}
+
+pub fn main() {
+    assert_eq!(size_of::<Ei8>(), 1);
+    assert_eq!(size_of::<Eu8>(), 1);
+    assert_eq!(size_of::<Ei16>(), 2);
+    assert_eq!(size_of::<Eu16>(), 2);
+    assert_eq!(size_of::<Ei32>(), 4);
+    assert_eq!(size_of::<Eu32>(), 4);
+    assert_eq!(size_of::<Ei64>(), 8);
+    assert_eq!(size_of::<Eu64>(), 8);
+}
diff --git a/src/test/run-pass/enum-discrim-manual-sizing.rs b/src/test/run-pass/enum-discrim-manual-sizing.rs
new file mode 100644 (file)
index 0000000..16eaac0
--- /dev/null
@@ -0,0 +1,84 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::mem::size_of;
+
+#[repr(i8)]
+enum Ei8 {
+    Ai8 = 0,
+    Bi8 = 1
+}
+
+#[repr(u8)]
+enum Eu8 {
+    Au8 = 0,
+    Bu8 = 1
+}
+
+#[repr(i16)]
+enum Ei16 {
+    Ai16 = 0,
+    Bi16 = 1
+}
+
+#[repr(u16)]
+enum Eu16 {
+    Au16 = 0,
+    Bu16 = 1
+}
+
+#[repr(i32)]
+enum Ei32 {
+    Ai32 = 0,
+    Bi32 = 1
+}
+
+#[repr(u32)]
+enum Eu32 {
+    Au32 = 0,
+    Bu32 = 1
+}
+
+#[repr(i64)]
+enum Ei64 {
+    Ai64 = 0,
+    Bi64 = 1
+}
+
+#[repr(u64)]
+enum Eu64 {
+    Au64 = 0,
+    Bu64 = 1
+}
+
+#[repr(int)]
+enum Eint {
+    Aint = 0,
+    Bint = 1
+}
+
+#[repr(uint)]
+enum Euint {
+    Auint = 0,
+    Buint = 1
+}
+
+pub fn main() {
+    assert_eq!(size_of::<Ei8>(), 1);
+    assert_eq!(size_of::<Eu8>(), 1);
+    assert_eq!(size_of::<Ei16>(), 2);
+    assert_eq!(size_of::<Eu16>(), 2);
+    assert_eq!(size_of::<Ei32>(), 4);
+    assert_eq!(size_of::<Eu32>(), 4);
+    assert_eq!(size_of::<Ei64>(), 8);
+    assert_eq!(size_of::<Eu64>(), 8);
+    assert_eq!(size_of::<Eint>(), size_of::<int>());
+    assert_eq!(size_of::<Euint>(), size_of::<uint>());
+}
index bc4cb87f06b7a15c6eae185a9abb9bccec7ff788..61efb2c8a4299734c806400f7838551cb0f082f0 100644 (file)
@@ -8,13 +8,48 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use std::mem;
+#[feature(macro_rules)];
+
+macro_rules! check {
+    ($m:ident, $t:ty, $v:expr) => {{
+        mod $m {
+            use std::mem::size_of;
+            enum E {
+                V = $v,
+                A = 0
+            }
+            static C: E = V;
+            pub fn check() {
+                assert_eq!(size_of::<E>(), size_of::<$t>());
+                assert_eq!(V as $t, $v);
+                assert_eq!(C as $t, $v);
+                assert_eq!(format!("{:?}", V), ~"V");
+                assert_eq!(format!("{:?}", C), ~"V");
+            }
+        }
+        $m::check();
+    }}
+}
 
 pub fn main() {
-    enum E { V = 0x1717171717171717 }
-    static C: E = V;
-    assert_eq!(V as u64, 0x1717171717171717u64);
-    assert_eq!(C as u64, 0x1717171717171717u64);
-    assert_eq!(format!("{:?}", V), ~"V");
-    assert_eq!(format!("{:?}", C), ~"V");
+    check!(a, u8, 0x17);
+    check!(b, u8, 0xe8);
+    check!(c, u16, 0x1727);
+    check!(d, u16, 0xe8d8);
+    check!(e, u32, 0x17273747);
+    check!(f, u32, 0xe8d8c8b8);
+    check!(g, u64, 0x1727374757677787u64);
+    check!(h, u64, 0xe8d8c8b8a8988878u64);
+
+    check!(z, i8, 0x17);
+    check!(y, i8, -0x17);
+    check!(x, i16, 0x1727);
+    check!(w, i16, -0x1727);
+    check!(v, i32, 0x17273747);
+    check!(u, i32, -0x17273747);
+    check!(t, i64, 0x1727374757677787);
+    check!(s, i64, -0x1727374757677787);
+
+    enum Simple { A, B }
+    assert_eq!(std::mem::size_of::<Simple>(), 1);
 }