cadmpeg-asm 0.6.6

Parse and write Autodesk Shape Manager record streams.
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// SPDX-License-Identifier: Apache-2.0

use crate::kernel_header::RefWidth;
use crate::nurbs::core::curve_cache;
use crate::nurbs::core::decode_curve_cache;
use crate::nurbs::core::decode_surface_cache;
use crate::nurbs::core::final_curve_patch_layout;
use crate::nurbs::core::final_surface_patch_layout;
use crate::nurbs::core::first_curve_patch_layout;
use crate::nurbs::core::surface_cache;
use crate::nurbs::core::surface_patch_layout_at;
use crate::nurbs::pcurve::tests::curve_block;
use crate::nurbs::pcurve::tests::curve_block_with_endpoint;
use crate::nurbs::pcurve::tests::pcurve_block;
use crate::nurbs::pcurve::tests::push_f64;
use crate::nurbs::pcurve::tests::push_ident;
use crate::nurbs::pcurve::tests::push_int;
use crate::nurbs::pcurve::tests::push_string;
use crate::nurbs::pcurve::tests::push_vector;
use crate::nurbs::pcurve::tests::surface_block;
use crate::nurbs::pcurve::tests::surface_block_with_x_offset;
use crate::nurbs::proc_curve::procedural_curve_resolving_refs;
use crate::nurbs::toks::lex_test_span;
use crate::nurbs::toks::test_table;

#[test]
fn curve_cache_decodes_in_both_integer_widths() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let block = curve_block(int_width);
        let curve = decode_curve_cache(&block)
            .unwrap_or_else(|| panic!("curve cache at width {int_width}"));
        assert_eq!(curve.degree(), 1);
        assert_eq!(curve.control_points().len(), 2);
        assert_eq!(curve.control_points()[1].x, 10.0); // cm→mm ×10
        assert_eq!(curve.knots().as_slice(), [0.0, 0.0, 1.0, 1.0]);
        assert!(first_curve_patch_layout(&block, int_width).is_some());
        assert!(final_curve_patch_layout(&block, int_width).is_some());
        let other_width = match int_width {
            RefWidth::Four => RefWidth::Eight,
            RefWidth::Eight => RefWidth::Four,
        };
        assert!(first_curve_patch_layout(&block, other_width).is_none());
        assert!(final_curve_patch_layout(&block, other_width).is_none());
    }
}

#[test]
fn generic_curve_and_pcurve_caches_withhold_multiple_candidates() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut curves = curve_block(int_width);
        curves.extend_from_slice(&curve_block(int_width));
        assert!(decode_curve_cache(&curves).is_none());

        let mut pcurves = pcurve_block(int_width);
        pcurves.extend_from_slice(&pcurve_block(int_width));
        assert!(super::decode_pcurve_cache(&pcurves).is_none());

        let block = pcurve_block(int_width);
        assert!(super::final_pcurve_patch_layout(&block, int_width).is_some());
        let other_width = match int_width {
            RefWidth::Four => RefWidth::Eight,
            RefWidth::Eight => RefWidth::Four,
        };
        assert!(super::final_pcurve_patch_layout(&block, other_width).is_none());

        let mut surfaces = b"comp_spl_sur".to_vec();
        surfaces.extend_from_slice(&surface_block(int_width));
        surfaces.extend_from_slice(&surface_block(int_width));
        assert!(decode_surface_cache(&surfaces).is_none());
    }
}

