1 | !> @file tests/util.f90 |
---|
2 | !------------------------------------------------------------------------------! |
---|
3 | ! This file is part of the PALM model system. |
---|
4 | ! |
---|
5 | ! PALM is free software: you can redistribute it and/or modify it under the |
---|
6 | ! terms of the GNU General Public License as published by the Free Software |
---|
7 | ! Foundation, either version 3 of the License, or (at your option) any later |
---|
8 | ! version. |
---|
9 | ! |
---|
10 | ! PALM is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
11 | ! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
---|
12 | ! A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
13 | ! |
---|
14 | ! You should have received a copy of the GNU General Public License along with |
---|
15 | ! PALM. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | ! |
---|
17 | ! Copyright 2017-2018 Leibniz Universitaet Hannover |
---|
18 | ! Copyright 2017-2018 Deutscher Wetterdienst Offenbach |
---|
19 | !------------------------------------------------------------------------------! |
---|
20 | ! |
---|
21 | ! Current revisions: |
---|
22 | ! ----------------- |
---|
23 | ! Expose error measure as parameter in assert_equal() |
---|
24 | ! |
---|
25 | ! |
---|
26 | ! Former revisions: |
---|
27 | ! ----------------- |
---|
28 | ! $Id: util.f90 3182 2018-07-27 13:36:03Z suehring $ |
---|
29 | ! Initial revision |
---|
30 | ! |
---|
31 | ! |
---|
32 | ! |
---|
33 | ! Authors: |
---|
34 | ! -------- |
---|
35 | ! @author Eckhard Kadasch |
---|
36 | ! |
---|
37 | ! Description: |
---|
38 | ! ------------ |
---|
39 | !> This module provides utiliy functions used in all test programs. |
---|
40 | !------------------------------------------------------------------------------! |
---|
41 | MODULE test_utils |
---|
42 | |
---|
43 | IMPLICIT NONE |
---|
44 | |
---|
45 | CONTAINS |
---|
46 | |
---|
47 | SUBROUTINE begin_test(title, res) |
---|
48 | CHARACTER(LEN=*), INTENT(IN) :: title |
---|
49 | LOGICAL, INTENT(OUT) :: res |
---|
50 | |
---|
51 | res = .TRUE. |
---|
52 | |
---|
53 | PRINT '(/A)', "******************************************************" |
---|
54 | PRINT '(A, A, A)', " [ ] Test '", TRIM(title), "' started." |
---|
55 | PRINT '(A/)', "******************************************************" |
---|
56 | END SUBROUTINE begin_test |
---|
57 | |
---|
58 | SUBROUTINE end_test(title, res) |
---|
59 | CHARACTER(LEN=*), INTENT(IN) :: title |
---|
60 | CHARACTER(LEN=30) :: msg, label |
---|
61 | LOGICAL, INTENT(IN) :: res |
---|
62 | |
---|
63 | |
---|
64 | IF (res .EQV. .TRUE.) THEN |
---|
65 | msg = 'completed successfully.' |
---|
66 | label = ' [OK]' |
---|
67 | ELSE |
---|
68 | msg = 'failed.' |
---|
69 | label = ' [XX]' |
---|
70 | END IF |
---|
71 | |
---|
72 | PRINT '(/A, A, A, A)', TRIM(label) // " Test '", TRIM(title), "' ", TRIM(msg) |
---|
73 | PRINT '(A/)', "******************************************************" |
---|
74 | |
---|
75 | END SUBROUTINE end_test |
---|
76 | |
---|
77 | LOGICAL FUNCTION assert_equal(a, b, msg, ratio) |
---|
78 | REAL, OPTIONAL, INTENT(IN) :: ratio |
---|
79 | REAL, DIMENSION(:), INTENT(IN) :: a, b |
---|
80 | CHARACTER(LEN=*), INTENT(IN) :: msg |
---|
81 | |
---|
82 | IF ( PRESENT(ratio) ) THEN |
---|
83 | assert_equal = assert(a, b, 'eq', ratio) |
---|
84 | ELSE |
---|
85 | assert_equal = assert(a, b, 'eq') |
---|
86 | END IF |
---|
87 | |
---|
88 | IF (assert_equal .eqv. .TRUE.) THEN |
---|
89 | PRINT *, "Equality assertion for ", msg, " was successful." |
---|
90 | ELSE |
---|
91 | PRINT *, "Equality assertion for ", msg, " failed. Maximum error is ", & |
---|
92 | MAXVAL( ABS( a - b)) |
---|
93 | END IF |
---|
94 | |
---|
95 | END FUNCTION assert_equal |
---|
96 | |
---|
97 | LOGICAL FUNCTION assert(a, b, mode, ratio) |
---|
98 | |
---|
99 | REAL, DIMENSION(:), INTENT(IN) :: a, b |
---|
100 | REAL, OPTIONAL, INTENT(IN) :: ratio |
---|
101 | CHARACTER(LEN=*), INTENT(IN) :: mode |
---|
102 | |
---|
103 | REAL :: diff, mag, max_rel_diff |
---|
104 | INTEGER :: i |
---|
105 | |
---|
106 | max_rel_diff = 10 * EPSILON(1.0) |
---|
107 | IF (PRESENT(ratio)) max_rel_diff = ratio |
---|
108 | |
---|
109 | SELECT CASE( TRIM(mode) ) |
---|
110 | |
---|
111 | ! This case is inspired by |
---|
112 | ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ |
---|
113 | CASE('eq') |
---|
114 | IF ( ALL(a(:) == b(:)) ) THEN |
---|
115 | PRINT *, "Checking for exact equality" |
---|
116 | assert = .TRUE. |
---|
117 | ELSE |
---|
118 | assert = .TRUE. |
---|
119 | PRINT *, "Checking for near equality" |
---|
120 | DO i = 1, SIZE(a) |
---|
121 | diff = ABS(a(i) - b(i)) |
---|
122 | mag = MAX( ABS(a(i)), ABS(b(i)) ) |
---|
123 | assert = assert .AND. (diff < mag * max_rel_diff ) |
---|
124 | END DO |
---|
125 | END IF |
---|
126 | |
---|
127 | CASE DEFAULT |
---|
128 | PRINT *, " Error: Assert mode ", mode, " not implemented. Stopping." |
---|
129 | STOP |
---|
130 | |
---|
131 | END SELECT |
---|
132 | |
---|
133 | END FUNCTION assert |
---|
134 | |
---|
135 | END MODULE test_utils |
---|
136 | |
---|