Skip to content

Commit b088ff6

Browse files
committed
Add size_of tests
1 parent 06e204e commit b088ff6

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,5 @@ mod nth;
117117
mod serializer;
118118
mod unicode_range;
119119

120-
#[cfg(test)]
121-
mod tests;
120+
#[cfg(test)] mod tests;
121+
#[cfg(test)] mod size_of_tests;

src/size_of_tests.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use std::borrow::Cow;
6+
use tokenizer::{Token, NumericValue, PercentageValue};
7+
8+
#[macro_export]
9+
macro_rules! size_of_test {
10+
($testname: ident, $t: ty, $expected_size: expr) => {
11+
#[test]
12+
fn $testname() {
13+
let new = ::std::mem::size_of::<$t>();
14+
let old = $expected_size;
15+
if new < old {
16+
panic!(
17+
"Your changes have decreased the stack size of {} from {} to {}. \
18+
Good work! Please update the expected size in {}.",
19+
stringify!($t), old, new, file!()
20+
)
21+
} else if new > old {
22+
panic!(
23+
"Your changes have increased the stack size of {} from {} to {}. \
24+
Please consider choosing a design which avoids this increase. \
25+
If you feel that the increase is necessary, update the size in {}.",
26+
stringify!($t), old, new, file!()
27+
)
28+
}
29+
}
30+
}
31+
}
32+
33+
// These assume 64-bit
34+
size_of_test!(token, Token, 56);
35+
size_of_test!(numeric_value, NumericValue, 16);
36+
size_of_test!(percentage_value, PercentageValue, 16);
37+
size_of_test!(std_cow_str, Cow<'static, str>, 32);

0 commit comments

Comments
 (0)