Skip to content

Commit 7d7e149

Browse files
committed
testsuite: tests for #[packed] structs.
1 parent cad2260 commit 7d7e149

11 files changed

+371
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This assumes the packed and non-packed structs are different sizes.
12+
13+
// the error points to the start of the file, not the line with the
14+
// transmute
15+
16+
// error-pattern: reinterpret_cast called on types with different size
17+
18+
#[packed]
19+
struct Foo<T,S> {
20+
bar: T,
21+
baz: S
22+
}
23+
24+
struct Oof<T, S> {
25+
rab: T,
26+
zab: S
27+
}
28+
29+
fn main() {
30+
let foo = Foo { bar: [1u8, 2, 3, 4, 5], baz: 10i32 };
31+
unsafe {
32+
let oof: Oof<[u8, .. 5], i32> = cast::transmute(foo);
33+
debug!(oof);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This assumes the packed and non-packed structs are different sizes.
12+
13+
// the error points to the start of the file, not the line with the
14+
// transmute
15+
16+
// error-pattern: reinterpret_cast called on types with different size
17+
18+
#[packed]
19+
struct Foo {
20+
bar: u8,
21+
baz: uint
22+
}
23+
24+
struct Oof {
25+
rab: u8,
26+
zab: uint
27+
}
28+
29+
fn main() {
30+
let foo = Foo { bar: 1, baz: 10 };
31+
unsafe {
32+
let oof: Oof = cast::transmute(foo);
33+
debug!(oof);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct Foo {
13+
bar: u8,
14+
baz: uint
15+
}
16+
17+
fn main() {
18+
let foo = Foo { bar: 1, baz: 2 };
19+
let brw = &foo.baz;
20+
21+
assert_eq!(*brw, 2);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S<T, S> {
13+
a: T,
14+
b: u8,
15+
c: S
16+
}
17+
18+
fn main() {
19+
unsafe {
20+
let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 };
21+
let transd : [u8, .. 9] = cast::transmute(s);
22+
// Don't worry about endianness, the numbers are palindromic.
23+
assert_eq!(transd,
24+
[0xff, 0xff, 0xff, 0xff,
25+
1,
26+
0xaa, 0xaa, 0xaa, 0xaa]);
27+
28+
29+
let s = S { a: 1u8, b: 2u8, c: 0b10000001_10000001 as i16};
30+
let transd : [u8, .. 4] = cast::transmute(s);
31+
// Again, no endianness problems.
32+
assert_eq!(transd,
33+
[1, 2, 0b10000001, 0b10000001]);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S<T, S> {
13+
a: T,
14+
b: u8,
15+
c: S
16+
}
17+
18+
fn main() {
19+
assert_eq!(sys::size_of::<S<u8, u8>>(), 3);
20+
21+
assert_eq!(sys::size_of::<S<u64, u16>>(), 11);
22+
23+
assert_eq!(sys::size_of::<S<~str, @mut [int]>>(),
24+
1 + sys::size_of::<~str>() + sys::size_of::<@mut [int]>());
25+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S4 {
13+
a: u8,
14+
b: [u8, .. 3],
15+
}
16+
17+
#[packed]
18+
struct S5 {
19+
a: u8,
20+
b: u32
21+
}
22+
23+
fn main() {
24+
unsafe {
25+
let s4 = S4 { a: 1, b: [2,3,4] };
26+
let transd : [u8, .. 4] = cast::transmute(s4);
27+
assert_eq!(transd, [1, 2, 3, 4]);
28+
29+
let s5 = S5 { a: 1, b: 0xff_00_00_ff };
30+
let transd : [u8, .. 5] = cast::transmute(s5);
31+
// Don't worry about endianness, the u32 is palindromic.
32+
assert_eq!(transd, [1, 0xff, 0, 0, 0xff]);
33+
}
34+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct Foo {
13+
bar: u8,
14+
baz: uint
15+
}
16+
17+
fn main() {
18+
let foo = Foo { bar: 1, baz: 2 };
19+
match foo {
20+
Foo {bar, baz} => {
21+
assert_eq!(bar, 1);
22+
assert_eq!(baz, 2);
23+
}
24+
}
25+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S4 {
13+
a: u8,
14+
b: [u8, .. 3],
15+
}
16+
17+
#[packed]
18+
struct S5 {
19+
a: u8,
20+
b: u32
21+
}
22+
23+
#[packed]
24+
struct S13_str {
25+
a: i64,
26+
b: f32,
27+
c: u8,
28+
d: ~str
29+
}
30+
31+
enum Foo {
32+
Bar = 1,
33+
Baz = 2
34+
}
35+
36+
#[packed]
37+
struct S3_Foo {
38+
a: u8,
39+
b: u16,
40+
c: Foo
41+
}
42+
43+
#[packed]
44+
struct S7_Option {
45+
a: f32,
46+
b: u8,
47+
c: u16,
48+
d: Option<@mut f64>
49+
}
50+
51+
52+
fn main() {
53+
assert_eq!(sys::size_of::<S4>(), 4);
54+
assert_eq!(sys::size_of::<S5>(), 5);
55+
assert_eq!(sys::size_of::<S13_str>(), 13 + sys::size_of::<~str>());
56+
assert_eq!(sys::size_of::<S3_Foo>(), 3 + sys::size_of::<Foo>());
57+
assert_eq!(sys::size_of::<S7_Option>(), 7 + sys::size_of::<Option<@mut f64>>());
58+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
#[deriving(Eq)]
13+
struct Foo {
14+
bar: u8,
15+
baz: u64
16+
}
17+
18+
fn main() {
19+
let foos = [Foo { bar: 1, baz: 2 }, .. 10];
20+
21+
assert_eq!(sys::size_of::<[Foo, .. 10]>(), 90);
22+
23+
for uint::range(0, 10) |i| {
24+
assert_eq!(foos[i], Foo { bar: 1, baz: 2});
25+
}
26+
27+
for foos.each |&foo| {
28+
assert_eq!(foo, Foo { bar: 1, baz: 2 });
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S4(u8,[u8, .. 3]);
13+
14+
#[packed]
15+
struct S5(u8,u32);
16+
17+
fn main() {
18+
unsafe {
19+
let s4 = S4(1, [2,3,4]);
20+
let transd : [u8, .. 4] = cast::transmute(s4);
21+
assert_eq!(transd, [1, 2, 3, 4]);
22+
23+
let s5 = S5(1, 0xff_00_00_ff);
24+
let transd : [u8, .. 5] = cast::transmute(s5);
25+
// Don't worry about endianness, the u32 is palindromic.
26+
assert_eq!(transd, [1, 0xff, 0, 0, 0xff]);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[packed]
12+
struct S4(u8,[u8, .. 3]);
13+
14+
#[packed]
15+
struct S5(u8, u32);
16+
17+
#[packed]
18+
struct S13_str(i64, f32, u8, ~str);
19+
20+
enum Foo {
21+
Bar = 1,
22+
Baz = 2
23+
}
24+
25+
#[packed]
26+
struct S3_Foo(u8, u16, Foo);
27+
28+
#[packed]
29+
struct S7_Option(f32, u8, u16, Option<@mut f64>);
30+
31+
fn main() {
32+
assert_eq!(sys::size_of::<S4>(), 4);
33+
34+
assert_eq!(sys::size_of::<S5>(), 5);
35+
36+
assert_eq!(sys::size_of::<S13_str>(),
37+
13 + sys::size_of::<~str>());
38+
39+
assert_eq!(sys::size_of::<S3_Foo>(),
40+
3 + sys::size_of::<Foo>());
41+
42+
assert_eq!(sys::size_of::<S7_Option>(),
43+
7 + sys::size_of::<Option<@mut f64>>());
44+
}

0 commit comments

Comments
 (0)