rust/compiler/rustc_serialize/src/json/tests.rs

148 lines
4.2 KiB
Rust
Raw Normal View History

2019-06-05 19:20:49 +00:00
// Benchmarks and tests that require private items
extern crate test;
2019-12-22 22:42:04 +00:00
use super::{from_str, Parser, Stack, StackElement};
2019-06-05 19:20:49 +00:00
use std::string;
2019-12-22 22:42:04 +00:00
use test::Bencher;
2019-06-05 19:20:49 +00:00
#[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);
2019-12-22 22:42:04 +00:00
assert!(stack.is_equal_to(&[
StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")
]));
2019-06-05 19:20:49 +00:00
assert!(stack.starts_with(&[StackElement::Index(1)]));
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2019-12-22 22:42:04 +00:00
assert!(stack.starts_with(&[
StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")
]));
2019-06-05 19:20:49 +00:00
assert!(stack.ends_with(&[StackElement::Key("bar")]));
assert!(stack.ends_with(&[StackElement::Key("foo"), StackElement::Key("bar")]));
2019-12-22 22:42:04 +00:00
assert!(stack.ends_with(&[
StackElement::Index(1),
StackElement::Key("foo"),
StackElement::Key("bar")
]));
2019-06-05 19:20:49 +00:00
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) {
2019-12-22 22:42:04 +00:00
b.iter(|| {
2019-06-05 19:20:49 +00:00
let mut parser = Parser::new(
r#"{
"a": 1.0,
"b": [
true,
"foo\nbar",
{ "c": {"d": null} }
]
2019-12-22 22:42:04 +00:00
}"#
.chars(),
2019-06-05 19:20:49 +00:00
);
loop {
match parser.next() {
None => return,
_ => {}
}
}
});
}
#[bench]
fn bench_small(b: &mut Bencher) {
2019-12-22 22:42:04 +00:00
b.iter(|| {
let _ = from_str(
r#"{
2019-06-05 19:20:49 +00:00
"a": 1.0,
"b": [
true,
"foo\nbar",
{ "c": {"d": null} }
]
2019-12-22 22:42:04 +00:00
}"#,
);
2019-06-05 19:20:49 +00:00
});
}
fn big_json() -> string::String {
let mut src = "[\n".to_string();
for _ in 0..500 {
2019-12-22 22:42:04 +00:00
src.push_str(
r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \
[1,2,3]},"#,
);
2019-06-05 19:20:49 +00:00
}
src.push_str("{}]");
return src;
}
#[bench]
fn bench_streaming_large(b: &mut Bencher) {
let src = big_json();
2019-12-22 22:42:04 +00:00
b.iter(|| {
2019-06-05 19:20:49 +00:00
let mut parser = Parser::new(src.chars());
loop {
match parser.next() {
None => return,
_ => {}
}
}
});
}
#[bench]
fn bench_large(b: &mut Bencher) {
let src = big_json();
2019-12-22 22:42:04 +00:00
b.iter(|| {
let _ = from_str(&src);
});
2019-06-05 19:20:49 +00:00
}