1
Fork 0

testsuite: tests for #[packed] structs.

This commit is contained in:
Huon Wilson 2013-04-10 23:47:53 +10:00
parent cad226025b
commit 7d7e149748
11 changed files with 371 additions and 0 deletions

View file

@ -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 <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.
// 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<T,S> {
bar: T,
baz: S
}
struct Oof<T, S> {
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);
}
}

View file

@ -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 <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.
// 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);
}
}

View file

@ -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 <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.
#[packed]
struct Foo {
bar: u8,
baz: uint
}
fn main() {
let foo = Foo { bar: 1, baz: 2 };
let brw = &foo.baz;
assert_eq!(*brw, 2);
}

View file

@ -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 <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.
#[packed]
struct S<T, 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]);
}
}

View file

@ -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.
#[packed]
struct S<T, S> {
a: T,
b: u8,
c: S
}
fn main() {
assert_eq!(sys::size_of::<S<u8, u8>>(), 3);
assert_eq!(sys::size_of::<S<u64, u16>>(), 11);
assert_eq!(sys::size_of::<S<~str, @mut [int]>>(),
1 + sys::size_of::<~str>() + sys::size_of::<@mut [int]>());
}

View file

@ -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 <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.
#[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]);
}
}

View file

@ -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.
#[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);
}
}
}

View file

@ -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.
#[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::<S4>(), 4);
assert_eq!(sys::size_of::<S5>(), 5);
assert_eq!(sys::size_of::<S13_str>(), 13 + sys::size_of::<~str>());
assert_eq!(sys::size_of::<S3_Foo>(), 3 + sys::size_of::<Foo>());
assert_eq!(sys::size_of::<S7_Option>(), 7 + sys::size_of::<Option<@mut f64>>());
}

View file

@ -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 <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.
#[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 });
}
}

View file

@ -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 <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.
#[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]);
}
}

View file

@ -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 <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.
#[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::<S4>(), 4);
assert_eq!(sys::size_of::<S5>(), 5);
assert_eq!(sys::size_of::<S13_str>(),
13 + sys::size_of::<~str>());
assert_eq!(sys::size_of::<S3_Foo>(),
3 + sys::size_of::<Foo>());
assert_eq!(sys::size_of::<S7_Option>(),
7 + sys::size_of::<Option<@mut f64>>());
}