nmbrs-workload 0.3.0

Uniform workload specification parsing and processing for nmbrs
Documentation
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Coverage for the six syntactic shapes of `for_each` and the
//! detection rule that picks between Cartesian (single or
//! multi-clause) and Union comprehension modes.
//!
//! Assertions walk the algebra AST directly: a `for_each:`
//! parses to a `polydat::iteration::comprehension::Comprehension`
//! whose top-level shape (Clause / Cartesian / Union / Zip,
//! optionally wrapped by Filter / Order) records the
//! parsed-out structure.

use nmbrs_workload::model::ScenarioNode;
use nmbrs_workload::parse::parse_workload;
use polydat::iteration::comprehension::Comprehension as Algebra;
use polydat::iteration::comprehension::source::{LiteralValue, Source};
use polydat::iteration::comprehension::strategy::ZipMode;
use std::collections::HashMap;

fn first_scenario_node(yaml: &str) -> ScenarioNode {
    let wl = parse_workload(yaml, &HashMap::new())
        .unwrap_or_else(|e| panic!("parse failed: {e}\n--- yaml ---\n{yaml}"));
    let nodes = wl.scenarios.values().next().expect("at least one scenario");
    nodes.first().cloned().expect("at least one scenario node")
}

/// Peel outer `Order` / `Filter` wrappers; return the
/// structural body. Tests that care about modifiers assert
/// on the modifier separately.
fn body_of(comp: &Algebra) -> &Algebra {
    let mut body = comp;
    loop {
        match body {
            Algebra::Order { child, .. } | Algebra::Filter { child, .. } => body = child,
            _ => break body,
        }
    }
}

/// Optional filter predicate, if the comprehension is wrapped
/// in (or below an Order wrapping) a Filter.
fn filter_of(comp: &Algebra) -> Option<&str> {
    let mut node = comp;
    loop {
        match node {
            Algebra::Order { child, .. } => node = child,
            Algebra::Filter { predicate, .. } => return Some(predicate.as_str()),
            _ => return None,
        }
    }
}

/// Get a leaf Clause's source if `node` is one.
fn clause_source(node: &Algebra) -> Option<&Source> {
    match node {
        Algebra::Clause { source, .. } => Some(source),
        _ => None,
    }
}

/// Get a leaf Clause's name if `node` is one.
fn clause_name(node: &Algebra) -> Option<&str> {
    match node {
        Algebra::Clause { name, .. } => Some(name.as_str()),
        _ => None,
    }
}

/// Extract Literal values from a source, panicking otherwise.
fn literal_values(source: &Source) -> &[LiteralValue] {
    match source {
        Source::Literal { values } => values.as_slice(),
        other => panic!("expected Literal source, got {other:?}"),
    }
}

const PHASES: &str = r#"
phases:
  p:
    ops:
      step:
        stmt: "noop"
"#;

#[test]
fn string_single_clause_is_single_clause_cartesian() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "x in 1,2,3"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    // Single-clause Cartesian collapses to bare Clause at the algebra layer.
    let body = body_of(&comprehension);
    assert_eq!(clause_name(body), Some("x"));
    let values = literal_values(clause_source(body).unwrap());
    assert_eq!(values.len(), 3);
}

#[test]
fn string_multi_clause_distinct_vars_is_multi_clause_cartesian() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "x in 1,2, y in a,b"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Cartesian { children } => {
            assert_eq!(children.len(), 2);
            assert_eq!(clause_name(&children[0]), Some("x"));
            assert_eq!(clause_name(&children[1]), Some("y"));
        }
        other => panic!("expected Cartesian, got {other:?}"),
    }
}

#[test]
fn string_multi_clause_repeated_var_is_union() {
    // Same var name twice in a single string ⇒ Union (each
    // clause is its own sub-space).
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "x in 1, x in 2"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Union { children } => {
            assert_eq!(children.len(), 2);
            assert_eq!(clause_name(&children[0]), Some("x"));
            assert_eq!(clause_name(&children[1]), Some("x"));
            let v0 = literal_values(clause_source(&children[0]).unwrap());
            assert_eq!(v0, &[LiteralValue::Int(1)]);
            let v1 = literal_values(clause_source(&children[1]).unwrap());
            assert_eq!(v1, &[LiteralValue::Int(2)]);
        }
        other => panic!("expected Union, got {other:?}"),
    }
}

#[test]
fn array_single_clause_distinct_vars_is_multi_clause_cartesian() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each:
        - "x in 1,2"
        - "y in a,b"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Cartesian { children } => {
            assert_eq!(children.len(), 2);
            assert_eq!(clause_name(&children[0]), Some("x"));
            assert_eq!(clause_name(&children[1]), Some("y"));
        }
        other => panic!("expected Cartesian, got {other:?}"),
    }
}

