Skip to content
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

Overhaul Dates #74

Merged
merged 1 commit into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions benches/jomini_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ pub fn text_parse_benchmark(c: &mut Criterion) {
pub fn date_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("eu4date-parse");
group.bench_function("valid-date", |b| {
b.iter(|| Date::parse_from_str("1444.11.11").unwrap())
b.iter(|| Date::parse("1444.11.11").unwrap())
});
group.bench_function("binary-date", |b| {
b.iter(|| Date::from_binary(56379360).unwrap())
});
group.bench_function("invalid-date", |b| {
b.iter(|| Date::parse_from_str("marketplace").is_none())
b.iter(|| Date::parse("marketplace").is_err())
});
group.bench_function("long-invalid-date", |b| {
b.iter(|| Date::parse_from_str("incidents_bur_inheritance.5").is_none())
b.iter(|| Date::parse("incidents_bur_inheritance.5").is_err())
});
group.finish();
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz_targets/fuzz_date.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use jomini::common::PdsDate;

fuzz_target!(|data: &[u8]| {
if data.len() < 4 {
return;
}

let num = i32::from_le_bytes([data[0], data[1], data[2], data[3]]);
let txt = jomini::Windows1252Encoding::decode(&data[4..]);
let _ = jomini::common::Date::from_binary(num);
if let Some(d) = jomini::common::Date::parse_from_str(txt) {
match d.game_fmt().as_str() {
if let Ok(d) = jomini::common::Date::parse(&data[4..]) {
match d.game_fmt().to_string().as_str() {
// I'm not sure how math works when we go across the border
// as I'm not familiar that EU4 recognizes the zeroth year
"1.1.1" | "-1.1.1" => {}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/fuzz_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
let _ = x.is_ascii();
let _ = x.to_bool();
let stringed = value.read_str().unwrap();
let _ = jomini::common::Date::parse_from_str(stringed);
let _ = jomini::common::Date::parse(stringed.as_ref());
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions src/binary/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ fn array_len(tokens: &[BinaryToken], mut val_ind: usize) -> usize {

#[cfg(test)]
mod tests {
use crate::common::{Date, DateHour};

use super::*;
use jomini_derive::JominiDeserialize;
use serde::{de::Deserializer, Deserialize};
Expand Down Expand Up @@ -850,6 +852,48 @@ mod tests {
);
}

#[test]
fn test_date_field() {
let data = [0x82, 0x2d, 0x01, 0x00, 0x0c, 0x00, 0xe0, 0x47, 0x5c, 0x03];

#[derive(Deserialize, PartialEq, Eq, Debug)]
struct MyStruct {
field1: Date,
}

let mut map = HashMap::new();
map.insert(0x2d82, String::from("field1"));

let actual: MyStruct = from_slice(&data[..], &map).unwrap();
assert_eq!(
actual,
MyStruct {
field1: Date::from_ymd(1436, 1, 1)
}
);
}

#[test]
fn test_datehour_field() {
let data = [0x82, 0x2d, 0x01, 0x00, 0x0c, 0x00, 0x4b, 0x1d, 0x9f, 0x03];

#[derive(Deserialize, PartialEq, Eq, Debug)]
struct MyStruct {
field1: DateHour,
}

let mut map = HashMap::new();
map.insert(0x2d82, String::from("field1"));

let actual: MyStruct = from_slice(&data[..], &map).unwrap();
assert_eq!(
actual,
MyStruct {
field1: DateHour::from_ymdh(1936, 1, 1, 12)
}
);
}

#[test]
fn test_multiple_top_level_events() {
let data = [
Expand Down
Loading