|
1 | | -](https://user-images.githubusercontent.com/13108868/130385757-6b79e5f1-5dec-440c-98f5-0f659c538754.png) |
| 1 | +------------------------------------------- |
| 2 | + |
| 3 | +This directory contains two example programs. |
| 4 | + |
| 5 | +Simple examples |
| 6 | +--------------- |
| 7 | + |
| 8 | +The [simple_assertions.f90] example demonstrates a precondition and a |
| 9 | +postcondition, each with an assertion that checks the truth of a logical |
| 10 | +expression based on scalar, real values. |
| 11 | + |
| 12 | +Derived type diagnostic data |
| 13 | +---------------------------- |
| 14 | + |
| 15 | +See [derived_type_diagnostic.f90]. For reasons related to runtime performance, |
| 16 | +it is desirable to ensure that any computation required to extract diagnostic |
| 17 | +data from an object only take place if the assertion fails. This is one of the |
| 18 | +main motivations for allowing objects to be passed to the `diagnostic_data` |
| 19 | +argument of `assert`. The generic programming facilities planned for |
| 20 | +"Fortran 202y" (two standards after Fortran 2018) will ultimately provide the |
| 21 | +best way to facilitate the extraction of diagnostic data from objects by |
| 22 | +empowering developers to express requirements on types such as that the types |
| 23 | +must support a specific procedure binding that can be used to extract output |
| 24 | +in character form, the form that `assert` uses for its error stop code. For |
| 25 | +now, we impose such a requirement through an `as_character` deferred binding |
| 26 | +on the provided `characterizable_t` abstract type. |
| 27 | + |
| 28 | +Because might be problematic to require that a user type to extend the |
| 29 | +`characterizable_t` abstract type, the [derived_type_diagnostic.f90] example |
| 30 | +shows a workaround based on the class hierarchy described in the figure |
| 31 | +below. The pattern expressed in the workaround aggregates the example user |
| 32 | +type, `stuff_t` as a component inside the encapsulating `characterizable_stuff_t` |
| 33 | +type. The latter type extends `characterizable_t` and implements this parent |
| 34 | +type's deferred binding. |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +Running the examples |
| 39 | +-------------------- |
| 40 | + |
| 41 | +### Single-image execution |
| 42 | +``` |
| 43 | +fpm run --example simple_assertions |
| 44 | +fpm run --example derived_type_diagnostic |
| 45 | +``` |
| 46 | +where the `fpm run` automatically invokes `fpm build` if necessary, .e.g., the `fpm` source has |
| 47 | +changed since the most recent build. If `assert` is working correctly, the first `fpm run` above |
| 48 | +will error-terminate with the character stop code |
| 49 | +``` |
| 50 | +Assertion "reciprocal: abs(error) < tolerance" failed on image 1 with diagnostic data "-1.00000000" |
| 51 | +``` |
| 52 | +and the second `fpm run` above will error-terminate with the character stop code |
| 53 | +``` |
| 54 | +Assertion "stuff_t%z(): self%defined()" failed on image 1 with diagnostic data "(none provided)" |
| 55 | +``` |
| 56 | + |
| 57 | +### Multi-image execution with `gfortran` and OpenCoarrays |
| 58 | +``` |
| 59 | +git clone [email protected]/sourceryinstitute/assert |
| 60 | +cd assert |
| 61 | +fpm run --compiler caf --runner "cafrun -n 2" --example simple_assertions |
| 62 | +fpm run --compiler caf --runner "cafrun -n 2" --example derived_type_diagnostic |
| 63 | +``` |
| 64 | +Replace either instance of `2` above with the desired number of images to run for parallel execution. |
| 65 | +If `assert` is working correctly, both of the latter `fpm run` commands will error-terminate with one |
| 66 | +or more images providing stop codes analogous to those quoted in the [Single-image execution] section. |
| 67 | + |
| 68 | +## Derived-type diagnostic data output |
| 69 | +To demonstrate the derived-type diagnostic data output capability, try replacing the |
| 70 | +`i%defined()` assertion in the [derived_type_diagnostic.f90](./derived_type_diagnostic.f90) |
| 71 | +with `.false.`. |
| 72 | + |
| 73 | +[Hyperlinks]:# |
| 74 | +[OpenCoarrays]: https://github.com/sourceryinstitute/opencoarrays |
| 75 | +[Enforcing programming contracts]: #enforcing-programming-contracts |
| 76 | +[Single-image execution]: #single-image-execution |
| 77 | +[derived_type_diagnostic.f90]: ./derived_type_diagnostic.f90 |
| 78 | +[simple_assertions.f90]: ./simple_assertions.f90 |
0 commit comments