8000 Separate unit tests by chansuke · Pull Request #61739 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Separate unit tests #61739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jun 16, 2019
Prev Previous commit
Next Next commit
Separate libserialize module
  • Loading branch information
chansuke authored and petrochenkov committed Jun 16, 2019
commit db55aafd72d88c64a8fe3959625edcd207f0d25d
77 changes: 1 addition & 76 deletions src/libserialize/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,79 +143,4 @@ impl FromHex for str {
}

#[cfg(test)]
mod tests {
extern crate test;
use test::Bencher;
use crate::hex::{FromHex, ToHex};

#[test]
pub fn test_to_hex() {
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
}

#[test]
pub fn test_from_hex_okay() {
assert_eq!("666f6f626172".from_hex().unwrap(),
b"foobar");
assert_eq!("666F6F626172".from_hex().unwrap(),
b"foobar");
}

#[test]
pub fn test_from_hex_odd_len() {
assert!("666".from_hex().is_err());
assert!("66 6".from_hex().is_err());
}

#[test]
pub fn test_from_hex_invalid_char() {
assert!("66y6".from_hex().is_err());
}

#[test]
pub fn test_from_hex_ignores_whitespace() {
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
b"foobar");
}

#[test]
pub fn test_to_hex_all_bytes() {
for i in 0..256 {
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
}
}

#[test]
pub fn test_from_hex_all_bytes() {
for i in 0..256 {
let ii: &[u8] = &[i as u8];
assert_eq!(format!("{:02x}", i as usize).from_hex()
.unwrap(),
ii);
assert_eq!(format!("{:02X}", i as usize).from_hex()
.unwrap(),
ii);
}
}

#[bench]
pub fn bench_to_hex(b: &mut Bencher) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
b.iter(|| {
s.as_bytes().to_hex();
});
b.bytes = s.len() as u64;
}

#[bench]
pub fn bench_from_hex(b: &mut Bencher) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
let sb = s.as_bytes().to_hex();
b.iter(|| {
sb.from_hex().unwrap();
});
b.bytes = sb.len() as u64;
}
}
mod tests;
74 changes: 74 additions & 0 deletions src/libserialize/hex/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
extern crate test;
use test::Bencher;
use crate::hex::{FromHex, ToHex};

#[test]
pub fn test_to_hex() {
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
}

#[test]
pub fn test_from_hex_okay() {
assert_eq!("666f6f626172".from_hex().unwrap(),
b"foobar");
assert_eq!("666F6F626172".from_hex().unwrap(),
b"foobar");
}

#[test]
pub fn test_from_hex_odd_len() {
assert!("666".from_hex().is_err());
assert!("66 6".from_hex().is_err());
}

#[test]
pub fn test_from_hex_invalid_char() {
assert!("66y6".from_hex().is_err());
}

#[test]
pub fn test_from_hex_ignores_whitespace() {
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
b"foobar");
}

#[test]
pub fn test_to_hex_all_bytes() {
for i in 0..256 {
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
}
}

#[test]
pub fn test_from_hex_all_bytes() {
for i in 0..256 {
let ii: &[u8] = &[i as u8];
assert_eq!(format!("{:02x}", i as usize).from_hex()
.unwrap(),
ii);
assert_eq!(format!("{:02X}", i as usize).from_hex()
.unwrap(),
ii);
}
}

#[bench]
pub fn bench_to_hex(b: &mut Bencher) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
b.iter(|| {
s.as_bytes().to_hex();
});
b.bytes = s.len() as u64;
}

#[bench]
pub fn bench_from_hex(b: &mut Bencher) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
let sb = s.as_bytes().to_hex();
b.iter(|| {
sb.from_hex().unwrap();
});
b.bytes = sb.len() as u64;
}
137 changes: 1 addition & 136 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2582,139 +2582,4 @@ impl FromStr for Json {
}

#[cfg(test)]
mod tests {
// Benchmarks and tests that require private items

extern crate test;
use test::Bencher;
use super::{from_str, Parser, StackElement, Stack};
use std::string;

#[test]
fn test_stack() {
let mut stack = Stack::new();

assert!(stack.is_empty());
assert!(stack.is_empty());
assert!(!stack.last_is_index());

stack.push_index(0);
stack.bump_index();

assert!(stack.len() == 1);
assert!(stack.is_equal_to(&[StackElement::Index(1)]));
assert!(stack.starts_with(&[StackElement::Index(1)]));
assert!(stack.ends_with(&[StackElement::Index(1)]));
assert!(stack.last_is_index());
assert!(stack.get(0) == StackElement::Index(1));

stack.push_key("foo".to_string());

assert!(stack.len() == 2);
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.starts_with(&[StackElement::Index(1)]));
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.ends_with(&[StackElement::Key("foo")]));
assert!(!stack.last_is_index());
assert!(stack.get(0) == StackElement::Index(1));
assert!(stack.get(1) == StackElement::Key("foo"));

stack.push_key("bar".to_string());

assert!(stack.len() == 3);
assert!(stack.is_equal_to(&[StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")]));
assert!(stack.starts_with(&[StackElement::Index(1)]));
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.starts_with(&[StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")]));
assert!(stack.ends_with(&[StackElement::Key("bar")]));
assert!(stack.ends_with(&[StackElement::Key("foo"), StackElement::Key("bar")]));
assert!(stack.ends_with(&[StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")]));
assert!(!stack.last_is_index());
assert!(stack.get(0) == StackElement::Index(1));
assert!(stack.get(1) == StackElement::Key("foo"));
assert!(stack.get(2) == StackElement::Key("bar"));

stack.pop();

assert!(stack.len() == 2);
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.starts_with(&[StackElement::Index(1)]));
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
assert!(stack.ends_with(&[StackElement::Key("foo")]));
assert!(!stack.last_is_index());
assert!(stack.get(0) == StackElement::Index(1));
assert!(stack.get(1) == StackElement::Key("foo"));
}

#[bench]
fn bench_streaming_small(b: &mut Bencher) {
b.iter( || {
let mut parser = Parser::new(
r#"{
"a": 1.0,
"b": [
true,
"foo\nbar",
{ "c": {"d": null} }
]
}"#.chars()
);
loop {
match parser.next() {
None => return,
_ => {}
}
}
});
}
#[bench]
fn bench_small(b: &mut Bencher) {
b.iter( || {
let _ = from_str(r#"{
"a": 1.0,
"b": [
true,
"foo\nbar",
{ "c": {"d": null} }
]
}"#);
});
}

fn big_json() -> string::String {
let mut src = "[\n".to_string();
for _ in 0..500 {
src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \
[1,2,3]},"#);
}
src.push_str("{}]");
return src;
}

#[bench]
fn bench_streaming_large(b: &mut Bencher) {
let src = big_json();
b.iter( || {
let mut parser = Parser::new(src.chars());
loop {
match parser.next() {
None => return,
_ => {}
}
}
});
}
#[bench]
fn bench_large(b: &mut Bencher) {
let src = big_json();
b.iter( || { let _ = from_str(&src); });
}
}
mod tests;
Loading
0