source: palm/trunk/UTIL/inifor/tests/util.f90 @ 3157

Last change on this file since 3157 was 2718, checked in by maronga, 7 years ago

deleting of deprecated files; headers updated where needed

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
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!
24!
25! Former revisions:
26! -----------------
27! $Id: util.f90 2718 2018-01-02 08:49:38Z maronga $
28! Initial revision
29!
30!
31!
32! Authors:
33! --------
34! @author Eckhard Kadasch
35!
36! Description:
37! ------------
38!> This module provides utiliy functions used in all test programs.
39!------------------------------------------------------------------------------!
40 MODULE test_utils
41 
42    IMPLICIT NONE
43
44 CONTAINS
45
46    SUBROUTINE begin_test(title, res)
47       CHARACTER(LEN=*), INTENT(IN) ::  title
48       LOGICAL, INTENT(OUT)         ::  res
49
50       res = .TRUE.
51
52       PRINT '(/A)',       "******************************************************"
53       PRINT '(A, A, A)',  " [  ]  Test '", TRIM(title), "' started."
54       PRINT '(A/)',       "******************************************************"
55    END SUBROUTINE begin_test
56
57    SUBROUTINE end_test(title, res)
58       CHARACTER(LEN=*), INTENT(IN) ::  title
59       CHARACTER(LEN=30)            ::  msg, label
60       LOGICAL, INTENT(IN)          ::  res
61
62
63       IF (res .EQV. .TRUE.)  THEN
64          msg = 'completed successfully.'
65          label = ' [OK]'
66       ELSE
67          msg = 'failed.'
68          label = ' [XX]'
69       END IF
70
71       PRINT '(/A, A, A, A)', TRIM(label) // "  Test '", TRIM(title), "' ", TRIM(msg)
72       PRINT '(A/)',       "******************************************************"
73
74    END SUBROUTINE end_test
75
76    LOGICAL FUNCTION assert_equal(a, b, msg)
77       REAL, DIMENSION(:), INTENT(IN) ::  a, b
78       CHARACTER(LEN=*), INTENT(IN)   :: msg
79
80       assert_equal = assert(a, b, 'eq')
81       IF (assert_equal .eqv. .TRUE.)  THEN
82           PRINT *, "Equality assertion for ", msg, " was successful."
83       ELSE
84           PRINT *, "Equality assertion for ", msg, " failed. Maximum error is ",               &
85              MAXVAL( ABS( a - b))
86       END IF
87
88    END FUNCTION assert_equal
89
90    LOGICAL FUNCTION assert(a, b, mode, eps)
91
92       REAL, DIMENSION(:), INTENT(IN) ::  a, b
93       REAL, OPTIONAL, INTENT(IN)     ::  eps
94       CHARACTER(LEN=*), INTENT(IN)   ::  mode
95
96       REAL    ::  diff, mag, max_rel_diff
97       INTEGER ::  i
98
99       max_rel_diff = 10 * EPSILON(1.0)
100       IF (PRESENT(eps)) max_rel_diff = eps
101
102       SELECT CASE( TRIM(mode) )
103
104       ! This case is inspired by
105       ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
106       CASE('eq')
107          IF ( ALL(a(:) == b(:)) )  THEN
108             PRINT *, "Checking for exact equality"
109             assert = .TRUE.
110          ELSE
111             assert = .TRUE.
112             PRINT *, "Checking for near equality"
113             DO i = 1, SIZE(a)
114                diff   = ABS(a(i) - b(i)) 
115                mag    = MAX( ABS(a(i)), ABS(b(i)) )
116                assert = assert .AND. (diff < mag * max_rel_diff )
117             END DO
118          END IF
119
120       CASE DEFAULT
121          PRINT *, " Error: Assert mode ", mode, " not implemented. Stopping."
122          STOP
123
124       END SELECT
125   
126    END FUNCTION assert
127
128 END MODULE test_utils
129
Note: See TracBrowser for help on using the repository browser.