Skip to content

Commit db55aaf

Browse files
chansukepetrochenkov
chansuke
authored andcommitted
Separate libserialize module
1 parent 46e622b commit db55aaf

File tree

4 files changed

+210
-212
lines changed

4 files changed

+210
-212
lines changed

src/libserialize/hex.rs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -143,79 +143,4 @@ impl FromHex for str {
143143
}
144144

145145
#[cfg(test)]
146-
mod tests {
147-
extern crate test;
148-
use test::Bencher;
149-
use crate::hex::{FromHex, ToHex};
150-
151-
#[test]
152-
pub fn test_to_hex() {
153-
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
154-
}
155-
156-
#[test]
157-
pub fn test_from_hex_okay() {
158-
assert_eq!("666f6f626172".from_hex().unwrap(),
159-
b"foobar");
160-
assert_eq!("666F6F626172".from_hex().unwrap(),
161-
b"foobar");
162-
}
163-
164-
#[test]
165-
pub fn test_from_hex_odd_len() {
166-
assert!("666".from_hex().is_err());
167-
assert!("66 6".from_hex().is_err());
168-
}
169-
170-
#[test]
171-
pub fn test_from_hex_invalid_char() {
172-
assert!("66y6".from_hex().is_err());
173-
}
174-
175-
#[test]
176-
pub fn test_from_hex_ignores_whitespace() {
177-
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
178-
b"foobar");
179-
}
180-
181-
#[test]
182-
pub fn test_to_hex_all_bytes() {
183-
for i in 0..256 {
184-
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
185-
}
186-
}
187-
188-
#[test]
189-
pub fn test_from_hex_all_bytes() {
190-
for i in 0..256 {
191-
let ii: &[u8] = &[i as u8];
192-
assert_eq!(format!("{:02x}", i as usize).from_hex()
193-
.unwrap(),
194-
ii);
195-
assert_eq!(format!("{:02X}", i as usize).from_hex()
196-
.unwrap(),
197-
ii);
198-
}
199-
}
200-
201-
#[bench]
202-
pub fn bench_to_hex(b: &mut Bencher) {
203-
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
204-
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
205-
b.iter(|| {
206-
s.as_bytes().to_hex();
207-
});
208-
b.bytes = s.len() as u64;
209-
}
210-
211-
#[bench]
212-
pub fn bench_from_hex(b: &mut Bencher) {
213-
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
214-
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
215-
let sb = s.as_bytes().to_hex();
216-
b.iter(|| {
217-
sb.from_hex().unwrap();
218-
});
219-
b.bytes = sb.len() as u64;
220-
}
221-
}
146+
mod tests;

src/libserialize/hex/tests.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
extern crate test;
2+
use test::Bencher;
3+
use crate::hex::{FromHex, ToHex};
4+
5+
#[test]
6+
pub fn test_to_hex() {
7+
assert_eq!("foobar".as_bytes().to_hex(), "666f6f626172");
8+
}
9+
10+
#[test]
11+
pub fn test_from_hex_okay() {
12+
assert_eq!("666f6f626172".from_hex().unwrap(),
13+
b"foobar");
14+
assert_eq!("666F6F626172".from_hex().unwrap(),
15+
b"foobar");
16+
}
17+
18+
#[test]
19+
pub fn test_from_hex_odd_len() {
20+
assert!("666".from_hex().is_err());
21+
assert!("66 6".from_hex().is_err());
22+
}
23+
24+
#[test]
25+
pub fn test_from_hex_invalid_char() {
26+
assert!("66y6".from_hex().is_err());
27+
}
28+
29+
#[test]
30+
pub fn test_from_hex_ignores_whitespace() {
31+
assert_eq!("666f 6f6\r\n26172 ".from_hex().unwrap(),
32+
b"foobar");
33+
}
34+
35+
#[test]
36+
pub fn test_to_hex_all_bytes() {
37+
for i in 0..256 {
38+
assert_eq!([i as u8].to_hex(), format!("{:02x}", i as usize));
39+
}
40+
}
41+
42+
#[test]
43+
pub fn test_from_hex_all_bytes() {
44+
for i in 0..256 {
45+
let ii: &[u8] = &[i as u8];
46+
assert_eq!(format!("{:02x}", i as usize).from_hex()
47+
.unwrap(),
48+
ii);
49+
assert_eq!(format!("{:02X}", i as usize).from_hex()
50+
.unwrap(),
51+
ii);
52+
}
53+
}
54+
55+
#[bench]
56+
pub fn bench_to_hex(b: &mut Bencher) {
57+
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
58+
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
59+
b.iter(|| {
60+
s.as_bytes().to_hex();
61+
});
62+
b.bytes = s.len() as u64;
63+
}
64+
65+
#[bench]
66+
pub fn bench_from_hex(b: &mut Bencher) {
67+
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
68+
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
69+
let sb = s.as_bytes().to_hex();
70+
b.iter(|| {
71+
sb.from_hex().unwrap();
72+
});
73+
b.bytes = sb.len() as u64;
74+
}

src/libserialize/json.rs

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,139 +2582,4 @@ impl FromStr for Json {
25822582
}
25832583

