Skip to content

Property-Based testing tool for futhark (solving #2163)#2405

Draft
Black-Speedy wants to merge 40 commits intodiku-dk:masterfrom
Black-Speedy:pbt
Draft

Property-Based testing tool for futhark (solving #2163)#2405
Black-Speedy wants to merge 40 commits intodiku-dk:masterfrom
Black-Speedy:pbt

Conversation

@Black-Speedy
Copy link
Copy Markdown

This is not the finished work but it's a start. We have made a basic integration of a separate property based testing tool into futhark test. The basic use inside of a futhark program is:

entry bar (size: i64) (seed: i32): testType

#[prop(gen(bar), shrink(auto)]
entry foo (x: testType) : bool

-- possible shrinker with type:
entry baz (x: testType) (strat: i32) : (testType, i8)

-- if you have an opaque type or you want custom pretty printing you can provide it
entry fooz (x: testType) : []u8

The attribute can take a size, seed, and pretty printer as well. So a fully custom attribute could be:
#[prop(gen(bar), shrink(baz), pprint(fooz), size(10), seed(12347)].
The generator, property, and shrinker must be entry points or it will not work. Every attribute is assigned to a property and is specific to each property, they do not share anything. For the shrinker you can write auto to use our auto shrinking strategy or omit it completely which will not shrink. If you have created your own shrinker use its function name instead.

Note that currently it is not fully working perfectly together with the regular test. If the regular test fails on any test it will overwrite some of our printed outputs.

Very ugly but a start, also our prints dont work well with the current
test printer.
…working)

Currently it makes all test cases fail which is very bad
…ectory

It now somewhat works, however it is a little ugly. To TestResult we added PropOut which needs to be explictly handled in runTestM, pureTestResults, and runTests. We should probably also move our logic of printing to stdout and using capture to get it as a string to just using a string instead or find another strategy
@Black-Speedy Black-Speedy changed the title Pbt Property-Based testing tool for futhark (solving #2163) Mar 24, 2026
@athas athas marked this pull request as draft March 25, 2026 09:35
Black-Speedy and others added 10 commits March 25, 2026 17:17
We still need to find out how to add properties to runs so that we are counted. For now we have just added 2 (which is bad but works for our use since we always have a passing and failing while testing)
Now it is a requirement to in addition to the attribute also declare a test field meaning:
-- ==
-- property: some_prop

Additionally if you have multiple tests in the form of multiple attributes for a single property you additionally have to write a num field in the test block with the number of tests. If this is not done it will not complain but the result will not be entirely correct. Like the regular test if the entrypoint/propertyname is incorrect only a single of the test cases will count as a failure (could probably update this if needed/wanted)
Now

-- ==
-- property: prop_simple_fail

will match against something like:

#[prop(gen(gen_simple), shrink(auto))]
entry prop_simple_fail (x: i8) : bool =

but

-- ==
-- property: prop_simple_fai

will not match and if no atttributed function of that name exists, it will count as a fail.
…unit tests and prop tests were in the same file

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
@athas
Copy link
Copy Markdown
Member

athas commented Apr 8, 2026

I would like to see the tests related to testing property-based testing to a directory tests_property in the root of the repository. Long term, we will of course use property-based tests as part of the normal integration test suite in tests, but it is good to have tests for the testing tool itself as well.

athas and others added 11 commits April 8, 2026 13:48
Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
…te + some cleanup

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
… prettier, moving them to messages instead of errors/fail

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Still needs some cleanup but now extra info is provided if a property hasn't finished before timout is reached

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Would probably benefit from some color, can maybe do that later. This
error should be relatively rare anyway.
Have changed a large portion of fails to error messages but code still
needs an overall look to finish and also make code easier to read.
Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Black-Speedy and others added 8 commits April 16, 2026 16:06
…ailures and user failures. Also moved tests.

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
Changed tactic to be a random number that can be used for whatever the user feels like. The return type no longer has a status, the shrinker logic will automatically stop after a property no longer fails a consecutive number of times (default is 5 at the moment) which can be set from cli with num-tries.

Co-Authored-By: Simon August Mørk <100700440+simoerk@users.noreply.github.com>
… script.

We should take a greater look at the array test, as they were harder to
change since more is going on. It would probably be better to make the
array tests simpler. Also look into making the shrinker library so that
we can make fewer changes in case of protocol change later.
@athas athas mentioned this pull request Apr 28, 2026
Comment thread src/Futhark/CLI/Test.hs
-- Save to file helper functions
propToFile :: FilePath -> [TestResult] -> IO ()
propToFile testFile results = do
let fileName = testFile <> ".propResult"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: Futhark convention is for all files to use snake_case, except where other languages (such as Haskell) demands otherwise.

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.

3 participants