#[test]
fn array_single_clause_repeated_var_is_union() {
    // Two array entries each with one clause, same var name ⇒
    // Union of single-var sub-spaces.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each:
        - "x in 1"
        - "x in 2"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Union { children } => {
            assert_eq!(children.len(), 2);
        }
        other => panic!("expected Union, got {other:?}"),
    }
}

#[test]
fn array_multi_clause_repeated_vars_is_union() {
    // The motivating union shape: two array entries, each is a
    // multi-dim sub-space; vars repeat across entries.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each:
        - "k in 10, limit in 10,20,30"
        - "k in 100, limit in 100,200,300"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Union { children } => {
            assert_eq!(children.len(), 2);
            // Each sub-space is a 2-clause Cartesian.
            for child in children {
                match child {
                    Algebra::Cartesian { children: inner } => {
                        assert_eq!(inner.len(), 2);
                        assert_eq!(clause_name(&inner[0]), Some("k"));
                        assert_eq!(clause_name(&inner[1]), Some("limit"));
                    }
                    other => panic!("expected Cartesian sub-space, got {other:?}"),
                }
            }
        }
        other => panic!("expected Union, got {other:?}"),
    }
}

#[test]
fn array_multi_clause_distinct_vars_is_multi_clause_cartesian() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each:
        - "x in 1,2, y in a,b, z in p,q"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Cartesian { children } => {
            assert_eq!(children.len(), 3);
            assert_eq!(clause_name(&children[0]), Some("x"));
            assert_eq!(clause_name(&children[1]), Some("y"));
            assert_eq!(clause_name(&children[2]), Some("z"));
        }
        other => panic!("expected Cartesian, got {other:?}"),
    }
}

#[test]
fn for_keyword_alias_maps_to_same_comprehension() {
    // `for: ...` is accepted as a synonym for `for_each: ...`.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for: "k in 10,100"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    // Single clause; no filter, no order; coordinate_names = ["k"].
    assert!(filter_of(&comprehension).is_none());
    assert!(!matches!(comprehension, Algebra::Order { .. }));
    assert_eq!(comprehension.coordinate_names(), vec!["k"]);
}

#[test]
fn where_clause_attaches_to_comprehension() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for: "k in 10,100, limit in 10,20,30"
      where: "{{k}} * {{limit}} < 1000"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    assert_eq!(filter_of(&comprehension), Some("{k} * {limit} < 1000"));
}

#[test]
fn for_each_in_paren_call_does_not_split() {
    // Regression guard: commas inside `matching_profiles('a','b')`
    // must not be treated as top-level clause separators.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "p in matching_profiles('ds', 'pre')"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    assert_eq!(clause_name(body), Some("p"));
    match clause_source(body) {
        Some(Source::Generator { expr, .. }) => {
            assert!(expr.contains("matching_profiles"), "got: {expr}");
            assert!(expr.contains(","));
        }
        other => panic!("expected Generator source, got {other:?}"),
    }
}

// ---- Layer 7a parallel-iter through the YAML parser --------

#[test]
fn parallel_iter_string_form_round_trips_through_yaml() {
    // `(a, b) in (e1, e2)` survives the YAML → algebra path
    // as a top-level Zip with two Clause children and
    // Strict mode.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "(x, y) in (fib(5), pow2(5))"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Zip { children, mode } => {
            assert_eq!(*mode, ZipMode::Strict);
            assert_eq!(children.len(), 2);
            assert_eq!(clause_name(&children[0]), Some("x"));
            assert_eq!(clause_name(&children[1]), Some("y"));
        }
        other => panic!("expected Zip, got {other:?}"),
    }
}

#[test]
fn parallel_iter_zip_truncate_round_trips_through_yaml() {
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "(x, y) in zip_truncate(fib(5), pow2(3))"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Zip { mode, .. } => {
            assert_eq!(*mode, ZipMode::Truncate);
        }
        other => panic!("expected Zip, got {other:?}"),
    }
}

#[test]
fn parallel_iter_mixed_with_single_clause_round_trips() {
    // `(x, y) in (...), z in zs` — parallel group is one
    // clause, z is another. Cartesian over both: child 0 is
    // a Zip, child 1 is a Clause.
    let yaml = format!(
        r#"
scenarios:
  s:
    - for_each: "(x, y) in (fib(4), pow2(4)), z in 1..3"
      phases: [p]
{PHASES}
"#
    );
    let node = first_scenario_node(&yaml);
    let ScenarioNode::Comprehension { comprehension, .. } = node else {
        panic!("expected Comprehension");
    };
    let body = body_of(&comprehension);
    match body {
        Algebra::Cartesian { children } => {
            assert_eq!(children.len(), 2);
            assert!(matches!(&children[0], Algebra::Zip { .. }));
            assert_eq!(clause_name(&children[1]), Some("z"));
        }
        other => panic!("expected Cartesian, got {other:?}"),
    }
}