checksmix 0.3.13

A simplified MMIX simulator - Donald Knuth's hypothetical computer for teaching and learning assembly language programming.
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
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
#![cfg(feature = "cli")]

use std::path::PathBuf;
use std::process::{Command, Output, Stdio};

fn checksmix() -> Command {
    Command::new(env!("CARGO_BIN_EXE_checksmix"))
}

fn mmixasm() -> Command {
    Command::new(env!("CARGO_BIN_EXE_mmixasm"))
}

fn mmixdb() -> Command {
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_mmixdb"));
    // mmixdb assembles before opening its REPL; any invocation that
    // assembles successfully would otherwise block on readline.
    cmd.stdin(Stdio::null());
    cmd
}

fn fixture(name: &str) -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join(name)
}

fn assert_exit_1_names_input(out: &Output, input: &str) {
    assert_eq!(
        out.status.code(),
        Some(1),
        "expected exit 1; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains(input),
        "stderr should name '{input}'; stderr: {stderr}"
    );
    assert!(
        stderr.contains("contributed no source"),
        "stderr should say 'contributed no source'; stderr: {stderr}"
    );
}

// ── check: clean two-file program ────────────────────────────────────────────

#[test]
fn check_clean_two_file_program() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("multi_main.mms"))
        .arg(fixture("multi_lib.mms"))
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "check should succeed; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        out.stdout.is_empty(),
        "check should produce no stdout on success"
    );
}

// ── check: undefined symbol reference ────────────────────────────────────────

#[test]
fn check_undefined_symbol_exits_nonzero() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("undef_ref.mms"))
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "check should fail on undefined symbol"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("NoSuchLabel"),
        "error should name the undefined symbol; stderr: {stderr}"
    );
    // Verify file:line:col format appears in the message
    assert!(
        stderr.contains("undef_ref.mms:"),
        "error should contain the source file name; stderr: {stderr}"
    );
}

// ── check: duplicate :Global symbol across two files ─────────────────────────

#[test]
fn check_duplicate_global_names_both_files() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("dup_global_a.mms"))
        .arg(fixture("dup_global_b.mms"))
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "check should fail on duplicate symbol"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("dup_global_a.mms"),
        "error should mention first-definition file; stderr: {stderr}"
    );
    assert!(
        stderr.contains("dup_global_b.mms"),
        "error should mention redefinition file; stderr: {stderr}"
    );
}

// ── build: produces .mmo; round-trip run succeeds ────────────────────────────

#[test]
fn build_produces_mmo_and_run_succeeds() {
    let tmp_mmo = std::env::temp_dir().join("checksmix_test_hello.mmo");

    // build
    let build_out = checksmix()
        .args(["build", "-o"])
        .arg(&tmp_mmo)
        .arg(fixture("hello.mms"))
        .output()
        .unwrap();
    assert!(
        build_out.status.success(),
        "build should succeed; stderr: {}",
        String::from_utf8_lossy(&build_out.stderr)
    );
    let stdout = String::from_utf8_lossy(&build_out.stdout);
    let printed_path = stdout.trim();
    assert!(
        printed_path.ends_with("checksmix_test_hello.mmo"),
        "build should print the output path; got: {printed_path}"
    );
    assert!(tmp_mmo.exists(), "output .mmo file must exist");

    // run the .mmo via the explicit 'run' subcommand
    let run_status = checksmix().args(["run"]).arg(&tmp_mmo).status().unwrap();
    assert!(run_status.success(), "run of built .mmo should succeed");

    let _ = std::fs::remove_file(&tmp_mmo);
}

// ── run file.mmo: regression – MMO decode path unchanged ─────────────────────

#[test]
fn run_mmo_bare_invocation() {
    let tmp_mmo = std::env::temp_dir().join("checksmix_test_bare_run.mmo");

    // build first so we have a known-good .mmo
    let build_status = checksmix()
        .args(["build", "-o"])
        .arg(&tmp_mmo)
        .arg(fixture("hello.mms"))
        .status()
        .unwrap();
    assert!(build_status.success());

    // run without explicit subcommand
    let run_output = checksmix().arg(&tmp_mmo).output().unwrap();
    assert!(
        run_output.status.success(),
        "bare run of .mmo should succeed"
    );

    // The .mmo load path reports the program, never the loader's own probes.
    let stdout = String::from_utf8_lossy(&run_output.stdout);
    assert!(
        !stdout.contains("Debug:"),
        "loading a .mmo emitted debug output:\n{stdout}"
    );

    let _ = std::fs::remove_file(&tmp_mmo);
}