#[test]
fn wrapper_directrix_fields_reject_nested_curve_substitution() {
    let asm_decode_arena = cadmpeg_core::decode::DecodeArena::new();
    let (asm_decode_ctx, _) = cadmpeg_core::decode::DecodeContext::from_root_bytes(
        &[],
        &asm_decode_arena,
        &cadmpeg_core::decode::DecodePolicy::default(),
    )
    .expect("test decode context");
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut subset = vec![0x0f];
        push_ident(&mut subset, "subset_int_cur");
        subset.push(0x0f);
        push_ident(&mut subset, "support");
        subset.extend_from_slice(&curve_block(int_width));
        subset.push(0x10);
        push_f64(&mut subset, -1.0);
        push_f64(&mut subset, 2.0);
        subset.extend_from_slice(&curve_block(int_width));
        push_f64(&mut subset, 0.001);
        subset.push(0x10);
        let subset_tokens =
            lex_test_span(&subset, int_width).expect("valid single-record byte fixture");
        let subset_decoded = procedural_curve_resolving_refs(
            &asm_decode_ctx,
            &subset_tokens,
            &test_table(&subset, int_width).expect("valid single-record byte fixture"),
        )
        .transpose()
        .expect("decode the procedural curve")
        .unwrap_or_else(|| panic!("nested subset source at width {int_width}"));
        assert!(!matches!(
            subset_decoded.construction,
            crate::nurbs::proc_curve::ProceduralCurveConstruction::Subset(_)
        ));

        let mut offset = vec![0x0f];
        push_ident(&mut offset, "offset_int_cur");
        offset.push(0x0b);
        offset.push(0x0f);
        push_ident(&mut offset, "support");
        offset.extend_from_slice(&curve_block(int_width));
        offset.push(0x10);
        push_f64(&mut offset, -1.0);
        push_f64(&mut offset, 2.0);
        push_vector(&mut offset, [1.0, 2.0, 3.0]);
        push_string(&mut offset, "first");
        push_int(&mut offset, 0x04, 4, int_width);
        push_string(&mut offset, "second");
        push_int(&mut offset, 0x04, 5, int_width);
        offset.extend_from_slice(&curve_block(int_width));
        push_f64(&mut offset, 0.001);
        offset.push(0x10);
        let offset_tokens =
            lex_test_span(&offset, int_width).expect("valid single-record byte fixture");
        let offset_decoded = procedural_curve_resolving_refs(
            &asm_decode_ctx,
            &offset_tokens,
            &test_table(&offset, int_width).expect("valid single-record byte fixture"),
        )
        .transpose()
        .expect("decode the procedural curve")
        .unwrap_or_else(|| panic!("nested vector-offset source at width {int_width}"));
        assert!(!matches!(
            offset_decoded.construction,
            crate::nurbs::proc_curve::ProceduralCurveConstruction::VectorOffset(_)
        ));
    }
}

#[test]
fn pcurve_fit_tolerance_withholds_nested_only_cache() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exp_par_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&pcurve_block(int_width));
        push_f64(&mut bytes, 0.9);
        bytes.push(0x10);
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let scope = crate::nurbs::toks::subtype_span(&tokens, 0)
            .expect("the fixture opens one balanced scope");
        assert!(super::pcurve_fit_tolerance(scope).is_none());
    }
}

#[test]
fn patch_layout_roles_exclude_nested_construction_caches() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut surfaces = Vec::new();
        push_ident(&mut surfaces, "spline");
        surfaces.push(0x0f);
        push_ident(&mut surfaces, "auxiliary");
        surfaces.push(0x10);
        surfaces.push(0x0f);
        push_ident(&mut surfaces, "carrier");
        surfaces.extend_from_slice(&surface_block(int_width));
        let surface_end = surfaces.len();
        surfaces.push(0x0f);
        push_ident(&mut surfaces, "support");
        surfaces.extend_from_slice(&surface_block_with_x_offset(int_width, 9.0));
        surfaces.extend_from_slice(&[0x10, 0x10]);
        assert_eq!(
            final_surface_patch_layout(&surfaces, int_width)
                .expect("owned surface layout")
                .end(),
            surface_end
        );
        assert!(surface_patch_layout_at(&surfaces, 1, int_width).is_none());
        let mut ambiguous_surfaces = surfaces.clone();
        ambiguous_surfaces.push(0x0f);
        push_ident(&mut ambiguous_surfaces, "competing");
        ambiguous_surfaces.extend_from_slice(&surface_block(int_width));
        ambiguous_surfaces.push(0x10);
        assert!(final_surface_patch_layout(&ambiguous_surfaces, int_width).is_none());

        let mut curves = Vec::new();
        push_ident(&mut curves, "intcurve");
        curves.push(0x0f);
        push_ident(&mut curves, "auxiliary");
        curves.push(0x10);
        curves.push(0x0f);
        push_ident(&mut curves, "carrier");
        curves.extend_from_slice(&curve_block(int_width));
        let curve_end = curves.len();
        curves.push(0x0f);
        push_ident(&mut curves, "support");
        curves.extend_from_slice(&curve_block_with_endpoint(int_width, [9.0, 0.0, 0.0]));
        curves.extend_from_slice(&[0x10, 0x10]);
        assert_eq!(
            first_curve_patch_layout(&curves, int_width)
                .expect("first owned curve layout")
                .end(),
            curve_end
        );
        assert_eq!(
            final_curve_patch_layout(&curves, int_width)
                .expect("final owned curve layout")
                .end(),
            curve_end
        );

        let mut pcurves = Vec::new();
        push_ident(&mut pcurves, "pcurve");
        pcurves.push(0x0f);
        push_ident(&mut pcurves, "auxiliary");
        pcurves.push(0x10);
        pcurves.push(0x0f);
        push_ident(&mut pcurves, "carrier");
        pcurves.extend_from_slice(&pcurve_block(int_width));
        let pcurve_end = pcurves.len();
        pcurves.push(0x0f);
        push_ident(&mut pcurves, "support");
        pcurves.extend_from_slice(&pcurve_block(int_width));
        pcurves.extend_from_slice(&[0x10, 0x10]);
        assert_eq!(
            super::final_pcurve_patch_layout(&pcurves, int_width)
                .expect("final owned pcurve layout")
                .control_end(),
            pcurve_end
        );
    }
}

