1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0
//! Parity smoke tests for the [`ComprehensionSpec`] friendly
//! surface — confirms that for representative shapes, the
//! algebra AST built via the new spec surface dispenses the
//! same tuple sequence as the legacy parser + executor path
//! (compiled IR interpreter).
//!
//! The smoke test deliberately doesn't try to enumerate every
//! algebra shape — the per-PR unit tests do that. It exists to
//! catch any global drift between the two paths (e.g., source-
//! string parsing diverging from legacy semantics, structural
//! detection rule mismatch).
//!
//! ## Scope
//!
//! - Single-clause cartesian
//! - Multi-clause cartesian
//! - Union of sub-spaces (name-repetition triggers Union)
//! - With `where` filter
//! - With `order` truncation
//!
//! Each case constructs the algebra AST via both paths
//! (`parse_comprehension_algebra` vs.
//! `ComprehensionSpec::into_algebra`) and asserts the
//! resulting compiled programs dispense identical tuples.
use polydat::iteration::comprehension::spec::{parse_comprehension_algebra, parse_text};
use polydat::iteration::comprehension::surfaces::{CoordinateStream, compile};
/// Dispense up to `cap` tuples from a coord stream and return
/// them as Debug-rendered strings (cheap structural compare).
fn dispense(stream: &mut CoordinateStream, cap: usize) -> Vec<String> {
let mut out = Vec::new();
for _ in 0..cap {
match stream.next() {
Some(tuple) => out.push(format!("{tuple:?}")),
None => break,
}
}
out
}
/// Compile both ASTs and assert their dispense sequences match.
fn assert_dispense_parity(legacy_text: &str, spec_yaml: &str) {
// Path A: the comprehension text.
let algebra_a = parse_comprehension_algebra(legacy_text).expect("text parse should succeed");
// Path B: YAML text → ComprehensionSpec → algebra AST.
let algebra_b = parse_text(spec_yaml).expect("spec parse should succeed");
// Dispense both, compare tuple-by-tuple.
let mut stream_a = compile(&algebra_a).unwrap().coordinate_stream();
let mut stream_b = compile(&algebra_b).unwrap().coordinate_stream();
let tuples_a = dispense(&mut stream_a, 200);
let tuples_b = dispense(&mut stream_b, 200);
assert_eq!(
tuples_a, tuples_b,
"dispense sequence parity failed:\n text: {legacy_text}\n spec: {spec_yaml}\n tree: {algebra_a:?}"
);
}
#[test]
fn single_clause_cartesian_parity() {
assert_dispense_parity(
"k in 1..5",
r#"
for: "k in 1..5"
"#,
);
}
#[test]
fn multi_clause_cartesian_parity() {
assert_dispense_parity(
"k in 1..3, limit in [10, 100]",
r#"
for: "k in 1..3, limit in [10, 100]"
"#,
);
}
#[test]
fn multi_clause_cartesian_via_list_form_parity() {
assert_dispense_parity(
"k in 1..3, limit in [10, 100]",
r#"
for:
- "k in 1..3"
- "limit in [10, 100]"
"#,
);
}
#[test]
fn union_via_repeated_names_parity() {
// Legacy detection rule: name `k` repeats across sub-spaces
// ⇒ Union. The spec surface's UnionOfClauseLists form
// makes this explicit.
assert_dispense_parity(
"k in [10, 20], k in [100, 200]",
r#"
for:
- ["k in [10, 20]"]
- ["k in [100, 200]"]
"#,
);
}
#[test]
fn cartesian_with_where_parity() {
assert_dispense_parity(
"k in 1..5, limit in 1..5 where {k} + {limit} <= 5",
r#"
for: "k in 1..5, limit in 1..5"
where: "{k} + {limit} <= 5"
"#,
);
}
#[test]
fn cartesian_with_order_truncation_parity() {
assert_dispense_parity(
"k in 1..3, limit in 1..3 order lex/4",
r#"
for: "k in 1..3, limit in 1..3"
order: "lex/4"
"#,
);
}
#[test]
fn cartesian_with_where_and_order_parity() {
assert_dispense_parity(
"k in 1..5, limit in 1..5 where {k} * {limit} <= 10 order lex/6",
r#"
for: "k in 1..5, limit in 1..5"
where: "{k} * {limit} <= 10"
order: "lex/6"
"#,
);
}
#[test]
fn json_input_parity_with_yaml() {
// Same algebra from a JSON-shaped text block.
assert_dispense_parity(
"k in 1..4, limit in [1, 2]",
r#"{
"for": "k in 1..4, limit in [1, 2]"
}"#,
);
}
#[test]
fn scalar_literal_parity() {
// Single-element literal: `k in 10` dispenses one tuple
// {k=10}. Confirms parse_source's bare-scalar handling
// matches legacy semantics.
assert_dispense_parity(
"k in 10",
r#"
for: "k in 10"
"#,
);
}