Skip to content

Check test images against stored fuzzy fingerprints - #26

Merged
certik merged 3 commits into
mainfrom
fingerprint-tests
Sep 14, 2026
Merged

certik merged 3 commits into
mainfrom
fingerprint-tests

Conversation

@certik

@certik certik commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

fpm test (and pixi run test-flang / test-lfortran) now checks every PNG and GIF frame written by tests/test_plots.f90 against a 4096-bit fingerprint stored in tests/fingerprints.txt (150 kB). No Python, matplotlib or reference images are needed.

How it works (tests/test_fingerprint.f90, integer arithmetic only): ink is spread onto a 160×128 cell grid, a discrete Laplacian is taken, and each of the 64 tiles gets a 64-bit word. Each bit is the parity of a sparse random projection divided by a fixed period. A case fails when any word differs in more than 10 bits, and the report names the image region. tests/test_image_io.f90 decodes the PNG and GIF files, so the encoders are covered too.

Measured locally (macOS):

  • gfortran (fpm test) and flang: all 144 images identical to the stored fingerprints.
  • CI (Linux flang), against the fingerprints generated on macOS: largest difference 5 bits (bar3d), limit 10.
  • gfortran -O3 vs -O0: 105 images differ by one intensity level on up to 21,000 pixels, which moves no word by more than 6 bits.
  • A tick moved by 1 px, a changed tick label, a one-letter title typo or a changed line colour moves a word by 18–40 bits. A tick moved by 0.25 px or less passes.

Known failures this exposes:

  • LFortran 0.64 miscompiled legend_labels (a constant substring of a character component with a default initializer was read from the initializer, so a line labelled _hidden appeared in the legend), reported as Wrong value for constant substring of character component with default initialization lfortran/lfortran#12788. Fixed in LFortran 0.66.0, which this PR now uses; the Test with LFortran CI step runs and passes (largest difference 5 bits).
  • fpm test --profile release (gfortran -O3) fails 6 cases (clabel, quiver3d, trisurf, mosaic, subplots_2x2, subplots_shared). These are numerical-robustness bugs in fplot (tick labels 0.0 vs 0, contour label placement) and will be fixed separately.

After an intended change of the pictures: fpm test -- --update.

LFortran 0.66.0 and pixi environments: conda-forge builds LFortran 0.66.0 against clang 21 and flang 22 against clang 22, which cannot be installed together. LFortran now has its own pixi environment (lfortran), and its tasks are defined only there, so pixi run test-lfortran and pixi run fpm-lfortran select it by themselves. The default environment keeps flang; the only packages it loses are LFortran and its runtime dependencies. CI installs both environments.

fpm test now checks every PNG and every GIF frame the tests write against
a fingerprint stored in tests/fingerprints.txt, so the test suite catches
changed pictures without Python, matplotlib or reference images.

Each image becomes 64 words of 64 bits, one word per tile, computed with
integer arithmetic only: ink splatted onto a 160x128 cell grid, a discrete
Laplacian, then sparse random projections quantized with a period so that
a bit flips in proportion to how much its projection changed. A case fails
when any word differs in more than 10 bits. Compiler rounding noise (one
intensity level on thousands of antialiased pixels) moves a word by at most
6 bits; a tick moved by 1 px, a missing label or a typo in a title move it
by 18 to 40.

Regenerate the fingerprints after an intended change with
fpm test -- --update.
@certik

certik commented Sep 13, 2026

Copy link
Copy Markdown
Member Author

@jalvesz let me know what you think. I fixed LFortran, but must make a new release and get it into conda, so that we can use it here.

@jalvesz

jalvesz commented Sep 13, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @certik I think this looks pretty good. I wonder if instead of a single file containing all the fingerprints, there should be one per image ? That could facilitate running the tests in parallel

@certik

certik commented Sep 13, 2026

Copy link
Copy Markdown
Member Author

The finger print file is only read, right? So it can run in parallel?

But we could save the fingerprint into the test file itself, that's another option. And automatically update with a script.

LFortran 0.66.0 fixes lfortran/lfortran#12788, which made legend_labels
fail its fingerprint, so the LFortran test step runs again.

conda-forge builds LFortran 0.66.0 against clang 21 and flang 22 against
clang 22, and the two cannot be installed together. LFortran therefore
gets its own pixi environment; its tasks are defined only there, so
`pixi run test-lfortran` and `pixi run fpm-lfortran` select it without
naming it. The default environment keeps flang and loses nothing else.
@certik

certik commented Sep 14, 2026

Copy link
Copy Markdown
Member Author

I tested this by hand locally and it seems to work. Omitting a label or changing a label from y to x fails the tests as it should. But changing compilers or compiler options (Debug/Release) shows differences in the fingerprint, but below the threshold, so tests pass. We can further fine tune it as needed, but it seems to work already quite well.

@certik
certik merged commit 525ced0 into main Sep 14, 2026
1 check passed
@certik
certik deleted the fingerprint-tests branch September 14, 2026 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants