From 36704126c4156f1371bde67417a290b7ddcd57da Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 4 Apr 2025 07:04:38 -0700 Subject: [PATCH] add tests for TIME and MTIME --- rust/pspp/src/format/display.rs | 528 +++++++++++++++++++------------- 1 file changed, 318 insertions(+), 210 deletions(-) diff --git a/rust/pspp/src/format/display.rs b/rust/pspp/src/format/display.rs index 61b84bd0ea..135a1b3ba3 100644 --- a/rust/pspp/src/format/display.rs +++ b/rust/pspp/src/format/display.rs @@ -510,7 +510,7 @@ impl<'a, 'b> DisplayValue<'a, 'b> { { write!(&mut output, ":{:02.0}", time.floor()).unwrap(); } else if excess_width >= 5 { - let d = min(self.format.d(), excess_width as usize); + let d = min(self.format.d(), excess_width as usize - 4); let w = d + 3; write!(&mut output, ":{:02$.*}", d, time, w).unwrap(); if self.settings.decimal == Decimal::Comma { @@ -1047,6 +1047,7 @@ mod test { use binrw::io::BufReader; use encoding_rs::UTF_8; + use itertools::Itertools; use smallstr::SmallString; use smallvec::SmallVec; @@ -1373,7 +1374,7 @@ mod test { "18-7-2094 1:56:51.59319", ]; assert_eq!(expect.len(), INPUTS.len()); - for (input, expect) in INPUTS.iter().copied().zip(expect.iter().copied()) { + for (input, expect) in INPUTS.iter().copied().zip_eq(expect.iter().copied()) { let value = parser.parse(input, UTF_8).unwrap(); let formatted = value .display(format, UTF_8) @@ -2315,216 +2316,323 @@ mod test { ); } - fn test_times(format: Format, expect: &[&str]) { + fn test_times(format: Format, name: &str) { + let directory = Path::new(env!("CARGO_MANIFEST_DIR")).join("src/format/testdata/display"); + let input_filename = directory.join("time-input.txt"); + let input = BufReader::new(File::open(&input_filename).unwrap()); + + let output_filename = directory.join(name); + let output = BufReader::new(File::open(&output_filename).unwrap()); + let parser = Format::new(Type::DTime, 40, 0).unwrap().parser(); - static INPUTS: &[&str; 200] = &[ - "0 0:0:0", - "0 4:50:38.12301", - "0 12:31:35.23453", - "0 12:47:53.34505", - "0 1:26:0.45615", - "0 20:58:11.56677", - "0 7:36:5.18964", - "0 15:43:49.83132", - "0 4:25:9.01293", - "0 6:49:27.52375", - "0 2:57:52.01565", - "0 16:45:44.86529", - "0 21:30:57.82047", - "0 4:25:9.15395", - "0 6:49:27.10533", - "0 2:57:52.48229", - "0 16:45:44.65827", - "0 21:30:57.58219", - "0 22:30:4.18347", - "0 1:56:51.59319", - "1 0:0:0", - "1 4:50:38.12301", - "1 12:31:35.23453", - "1 12:47:53.34505", - "1 1:26:0.45615", - "1 20:58:11.56677", - "1 7:36:5.18964", - "1 15:43:49.83132", - "1 4:25:9.01293", - "1 6:49:27.52375", - "1 2:57:52.01565", - "1 16:45:44.86529", - "1 21:30:57.82047", - "1 4:25:9.15395", - "1 6:49:27.10533", - "1 2:57:52.48229", - "1 16:45:44.65827", - "1 21:30:57.58219", - "1 22:30:4.18347", - "1 1:56:51.59319", - "2 0:0:0", - "2 4:50:38.12301", - "2 12:31:35.23453", - "2 12:47:53.34505", - "2 1:26:0.45615", - "2 20:58:11.56677", - "2 7:36:5.18964", - "2 15:43:49.83132", - "2 4:25:9.01293", - "2 6:49:27.52375", - "2 2:57:52.01565", - "2 16:45:44.86529", - "2 21:30:57.82047", - "2 4:25:9.15395", - "2 6:49:27.10533", - "2 2:57:52.48229", - "2 16:45:44.65827", - "2 21:30:57.58219", - "2 22:30:4.18347", - "2 1:56:51.59319", - "54 0:0:0", - "54 4:50:38.12301", - "54 12:31:35.23453", - "54 12:47:53.34505", - "54 1:26:0.45615", - "54 20:58:11.56677", - "54 7:36:5.18964", - "54 15:43:49.83132", - "54 4:25:9.01293", - "54 6:49:27.52375", - "54 2:57:52.01565", - "54 16:45:44.86529", - "54 21:30:57.82047", - "54 4:25:9.15395", - "54 6:49:27.10533", - "54 2:57:52.48229", - "54 16:45:44.65827", - "54 21:30:57.58219", - "54 22:30:4.18347", - "54 1:56:51.59319", - "681 0:0:0", - "681 4:50:38.12301", - "681 12:31:35.23453", - "681 12:47:53.34505", - "681 1:26:0.45615", - "681 20:58:11.56677", - "681 7:36:5.18964", - "681 15:43:49.83132", - "681 4:25:9.01293", - "681 6:49:27.52375", - "681 2:57:52.01565", - "681 16:45:44.86529", - "681 21:30:57.82047", - "681 4:25:9.15395", - "681 6:49:27.10533", - "681 2:57:52.48229", - "681 16:45:44.65827", - "681 21:30:57.58219", - "681 22:30:4.18347", - "681 1:56:51.59319", - "-0 0:0:0", - "-0 4:50:38.12301", - "-0 12:31:35.23453", - "-0 12:47:53.34505", - "-0 1:26:0.45615", - "-0 20:58:11.56677", - "-0 7:36:5.18964", - "-0 15:43:49.83132", - "-0 4:25:9.01293", - "-0 6:49:27.52375", - "-0 2:57:52.01565", - "-0 16:45:44.86529", - "-0 21:30:57.82047", - "-0 4:25:9.15395", - "-0 6:49:27.10533", - "-0 2:57:52.48229", - "-0 16:45:44.65827", - "-0 21:30:57.58219", - "-0 22:30:4.18347", - "-0 1:56:51.59319", - "-1 0:0:0", - "-1 4:50:38.12301", - "-1 12:31:35.23453", - "-1 12:47:53.34505", - "-1 1:26:0.45615", - "-1 20:58:11.56677", - "-1 7:36:5.18964", - "-1 15:43:49.83132", - "-1 4:25:9.01293", - "-1 6:49:27.52375", - "-1 2:57:52.01565", - "-1 16:45:44.86529", - "-1 21:30:57.82047", - "-1 4:25:9.15395", - "-1 6:49:27.10533", - "-1 2:57:52.48229", - "-1 16:45:44.65827", - "-1 21:30:57.58219", - "-1 22:30:4.18347", - "-1 1:56:51.59319", - "-2 0:0:0", - "-2 4:50:38.12301", - "-2 12:31:35.23453", - "-2 12:47:53.34505", - "-2 1:26:0.45615", - "-2 20:58:11.56677", - "-2 7:36:5.18964", - "-2 15:43:49.83132", - "-2 4:25:9.01293", - "-2 6:49:27.52375", - "-2 2:57:52.01565", - "-2 16:45:44.86529", - "-2 21:30:57.82047", - "-2 4:25:9.15395", - "-2 6:49:27.10533", - "-2 2:57:52.48229", - "-2 16:45:44.65827", - "-2 21:30:57.58219", - "-2 22:30:4.18347", - "-2 1:56:51.59319", - "-54 0:0:0", - "-54 4:50:38.12301", - "-54 12:31:35.23453", - "-54 12:47:53.34505", - "-54 1:26:0.45615", - "-54 20:58:11.56677", - "-54 7:36:5.18964", - "-54 15:43:49.83132", - "-54 4:25:9.01293", - "-54 6:49:27.52375", - "-54 2:57:52.01565", - "-54 16:45:44.86529", - "-54 21:30:57.82047", - "-54 4:25:9.15395", - "-54 6:49:27.10533", - "-54 2:57:52.48229", - "-54 16:45:44.65827", - "-54 21:30:57.58219", - "-54 22:30:4.18347", - "-54 1:56:51.59319", - "-681 0:0:0", - "-681 4:50:38.12301", - "-681 12:31:35.23453", - "-681 12:47:53.34505", - "-681 1:26:0.45615", - "-681 20:58:11.56677", - "-681 7:36:5.18964", - "-681 15:43:49.83132", - "-681 4:25:9.01293", - "-681 6:49:27.52375", - "-681 2:57:52.01565", - "-681 16:45:44.86529", - "-681 21:30:57.82047", - "-681 4:25:9.15395", - "-681 6:49:27.10533", - "-681 2:57:52.48229", - "-681 16:45:44.65827", - "-681 21:30:57.58219", - "-681 22:30:4.18347", - "-681 1:56:51.59319", - ]; - assert_eq!(expect.len(), INPUTS.len()); - for (input, expect) in INPUTS.iter().copied().zip(expect.iter().copied()) { - let value = parser.parse(input, UTF_8).unwrap(); + for ((input, expect), line_number) in input + .lines() + .map(|r| r.unwrap()) + .zip_eq(output.lines().map(|r| r.unwrap())) + .zip(1..) + { + let value = parser.parse(&input, UTF_8).unwrap(); let formatted = value.display(format, UTF_8).to_string(); - assert_eq!(&formatted, expect); + assert!( + formatted == expect, + "formatting {}:{line_number} as {format}:\n actual: {formatted:?}\nexpected: {expect:?}", + input_filename.display() + ); } } - + + #[test] + fn time5() { + test_times(Format::new(Type::Time, 5, 0).unwrap(), "time5.txt"); + } + + #[test] + fn time6() { + test_times(Format::new(Type::Time, 6, 0).unwrap(), "time6.txt"); + } + + #[test] + fn time7() { + test_times(Format::new(Type::Time, 7, 0).unwrap(), "time7.txt"); + } + + #[test] + fn time8() { + test_times(Format::new(Type::Time, 8, 0).unwrap(), "time8.txt"); + } + + #[test] + fn time9() { + test_times(Format::new(Type::Time, 9, 0).unwrap(), "time9.txt"); + } + + #[test] + fn time10() { + test_times(Format::new(Type::Time, 10, 0).unwrap(), "time10.txt"); + } + + #[test] + fn time10_1() { + test_times(Format::new(Type::Time, 10, 1).unwrap(), "time10.1.txt"); + } + + #[test] + fn time11() { + test_times(Format::new(Type::Time, 11, 0).unwrap(), "time11.txt"); + } + + #[test] + fn time11_1() { + test_times(Format::new(Type::Time, 11, 1).unwrap(), "time11.1.txt"); + } + + #[test] + fn time11_2() { + test_times(Format::new(Type::Time, 11, 2).unwrap(), "time11.2.txt"); + } + + #[test] + fn time12() { + test_times(Format::new(Type::Time, 12, 0).unwrap(), "time12.txt"); + } + + #[test] + fn time12_1() { + test_times(Format::new(Type::Time, 12, 1).unwrap(), "time12.1.txt"); + } + + #[test] + fn time12_2() { + test_times(Format::new(Type::Time, 12, 2).unwrap(), "time12.2.txt"); + } + + #[test] + fn time12_3() { + test_times(Format::new(Type::Time, 12, 3).unwrap(), "time12.3.txt"); + } + + #[test] + fn time13() { + test_times(Format::new(Type::Time, 13, 0).unwrap(), "time13.txt"); + } + + #[test] + fn time13_1() { + test_times(Format::new(Type::Time, 13, 1).unwrap(), "time13.1.txt"); + } + + #[test] + fn time13_2() { + test_times(Format::new(Type::Time, 13, 2).unwrap(), "time13.2.txt"); + } + + #[test] + fn time13_3() { + test_times(Format::new(Type::Time, 13, 3).unwrap(), "time13.3.txt"); + } + + #[test] + fn time13_4() { + test_times(Format::new(Type::Time, 13, 4).unwrap(), "time13.4.txt"); + } + + #[test] + fn time14() { + test_times(Format::new(Type::Time, 14, 0).unwrap(), "time14.txt"); + } + + #[test] + fn time14_1() { + test_times(Format::new(Type::Time, 14, 1).unwrap(), "time14.1.txt"); + } + + #[test] + fn time14_2() { + test_times(Format::new(Type::Time, 14, 2).unwrap(), "time14.2.txt"); + } + + #[test] + fn time14_3() { + test_times(Format::new(Type::Time, 14, 3).unwrap(), "time14.3.txt"); + } + + #[test] + fn time14_4() { + test_times(Format::new(Type::Time, 14, 4).unwrap(), "time14.4.txt"); + } + + #[test] + fn time14_5() { + test_times(Format::new(Type::Time, 14, 5).unwrap(), "time14.5.txt"); + } + + #[test] + fn time15() { + test_times(Format::new(Type::Time, 15, 0).unwrap(), "time15.txt"); + } + + #[test] + fn time15_1() { + test_times(Format::new(Type::Time, 15, 1).unwrap(), "time15.1.txt"); + } + + #[test] + fn time15_2() { + test_times(Format::new(Type::Time, 15, 2).unwrap(), "time15.2.txt"); + } + + #[test] + fn time15_3() { + test_times(Format::new(Type::Time, 15, 3).unwrap(), "time15.3.txt"); + } + + #[test] + fn time15_4() { + test_times(Format::new(Type::Time, 15, 4).unwrap(), "time15.4.txt"); + } + + #[test] + fn time15_5() { + test_times(Format::new(Type::Time, 15, 5).unwrap(), "time15.5.txt"); + } + + #[test] + fn time15_6() { + test_times(Format::new(Type::Time, 15, 6).unwrap(), "time15.6.txt"); + } + + #[test] + fn mtime5() { + test_times(Format::new(Type::MTime, 5, 0).unwrap(), "mtime5.txt"); + } + + #[test] + fn mtime6() { + test_times(Format::new(Type::MTime, 6, 0).unwrap(), "mtime6.txt"); + } + + #[test] + fn mtime7() { + test_times(Format::new(Type::MTime, 7, 0).unwrap(), "mtime7.txt"); + } + + #[test] + fn mtime7_1() { + test_times(Format::new(Type::MTime, 7, 1).unwrap(), "mtime7.1.txt"); + } + + #[test] + fn mtime8() { + test_times(Format::new(Type::MTime, 8, 0).unwrap(), "mtime8.txt"); + } + + #[test] + fn mtime8_1() { + test_times(Format::new(Type::MTime, 8, 1).unwrap(), "mtime8.1.txt"); + } + + #[test] + fn mtime8_2() { + test_times(Format::new(Type::MTime, 8, 2).unwrap(), "mtime8.2.txt"); + } + + #[test] + fn mtime9() { + test_times(Format::new(Type::MTime, 9, 0).unwrap(), "mtime9.txt"); + } + + #[test] + fn mtime9_1() { + test_times(Format::new(Type::MTime, 9, 1).unwrap(), "mtime9.1.txt"); + } + + #[test] + fn mtime9_2() { + test_times(Format::new(Type::MTime, 9, 2).unwrap(), "mtime9.2.txt"); + } + + #[test] + fn mtime9_3() { + test_times(Format::new(Type::MTime, 9, 3).unwrap(), "mtime9.3.txt"); + } + + #[test] + fn mtime10() { + test_times(Format::new(Type::MTime, 10, 0).unwrap(), "mtime10.txt"); + } + + #[test] + fn mtime10_1() { + test_times(Format::new(Type::MTime, 10, 1).unwrap(), "mtime10.1.txt"); + } + + #[test] + fn mtime10_2() { + test_times(Format::new(Type::MTime, 10, 2).unwrap(), "mtime10.2.txt"); + } + + #[test] + fn mtime10_3() { + test_times(Format::new(Type::MTime, 10, 3).unwrap(), "mtime10.3.txt"); + } + + #[test] + fn mtime10_4() { + test_times(Format::new(Type::MTime, 10, 4).unwrap(), "mtime10.4.txt"); + } + + #[test] + fn mtime11() { + test_times(Format::new(Type::MTime, 11, 0).unwrap(), "mtime11.txt"); + } + + #[test] + fn mtime11_1() { + test_times(Format::new(Type::MTime, 11, 1).unwrap(), "mtime11.1.txt"); + } + + #[test] + fn mtime11_2() { + test_times(Format::new(Type::MTime, 11, 2).unwrap(), "mtime11.2.txt"); + } + + #[test] + fn mtime11_3() { + test_times(Format::new(Type::MTime, 11, 3).unwrap(), "mtime11.3.txt"); + } + + #[test] + fn mtime11_4() { + test_times(Format::new(Type::MTime, 11, 4).unwrap(), "mtime11.4.txt"); + } + + #[test] + fn mtime11_5() { + test_times(Format::new(Type::MTime, 11, 5).unwrap(), "mtime11.5.txt"); + } + + #[test] + fn mtime12_5() { + test_times(Format::new(Type::MTime, 12, 5).unwrap(), "mtime12.5.txt"); + } + + #[test] + fn mtime13_5() { + test_times(Format::new(Type::MTime, 13, 5).unwrap(), "mtime13.5.txt"); + } + + #[test] + fn mtime14_5() { + test_times(Format::new(Type::MTime, 14, 5).unwrap(), "mtime14.5.txt"); + } + + #[test] + fn mtime15_5() { + test_times(Format::new(Type::MTime, 15, 5).unwrap(), "mtime15.5.txt"); + } + + #[test] + fn mtime16_5() { + test_times(Format::new(Type::MTime, 16, 5).unwrap(), "mtime16.5.txt"); + } } -- 2.30.2