Can Your Ada and SPARK Tests Notice Changes to the Code? ── Introducing AdaMutant

· Updated: · · Ada, SPARK, Mutation Testing, Testing, GNATprove, Alire, AdaMutant

Post on X Submit to Hacker News Share on Reddit Share on LinkedIn Share on WhatsApp
Revision history (first version, published Nov 17, 2026)
First published

Even when all your tests pass, those tests will not necessarily notice mistakes in the code. A test that does not check boundary values still passes when >= is rewritten as >.

AdaMutant is a tool for checking exactly this. It automatically makes small changes to Ada and SPARK source code and checks whether your tests or GNATprove proofs can detect those changes. KomuraSoft publishes it under the Apache License 2.0.1

This article introduces what AdaMutant does, together with the results of actually running its bundled samples. The environment was Ubuntu 24.04, GNAT 14.2.0 and 4 cores, and the AdaMutant version was 0.1.0. The description of the Alire crate says it runs on Linux, macOS and Windows, but for this article I tried it only on Linux.2

What mutation testing is

Mutation testing automatically creates slightly changed versions of the production code (mutants), runs the tests, and looks at whether they fail. If a test fails, the tests noticed that change. If they all pass, they did not. I wrote about the idea in detail in Testing Your Tests with Mutation Testing.

.NET has a tool called Stryker.NET. AdaMutant brings the same idea to Ada and SPARK.

The flow of mutation testingShows the flow of putting a mutant into the original code, building it and running the tests, where a failing test means the change was detected and all tests passing means it was not detected.A test failsAll passMake a small change to the original codeBuild and run the testsKilled: the change was detectedSurvived: the change was not detected

Figure 1: A mutant that makes a test fail is counted as detected.

In the diagram a solid line marks a relation that always holds and a dashed line marks a conditional one (the conditions are given per relation on the detail page). The full list of relations (4 in total, with evidence and certainty) and the definitions of the main concepts are collected on the knowledge map detail page (in Japanese). Data: JSON-LD / Turtle

AdaMutant’s features

The README lists the following features.1

  • It has a built-in lexer for Ada up to Ada 2022, so it needs no extra dependencies such as libadalang
  • In addition to test failures, it can detect mutants through failed GNATprove proofs
  • It builds mutants together in one build and switches which mutant is active when the tests run
  • It creates several copies of the project (sandboxes) to run in parallel, and stops tests that do not finish in time
  • You can specify a git revision and check only the mutants on the lines you changed
  • It outputs the results as a console table, as a standalone HTML file and as JSON

It creates 10 kinds of mutants: arithmetic operators (such as swapping + and -), comparison operators (such as turning < into <=), logical operators, unary operators, Boolean values, membership tests (in and not in), conditions (turning a condition into True or False), statement removal, numeric literals and string literals.

The two ways AdaMutant detects mutantsShows that AdaMutant first runs the tests on a mutant, a failing test means it is detected, a mutant whose tests pass is then proved, a failing proof means it is detected, and if both pass it survives.FailPassFailPassMutantRun the testsKilledProve with GNATproveSurvived

Figure 2: When both tests and proofs are configured, only mutants whose tests pass are proved.

Getting and building it

You need a GNAT that supports Ada 2022 and GPRbuild. If you want to detect mutants with proofs, you also need GNATprove.

If you use Alire, fetch the crate from the index and build it. The crate is named mutant, and the executable it produces is named adamutant.2

alr get mutant
cd mutant_0.1.0_*
alr build

On my machine, these steps worked from fetching through building. The build took 43 seconds.

You can also clone the repository and build it with make.

git clone https://github.com/gomurin0428/AdaMutant
cd AdaMutant
make build

Either way, you get bin/adamutant. The rest of this article assumes this executable is on your path.

Two ways to get AdaMutantShows that you can fetch the crate with Alire and build it, or clone the repository and build it with make, and that either way produces the adamutant executable.Fetch with Alire: alr get mutantalr buildClone the repository: git clonemake buildExecutable bin/adamutant

Figure 3: Both methods produce the same executable.

Running the sample

