more layers tests
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 12 Apr 2025 16:04:28 +0000 (09:04 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 12 Apr 2025 16:04:28 +0000 (09:04 -0700)
rust/pspp/src/output/pivot/output.rs
rust/pspp/src/output/pivot/test.rs

index 844e3f3165bf9f39cea5f24e9cb5ed6f503e4413..8c769eee5f8516b86b734cc9daaa33177e7ccf17 100644 (file)
@@ -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<Table> {
-        self.create_aux_table_if_nonempty(
-            Area::Layers,
-            self.nonempty_layer_dimensions()
-                .collect::<Vec<_>>()
-                .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<Table> {
index a956b5c4f127c729e972fd8cce5ff21f31645af5..6b4a0155cc0952751eacbe516e462893b906f8ea 100644 (file)
@@ -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 =