diff --git a/src/test/compile-fail/packed-struct-generic-transmute.rs b/src/test/compile-fail/packed-struct-generic-transmute.rs new file mode 100644 index 00000000000..3ed9d00be28 --- /dev/null +++ b/src/test/compile-fail/packed-struct-generic-transmute.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This assumes the packed and non-packed structs are different sizes. + +// the error points to the start of the file, not the line with the +// transmute + +// error-pattern: reinterpret_cast called on types with different size + +#[packed] +struct Foo { + bar: T, + baz: S +} + +struct Oof { + rab: T, + zab: S +} + +fn main() { + let foo = Foo { bar: [1u8, 2, 3, 4, 5], baz: 10i32 }; + unsafe { + let oof: Oof<[u8, .. 5], i32> = cast::transmute(foo); + debug!(oof); + } +} diff --git a/src/test/compile-fail/packed-struct-transmute.rs b/src/test/compile-fail/packed-struct-transmute.rs new file mode 100644 index 00000000000..d2aca3c0d6b --- /dev/null +++ b/src/test/compile-fail/packed-struct-transmute.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This assumes the packed and non-packed structs are different sizes. + +// the error points to the start of the file, not the line with the +// transmute + +// error-pattern: reinterpret_cast called on types with different size + +#[packed] +struct Foo { + bar: u8, + baz: uint +} + +struct Oof { + rab: u8, + zab: uint +} + +fn main() { + let foo = Foo { bar: 1, baz: 10 }; + unsafe { + let oof: Oof = cast::transmute(foo); + debug!(oof); + } +} diff --git a/src/test/run-pass/packed-struct-borrow-element.rs b/src/test/run-pass/packed-struct-borrow-element.rs new file mode 100644 index 00000000000..a331b80a894 --- /dev/null +++ b/src/test/run-pass/packed-struct-borrow-element.rs @@ -0,0 +1,22 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct Foo { + bar: u8, + baz: uint +} + +fn main() { + let foo = Foo { bar: 1, baz: 2 }; + let brw = &foo.baz; + + assert_eq!(*brw, 2); +} diff --git a/src/test/run-pass/packed-struct-generic-layout.rs b/src/test/run-pass/packed-struct-generic-layout.rs new file mode 100644 index 00000000000..fd6e3b670f5 --- /dev/null +++ b/src/test/run-pass/packed-struct-generic-layout.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S { + a: T, + b: u8, + c: S +} + +fn main() { + unsafe { + let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 }; + let transd : [u8, .. 9] = cast::transmute(s); + // Don't worry about endianness, the numbers are palindromic. + assert_eq!(transd, + [0xff, 0xff, 0xff, 0xff, + 1, + 0xaa, 0xaa, 0xaa, 0xaa]); + + + let s = S { a: 1u8, b: 2u8, c: 0b10000001_10000001 as i16}; + let transd : [u8, .. 4] = cast::transmute(s); + // Again, no endianness problems. + assert_eq!(transd, + [1, 2, 0b10000001, 0b10000001]); + } +} diff --git a/src/test/run-pass/packed-struct-generic-size.rs b/src/test/run-pass/packed-struct-generic-size.rs new file mode 100644 index 00000000000..a5c4d5385a2 --- /dev/null +++ b/src/test/run-pass/packed-struct-generic-size.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S { + a: T, + b: u8, + c: S +} + +fn main() { + assert_eq!(sys::size_of::>(), 3); + + assert_eq!(sys::size_of::>(), 11); + + assert_eq!(sys::size_of::>(), + 1 + sys::size_of::<~str>() + sys::size_of::<@mut [int]>()); +} diff --git a/src/test/run-pass/packed-struct-layout.rs b/src/test/run-pass/packed-struct-layout.rs new file mode 100644 index 00000000000..8d27e55e191 --- /dev/null +++ b/src/test/run-pass/packed-struct-layout.rs @@ -0,0 +1,34 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S4 { + a: u8, + b: [u8, .. 3], +} + +#[packed] +struct S5 { + a: u8, + b: u32 +} + +fn main() { + unsafe { + let s4 = S4 { a: 1, b: [2,3,4] }; + let transd : [u8, .. 4] = cast::transmute(s4); + assert_eq!(transd, [1, 2, 3, 4]); + + let s5 = S5 { a: 1, b: 0xff_00_00_ff }; + let transd : [u8, .. 5] = cast::transmute(s5); + // Don't worry about endianness, the u32 is palindromic. + assert_eq!(transd, [1, 0xff, 0, 0, 0xff]); + } +} diff --git a/src/test/run-pass/packed-struct-match.rs b/src/test/run-pass/packed-struct-match.rs new file mode 100644 index 00000000000..15e7b6b0ce0 --- /dev/null +++ b/src/test/run-pass/packed-struct-match.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct Foo { + bar: u8, + baz: uint +} + +fn main() { + let foo = Foo { bar: 1, baz: 2 }; + match foo { + Foo {bar, baz} => { + assert_eq!(bar, 1); + assert_eq!(baz, 2); + } + } +} diff --git a/src/test/run-pass/packed-struct-size.rs b/src/test/run-pass/packed-struct-size.rs new file mode 100644 index 00000000000..372fe3d37f6 --- /dev/null +++ b/src/test/run-pass/packed-struct-size.rs @@ -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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S4 { + a: u8, + b: [u8, .. 3], +} + +#[packed] +struct S5 { + a: u8, + b: u32 +} + +#[packed] +struct S13_str { + a: i64, + b: f32, + c: u8, + d: ~str +} + +enum Foo { + Bar = 1, + Baz = 2 +} + +#[packed] +struct S3_Foo { + a: u8, + b: u16, + c: Foo +} + +#[packed] +struct S7_Option { + a: f32, + b: u8, + c: u16, + d: Option<@mut f64> +} + + +fn main() { + assert_eq!(sys::size_of::(), 4); + assert_eq!(sys::size_of::(), 5); + assert_eq!(sys::size_of::(), 13 + sys::size_of::<~str>()); + assert_eq!(sys::size_of::(), 3 + sys::size_of::()); + assert_eq!(sys::size_of::(), 7 + sys::size_of::>()); +} diff --git a/src/test/run-pass/packed-struct-vec.rs b/src/test/run-pass/packed-struct-vec.rs new file mode 100644 index 00000000000..b1ac29b7721 --- /dev/null +++ b/src/test/run-pass/packed-struct-vec.rs @@ -0,0 +1,30 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +#[deriving(Eq)] +struct Foo { + bar: u8, + baz: u64 +} + +fn main() { + let foos = [Foo { bar: 1, baz: 2 }, .. 10]; + + assert_eq!(sys::size_of::<[Foo, .. 10]>(), 90); + + for uint::range(0, 10) |i| { + assert_eq!(foos[i], Foo { bar: 1, baz: 2}); + } + + for foos.each |&foo| { + assert_eq!(foo, Foo { bar: 1, baz: 2 }); + } +} diff --git a/src/test/run-pass/packed-tuple-struct-layout.rs b/src/test/run-pass/packed-tuple-struct-layout.rs new file mode 100644 index 00000000000..9c2fe621a32 --- /dev/null +++ b/src/test/run-pass/packed-tuple-struct-layout.rs @@ -0,0 +1,28 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S4(u8,[u8, .. 3]); + +#[packed] +struct S5(u8,u32); + +fn main() { + unsafe { + let s4 = S4(1, [2,3,4]); + let transd : [u8, .. 4] = cast::transmute(s4); + assert_eq!(transd, [1, 2, 3, 4]); + + let s5 = S5(1, 0xff_00_00_ff); + let transd : [u8, .. 5] = cast::transmute(s5); + // Don't worry about endianness, the u32 is palindromic. + assert_eq!(transd, [1, 0xff, 0, 0, 0xff]); + } +} diff --git a/src/test/run-pass/packed-tuple-struct-size.rs b/src/test/run-pass/packed-tuple-struct-size.rs new file mode 100644 index 00000000000..23faa2eb0ad --- /dev/null +++ b/src/test/run-pass/packed-tuple-struct-size.rs @@ -0,0 +1,44 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[packed] +struct S4(u8,[u8, .. 3]); + +#[packed] +struct S5(u8, u32); + +#[packed] +struct S13_str(i64, f32, u8, ~str); + +enum Foo { + Bar = 1, + Baz = 2 +} + +#[packed] +struct S3_Foo(u8, u16, Foo); + +#[packed] +struct S7_Option(f32, u8, u16, Option<@mut f64>); + +fn main() { + assert_eq!(sys::size_of::(), 4); + + assert_eq!(sys::size_of::(), 5); + + assert_eq!(sys::size_of::(), + 13 + sys::size_of::<~str>()); + + assert_eq!(sys::size_of::(), + 3 + sys::size_of::()); + + assert_eq!(sys::size_of::(), + 7 + sys::size_of::>()); +}