#[test]
fn surface_cache_decodes_in_both_integer_widths() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let block = surface_block(int_width);
        let surface = decode_surface_cache(&block)
            .unwrap_or_else(|| panic!("surface cache at width {int_width}"));
        assert_eq!((surface.u_degree(), surface.v_degree()), (1, 1));
        assert_eq!((surface.u_count(), surface.v_count()), (2, 2));
        assert!(final_surface_patch_layout(&block, int_width).is_some());
        let other_width = match int_width {
            RefWidth::Four => RefWidth::Eight,
            RefWidth::Eight => RefWidth::Four,
        };
        assert!(final_surface_patch_layout(&block, other_width).is_none());
    }
}

#[test]
fn token_curve_cache_ignores_nested_support_scope() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exact_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [2.0, 0.0, 0.0]));
        bytes.push(0x10);
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [7.0, 0.0, 0.0]));
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let curve = curve_cache(&tokens)
            .unwrap_or_else(|| panic!("owned curve cache at width {int_width}"));

        assert!((curve.control_points()[1].x - 70.0).abs() < f64::EPSILON);
    }
}

#[test]
fn procedural_curve_cache_ignores_nested_support_scope() {
    let asm_decode_arena = cadmpeg_core::decode::DecodeArena::new();
    let (asm_decode_ctx, _) = cadmpeg_core::decode::DecodeContext::from_root_bytes(
        &[],
        &asm_decode_arena,
        &cadmpeg_core::decode::DecodePolicy::default(),
    )
    .expect("test decode context");
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "spring_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [2.0, 0.0, 0.0]));
        bytes.push(0x10);
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [7.0, 0.0, 0.0]));
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let decoded = procedural_curve_resolving_refs(
            &asm_decode_ctx,
            &tokens,
            &test_table(&bytes, int_width).expect("valid single-record byte fixture"),
        )
        .transpose()
        .expect("decode the procedural curve")
        .unwrap_or_else(|| panic!("procedural curve at width {int_width}"));

        assert!((decoded.curve.control_points()[1].x - 70.0).abs() < f64::EPSILON);
    }
}

#[test]
fn procedural_curve_with_only_nested_cache_is_withheld() {
    let asm_decode_arena = cadmpeg_core::decode::DecodeArena::new();
    let (asm_decode_ctx, _) = cadmpeg_core::decode::DecodeContext::from_root_bytes(
        &[],
        &asm_decode_arena,
        &cadmpeg_core::decode::DecodePolicy::default(),
    )
    .expect("test decode context");
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "spring_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block(int_width));
        bytes.push(0x10);
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");

        assert!(procedural_curve_resolving_refs(
            &asm_decode_ctx,
            &tokens,
            &test_table(&bytes, int_width).expect("valid single-record byte fixture")
        )
        .transpose()
        .expect("decode the procedural curve")
        .is_none());
    }
}

