Skip to content

Commit 0d8254c

Browse files
committed
example/simple-assertions: add an explicit allocation
LFortran generates a segfault upon first assignemnt to the allocatable array without a preceding allocation. Also, add some comments to clarify the math error in this example code is deliberate, and update the documentation to match the actual program.
1 parent 8d02da3 commit 0d8254c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

example/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fpm run --example invoke-via-macro --flag "-DASSERTIONS"
2323
Simple examples
2424
---------------
2525

26-
The [simple_assertions.f90] example demonstrates a precondition and a
26+
The [simple-assertions.f90] example demonstrates a precondition and a
2727
postcondition, each with an assertion that checks the truth of a logical
2828
expression based on scalar, real values.
2929

@@ -32,20 +32,20 @@ Running the examples
3232

3333
### Single-image execution
3434
```
35-
fpm run --example simple_assertions
35+
fpm run --example simple-assertions --flag "-DASSERTIONS"
3636
```
3737
where `fpm run` automatically invokes `fpm build` if necessary, .e.g., if the package's source code
3838
has changed since the most recent build. If `assert` is working correctly, the `fpm run` above
3939
will error-terminate with the character stop code similar to the following
4040
```
41-
Assertion failure on image 1: reciprocal: abs(error) < tolerance
41+
Assertion failure on image 1: All residuals within tolerance
4242
```
4343

4444
### Multi-image execution with `gfortran` and OpenCoarrays
4545
```
4646
git clone [email protected]/sourceryinstitute/assert
4747
cd assert
48-
fpm run --compiler caf --runner "cafrun -n 2" --example simple_assertions
48+
fpm run --compiler caf --runner "cafrun -n 2" --example simple-assertions --flag "-DASSERTIONS"
4949
```
5050
Replace either instance of `2` above with the desired number of images to run for parallel execution.
5151
If `assert` is working correctly, both of the latter `fpm run` commands will error-terminate with one
@@ -55,7 +55,7 @@ or more images providing stop codes analogous to those quoted in the [Single-ima
5555
[OpenCoarrays]: https://github.com/sourceryinstitute/opencoarrays
5656
[Enforcing programming contracts]: #enforcing-programming-contracts
5757
[Single-image execution]: #single-image-execution
58-
[simple_assertions.f90]: ./simple_assertions.f90
58+
[simple-assertions.f90]: ./simple-assertions.f90
5959
[invoke-via-macro.F90]: ./invoke-via-macro.F90
6060
[UML]: https://en.wikipedia.org/wiki/Unified_Modeling_Language
6161
[OCL]: https://en.wikipedia.org/wiki/Object_Constraint_Language

example/simple-assertions.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ pure function roots(a,b,c) result(zeros)
1818

1919
associate(discriminant => b**2 - 4*a*c)
2020
call assert(assertion = discriminant >= 0., description = "discriminant >= 0") ! precondition
21+
allocate(zeros(2))
22+
! there's a deliberate math bug in the following line, to help demonstrate assertion failure
2123
zeros = -b + [sqrt(discriminant), -sqrt(discriminant)]
2224
end associate
2325

26+
! This assertion will fail (due to the defect above) when ASSERTIONS are enabled:
2427
call assert(all(abs(a*zeros**2 + b*zeros + c) < tolerance), "All residuals within tolerance.") ! postcondition
2528
end function
2629

0 commit comments

Comments
 (0)