]> git.lizzy.rs Git - rust.git/blob - src/libserialize/json/tests.rs
Auto merge of #61889 - matthiaskrgr:submodule_upd, r=oli-obk
[rust.git] / src / libserialize / json / tests.rs
1 // Benchmarks and tests that require private items
2
3 extern crate test;
4 use test::Bencher;
5 use super::{from_str, Parser, StackElement, Stack};
6 use std::string;
7
8 #[test]
9 fn test_stack() {
10     let mut stack = Stack::new();
11
12     assert!(stack.is_empty());
13     assert!(stack.is_empty());
14     assert!(!stack.last_is_index());
15
16     stack.push_index(0);
17     stack.bump_index();
18
19     assert!(stack.len() == 1);
20     assert!(stack.is_equal_to(&[StackElement::Index(1)]));
21     assert!(stack.starts_with(&[StackElement::Index(1)]));
22     assert!(stack.ends_with(&[StackElement::Index(1)]));
23     assert!(stack.last_is_index());
24     assert!(stack.get(0) == StackElement::Index(1));
25
26     stack.push_key("foo".to_string());
27
28     assert!(stack.len() == 2);
29     assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
30     assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
31     assert!(stack.starts_with(&[StackElement::Index(1)]));
32     assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
33     assert!(stack.ends_with(&[StackElement::Key("foo")]));
34     assert!(!stack.last_is_index());
35     assert!(stack.get(0) == StackElement::Index(1));
36     assert!(stack.get(1) == StackElement::Key("foo"));
37
38     stack.push_key("bar".to_string());
39
40     assert!(stack.len() == 3);
41     assert!(stack.is_equal_to(&[StackElement::Index(1),
42                                 StackElement::Key("foo"),
43                                 StackElement::Key("bar")]));
44     assert!(stack.starts_with(&[StackElement::Index(1)]));
45     assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
46     assert!(stack.starts_with(&[StackElement::Index(1),
47                                 StackElement::Key("foo"),
48                                 StackElement::Key("bar")]));
49     assert!(stack.ends_with(&[StackElement::Key("bar")]));
50     assert!(stack.ends_with(&[StackElement::Key("foo"), StackElement::Key("bar")]));
51     assert!(stack.ends_with(&[StackElement::Index(1),
52                               StackElement::Key("foo"),
53                               StackElement::Key("bar")]));
54     assert!(!stack.last_is_index());
55     assert!(stack.get(0) == StackElement::Index(1));
56     assert!(stack.get(1) == StackElement::Key("foo"));
57     assert!(stack.get(2) == StackElement::Key("bar"));
58
59     stack.pop();
60
61     assert!(stack.len() == 2);
62     assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
63     assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
64     assert!(stack.starts_with(&[StackElement::Index(1)]));
65     assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
66     assert!(stack.ends_with(&[StackElement::Key("foo")]));
67     assert!(!stack.last_is_index());
68     assert!(stack.get(0) == StackElement::Index(1));
69     assert!(stack.get(1) == StackElement::Key("foo"));
70 }
71
72 #[bench]
73 fn bench_streaming_small(b: &mut Bencher) {
74     b.iter( || {
75         let mut parser = Parser::new(
76             r#"{
77                 "a": 1.0,
78                 "b": [
79                     true,
80                     "foo\nbar",
81                     { "c": {"d": null} }
82                 ]
83             }"#.chars()
84         );
85         loop {
86             match parser.next() {
87                 None => return,
88                 _ => {}
89             }
90         }
91     });
92 }
93 #[bench]
94 fn bench_small(b: &mut Bencher) {
95     b.iter( || {
96         let _ = from_str(r#"{
97             "a": 1.0,
98             "b": [
99                 true,
100                 "foo\nbar",
101                 { "c": {"d": null} }
102             ]
103         }"#);
104     });
105 }
106
107 fn big_json() -> string::String {
108     let mut src = "[\n".to_string();
109     for _ in 0..500 {
110         src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \
111                         [1,2,3]},"#);
112     }
113     src.push_str("{}]");
114     return src;
115 }
116
117 #[bench]
118 fn bench_streaming_large(b: &mut Bencher) {
119     let src = big_json();
120     b.iter( || {
121         let mut parser = Parser::new(src.chars());
122         loop {
123             match parser.next() {
124                 None => return,
125                 _ => {}
126             }
127         }
128     });
129 }
130 #[bench]
131 fn bench_large(b: &mut Bencher) {
132     let src = big_json();
133     b.iter( || { let _ = from_str(&src); });
134 }