libCellML: Tests for Real Number Handling
Context
A CellML model is an XML file. Real numbers appear in several
places: unit exponent and
multiplier attributes, variable initial values,
MathML cn elements, and degree
elements.
The CellML 2.0 specification defines exactly what a real number string may look like (section 1.3), and libCellML checks every number against those rules. When the rules and the checks agree, a model means the same thing in every tool that reads it.
The Issue
A member of the libCellML team opened issue #1505 after noticing
that exponent="+2" was reported as invalid, even
though rule 1.3.3.2 allows a leading plus sign. The issue listed
more cases to look at:
- numbers starting with
+ - degenerate strings such as
-. - out-of-range numbers such as
1e999 - empty
cnelements
A follow-up comment asked for every place a real number can be entered to be covered.
How I Got Involved
I read the number handling in src/utilities.cpp
and offered on the issue to take it on, starting with a pull
request that only adds tests. That way, the expected behavior
can be agreed before any implementation changes.
The maintainer welcomed the plan, and also suggested that
numbers too large for the machine, like 1e999, are
a question for the analyser rather than the validator, and
belong in a separate issue. So I left those out of this pull
request.
What I Found
1. An empty string that counts as a number
isCellMLBasicReal() removes the sign and the
decimal point, then checks that every character left is a
digit using std::all_of. For -.,
- and ., nothing is left. And
std::all_of on an empty range returns
true: every item in nothing passes.
"-." → remove "-" and "." → ""
std::all_of("") → true (so "-." is treated as a number)
The same function also doesn't expect a leading
+, which is why +2 is reported.
2. An exception instead of an issue
In unit attributes and cn elements, the
degenerate strings then reach std::stod, which
throws std::invalid_argument.
stringToDouble() only handles
std::out_of_range, so the exception leaves
parseModel() and validateModel()
instead of becoming a clear validation message. Initial values
take a different path, so there they are accepted without any
message at all.
3. Numbers inside degree, bvar and logbase are never checked
validateMathMLElementsChildrenAndSiblings() looks
inside apply, piecewise,
piece and otherwise, but not inside
degree, bvar or
logbase. A cn in one of those places
is accepted whatever it contains, even when it's empty. This is
a second cause, separate from the first.
4. The specification's examples and rule disagree
Rule 1.3.3.2 says a basic real number string MAY begin with
+, but the examples in the same section list
"+123.4567" and "+1.234E03" as not
valid. The tests follow the rule, which is how the team reads
it, and I mentioned the examples in the pull request in case
the specification needs a small update.
My Contribution
Pull request #1516 adds twelve tests and no library changes:
two in the parser tests and ten in the validator tests. Every
expected message reuses a message libCellML already has, so
there is no new wording to review. A small shared helper,
createModelWithMath(), keeps the MathML tests short
and follows the style of the existing helpers.
| What the tests describe | Where | Today |
|---|---|---|
A leading + is accepted |
Unit attributes, initial values, cn (including e-notation), degree |
Reported as invalid, except in degree, where it is never checked |
-., - and . are rejected |
Unit attributes, initial values, cn, degree, logbase |
An exception in unit attributes and cn; accepted elsewhere |
Empty cn elements are rejected |
Top level, degree, logbase |
Rejected at the top level; accepted inside degree and logbase |
Ten of the twelve tests fail for now, on purpose: each one describes something the fix needs to make true. The other two already pass and stay as guards, so a future change can't quietly undo them. All of libCellML's existing tests still pass.
Process
- libCellML's contributing guide asks for test-driven development, so the tests come first and the fix follows on the same branch.
-
The guide still mentions a
developbranch, but the project now works onmain, so the pull request targetsmain. I noted this in the pull request. -
The code passes the project's
clang-formatcheck and uses the same line endings as the files around it. -
For the separate analyser issue, I recorded how
1e999behaves today in each place, without adding tests for it.
The pull request ends with three questions for the maintainers:
whether the degree, bvar and
logbase fix belongs in this issue, whether a
leading + should be accepted the same way in
e-notation and in degree, and how unit attributes
should treat out-of-range values.
Related Work
Working on this issue raised a wider question: how do different CellML tools read the same numbers? To answer it, I built an independent CellML 2.0 conformance corpus. Each test file is a small model that shows one rule of the specification, including the real number rules from section 1.3.
A runner checks how several CellML tools, libCellML among them, read every file. The conformance report shows where they agree and where they differ, rule by rule.
The out-of-range question, which the maintainer suggested belongs in a separate issue, is now finding 9 in my conformance findings issue, #1517, ready to stand on its own.
What's Next
Once the maintainers have answered, I'll write the fix on the same branch. Then the failing tests turn green one by one, and everyone can see the fix prove itself.
Reflection
The most interesting detail here is a very small one: on an empty range, "every item passes" is true. It's correct logic, and it still let three strings that aren't numbers through.
Writing the tests first turned "what should happen?" into concrete examples that anyone can read and agree on. That's what I value most in this kind of work: when something goes wrong with a number, the person reading the model should be told clearly, never left to find out in silence.