Skip to content

Rewrite test-float-parse in Rust #127510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Rewrite test-float-parse in Rust
The existing implementation uses Python to launch a set of Rust-written
binaries. Unfortunately, this is currently broken; it seems that some
updates meant it no longer compiles.

There is also a problem that support for more float types (`f16`,
`f128`) would be difficult to add since this is very specialized to
`f32` and `f64`.

Because of these sortcomings, migrate to a version written in Rust. This
version should be significantly faster; test generators can execute in
parallel, and test cases are chunked and parallelized. This should also
resolve the preexisting "... the worker processes are leaked and stick
around forever" comment.

This change also introduces genericism over float types and properties,
meaning it will be much easier to extend support to newly added types.

`num::BigRational` is used in place of Python's fractions for
infinite-precision calculations.
  • Loading branch information
tgross35 committed Jul 20, 2024
commit 59429e67f91e9db38ea2dca8f2a99ec42aea68d7
9 changes: 5 additions & 4 deletions src/etc/test-float-parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ version = "0.1.0"
edition = "2021"
publish = false

[workspace]
resolver = "1"

[dependencies]
rand = "0.8"
indicatif = { version = "0.17.8", default-features = false }
num = "0.4.3"
rand = "0.8.5"
rand_chacha = "0.3"
rayon = "1"

[lib]
name = "test_float_parse"
55 changes: 55 additions & 0 deletions src/etc/test-float-parse/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Float Parsing Tests

These are tests designed to test decimal to float conversions (`dec2flt`) used
by the standard library.

It consistes of a collection of test generators that each generate a set of
patterns intended to test a specific property. In addition, there are exhaustive
tests (for <= `f32`) and fuzzers (for anything that can't be run exhaustively).

The generators work as follows:

- Each generator is a struct that lives somewhere in the `gen` module. Usually
it is generic over a float type.
- These generators must implement `Iterator`, which should return a context type
that can be used to construct a test string (but usually not the string
itself).
- They must also implement the `Generator` trait, which provides a method to
write test context to a string as a test case, as well as some extra metadata.

The split between context generation and string construction is so that we can
reuse string allocations.
- Each generator gets registered once for each float type. Each of these
generators then get their iterator called, and each test case checked against
the float type's parse implementation.

Some generators produce decimal strings, others create bit patterns that need to
be bitcasted to the float type, which then uses its `Display` implementation to
write to a string. For these, float to decimal (`flt2dec`) conversions also get
tested, if unintentionally.

For each test case, the following is done:

- The test string is parsed to the float type using the standard library's
implementation.
- The test string is parsed separately to a `BigRational`, which acts as a
representation with infinite precision.
- The rational value then gets checked that it is within the float's
representable values (absolute value greater than the smallest number to round
to zero, but less less than the first value to round to infinity). If these
limits are exceeded, check that the parsed float reflects that.
- For real nonzero numbers, the parsed float is converted into a rational using
`significand * 2^exponent`. It is then checked against the actual rational
value, and verified to be within half a bit's precision of the parsed value.
Also it is checked that ties round to even.

This is all highly parallelized with `rayon`; test generators can run in
parallel, and their tests get chunked and run in parallel.

There is a simple command line that allows filtering which tests are run,
setting the number of iterations for fuzzing tests, limiting failures, setting
timeouts, etc. See `main.rs` or run with `--help` for options.

Note that when running via `./x`, only tests that take less than a few minutes
are run by default. Navigate to the crate (or pass `-C` to Cargo) and run it
directly to run all tests or pass specific arguments.
Loading