@@ -9,13 +9,32 @@ extern crate heapsize;
9
9
use heapsize:: { HeapSizeOf , heap_size_of} ;
10
10
use std:: os:: raw:: c_void;
11
11
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
+ }
13
32
14
33
#[ cfg( feature = "unstable" ) ]
15
34
mod unstable {
16
35
extern crate alloc;
17
36
18
- use heapsize:: { HeapSizeOf , heap_size_of} ;
37
+ use heapsize:: heap_size_of;
19
38
use std:: os:: raw:: c_void;
20
39
21
40
#[ repr( C , simd) ]
@@ -32,22 +51,22 @@ mod unstable {
32
51
unsafe {
33
52
// A 64 byte request is allocated exactly.
34
53
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 ) ;
36
55
alloc:: heap:: deallocate ( x, 64 , 0 ) ;
37
56
38
57
// A 255 byte request is rounded up to 256 bytes.
39
58
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 ) ;
41
60
alloc:: heap:: deallocate ( x, 255 , 0 ) ;
42
61
43
62
// A 1MiB request is allocated exactly.
44
63
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 ) ;
46
65
alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
47
66
48
67
// An overaligned 1MiB request is allocated exactly.
49
68
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 ) ;
51
70
alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
52
71
}
53
72
}
@@ -58,22 +77,22 @@ mod unstable {
58
77
unsafe {
59
78
// A 64 byte request is allocated exactly.
60
79
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 ) ;
62
81
alloc:: heap:: deallocate ( x, 64 , 0 ) ;
63
82
64
83
// A 255 byte request is allocated exactly.
65
84
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 ) ;
67
86
alloc:: heap:: deallocate ( x, 255 , 0 ) ;
68
87
69
88
// A 1MiB request is allocated exactly.
70
89
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 ) ;
72
91
alloc:: heap:: deallocate ( x, 1024 * 1024 , 0 ) ;
73
92
74
93
// An overaligned 1MiB request is over-allocated.
75
94
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 ) ;
77
96
alloc:: heap:: deallocate ( x, 1024 * 1024 , 32 ) ;
78
97
}
79
98
}
@@ -82,21 +101,21 @@ mod unstable {
82
101
#[ test]
83
102
fn test_simd ( ) {
84
103
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 ) ;
86
105
}
87
106
88
107
#[ cfg( target_os = "windows" ) ]
89
108
#[ test]
90
109
fn test_simd ( ) {
91
110
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 ) ;
93
112
}
113
+ }
94
114
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 ) ;
100
119
}
101
120
102
121
#[ test]
@@ -111,61 +130,61 @@ fn test_heap_size() {
111
130
112
131
unsafe {
113
132
// 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 ) ;
115
134
}
116
135
117
136
//-----------------------------------------------------------------------
118
137
// Test HeapSizeOf implementations for various built-in types.
119
138
120
139
// Not on the heap; 0 bytes.
121
140
let x = 0i64 ;
122
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
141
+ assert_size ! ( x. heap_size_of_children( ) , 0 ) ;
123
142
124
143
// An i64 is 8 bytes.
125
144
let x = Box :: new ( 0i64 ) ;
126
- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
145
+ assert_size ! ( x. heap_size_of_children( ) , 8 ) ;
127
146
128
147
// An ascii string with 16 chars is 16 bytes in UTF-8.
129
148
let string = String :: from ( "0123456789abcdef" ) ;
130
- assert_eq ! ( string. heap_size_of_children( ) , 16 ) ;
149
+ assert_size ! ( string. heap_size_of_children( ) , 16 ) ;
131
150
132
151
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 ) ;
134
153
135
154
let slice: & str = & * string;
136
- assert_eq ! ( slice. heap_size_of_children( ) , 0 ) ;
155
+ assert_size ! ( slice. heap_size_of_children( ) , 0 ) ;
137
156
138
157
// Not on the heap.
139
158
let x: Option < i32 > = None ;
140
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
159
+ assert_size ! ( x. heap_size_of_children( ) , 0 ) ;
141
160
142
161
// Not on the heap.
143
162
let x = Some ( 0i64 ) ;
144
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
163
+ assert_size ! ( x. heap_size_of_children( ) , 0 ) ;
145
164
146
165
// The `Some` is not on the heap, but the Box is.
147
166
let x = Some ( Box :: new ( 0i64 ) ) ;
148
- assert_eq ! ( x. heap_size_of_children( ) , 8 ) ;
167
+ assert_size ! ( x. heap_size_of_children( ) , 8 ) ;
149
168
150
169
// Not on the heap.
151
170
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 ) ;
153
172
154
173
// The `Arc` is not on the heap, but the Box is.
155
174
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 ) ;
157
176
158
177
// Zero elements, no heap storage.
159
178
let x: Vec < i64 > = vec ! [ ] ;
160
- assert_eq ! ( x. heap_size_of_children( ) , 0 ) ;
179
+ assert_size ! ( x. heap_size_of_children( ) , 0 ) ;
161
180
162
181
// Four elements, 8 bytes per element.
163
182
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 ) ;
165
184
}
166
185
167
186
#[ test]
168
187
fn test_boxed_slice ( ) {
169
188
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 )
171
190
}
0 commit comments