Skip to content

Commit aaef1a1

Browse files
adpaco-awstedinski
authored andcommitted
Basic tests for size_of_val and min_alig_of_val (rust-lang#1101)
* Basic tests for `size_of_val` and `min_alig_of_val` * Fix format * Add cases for `repr(C)` struct * Add note
1 parent c6981c0 commit aaef1a1

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `min_align_of_val` intrinsic
5+
// with common data types. Note that these tests assume an x86_64 architecture,
6+
// which is the only architecture supported by Kani at the moment.
7+
#![feature(core_intrinsics)]
8+
use std::intrinsics::min_align_of_val;
9+
10+
struct MyStruct {
11+
val: u32,
12+
}
13+
14+
#[repr(C)]
15+
struct CStruct {
16+
a: u8,
17+
b: i32,
18+
}
19+
20+
enum MyEnum {
21+
Variant,
22+
}
23+
24+
#[kani::proof]
25+
fn main() {
26+
unsafe {
27+
// Scalar types
28+
assert!(min_align_of_val(&0i8) == 1);
29+
assert!(min_align_of_val(&0i16) == 2);
30+
assert!(min_align_of_val(&0i32) == 4);
31+
assert!(min_align_of_val(&0i64) == 8);
32+
assert!(min_align_of_val(&0i128) == 8);
33+
assert!(min_align_of_val(&0isize) == 8);
34+
assert!(min_align_of_val(&0u8) == 1);
35+
assert!(min_align_of_val(&0u16) == 2);
36+
assert!(min_align_of_val(&0u32) == 4);
37+
assert!(min_align_of_val(&0u64) == 8);
38+
assert!(min_align_of_val(&0u128) == 8);
39+
assert!(min_align_of_val(&0usize) == 8);
40+
assert!(min_align_of_val(&0f32) == 4);
41+
assert!(min_align_of_val(&0f64) == 8);
42+
assert!(min_align_of_val(&false) == 1);
43+
assert!(min_align_of_val(&(0 as char)) == 4);
44+
// Compound types (tuple and array)
45+
assert!(min_align_of_val(&(0i32, 0i32)) == 4);
46+
assert!(min_align_of_val(&[0i32; 5]) == 4);
47+
// Custom data types (struct and enum)
48+
assert!(min_align_of_val(&MyStruct { val: 0u32 }) == 4);
49+
assert!(min_align_of_val(&MyEnum::Variant) == 1);
50+
assert!(min_align_of_val(&CStruct { a: 0u8, b: 0i32 }) == 4);
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that we get the expected results for the `size_of_val` intrinsic with
5+
// common data types. Note that these tests assume an x86_64 architecture, which
6+
// is the only architecture supported by Kani at the moment.
7+
#![feature(core_intrinsics)]
8+
use std::intrinsics::size_of_val;
9+
10+
struct MyStruct {
11+
val: u32,
12+
}
13+
14+
#[repr(C)]
15+
struct CStruct {
16+
a: u8,
17+
b: i32,
18+
}
19+
20+
enum MyEnum {
21+
Variant,
22+
}
23+
24+
#[kani::proof]
25+
fn main() {
26+
unsafe {
27+
// Scalar types
28+
assert!(size_of_val(&0i8) == 1);
29+
assert!(size_of_val(&0i16) == 2);
30+
assert!(size_of_val(&0i32) == 4);
31+
assert!(size_of_val(&0i64) == 8);
32+
assert!(size_of_val(&0i128) == 16);
33+
assert!(size_of_val(&0isize) == 8);
34+
assert!(size_of_val(&0u8) == 1);
35+
assert!(size_of_val(&0u16) == 2);
36+
assert!(size_of_val(&0u32) == 4);
37+
assert!(size_of_val(&0u64) == 8);
38+
assert!(size_of_val(&0u128) == 16);
39+
assert!(size_of_val(&0usize) == 8);
40+
assert!(size_of_val(&0f32) == 4);
41+
assert!(size_of_val(&0f64) == 8);
42+
assert!(size_of_val(&false) == 1);
43+
assert!(size_of_val(&(0 as char)) == 4);
44+
// Compound types (tuple and array)
45+
assert!(size_of_val(&(0i32, 0i32)) == 8);
46+
assert!(size_of_val(&[0i32; 5]) == 20);
47+
// Custom data types (struct and enum)
48+
assert!(size_of_val(&MyStruct { val: 0u32 }) == 4);
49+
assert!(size_of_val(&MyEnum::Variant) == 0);
50+
assert!(size_of_val(&CStruct { a: 0u8, b: 0i32 }) == 8);
51+
}
52+
}

0 commit comments

Comments
 (0)