25842584
#[cfg(test)]
2585-
mod tests {
2586-
// Benchmarks and tests that require private items
2587-
2588-
extern crate test;
2589-
use test::Bencher;
2590-
use super::{from_str, Parser, StackElement, Stack};
2591-
use std::string;
2592-
2593-
#[test]
2594-
fn test_stack() {
2595-
let mut stack = Stack::new();
2596-
2597-
assert!(stack.is_empty());
2598-
assert!(stack.is_empty());
2599-
assert!(!stack.last_is_index());
2600-
2601-
stack.push_index(0);
2602-
stack.bump_index();
2603-
2604-
assert!(stack.len() == 1);
2605-
assert!(stack.is_equal_to(&[StackElement::Index(1)]));
2606-
assert!(stack.starts_with(&[StackElement::Index(1)]));
2607-
assert!(stack.ends_with(&[StackElement::Index(1)]));
2608-
assert!(stack.last_is_index());
2609-
assert!(stack.get(0) == StackElement::Index(1));
2610-
2611-
stack.push_key("foo".to_string());
2612-
2613-
assert!(stack.len() == 2);
2614-
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
2615-
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2616-
assert!(stack.starts_with(&[StackElement::Index(1)]));
2617-
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2618-
assert!(stack.ends_with(&[StackElement::Key("foo")]));
2619-
assert!(!stack.last_is_index());
2620-
assert!(stack.get(0) == StackElement::Index(1));
2621-
assert!(stack.get(1) == StackElement::Key("foo"));
2622-
2623-
stack.push_key("bar".to_string());
2624-
2625-
assert!(stack.len() == 3);
2626-
assert!(stack.is_equal_to(&[StackElement::Index(1),
2627-
StackElement::Key("foo"),
2628-
StackElement::Key("bar")]));
2629-
assert!(stack.starts_with(&[StackElement::Index(1)]));
2630-
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2631-
assert!(stack.starts_with(&[StackElement::Index(1),
2632-
StackElement::Key("foo"),
2633-
StackElement::Key("bar")]));
2634-
assert!(stack.ends_with(&[StackElement::Key("bar")]));
2635-
assert!(stack.ends_with(&[StackElement::Key("foo"), StackElement::Key("bar")]));
2636-
assert!(stack.ends_with(&[StackElement::Index(1),
2637-
StackElement::Key("foo"),
2638-
StackElement::Key("bar")]));
2639-
assert!(!stack.last_is_index());
2640-
assert!(stack.get(0) == StackElement::Index(1));
2641-
assert!(stack.get(1) == StackElement::Key("foo"));
2642-
assert!(stack.get(2) == StackElement::Key("bar"));
2643-
2644-
stack.pop();
2645-
2646-
assert!(stack.len() == 2);
2647-
assert!(stack.is_equal_to(&[StackElement::Index(1), StackElement::Key("foo")]));
2648-
assert!(stack.starts_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2649-
assert!(stack.starts_with(&[StackElement::Index(1)]));
2650-
assert!(stack.ends_with(&[StackElement::Index(1), StackElement::Key("foo")]));
2651-
assert!(stack.ends_with(&[StackElement::Key("foo")]));
2652-
assert!(!stack.last_is_index());
2653-
assert!(stack.get(0) == StackElement::Index(1));
2654-
assert!(stack.get(1) == StackElement::Key("foo"));
2655-
}
2656-
2657-
#[bench]
2658-
fn bench_streaming_small(b: &mut Bencher) {
2659-
b.iter( || {
2660-
let mut parser = Parser::new(
2661-
r#"{
2662-
"a": 1.0,
2663-
"b": [
2664-
true,
2665-
"foo\nbar",
2666-
{ "c": {"d": null} }
2667-
]
2668-
}"#.chars()
2669-
);
2670-
loop {
2671-
match parser.next() {
2672-
None => return,
2673-
_ => {}
2674-
}
2675-
}
2676-
});
2677-
}
2678-
#[bench]
2679-
fn bench_small(b: &mut Bencher) {
2680-
b.iter( || {
2681-
let _ = from_str(r#"{
2682-
"a": 1.0,
2683-
"b": [
2684-
true,
2685-
"foo\nbar",
2686-
{ "c": {"d": null} }
2687-
]
2688-
}"#);
2689-
});
2690-
}
2691-
2692-
fn big_json() -> string::String {
2693-
let mut src = "[\n".to_string();
2694-
for _ in 0..500 {
2695-
src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \
2696-
[1,2,3]},"#);
2697-
}
2698-
src.push_str("{}]");
2699-
return src;
2700-
}
2701-
2702-
#[bench]
2703-
fn bench_streaming_large(b: &mut Bencher) {
2704-
let src = big_json();
2705-
b.iter( || {
2706-
let mut parser = Parser::new(src.chars());
2707-
loop {
2708-
match parser.next() {
2709-
None => return,
2710-
_ => {}
2711-
}
2712-
}
2713-
});
2714-
}
2715-
#[bench]
2716-
fn bench_large(b: &mut Bencher) {
2717-
let src = big_json();
2718-
b.iter( || { let _ = from_str(&src); });
2719-
}
2720-
}
2585+
mod tests;

0 commit comments

Comments
 (0)