Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add size_of tests
  • Loading branch information
SimonSapin committed Jun 16, 2017
commit b088ff60752634d207f957d00ed3d10731e5dc81
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ mod nth;
mod serializer;
mod unicode_range;

#[cfg(test)]
mod tests;
#[cfg(test)] mod tests;
#[cfg(test)] mod size_of_tests;
37 changes: 37 additions & 0 deletions src/size_of_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::borrow::Cow;
use tokenizer::{Token, NumericValue, PercentageValue};

#[macro_export]
macro_rules! size_of_test {
($testname: ident, $t: ty, $expected_size: expr) => {
#[test]
fn $testname() {
let new = ::std::mem::size_of::<$t>();
let old = $expected_size;
if new < old {
panic!(
"Your changes have decreased the stack size of {} from {} to {}. \
Good work! Please update the expected size in {}.",
stringify!($t), old, new, file!()
)
} else if new > old {
panic!(
"Your changes have increased the stack size of {} from {} to {}. \
Please consider choosing a design which avoids this increase. \
If you feel that the increase is necessary, update the size in {}.",
stringify!($t), old, new, file!()
)
}
}
}
}

// These assume 64-bit
size_of_test!(token, Token, 56);
size_of_test!(numeric_value, NumericValue, 16);
size_of_test!(percentage_value, PercentageValue, 16);
size_of_test!(std_cow_str, Cow<'static, str>, 32);