Nilo

Journal10

Teaching the tests to change

A branching growth of short right-angled twigs rising from the foot of a pale sheet, heavy and dark green at the base and thinning to fine grey at the top

Frost: specks fall one at a time and stick where they first touch, and a growth branches up from the ground. It is 41 lines.

Entry 9 ended with a catch. The tests compare what the compiler makes with saved answers, recorded from the old Dart compiler, which is gone. Any change I made on purpose would fail them, with no way to say “this is right, keep the new answer”. Some saved answers were only a fingerprint of the output, a short code that changes whenever the output does, so a failing test could say that something had changed but not what.

That was first on the list. It was not the small job I expected.

Two commands

show prints what a change to the compiler does to the saved answers, as differences I can read. accept installs the new answers, runs every test, and puts everything back if any test fails.

The old output is not kept in full, so for the “before”, show builds the compiler as it was before the change and runs the two side by side.

Eleven sets of saved answers can be re-recorded this way. Four cannot, on purpose. They record what something outside the compiler does: how numbers are printed, what the maths functions give, how things sort, how compression packs. If the compiler re-recorded those, it would only be agreeing with itself.

A tool that has to be attacked

A tool that saves new right answers is dangerous if it can save a wrong one. So before I trusted it, it was handed to reviewers whose only job was to fool it. They did, again and again.

  • It would save a crash as a right answer.
  • It would save answers during an ordinary test run if a setting was left switched on.
  • It would say every test passed when only some had run.
  • A test input could be edited without anything noticing.
  • A piece that used to build could be recorded as refused, with nothing to mark that as unusual.

Each hole was closed and the tool was attacked again. One planned job became many.

A test can no longer write into the folder of saved answers at all; only accept can install. Every one of the 1,056 test inputs now has a checksum, a fingerprint of its contents, so an edited input is noticed. accept turns away a crash, an answer it cannot read, and a build that says it worked but wrote nothing. And it will not record “built before, refused now” unless I tell it to, explicitly.

Two reviewers of different kinds were used. Each found things the other had missed.

Wrong art, no error

The worst find was in the compiler, not the tool.

Nilo lets me call a function with its arguments named, in any order. The compiler also has a step that works some values out ahead of time, while building, and ships the answers as a small table, one of the things that keeps the file small. That step ignored the names and took the arguments in the order they were written.

plate(100, 100)
random layout
fn f(p: vec, xs: int[]): int = floor(p.y) + xs.len
let k = layout.int(0 ..< 4)
let z = f(xs: [k, 1, 2], p: vec(1, 2))
background(rgb(z, z, z))

k comes from the hash and can only be 0 to 3, so z is worked out for all four while building and shipped as a table. The right answer is 5: the point’s y is 2, and the list holds three things. Built without the table, the background came out rgb(5,5,5). Built with it, rgb(3,3,3). Same piece, different art, no warning.

It needs three things at once: a function the piece writes itself, named arguments written out of their declared order, and a value worked out ahead of time. The old Dart compiler had the same fault, and the Go one copied it faithfully, because copying was the job. A reviewer found it while writing a piece meant to break a different fix.

So every existing piece was checked: 22 pieces and libraries, 353 calls. None was affected. Only one piece, the daily one, shipped a table at all, and it had no such call. Nothing I have made drew the wrong thing. But nothing would have told me if it had.

The fix puts the rule for matching arguments to names in one place, used by both the table step and the part that writes the shipped file, so they cannot drift apart again. A new check builds 53 small pieces both ways, with the table and without, runs them, and compares what they draw with each other and with answers worked out by hand. Run against the compiler as it was, 40 of an earlier 48 failed.

Hidden crashes

The same digging turned up something quieter. In five places, the compiler caught every failure, its own crashes included, and passed it off as an ordinary result. A hidden crash is a wrong answer nobody sees.

Measured across the whole test run and all 180 recorded builds, none of the five ever caught a real crash, so nothing that already works has changed. Now they catch only the failures they are meant to, and a real crash comes through as a crash.