#[test]
fn token_surface_cache_ignores_later_nested_support_scope() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "off_spl_sur");
        bytes.extend_from_slice(&surface_block_with_x_offset(int_width, 5.0));
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&surface_block_with_x_offset(int_width, 9.0));
        bytes.push(0x10);
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let surface = surface_cache(&tokens)
            .unwrap_or_else(|| panic!("owned surface cache at width {int_width}"));

        assert!((surface.poles()[0].x - 50.0).abs() < f64::EPSILON);
    }
}

/// `docs/formats/asm.md` states construction-cache ownership: "the outer
/// non-`ref` procedural subtype owns the record's solved curve or surface
/// cache". Two cache-bearing owned scopes name no single owner, so the record
/// states no cache and every route that reads one refuses. The record's own
/// stream is not a second answer.
#[test]
fn two_cache_bearing_owned_scopes_state_no_record_cache() {
    let asm_decode_arena = cadmpeg_core::decode::DecodeArena::new();
    let (asm_decode_ctx, _) = cadmpeg_core::decode::DecodeContext::from_root_bytes(
        &[],
        &asm_decode_arena,
        &cadmpeg_core::decode::DecodePolicy::default(),
    )
    .expect("test decode context");
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exact_int_cur");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [2.0, 0.0, 0.0]));
        bytes.push(0x10);
        bytes.push(0x0f);
        push_ident(&mut bytes, "exact_int_cur");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [7.0, 0.0, 0.0]));
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        assert!(crate::nurbs::toks::cache_scope(&tokens).is_none());
        assert!(curve_cache(&tokens).is_none());
        assert!(procedural_curve_resolving_refs(
            &asm_decode_ctx,
            &tokens,
            &test_table(&bytes, int_width).expect("valid single-record byte fixture")
        )
        .transpose()
        .expect("decode the procedural curve")
        .is_none());

        let mut surfaces = vec![0x0f];
        push_ident(&mut surfaces, "off_spl_sur");
        surfaces.extend_from_slice(&surface_block_with_x_offset(int_width, 5.0));
        surfaces.push(0x10);
        surfaces.push(0x0f);
        push_ident(&mut surfaces, "off_spl_sur");
        surfaces.extend_from_slice(&surface_block_with_x_offset(int_width, 9.0));
        surfaces.push(0x10);

        let surface_tokens =
            lex_test_span(&surfaces, int_width).expect("valid single-record byte fixture");
        assert!(crate::nurbs::toks::cache_scope(&surface_tokens).is_none());
        assert!(surface_cache(&surface_tokens).is_none());
    }
}

/// A record that owns a construction whose scopes bear no B-spline marker
/// states no cache: the blocks that remain belong to nested support, source,
/// guide or child fields, which the ownership sentence excludes.
#[test]
fn a_construction_with_no_cache_bearing_owned_scope_states_no_record_cache() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "spring_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block(int_width));
        bytes.push(0x10);
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        assert!(crate::nurbs::toks::cache_scope(&tokens).is_none());
        assert!(curve_cache(&tokens).is_none());
        assert!(surface_cache(&tokens).is_none());
    }
}

/// A record that owns no non-`ref` subtype states no construction, so no
/// construction's ownership rule applies to its blocks and its own stream is
/// the cache span.
#[test]
fn a_record_with_no_construction_carries_its_cache_in_its_own_stream() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let bytes = curve_block(int_width);
        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");

        assert_eq!(crate::nurbs::toks::cache_scope(&tokens), Some(&tokens[..]));
        let curve = curve_cache(&tokens)
            .unwrap_or_else(|| panic!("record-stream curve cache at width {int_width}"));
        assert_eq!(curve.degree(), 1);
    }
}

/// The token-space owned-cache decoder reads the scope, not a raw stream.
/// `subtype_span` is its only source, so the balance the raw walk refuses is a
/// state the argument cannot hold and the decoder states no refusal for it.
#[test]
fn a_token_scope_decodes_the_curve_cache_it_owns_through_the_scope_type() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exact_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [2.0, 0.0, 0.0]));
        bytes.push(0x10);
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [7.0, 0.0, 0.0]));
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let scope = crate::nurbs::toks::subtype_span(&tokens, 0).expect("balanced scope");
        let curve = crate::nurbs::core::owned_curve_cache(scope)
            .unwrap_or_else(|| panic!("owned curve cache at width {int_width}"));

        assert!((curve.control_points()[1].x - 70.0).abs() < f64::EPSILON);
    }
}

