move the ini benches to their own files

This commit is contained in:
Geoffroy Couprie 2017-03-23 11:44:27 +01:00
parent 88d29aace4
commit ef1811667e
4 changed files with 502 additions and 44 deletions

243
benches/ini.rs Normal file
View File

@ -0,0 +1,243 @@
#![feature(test)]
extern crate test;
#[macro_use]
extern crate nom;
use nom::{IResult,not_line_ending, space, alphanumeric, multispace};
use std::str;
use std::collections::HashMap;
named!(category<&str>, map_res!(
terminated!(
delimited!(tag!("["), take_until!("]"), tag!("]")),
opt!(multispace)
),
str::from_utf8
));
named!(key_value <&[u8],(&str,&str)>,
do_parse!(
key: map_res!(alphanumeric, std::str::from_utf8) >>
opt!(space) >>
tag!("=") >>
opt!(space) >>
val: map_res!(
take_until_either!("\n;"),
str::from_utf8
) >>
opt!(space) >>
opt!(do_parse!(
tag!(";") >>
not_line_ending >>
()
)) >>
opt!(multispace) >>
(key, val)
)
);
named!(keys_and_values_aggregator<&[u8], Vec<(&str,&str)> >, many0!(key_value));
fn keys_and_values(input:&[u8]) -> IResult<&[u8], HashMap<&str, &str> > {
match keys_and_values_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
}
}
named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>,
do_parse!(
category: category >>
keys: keys_and_values >>
(category, keys)
)
);
named!(categories_aggregator<&[u8], Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
fn categories(input: &[u8]) -> IResult<&[u8], HashMap<&str, HashMap<&str, &str> > > {
match categories_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
}
}
#[test]
fn parse_category_test() {
let ini_file = &b"[category]
parameter=value
key = value2"[..];
let ini_without_category = &b"parameter=value
key = value2"[..];
let res = category(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_category, "category"));
}
#[test]
fn parse_key_value_test() {
let ini_file = &b"parameter=value
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_space_test() {
let ini_file = &b"parameter = value
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_comment_test() {
let ini_file = &b"parameter=value;abc
key = value2"[..];
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_multiple_keys_and_values_test() {
let ini_file = &b"parameter=value;abc
key = value2
[category]"[..];
let ini_without_key_value = &b"[category]"[..];
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
}
#[test]
fn parse_category_then_multiple_keys_and_values_test() {
//FIXME: there can be an empty line or a comment line after a category
let ini_file = &b"[abcd]
parameter=value;abc
key = value2
[category]"[..];
let ini_after_parser = &b"[category]"[..];
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
}
#[test]
fn parse_multiple_categories_test() {
let ini_file = &b"[abcd]
parameter=value;abc
key = value2
[category]
parameter3=value3
key4 = value4
"[..];
let ini_after_parser = &b""[..];
let res = categories(ini_file);
//println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_1: HashMap<&str, &str> = HashMap::new();
expected_1.insert("parameter", "value");
expected_1.insert("key", "value2");
let mut expected_2: HashMap<&str, &str> = HashMap::new();
expected_2.insert("parameter3", "value3");
expected_2.insert("key4", "value4");
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done(ini_after_parser, expected_h));
}
//#[cfg(feature = "test")]
#[bench]
fn bench_ini(b: &mut test::Bencher) {
let str = "[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
server=192.0.2.62
port=143
file=payroll.dat
";
b.iter(|| categories(str.as_bytes()).unwrap());
b.bytes = str.len() as u64;
}

259
benches/ini_str.rs Normal file
View File

@ -0,0 +1,259 @@
#![feature(test)]
extern crate test;
#[macro_use]
extern crate nom;
use nom::IResult;
use std::collections::HashMap;
fn is_alphabetic(chr:char) -> bool {
(chr as u8 >= 0x41 && chr as u8 <= 0x5A) || (chr as u8 >= 0x61 && chr as u8 <= 0x7A)
}
fn is_digit(chr: char) -> bool {
chr as u8 >= 0x30 && chr as u8 <= 0x39
}
fn is_alphanumeric(chr: char) -> bool {
is_alphabetic(chr) || is_digit(chr)
}
fn is_space(chr:char) -> bool {
chr == ' ' || chr == '\t'
}
fn is_line_ending_or_comment(chr:char) -> bool {
chr == ';' || chr == '\n'
}
named!(alphanumeric<&str,&str>, take_while_s!(is_alphanumeric));
named!(not_line_ending<&str,&str>, is_not_s!("\r\n"));
named!(space<&str,&str>, take_while_s!(is_space));
named!(space_or_line_ending<&str,&str>, is_a_s!(" \r\n"));
fn right_bracket(c:char) -> bool {
c == ']'
}
named!(category <&str, &str>,
do_parse!(
tag_s!("[") >>
name: take_till_s!(right_bracket) >>
tag_s!("]") >>
opt!(space_or_line_ending) >>
(name)
)
);
named!(key_value <&str,(&str,&str)>,
do_parse!(
key: alphanumeric >>
opt!(space) >>
tag_s!("=") >>
opt!(space) >>
val: take_till_s!(is_line_ending_or_comment) >>
opt!(space) >>
opt!(pair!(tag_s!(";"), not_line_ending)) >>
opt!(space_or_line_ending) >>
(key, val)
)
);
named!(keys_and_values_aggregator<&str, Vec<(&str,&str)> >, many0!(key_value));
fn keys_and_values(input:&str) -> IResult<&str, HashMap<&str, &str> > {
match keys_and_values_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
}
}
named!(category_and_keys<&str,(&str,HashMap<&str,&str>)>,
pair!(category, keys_and_values)
);
named!(categories_aggregator<&str, Vec<(&str, HashMap<&str,&str>)> >, many0!(category_and_keys));
fn categories(input: &str) -> IResult<&str, HashMap<&str, HashMap<&str, &str> > > {
match categories_aggregator(input) {
IResult::Done(i,tuple_vec) => {
IResult::Done(i, tuple_vec.into_iter().collect())
},
IResult::Incomplete(a) => IResult::Incomplete(a),
IResult::Error(a) => IResult::Error(a)
}
}
#[test]
fn parse_category_test() {
let ini_file = "[category]
parameter=value
key = value2";
let ini_without_category = "parameter=value
key = value2";
let res = category(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, o) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_category, "category"));
}
#[test]
fn parse_key_value_test() {
let ini_file = "parameter=value
key = value2";
let ini_without_key_value = "key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_space_test() {
let ini_file = "parameter = value
key = value2";
let ini_without_key_value = "key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_comment_test() {
let ini_file = "parameter=value;abc
key = value2";
let ini_without_key_value = "key = value2";
let res = key_value(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, (o1, o2)) => println!("i: {} | o: ({:?},{:?})", i, o1, o2),
_ => println!("error")
}
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_multiple_keys_and_values_test() {
let ini_file = "parameter=value;abc
key = value2
[category]";
let ini_without_key_value = "[category]";
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
}
#[test]
fn parse_category_then_multiple_keys_and_values_test() {
//FIXME: there can be an empty line or a comment line after a category
let ini_file = "[abcd]
parameter=value;abc
key = value2
[category]";
let ini_after_parser = "[category]";
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
}
#[test]
fn parse_multiple_categories_test() {
let ini_file = "[abcd]
parameter=value;abc
key = value2
[category]
parameter3=value3
key4 = value4
";
let res = categories(ini_file);
//println!("{:?}", res);
match res {
IResult::Done(i, ref o) => println!("i: {} | o: {:?}", i, o),
_ => println!("error")
}
let mut expected_1: HashMap<&str, &str> = HashMap::new();
expected_1.insert("parameter", "value");
expected_1.insert("key", "value2");
let mut expected_2: HashMap<&str, &str> = HashMap::new();
expected_2.insert("parameter3", "value3");
expected_2.insert("key4", "value4");
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done("", expected_h));
}
#[bench]
fn bench_ini_str(b: &mut test::Bencher) {
let str = "[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
server=192.0.2.62
port=143
file=payroll.dat
";
b.iter(|| categories(str).unwrap());
b.bytes = str.len() as u64;
}

View File

@ -1,8 +1,3 @@
#![cfg_attr(feature = "nightly", feature(test))]
#[cfg(feature = "nightly")]
extern crate test;
#[macro_use]
extern crate nom;
@ -226,20 +221,3 @@ key4 = value4
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done(ini_after_parser, expected_h));
}
#[cfg(feature = "nightly")]
#[bench]
fn bench_ini(b: &mut test::Bencher) {
let str = "[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
server=192.0.2.62
port=143
file=payroll.dat
";
b.iter(|| categories(str.as_bytes()).unwrap());
b.bytes = str.len() as u64;
}

View File

@ -1,8 +1,3 @@
#![cfg_attr(feature = "nightly", feature(test))]
#[cfg(feature = "nightly")]
extern crate test;
#[macro_use]
extern crate nom;
@ -243,20 +238,3 @@ key4 = value4
expected_h.insert("category", expected_2);
assert_eq!(res, IResult::Done("", expected_h));
}
#[cfg(feature = "nightly")]
#[bench]
fn bench_ini(b: &mut test::Bencher) {
let str = "[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
server=192.0.2.62
port=143
file=payroll.dat
";
b.iter(|| categories(str).unwrap());
b.bytes = str.len() as u64;
}