The repository’s examples/calculator is a sample whose tests deliberately do not check boundary values. Its configuration file adamutant.toml is as follows.

mutate = ["src/**/*.adb", "src/**/*.ads"]
build = "gprbuild -p -q -P calculator_tests.gpr"
test = '"bin/test_calculator"'
concurrency = 2
sandbox_exclude = [".git", "obj", "bin"]

[thresholds]
high = 80
low = 60
break = 50

mutate is the files to change, build is the build command, and test is the test command. If the test command returns a nonzero exit code, that mutant counts as detected.

Here is the end of the output from running adamutant run in this folder.

File               | Score % | Killed | Timeout | Survived | No cov | Compile | Ignored
---------------------------------------------------------------------------------------
All files          |   77.50 |     29 |       2 |        9 |      0 |       0 |       0
src/calculator.adb |   75.00 |     25 |       2 |        9 |      0 |       0 |       0
src/calculator.ads |  100.00 |      4 |       0 |        0 |      0 |       0 |       0

Mutation score: 77.50% (31 detected, 9 undetected, 0 compile errors, 0 ignored) in 12 s

Of the 40 mutants, 29 were detected because a test failed, 2 timed out, and 9 survived. It took 12 seconds.

Results for the 40 mutants of the calculator sampleShows that of the 40 mutants, 29 were detected because a test failed, 2 timed out and were treated as detected, and 9 survived.40 mutantsDetected: 31Undetected: 9 (Survived)29: a test failed (Killed)2: ran out of time (Timeout)

Figure 4: 31 mutants were detected, and 9 were not.

The 2 that timed out are mutants that change I := I + 1; inside a loop. One removes this statement and the other turns I + 1 into I + 0; both make the loop never end. AdaMutant sets a limit from how long the tests took before the change, stops the test there, and counts the mutant as detected.

The reports are written to .adamutant/reports. In the HTML report (mutation-report.html), the source lines are colored. The image below is part of the report for this sample. Green lines have detected mutants and no undetected ones. Red lines are lines with surviving mutants. Clicking a line opens the list of mutants on that line.

The AdaMutant HTML report. In the source of calculator.adb, lines 10, 19, 21, 29, 31 and 32 are shown in red, and the other lines that have mutants are shown in green

Reading the surviving mutants

Of the 9 survivors, 6 are in Grade, a function that returns a grade for a score.

function Grade (Score : Natural) return String is
begin
   if Score >= 80 then
      return "A";
   elsif Score >= 60 then
      return "B";
   end if;
   return "C";
end Grade;

The tests check only two things: that Grade (95) is "A" and that Grade (10) is "C". They check neither the boundaries at 80 and 60 nor the case that gives "B". So the tests pass whether >= is changed to > or 80 is changed to 81.

I added four tests that check the boundaries.

Check (Grade (80) = "A", "Grade A boundary");
Check (Grade (79) = "B", "Grade B upper");
Check (Grade (60) = "B", "Grade B boundary");
Check (Grade (59) = "C", "Grade C upper");

Running it again, the score rose to 92.50% and 3 mutants survived. These remaining 3 are of a different nature.

The first is a mutant that changes if A > B then in Max to if A >= B then.

function Max (A, B : Integer) return Integer is
begin
   if A > B then
      return A;
   else
      return B;
   end if;
end Max;

When A and B are equal, the original code returns B and the mutant returns A. The value is the same. No test you write can tell these two apart. A mutant like this, which behaves the same as the original code, is called an equivalent mutant.

The other 2 are comparisons in Clamp.

function Clamp (X, Low, High : Integer) return Integer is
begin
   if X < Low then
      return Low;
   elsif X > High then
      return High;
   end if;
   return X;
end Clamp;

The one that changes X > High to X >= High is equivalent for the same reason as in Max. When X equals High, both return the same value.

The one that changes X < Low to X <= Low looks the same at first glance, but it is not equivalent. The result changes for calls where Low is greater than High. For Clamp (5, 5, 3), 5 < 5 is false in the original code, so it moves on to the next condition, and since 5 > 3 is true it returns 3. The mutant returns 5, because 5 <= 5 is true.