// ── run a.mms b.mms: multi-source assemble + execute ─────────────────────────

#[test]
fn run_multi_source_mms() {
    let status = checksmix()
        .args(["run"])
        .arg(fixture("multi_main.mms"))
        .arg(fixture("multi_lib.mms"))
        .status()
        .unwrap();
    assert!(status.success(), "run of multi-source .mms should succeed");
}

// ── run: a diagnostic halt exits 1; a Halt trap keeps its own status ────────

#[test]
fn run_illegal_instruction_exits_1() {
    let status = checksmix()
        .args(["run"])
        .arg(fixture("put_rc_halts.mms"))
        .status()
        .unwrap();
    assert_eq!(status.code(), Some(1), "a diagnostic halt (PUT rC) exits 1");
}

#[test]
fn run_halt_trap_exits_with_its_own_255_value() {
    let status = checksmix()
        .args(["run"])
        .arg(fixture("halt_with_255.mms"))
        .status()
        .unwrap();
    assert_eq!(
        status.code(),
        Some(255),
        "TRAP 0,Halt,0 exits with $255, not 1"
    );
}

// ── run all_instructions_test.mms: no leftover debug noise on stderr ─────────

#[test]
fn run_all_instructions_test_has_no_debug_write_byte_noise() {
    let example = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("examples")
        .join("all_instructions_test.mms");
    let out = checksmix().arg(&example).output().unwrap();
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        !stderr.contains("DBG write_byte"),
        "stderr should not contain leftover debug output from write_byte; stderr: {}",
        stderr
    );
}

// ── build/mmixasm: a program with a GREG carries it in the postamble ────────

fn find_lop_post(mmo_data: &[u8]) -> usize {
    mmo_data
        .windows(2)
        .position(|w| w[0] == 0x98 && w[1] == 0x0A)
        .expect("built .mmo must have a lop_post record")
}

/// `checksmix build` and `mmixasm` share one object-code path; each must
/// carry a program's `GREG` value through its postamble.
#[test]
fn each_binarys_build_of_a_program_with_a_greg_carries_it_in_the_postamble() {
    let cases: [(Command, &[&str], &str); 2] = [
        (
            checksmix(),
            &["build", "-o"],
            "checksmix_test_greg_build.mmo",
        ),
        (mmixasm(), &["-o"], "checksmix_test_greg_mmixasm.mmo"),
    ];

    for (mut cmd, args, tmp_name) in cases {
        let tmp_mmo = std::env::temp_dir().join(tmp_name);

        let build_out = cmd
            .args(args)
            .arg(&tmp_mmo)
            .arg(fixture("greg_postamble.mms"))
            .output()
            .unwrap();
        assert!(
            build_out.status.success(),
            "build should succeed; stderr: {}",
            String::from_utf8_lossy(&build_out.stderr)
        );

        let mmo_data = std::fs::read(&tmp_mmo).unwrap();
        let post = find_lop_post(&mmo_data);
        assert_eq!(mmo_data[post + 3], 254, "G must be 254 for one GREG");
        let reg254 = u64::from_be_bytes(mmo_data[post + 4..post + 12].try_into().unwrap());
        assert_eq!(reg254, 1000, "$254 must carry the GREG's value");

        let _ = std::fs::remove_file(&tmp_mmo);
    }
}

// ── run greg_base_address.mms: the two-operand base-address form ─────────────
//
// The one operand form AGENTS.md's corpus rule excuses from
// examples/all_instructions_test.mms (see its "Intentional coverage
// exceptions" block): it needs a GREG holding a base address, which that
// file's harness cannot admit without a register-numbering rework. The
// fixture halts only through `TRAP 0,Halt,X`, whose exit status is `$255`'s
// own value rather than the 1 every other halt exits with, so checking it
// alongside the printed message confirms the run reached the fixture's own
// Pass or Fail branch rather than some other halt.

#[test]
fn run_greg_base_address_fixture_passes() {
    let out = checksmix()
        .args(["run"])
        .arg(fixture("greg_base_address.mms"))
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("All tests passed!"),
        "stdout should report success; stdout: {stdout}"
    );
    assert_eq!(out.status.code(), Some(0), "the fixture halts with $255=0");
}