/// The byte-space owned-cache decoder reads the scope, not a raw stream.
#[test]
fn a_byte_scope_decodes_the_curve_cache_it_owns_through_the_scope_type() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exact_int_cur");
        bytes.push(0x0f);
        push_ident(&mut bytes, "support");
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [2.0, 0.0, 0.0]));
        bytes.push(0x10);
        bytes.extend_from_slice(&curve_block_with_endpoint(int_width, [7.0, 0.0, 0.0]));
        bytes.push(0x10);

        let scope =
            crate::nurbs::subtypes::subtype_span(&bytes, 0, int_width).expect("balanced scope");
        let curve = crate::nurbs::core::decode_owned_curve_cache_at(scope, int_width)
            .unwrap_or_else(|| panic!("owned curve cache at width {int_width}"));

        assert!((curve.control_points()[1].x - 70.0).abs() < f64::EPSILON);
    }
}

/// The explicit-pcurve decoder reads an `exp_par_cur` scope, not a raw stream.
/// `subtype_span` and `payload_subtype_toks` are its only sources, so the
/// unbalanced stream the free walk refuses is a state the argument cannot hold.
#[test]
fn an_exp_par_cur_scope_decodes_its_own_bs2_field_through_the_scope_type() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut bytes = vec![0x0f];
        push_ident(&mut bytes, "exp_par_cur");
        bytes.extend_from_slice(&pcurve_block(int_width));
        bytes.push(0x10);

        let tokens = lex_test_span(&bytes, int_width).expect("valid single-record byte fixture");
        let scope = crate::nurbs::toks::subtype_span(&tokens, 0).expect("balanced scope");
        let pcurve = crate::nurbs::pcurve::explicit_pcurve_cache(scope)
            .unwrap_or_else(|| panic!("explicit pcurve cache at width {int_width}"));

        assert_eq!(pcurve.degree(), 1);
        assert_eq!(pcurve.knots().as_slice(), [0.0, 0.0, 1.0, 1.0]);
    }
}

/// Each subtype definition contributes one table entry in stream order, so an
/// index at or beyond the table's length names no definition the stream states
/// (`docs/formats/asm.md`). The decoder's decision at such an index: the
/// reference search refuses the stream there and does not go on to the
/// resolvable reference behind it.
#[test]
fn an_unresolvable_subtype_reference_refuses_the_search_behind_it() {
    for int_width in [RefWidth::Four, RefWidth::Eight] {
        let mut definition = vec![0x0f];
        push_ident(&mut definition, "exact_int_cur");
        definition.extend_from_slice(&curve_block(int_width));
        definition.push(0x10);

        let mut references = vec![0x0f];
        push_ident(&mut references, "ref");
        push_int(&mut references, 0x04, 99, int_width);
        references.push(0x10);
        references.push(0x0f);
        push_ident(&mut references, "ref");
        push_int(&mut references, 0x04, 0, int_width);
        references.push(0x10);

        let definition_tokens =
            lex_test_span(&definition, int_width).expect("valid single-record byte fixture");
        let reference_tokens =
            lex_test_span(&references, int_width).expect("valid single-record byte fixture");
        let records = [
            crate::sab::Record {
                index: 0,
                name: String::new(),
                tokens: definition_tokens,
                offset: 0,
                len: 0,
            },
            crate::sab::Record {
                index: 1,
                name: String::new(),
                tokens: reference_tokens.clone(),
                offset: 0,
                len: 0,
            },
        ];
        let table = crate::nurbs::toks::SubtypeTable::from_records(&records);

        // Entry zero is the cache-bearing definition, and the record states
        // `{ref 99}` before `{ref 0}`.
        assert!(table.span(0).is_some());
        assert!(table.span(99).is_none());
        assert!(
            crate::nurbs::core::curve_cache_resolving_refs(&reference_tokens, &table,).is_none(),
            "the unresolvable reference at width {int_width} must refuse the search"
        );
    }
}