How the original code and the mutant differ when Clamp gets a lower bound greater than its upper boundShows that when Clamp is given 5 with a lower bound of 5 and an upper bound of 3, the first condition is false in the original code and the next one is true, so it returns the upper bound 3, while in the mutant with the changed comparison the first condition is true and it returns the lower bound 5.Call Clamp (5, 5, 3)Original code: the first condition is falseMutant: the first condition is trueThe next condition is true, so it returns the upper bound 3Returns the lower bound 5

Figure 5: For calls where the lower bound is greater than the upper bound, the original code and the mutant give different results.

What this mutant points to is less a missing test than a place where the specification has not been decided. Clamp has no precondition that Low is less than or equal to High. Neither the code nor the tests tell you whether calls with the lower and upper bounds reversed are allowed, or, if they are, what they should return.

Here, I treated the current behavior (returning the upper bound) as the specification and added one test.

Check (Clamp (5, 5, 3) = 3, "Clamp inverted range");

The score became 95.00%, and the only survivors were the 2 equivalent mutants. If you decide not to allow reversed calls, another option is to add a precondition and exclude this mutant as equivalent.

Equivalent mutants can be excluded with a comment.

--  adamutant:disable-next-line EqualityOperator: equivalent mutant
if A > B then

The fourth row of the table below is the result of running it with this comment on the two places. The comment excludes all comparison operator mutants on that line. There are 2 on each line, so 4 were excluded. 2 of them had been detected before they were excluded.

Run Score Detected Survived Excluded
Sample as is 77.50% 31 9 0
Added four boundary tests 92.50% 37 3 0
Added a test for reversed lower and upper bounds 95.00% 38 2 0
Excluded equivalent mutants with comments 100.00% 36 0 4
Sorting the surviving mutantsShows reading a surviving mutant and checking whether there is an input where it behaves differently from the original code; if there is, decide the specification and add a test that checks the difference, and if not, exclude it with a comment as an equivalent mutant.YesNoRead the surviving mutantIs there an input where it behaves differently from the original code?Decide the specification and add a test that checks the differenceExclude it with a comment as an equivalent mutant

Figure 6: Survivors split into those that point to missing tests or an incomplete specification, and equivalent mutants.

A person decides which it is. Even while writing this article, I first mistook the Clamp one for an equivalent mutant. What each surviving mutant points to is more useful than the score.

Detecting mutants with proofs

In SPARK code, GNATprove proofs can detect mutants instead of tests. examples/spark_counter is a sample with no tests that uses only proofs.

