Skip to main content

vortex_array/test_harness/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Shared assertions and fixtures for array tests.
5//!
6//! These helpers are available to this crate's tests and to crates that enable `_test-harness`.
7
8use std::io::Write;
9
10use goldenfile::Mint;
11use goldenfile::differs::binary_diff;
12use itertools::Itertools;
13use vortex_error::VortexResult;
14use vortex_session::VortexSession;
15
16use crate::ArrayRef;
17use crate::ExecutionCtx;
18use crate::arrays::BoolArray;
19use crate::arrays::bool::BoolArrayExt;
20use crate::optimizer::ArrayOptimizer;
21use crate::optimizer::mark_decoded_optimized;
22
23mod execution_paths;
24pub use execution_paths::ExpectedOutcome;
25pub use execution_paths::assert_execution_paths;
26
27#[cfg(not(codspeed))]
28pub mod trace;
29
30/// Check that a named metadata matches its previous versioning.
31///
32/// Goldenfile takes care of checking for equality against a checked-in file.
33#[expect(clippy::unwrap_used)]
34pub fn check_metadata(name: &str, metadata: &[u8]) {
35    let mut mint = Mint::new("goldenfiles/");
36    let mut f = mint
37        .new_goldenfile_with_differ(name, Box::new(binary_diff))
38        .unwrap();
39    f.write_all(metadata).unwrap();
40}
41
42/// Outputs the indices of the true values in a BoolArray
43pub fn to_int_indices(indices_bits: BoolArray, ctx: &mut ExecutionCtx) -> VortexResult<Vec<u64>> {
44    let buffer = indices_bits.to_bit_buffer();
45    let mask = indices_bits
46        .as_ref()
47        .validity()?
48        .execute_mask(indices_bits.as_ref().len(), ctx)?;
49    Ok(buffer
50        .iter()
51        .enumerate()
52        .filter_map(|(idx, v)| (v && mask.value(idx)).then_some(idx as u64))
53        .collect_vec())
54}
55
56/// Marks a benchmark input as optimized, as decoding marks the arrays that a reader returns.
57///
58/// Execution optimizes each array that it reaches unless the array is already marked. An input
59/// that a benchmark builds with constructors is not marked, so without this call the benchmark
60/// also times the optimizer.
61///
62/// # Panics
63///
64/// Panics if the optimizer rewrites `array`. Then the benchmark would time an array other than
65/// the one it built.
66pub fn mark_optimized(array: &ArrayRef, session: &VortexSession) -> VortexResult<()> {
67    let optimized = array.optimize_ctx(session)?;
68    assert!(
69        ArrayRef::ptr_eq(&optimized, array),
70        "the optimizer rewrote the benchmark input:\n{}\ninto:\n{}",
71        array.display_tree(),
72        optimized.display_tree(),
73    );
74
75    Ok(())
76}
77
78/// Marks each node of a benchmark input as optimized without trying any rule, as decoding does.
79///
80/// Use this instead of [`mark_optimized`] when the benchmark times a shape that a file can hold
81/// but that the optimizer rewrites, such as an extension array over constant storage. Decoding
82/// keeps that shape, so the benchmark must keep it too.
83pub fn mark_decoded(array: &ArrayRef) {
84    for node in array.depth_first_traversal() {
85        mark_decoded_optimized(&node);
86    }
87}