From: Ben Pfaff Date: Sat, 12 Apr 2025 16:04:28 +0000 (-0700) Subject: more layers tests X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bcd151940a3b6d135247a8e4ba003c7445d4fa5b;p=pspp more layers tests --- diff --git a/rust/pspp/src/output/pivot/output.rs b/rust/pspp/src/output/pivot/output.rs index 844e3f3165..8c769eee5f 100644 --- a/rust/pspp/src/output/pivot/output.rs +++ b/rust/pspp/src/output/pivot/output.rs @@ -1,4 +1,4 @@ -use std::{ops::Range, sync::Arc}; +use std::{iter::zip, ops::Range, sync::Arc}; use enum_map::{enum_map, EnumMap}; use itertools::Itertools; @@ -233,14 +233,21 @@ impl PivotTable { } pub fn output_layers(&self, layer_indexes: &[usize]) -> Option { - self.create_aux_table_if_nonempty( - Area::Layers, - self.nonempty_layer_dimensions() - .collect::>() - .into_iter() - .enumerate() - .map(|(i, dimension)| dimension.nth_leaf(layer_indexes[i]).unwrap().name.clone()), - ) + let mut layers = Vec::new(); + for (dimension, &layer_index) in zip( + self.axes[Axis3::Z] + .dimensions + .iter() + .map(|index| &self.dimensions[*index]), + layer_indexes, + ) { + if !dimension.is_empty() { + layers.push(dimension.nth_leaf(layer_index).unwrap().name.clone()); + } + } + layers.reverse(); + + self.create_aux_table_if_nonempty(Area::Layers, layers.into_iter()) } pub fn output_caption(&self) -> Option
{ diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index a956b5c4f1..6b4a0155cc 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -501,6 +501,83 @@ b3 ); } +#[test] +fn d3() { + let a = ( + Axis3::Z, + Dimension::new(Group::new("a").with("a1").with("a2").with("a3")), + ); + let b = ( + Axis3::Z, + Dimension::new(Group::new("b").with("b1").with("b2").with("b3").with("b4")), + ); + let c = ( + Axis3::X, + Dimension::new( + Group::new("c") + .with("c1") + .with("c2") + .with("c3") + .with("c4") + .with("c5"), + ), + ); + let mut pt = + PivotTable::new("Column x b1 x a1", vec![a, b, c]).with_look(Arc::new(test_look())); + let mut i = 0; + for c in 0..5 { + for b in 0..4 { + for a in 0..3 { + pt.insert(&[a, b, c], Value::new_integer(Some(i as f64))); + i += 1; + } + } + } + assert_rendering( + &pt, + "\ +Column x b1 x a1 +b1 +a1 +╭──┬──┬──┬──┬──╮ +│c1│c2│c3│c4│c5│ +├──┼──┼──┼──┼──┤ +│ 0│12│24│36│48│ +╰──┴──┴──┴──┴──╯ +", + ); + + let pt = pt.with_layer(&[0, 1]).with_title("Column x b2 x a1"); + assert_rendering( + &pt, + "\ +Column x b2 x a1 +b2 +a1 +╭──┬──┬──┬──┬──╮ +│c1│c2│c3│c4│c5│ +├──┼──┼──┼──┼──┤ +│ 3│15│27│39│51│ +╰──┴──┴──┴──┴──╯ +", + ); + + let pt = pt.with_layer(&[1, 2]).with_title("Column x b3 x a2"); + assert_rendering( + &pt, + "\ +Column x b3 x a2 +b3 +a2 +╭──┬──┬──┬──┬──╮ +│c1│c2│c3│c4│c5│ +├──┼──┼──┼──┼──┤ +│ 7│19│31│43│55│ +╰──┴──┴──┴──┴──╯ +", + ); +} + #[test] fn title_and_caption() { let pivot_table =