// ── an input contributing no source: diagnostic, not a panic ─────────────────
//
// resolve_includes trims away a blank segment; an input made entirely of
// such segments collects zero translation units.

#[test]
fn check_empty_file_exits_one() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("empty.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

#[test]
fn mmixasm_empty_file_exits_one() {
    let out = mmixasm().arg(fixture("empty.mms")).output().unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

#[test]
fn mmixdb_empty_file_exits_one() {
    let out = mmixdb().arg(fixture("empty.mms")).output().unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

#[test]
fn check_whitespace_only_file_exits_one() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("whitespace_only.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "whitespace_only.mms");
}

#[test]
fn mmixasm_whitespace_only_file_exits_one() {
    let out = mmixasm()
        .arg(fixture("whitespace_only.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "whitespace_only.mms");
}

#[test]
fn mmixdb_whitespace_only_file_exits_one() {
    let out = mmixdb()
        .arg(fixture("whitespace_only.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "whitespace_only.mms");
}

#[test]
fn check_include_of_empty_file_exits_one() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("include_empty.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "include_empty.mms");
}

#[test]
fn mmixasm_include_of_empty_file_exits_one() {
    let out = mmixasm()
        .arg(fixture("include_empty.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "include_empty.mms");
}

#[test]
fn mmixdb_include_of_empty_file_exits_one() {
    let out = mmixdb().arg(fixture("include_empty.mms")).output().unwrap();
    assert_exit_1_names_input(&out, "include_empty.mms");
}

// ── checksmix run/build on an empty input ─────────────────────────────────────

#[test]
fn run_empty_file_exits_one_without_doubled_prefix() {
    let out = checksmix()
        .args(["run"])
        .arg(fixture("empty.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert_eq!(
        stderr.matches("Error: ").count(),
        1,
        "run_mms prefixes the error itself; embedding a second prefix in the \
         guard would double it; stderr: {stderr}"
    );
}

#[test]
fn build_empty_file_exits_one() {
    let out = checksmix()
        .args(["build"])
        .arg(fixture("empty.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

// ── one empty input among others: per-path, not aggregate ────────────────────
//
// hello.mms alone is a valid, self-contained program (multi_main.mms is not:
// it references :Lib, defined only in multi_lib.mms, so checking it alone
// fails on an undefined symbol and would mask what this case tests).

#[test]
fn check_empty_file_among_others_still_exits_one() {
    let out = checksmix()
        .args(["check"])
        .arg(fixture("empty.mms"))
        .arg(fixture("hello.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

#[test]
fn mmixasm_empty_file_among_others_exits_one_without_banner() {
    let out = mmixasm()
        .arg(fixture("empty.mms"))
        .arg(fixture("hello.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        !stdout.contains("Assembling 0 inputs:"),
        "the per-path guard fires before the input-count banner; stdout: {stdout}"
    );
}

#[test]
fn mmixdb_empty_file_among_others_still_exits_one() {
    let out = mmixdb()
        .arg(fixture("empty.mms"))
        .arg(fixture("hello.mms"))
        .output()
        .unwrap();
    assert_exit_1_names_input(&out, "empty.mms");
}

// ── regression: legitimate zero-instruction inputs stay unaffected ───────────
//
// The guard keys off resolved units, never instruction count: a comment-only
// file and a definitions-only module both contribute source and must keep
// exiting 0 on `check`.

#[test]
fn check_comment_only_file_exits_zero() {
    let status = checksmix()
        .args(["check"])
        .arg(fixture("comment_only.mms"))
        .status()
        .unwrap();
    assert!(status.success(), "a comment-only file is a valid program");
}

#[test]
fn check_defs_only_file_exits_zero() {
    let status = checksmix()
        .args(["check"])
        .arg(fixture("defs_only.mms"))
        .status()
        .unwrap();
    assert!(
        status.success(),
        "a definitions-only module has zero instructions and is still valid"
    );
}

#[test]
fn build_defs_only_file_fails_on_instruction_count_not_the_source_guard() {
    let out = checksmix()
        .args(["build"])
        .arg(fixture("defs_only.mms"))
        .output()
        .unwrap();
    assert_eq!(out.status.code(), Some(1));
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("no instructions"),
        "build's existing zero-instruction guard must still fire; stderr: {stderr}"
    );
    assert!(
        !stderr.contains("contributed no source"),
        "defs_only.mms resolves to a unit; the new guard must not fire; stderr: {stderr}"
    );
}