My mistakes

My first go at the hidden crashes broke some valid pieces. Where the compiler used to decide quietly not to bother with a table, it now fell over. A reviewer caught it before it went in, by writing a piece designed to trip it.

And after all that, show did not work in its ordinary mode, run from the real project. Every test of it had used a different mode. I let it in like that, and it was fixed in the next change. A tool can pass every test and still fail the first time it is used for real.

The first real use

Then two of the smaller faults from entry 9 were fixed. A layer that names a library that does not exist now gets a plain message, instead of the compiler falling over. And in the sketchbook, the short made-up names in the built file no longer creep upward with each save.

That second fix changed saved answers, so it was the new tool’s first real job. show listed 10 of the 1,080 sketchbook recordings as changed, each differing only in those made-up names. accept ran every test and installed them. The tool did what it was built for.

CheckResult
The rules and messages of the language641, up from 637
The sketchbook29 of 29
Builds the same, byte for byte180 of 180
Built both ways53, new

Still wrong

All of these turned up during this work. All are older than it, and none is fixed.

  • A function given the same name as a built-in one can draw the wrong art.
  • Names written on built-in functions are quietly ignored. clamp(lo: 1, hi: 9, x: 5) gives 9 where 5 was meant.
  • Arithmetic on the single bits of a number goes wrong when the number is enormous.
  • When the compiler looks for a library, it can wander out of the project folder.
  • A piece that declares an effect of its own still cannot get a table on the SVG target.
  • If accept is cut off at the wrong moment, it can leave new answers in place that were never fully tested.

Next

  • Fix what is still wrong.
  • Run the search side by side, to make it faster.
  • Then the editor support.

The piece at the top

The image at the top comes from this token, built for the svg target: 0x367561714266220e2bfbbb6640f395f41e1bc9d5498b49f38b5a4c2f9f1fcc63

// frost: specks fall, drifting a little to either side, and stick where they first touch the growth
plate(1920, 2400)
use colour v2
random layout, fall, ink

let cell = 9                                    // one square of the grid, in plate units
let cols = 213, rows = 266
let taken = int[cols * rows]                    // 1 where the growth is
let paper = #efe9dd
let deep = [#1d1a18, #1b2a4e, #5a1a1f, #24382e][ink.int(0 ..< 4)]
let ramp = [deep, colour.mix(deep, toward: paper, amount: 0.4), colour.mix(deep, toward: paper, amount: 0.7)]
let lean = layout.float(-0.12 ..< 0.12)         // the same sideways lean for every speck
// the ground that catches the first specks: a short stretch of the foot of the plate
let from = layout.int(70 ..< 120), to = from + layout.int(20 ..< 60)
for x in from ..< to { taken[(rows - 1) * cols + x] = 1 }

var top = rows - 2
let count = 16000
background(paper)
for n in 0 ..< count {
  var x = fall.int(from - 50 ..< to + 50), y = max(top - 10, 1)
  while y < rows - 1 {
    // the squares it could catch on: right, left, and below, which wins
    let below = (y + 1) * cols + x, left = y * cols + x - 1, right = y * cols + x + 1
    var hit = -1
    if taken[right] == 1 { hit = right }
    if taken[left] == 1 { hit = left }
    if taken[below] == 1 { hit = below }
    if hit >= 0 {
      taken[y * cols + x] = 1
      top = min(top, y)
      // a twig from this square to the one it caught on, heavier and darker near the ground
      let at = vec(x, y) * cell, onto = vec(hit % cols, floor(hit / cols)) * cell
      let age = n / count
      polyline([at, onto], stroke: colour.at(ramp, along: age), width: 4.5 - 3 * age, caps: "round")
      break
    }
    // one row down, and perhaps a square to the side
    let side = fall.unit() + lean
    x = clamp(x + (side < 0.33 ? -1 : 0) + (side > 0.67 ? 1 : 0), 1, cols - 2)
    y += 1
  }
}