Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 5f2f5b7

Browse files
author
bors-servo
authored
Auto merge of #79 - servo:nonjemalloc, r=nox
Add opt in for weaker tests, for non-jemalloc allocators. Fix #74 Distributions packages can use `cargo test --features flexible-tests` <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/heapsize/79) <!-- Reviewable:end -->
2 parents 8a0e3a2 + 11dd485 commit 5f2f5b7

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ kernel32-sys = "0.2.1"
1212

1313
[features]
1414
unstable = []
15+
16+
# https://github.com/servo/heapsize/issues/74
17+
flexible-tests = []

tests/tests.rs

+50-31
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,32 @@ extern crate heapsize;
99
use heapsize::{HeapSizeOf, heap_size_of};
1010
use std::os::raw::c_void;
1111

12-
pub const EMPTY: *mut () = 0x1 as *mut ();
12+
const EMPTY: *mut () = 0x1 as *mut ();
13+
14+
/// https://github.com/servo/heapsize/issues/74
15+
#[cfg(feature = "flexible-tests")]
16+
macro_rules! assert_size {
17+
($actual: expr, $expected: expr) => {
18+
{
19+
let actual = $actual;
20+
let expected = $expected;
21+
assert!(actual >= expected, "expected {:?} >= {:?}", actual, expected)
22+
}
23+
}
24+
}
25+
26+
#[cfg(not(feature = "flexible-tests"))]
27+
macro_rules! assert_size {
28+
($actual: expr, $expected: expr) => {
29+
assert_eq!($actual, $expected)
30+
}
31+
}
1332