function Saturating_Add (A, B : Count) return Count
with Post =>
  (if A + B <= Max then Saturating_Add'Result = A + B
   else Saturating_Add'Result = Max);

In the configuration, write prove instead of test. If you write both test and prove, only the mutants whose tests pass are proved. The following is an excerpt from the configuration file.

mutate = ["src/**/*.adb"]
build = "gprbuild -p -q -c -P counters.gpr"
prove = "gnatprove -P counters.gpr --level=1 --report=fail --checks-as-errors=on -j1"

When run, proofs detected 7 of the 8 mutants, and 1 survived. The score was 87.50%, and it took 25 seconds.

The body is as follows.

function Saturating_Add (A, B : Count) return Count is
begin
   if A + B > Max then
      return Max;
   else
      return A + B;
   end if;
end Saturating_Add;

Changing return A + B to return A - B makes the postcondition unprovable. Because GNATprove fails, this mutant counts as detected. Since the postcondition determines the result value, most changes to the body show up as proof failures. Of the 7 detected mutants, 5 failed to prove the postcondition, and the other 2 failed only on the check that the value fits within the range of its type.

The one that survived changes if A + B > Max then to if A + B >= Max then. When A + B equals Max, the original code returns A + B and the mutant returns Max. The value is the same, so this is an equivalent mutant.

By default, it does not create mutants that change the contracts themselves (Pre and Post). It creates them if you set mutate_contracts = true in the configuration.

The flow of detecting mutants with proofsShows putting a mutant into the body and running GNATprove, where a failing proof means it is detected and a passing proof means it survives.The proof failsThe proof passesPut a mutant into the bodyRun GNATproveKilled: the proof detected the changeSurvived

Figure 7: If the contract determines the result value, changes to the body show up as proof failures.

Building mutants together

Mutation testing takes time. If you rebuild for each mutant, you need as many builds as there are mutants.

AdaMutant puts the mutants that can be combined into one source and builds them in one build. The README calls this approach mutant schemata.1 The following is part of the source created while running the sample.

function Add (A, B : Integer) return Integer is
begin

case ADAMUTANT_ACTIVE_1 is
when 1 =>
return A - B;
when others =>
return A + B;
end case;

end Add;

ADAMUTANT_ACTIVE_1 is a function that returns the value of the environment variable ADAMUTANT_MUTANT_ID. By changing this environment variable when running the tests, you can switch the active mutant without rebuilding. When the value is 0, no mutant is active. Mutants of conditions become case expressions.

if (case ADAMUTANT_ACTIVE_1 is
when 2 => (
True
),
when 3 => (
False
),
when 4 => (
A >= B
),
when 5 => (
A <= B
),
when others => (
A > B
)) then

In the calculator sample, 36 of the 40 mutants were combined into a single build: 32 mutants of simple statements and conditions, and 4 mutants of the expression function Is_Even. The remaining 4 were built one at a time: 2 that change the initial value of a declaration and 2 that remove a statement. Including the build before any change, there were 6 builds in total.

There are reasons why some mutants are not combined. According to the README, mutants inside declarations may need static expressions, so they cannot be switched at run time. Statement removals are not combined because, when combined, optimization can make a mutant behave the same as the original program, and something that should have been detected could be missed.1

Mutants built together and mutants built one at a timeShows that mutants of simple statements and conditions are combined into one source, built once and switched with an environment variable during testing, while mutants inside declarations and statement removals are built one at a time.Check where the mutant isSimple statements, conditions, expression functionsInside declarations, statement removal, and so onCombine into one source and build onceSwitch mutants with an environment variable and testBuild and test one at a time

Figure 8: Mutants that can be combined need only one build.

Checking only the lines changed in a pull request

Checking every mutant every time in a large project is heavy work. With --since, it checks only the mutants on the lines changed since the merge base of the specified revision and HEAD.

As a test, I turned the sample into a git repository and, on a branch, rewrote the 60 in Grade as 70.

adamutant run --since main
File               | Score % | Killed | Timeout | Survived | No cov | Compile | Ignored
---------------------------------------------------------------------------------------
All files          |   40.00 |      2 |       0 |        3 |      0 |       0 |      31
src/calculator.adb |   40.00 |      2 |       0 |        3 |      0 |       0 |      31

Mutation score: 40.00% (2 detected, 3 undetected, 0 compile errors, 31 ignored) in 1 s
error: the mutation score is below the break threshold 50%

Only the 5 mutants on the one changed line were run, and the other 31 in the same file were excluded. The unchanged calculator.ads was not read, and its 4 mutants do not even appear in the list. It took 1 second.

The score was 40%, below the configured break threshold (50%), so the exit code was 2. The exit codes are 0 for success, 1 for an error and 2 for below the threshold. If you run it in CI, you can stop pull requests that lack tests for the lines they change. The README says that GitHub Actions needs the history, so you should specify fetch-depth: 0 for actions/checkout.

The flow of checking only the changed linesShows finding the changed lines from the diff since the merge base of the specified revision and HEAD, running only the mutants on those lines, excluding the rest, and ending with exit code 2 if the score is below the threshold.At or above the thresholdBelow the thresholdTake the diff from the merge baseRun only the mutants on the changed linesCompute the score from the changed lines onlyExit code 0Exit code 2

Figure 9: The score is computed from the mutants on the changed lines only, and the threshold can stop CI.

Statuses and the score

A mutant has one of seven statuses.1

Status Meaning Treatment in the score
Killed A test or proof failed Detected
Timeout The tests did not finish in time Detected
Survived Both the tests and the proof passed Undetected
NoCoverage The build does not compile that file Undetected
CompileError The mutant could not be built Excluded
RuntimeError The command could not be started, and so on Excluded
Ignored Excluded by a comment or similar Excluded

The score is the number detected divided by the total of detected and undetected.

Computing the score in the run that excluded equivalent mutantsShows that in the run where equivalent mutants were excluded with comments, the 34 Killed and 2 Timeout mutants make 36 detected and 0 are undetected, so 36 divided by 36 gives 100 percent, and the 4 excluded mutants are not part of the calculation.34 Killed and 2 TimeoutDetected: 36Survived and NoCoverage: 0Undetected: 036 / (36 + 0) = 100.00%Ignored: 4Not included in the calculation

Figure 10: The 4 excluded mutants are in neither the denominator nor the numerator.

Things to know before using it

Of the limitations and assumptions written in the README, these are the ones worth knowing before you adopt it.1

  • To build mutants together, the test program’s main must be written in Ada and the build must leave GNAT’s ALI files. When these conditions are not met, every mutant is built one at a time.
  • In projects that treat warnings as errors, which mutants cause compile errors can differ between building them together and building them one at a time. This is because the combined source turns off style checks, and warnings that come from looking at the whole file are not produced either.
  • A mutant that changes how many times a loop runs can allocate a large amount of memory. test_memory_limit_mb sets a memory limit for each test process.
  • On Windows, a path containing / in the test command must be enclosed in quotes.

The version is 0.1.0. What this article checked is the two bundled samples on Linux and how --since behaves.

Summary

  • AdaMutant is a tool that makes small changes to Ada and SPARK source code and checks whether tests or GNATprove proofs can detect them.
  • In the bundled sample, 9 of the 40 mutants survived. Adding four boundary tests brought the survivors down to 3. Of those, 2 were equivalent mutants, and 1 pointed to a place where the specification had not been decided.
  • Even in SPARK code with no tests, proofs alone detected 7 of 8 mutants.
  • Mutants that can be combined need only one build, and are switched with an environment variable during testing.
  • With --since, you can check only the mutants on the changed lines and stop CI with a threshold.

References

  1. KomuraSoft, AdaMutant (README.ja.md) (in Japanese). Sections referred to: the list of features at the top, “Usage”, “Mutants”, “Statuses and score”, “How it works” and “Building mutants together”. This article was checked against the content at commit 22c0fcb (November 15, 2026). ↩ ↩2 ↩3 ↩4 ↩5 ↩6

  2. Alire, mutant. Parts referred to: the crate name (mutant), the version (0.1.0), the license (Apache-2.0), the dependency (gnat >=14), and “AdaMutant runs on Linux, macOS and Windows.” in the description. Checked on November 17, 2026. ↩ ↩2

Recent articles sharing the same tags. Deepen your understanding with closely related topics.

These are topic pages close to this theme. Starting from the article, you can go on to related services and other articles.

This article leads to the following service pages. Start from the entry point closest to your situation.

Frequently Asked Questions

Questions commonly asked at the time of consultation about the topic of this article.

What does AdaMutant do?
It automatically makes small changes (mutants) to Ada and SPARK source code and checks whether your tests or GNATprove proofs can detect those changes. It reports the share it detected as the mutation score. KomuraSoft publishes it under the Apache License 2.0.
Can it be used on SPARK code that has no tests, only proofs?
Yes. If you write a GNATprove command in the prove setting, a mutant whose proof fails is counted as detected. In the bundled sample, proofs alone detected 7 of 8 mutants with no tests, and the one that remained was an equivalent mutant that behaves the same as the original code.
Should I aim for a mutation score of 100%?
Rather than treating it as a target number, we recommend reading the surviving mutants one by one. The survivors include some that point to missing tests or an incomplete specification, and some that cannot be detected because they behave the same as the original code (equivalent mutants). A person has to decide which is which.

Author Profile

Profile page for the article author.

Go Komura

Representative of KomuraSoft LLC

Focused on Windows software development, technical consulting, and bug investigation, with particular strength in projects where existing assets remain and in investigating failures whose cause is hard to see.

Back to the Blog