File: assertions.F90

package info (click to toggle)
fortran-julienne 3.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 828 kB
  • sloc: f90: 5,043; makefile: 22; ansic: 14
file content (42 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
! Copyright (c) 2024-2025, The Regents of the University of California and Sourcery Institute
! Terms of use are as specified in LICENSE.txt

#include "julienne-assert-macros.h"

program assertions
  !! Example: two true assertions followed by one intentionally false assertion
  use julienne_m, only : &
     call_julienne_assert_ &
    ,operator(.approximates.) &
    ,operator(.equalsExpected.) &
    ,operator(.within.) &
    ,operator(.withinPercentage.)
  implicit none

#if ! ASSERTIONS
  print *
  print '(a)', "Skipping asertions."
  print '(a)', "Use a command such as the following to rerun with assertions:"
  print '(a)', "fpm run --example assertions --flag -DASSERTIONS"
#else
  print '(a)', new_line('')
  print '(a)', "This program evaluates the following true assertions that should run silently:" // new_line('')
  print '(a)', "   call_julienne_assert(pi_ .approximates. pi .within. absolute_tolerance)"
  print '(a)', "   call_julienne_assert(pi_ .approximates. pi .withinPercentage. relative_tolerance)" // new_line('')
  print '(a)', "where pi_ = 22./7., pi 3.14152654, absolute_tolerance = 0.1, and relative_tolerance = 1.0." // new_line('')
  print '(a)', "The program will then evaluate one false assertion:" // new_line('')
  print '(a)', "   call_julienne_assert(1 .equalsExpected. 2)" // new_line('')
  print '(a)', "which should initiate error termination and provide a diagnostic message:" // new_line('')
#endif

  block
    real, parameter :: pi_ = 22./7.
    real, parameter :: pi  = 3.141592654
    real, parameter :: absolute_tolerance = 0.2
    real, parameter :: relative_tolerance = 1.0 ! percentage

    call_julienne_assert(pi_ .approximates. pi .within. absolute_tolerance)
    call_julienne_assert(pi_ .approximates. pi .withinPercentage. relative_tolerance)
    call_julienne_assert(1 .equalsExpected. 2) ! intentional failure
  end block
end program