1433
#[cfg(feature = "unstable")]
1534
mod unstable {
1635
extern crate alloc;
1736

18-
use heapsize::{HeapSizeOf, heap_size_of};
37+
use heapsize::heap_size_of;
1938
use std::os::raw::c_void;
2039

2140
#[repr(C, simd)]
@@ -32,22 +51,22 @@ mod unstable {
3251
unsafe {
3352
// A 64 byte request is allocated exactly.
3453
let x = alloc::heap::allocate(64, 0);
35-
assert_eq!(heap_size_of(x as *const c_void), 64);
54+
assert_size!(heap_size_of(x as *const c_void), 64);
3655
alloc::heap::deallocate(x, 64, 0);
3756

3857
// A 255 byte request is rounded up to 256 bytes.
3958
let x = alloc::heap::allocate(255, 0);
40-
assert_eq!(heap_size_of(x as *const c_void), 256);
59+
assert_size!(heap_size_of(x as *const c_void), 256);
4160
alloc::heap::deallocate(x, 255, 0);
4261

4362
// A 1MiB request is allocated exactly.
4463
let x = alloc::heap::allocate(1024 * 1024, 0);
45-
assert_eq!(heap_size_of(x as *const c_void), 1024 * 1024);
64+
assert_size!(heap_size_of(x as *const c_void), 1024 * 1024);
4665
alloc::heap::deallocate(x, 1024 * 1024, 0);
4766

4867
// An overaligned 1MiB request is allocated exactly.
4968
let x = alloc::heap::allocate(1024 * 1024, 32);
50-
assert_eq!(heap_size_of(x as *const c_void), 1024 * 1024);
69+
assert_size!(heap_size_of(x as *const c_void), 1024 * 1024);
5170
alloc::heap::deallocate(x, 1024 * 1024, 32);
5271
}
5372
}
@@ -58,22 +77,22 @@ mod unstable {
5877
unsafe {
5978
// A 64 byte request is allocated exactly.
6079
let x = alloc::heap::allocate(64, 0);
61-
assert_eq!(heap_size_of(x as *const c_void), 64);
80+
assert_size!(heap_size_of(x as *const c_void), 64);
6281
alloc::heap::deallocate(x, 64, 0);
6382

6483
// A 255 byte request is allocated exactly.
6584
let x = alloc::heap::allocate(255, 0);
66-
assert_eq!(heap_size_of(x as *const c_void), 255);
85+
assert_size!(heap_size_of(x as *const c_void), 255);
6786
alloc::heap::deallocate(x, 255, 0);
6887

6988
// A 1MiB request is allocated exactly.
7089
let x = alloc::heap::allocate(1024 * 1024, 0);
71-
assert_eq!(heap_size_of(x as *const c_void), 1024 * 1024);
90+
assert_size!(heap_size_of(x as *const c_void), 1024 * 1024);
7291
alloc::heap::deallocate(x, 1024 * 1024, 0);
7392

7493
// An overaligned 1MiB request is over-allocated.
7594
let x = alloc::heap::allocate(1024 * 1024, 32);
76-
assert_eq!(heap_size_of(x as *const c_void), 1024 * 1024 + 32);
95+
assert_size!(heap_size_of(x as *const c_void), 1024 * 1024 + 32);
7796
alloc::heap::deallocate(x, 1024 * 1024, 32);
7897
}
7998
}
@@ -82,21 +101,21 @@ mod unstable {
82101
#[test]
83102
fn test_simd() {
84103
let x = Box::new(OverAligned(0, 0, 0, 0));
85-
assert_eq!(unsafe { heap_size_of(&*x as *const _ as *const c_void) }, 32);
104+
assert_size!(unsafe { heap_size_of(&*x as *const _ as *const c_void) }, 32);
86105
}
87106

88107
#[cfg(target_os = "windows")]
89108
#[test]
90109
fn test_simd() {
91110
let x = Box::new(OverAligned(0, 0, 0, 0));
92-
assert_eq!(unsafe { heap_size_of(&*x as *const _ as *const c_void) }, 32 + 32);
111+
assert_size!(unsafe { heap_size_of(&*x as *const _ as *const c_void) }, 32 + 32);
93112
}
113+
}
94114

95-
#[test]
96-
fn test_boxed_str() {
97-
let x = "raclette".to_owned().into_boxed_str();
98-
assert_eq!(x.heap_size_of_children(), 8);
99-
}
115+
#[test]
116+
fn test_boxed_str() {
117+
let x = "raclette".to_owned().into_boxed_str();
118+
assert_size!(x.heap_size_of_children(), 8);
100119
}
101120

102121
#[test]
@@ -111,61 +130,61 @@ fn test_heap_size() {
111130

112131
unsafe {
113132
// EMPTY is the special non-null address used to represent zero-size allocations.
114-
assert_eq!(heap_size_of(EMPTY as *const c_void), 0);
133+
assert_size!(heap_size_of(EMPTY as *const c_void), 0);
115134
}
116135

117136
//-----------------------------------------------------------------------
118137
// Test HeapSizeOf implementations for various built-in types.
119138

120139
// Not on the heap; 0 bytes.
121140
let x = 0i64;
122-
assert_eq!(x.heap_size_of_children(), 0);
141+
assert_size!(x.heap_size_of_children(), 0);
123142

124143
// An i64 is 8 bytes.
125144
let x = Box::new(0i64);
126-
assert_eq!(x.heap_size_of_children(), 8);
145+
assert_size!(x.heap_size_of_children(), 8);
127146

128147
// An ascii string with 16 chars is 16 bytes in UTF-8.
129148
let string = String::from("0123456789abcdef");
130-
assert_eq!(string.heap_size_of_children(), 16);
149+
assert_size!(string.heap_size_of_children(), 16);
131150

132151
let string_ref: (&String, ()) = (&string, ());
133-
assert_eq!(string_ref.heap_size_of_children(), 0);
152+
assert_size!(string_ref.heap_size_of_children(), 0);
134153

135154
let slice: &str = &*string;
136-
assert_eq!(slice.heap_size_of_children(), 0);
155+
assert_size!(slice.heap_size_of_children(), 0);
137156

138157
// Not on the heap.
139158
let x: Option<i32> = None;
140-
assert_eq!(x.heap_size_of_children(), 0);
159+
assert_size!(x.heap_size_of_children(), 0);
141160

142161
// Not on the heap.
143162
let x = Some(0i64);
144-
assert_eq!(x.heap_size_of_children(), 0);
163+
assert_size!(x.heap_size_of_children(), 0);
145164

146165
// The `Some` is not on the heap, but the Box is.
147166
let x = Some(Box::new(0i64));
148-
assert_eq!(x.heap_size_of_children(), 8);
167+
assert_size!(x.heap_size_of_children(), 8);
149168

150169
// Not on the heap.
151170
let x = ::std::sync::Arc::new(0i64);
152-
assert_eq!(x.heap_size_of_children(), 0);
171+
assert_size!(x.heap_size_of_children(), 0);
153172

154173
// The `Arc` is not on the heap, but the Box is.
155174
let x = ::std::sync::Arc::new(Box::new(0i64));
156-
assert_eq!(x.heap_size_of_children(), 8);
175+
assert_size!(x.heap_size_of_children(), 8);
157176

158177
// Zero elements, no heap storage.
159178
let x: Vec<i64> = vec![];
160-
assert_eq!(x.heap_size_of_children(), 0);
179+
assert_size!(x.heap_size_of_children(), 0);
161180

162181
// Four elements, 8 bytes per element.
163182
let x = vec![0i64, 1i64, 2i64, 3i64];
164-
assert_eq!(x.heap_size_of_children(), 32);
183+
assert_size!(x.heap_size_of_children(), 32);
165184
}
166185

167186
#[test]
168187
fn test_boxed_slice() {
169188
let x = vec![1i64, 2i64].into_boxed_slice();
170-
assert_eq!(x.heap_size_of_children(), 16)
189+
assert_size!(x.heap_size_of_children(), 16)
171190
}

0 commit comments

Comments
 (0)