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