MONC
test_conversions.F90
Go to the documentation of this file.
1 ! Tests the conversion aspect of the core utilities. This covers all conversion procedures as each test goes to and from
2 ! hence testing two bits of functionality
4  use fruit, only : assert_true, assert_false, assert_not_equals, assert_equals
7  implicit none
8 
9  contains
10 
11  ! Tests the string is an integer functionality
12  subroutine test_is_integer()
13  call assert_true(conv_is_integer("1"), "Small number")
14  call assert_true(conv_is_integer("9874321"), "Large number")
15  call assert_true(conv_is_integer("-1024"), "Negative number")
16  call assert_true(conv_is_integer("+986"), "Positive number")
17  call assert_false(conv_is_integer("1.2"), "Floating point")
18  call assert_false(conv_is_integer("ABCD"), "Non number")
19  call assert_false(conv_is_integer("87T"), "Numeric and non number characters")
20  call assert_false(conv_is_integer("0xAB"), "Hexadecimal")
21  end subroutine test_is_integer
22 
23  ! Tests the string is a real functionality
24  subroutine test_is_real()
25  call assert_true(conv_is_real("1"), "Small number")
26  call assert_true(conv_is_real("9874321"), "Large number")
27  call assert_true(conv_is_real("-1024"), "Negative number")
28  call assert_true(conv_is_real("+986"), "Positive number")
29  call assert_true(conv_is_real("1.2"), "Floating point")
30  call assert_true(conv_is_real("45432.2343"), "Large floating point")
31  call assert_true(conv_is_real("1e2"), "Exponent floating point")
32  call assert_true(conv_is_real("1.56e+6"), "Large positive exponent floating point")
33  call assert_true(conv_is_real("765.98e-6"), "Small negative exponent floating point")
34  call assert_false(conv_is_real("ABCD"), "Non number")
35  call assert_false(conv_is_real("87T"), "Numeric and non numeric characters")
36  call assert_false(conv_is_real("0xAB"), "Hexadecimal")
37  end subroutine test_is_real
38 
39  ! Tests the string is a logical functionality
40  subroutine test_is_logical()
41  call assert_true(conv_is_logical("true"), "True value")
42  call assert_true(conv_is_logical("false"), "False value")
43  call assert_false(conv_is_logical("1"), "One number")
44  call assert_false(conv_is_logical("0"), "Zero number")
45  call assert_false(conv_is_logical("dsfdsfsd"), "Random characters")
46  call assert_false(conv_is_logical("truexyz"), "Append characters to true")
47  end subroutine test_is_logical
48 
49  ! Tests conversion from real to integer and back again
50  subroutine test_real_to_integer()
51  real :: test_real, retrieve_real
52  integer :: retrieval_int
53 
54  test_real=19
55  retrieval_int = conv_to_integer(test_real)
56  retrieve_real = conv_to_real(retrieval_int)
57  call assert_equals(retrieve_real, test_real, "Reals after conversion to and from integer are equal")
58  end subroutine test_real_to_integer
59 
60  ! Tests conversion from logical to integer and then back again
61  subroutine test_logical_to_integer()
62  logical :: test_logical, retrieve_logical
63  integer :: retrieval_int
64 
65  test_logical=.true.
66  retrieval_int = conv_to_integer(test_logical)
67  retrieve_logical = conv_to_logical(retrieval_int)
68  call assert_equals(retrieve_logical, test_logical, "Logicals after conversion to and from integer are equal")
69  end subroutine test_logical_to_integer
70 
71  ! Tests conversion from logical to real and back again
72  subroutine test_logical_to_real()
73  logical :: test_logical, retrieve_logical
74  real :: retrieval_real
75 
76  test_logical=.true.
77  retrieval_real = conv_to_real(test_logical)
78  retrieve_logical = conv_to_logical(retrieval_real)
79  call assert_equals(retrieve_logical, test_logical, "Logicals after conversion to and from real are equal")
80  end subroutine test_logical_to_real
81 
82  ! Tests conversion from integer to string and bacl
83  subroutine test_integer_to_string()
84  integer :: test_int, retrieve_int
85  character(len=15) :: retrieval_string
86 
87  test_int = 92
88  retrieval_string = conv_to_string(test_int)
89  retrieve_int = conv_to_integer(retrieval_string)
90  call assert_equals(retrieve_int, test_int, "Integers after conversion to and from string are equal")
91  end subroutine test_integer_to_string
92 
93  ! Tests conversion from real to string and back
94  subroutine test_real_to_string()
95  real :: test_real, retrieve_real, diff
96  character(len=30) :: retrieval_string
97 
98  test_real = 63.45
99  retrieval_string = conv_to_string(test_real)
100  retrieve_real = conv_to_real(retrieval_string)
101  ! Conversion to string is not exact, therefore take the difference and ensure it is within permissable bounds
102  diff = test_real - retrieve_real
103  call assert_true(diff .gt. -0.0001 .and. diff .lt. 0.0001, "Reals after conversion to and from string are equal")
104  end subroutine test_real_to_string
105 
106  ! Tests conversion from logical to string and back
107  subroutine test_logical_to_string()
108  logical :: test_logical, retrieve_logical
109  character(len=5) :: retrieval_string
110 
111  test_logical = .true.
112  retrieval_string = conv_to_string(test_logical)
113  retrieve_logical = conv_to_logical(retrieval_string)
114  call assert_equals(retrieve_logical, test_logical, "Logicals after conversion to and from string are equal")
115  end subroutine test_logical_to_string
116 
117  ! Tests generic conversion from string and back, ensuring that with false supplied as copy arguments then both point to the same
118  ! bit of memory and hence changing one will change the other
119  subroutine test_string_to_generic()
120  character(len=100) :: test_string
121  character(len=100), pointer :: retrieval_string
122  class(*), pointer :: generic_data
123 
124  integer :: i
125  do i=1,100
126  test_string(i:i)='C'
127  end do
128 
129  generic_data => conv_to_generic(test_string, .false.)
130  call assert_true(associated(generic_data), "Generic data not null")
131  retrieval_string => conv_to_string(generic_data, .false., 100)
132  call assert_true(associated(retrieval_string), "Retrieved string not null")
133  call assert_equals(test_string, retrieval_string, "To and from generic strings are equal")
134  retrieval_string(4:6) = "LKJ"
135  call assert_equals(test_string, retrieval_string, "To and from generic strings are equal after one modified")
136  end subroutine test_string_to_generic
137 
138  ! Tests conversion from string to generic and that specifying copy (in the to or from) will make a separate
139  ! copy of the memory so that changing one will not affect the other
140  subroutine test_string_to_generic_copy()
141  character(len=100) :: test_string
142  character(len=100), pointer :: retrieval_string
143  class(*), pointer :: generic_data
144 
145  integer :: i
146  do i=1,100
147  test_string(i:i)='C'
148  end do
149 
150  generic_data => conv_to_generic(test_string, .true.)
151  call assert_true(associated(generic_data), "Generic data not null")
152  retrieval_string => conv_to_string(generic_data, .false., 100)
153  call assert_true(associated(retrieval_string), "Retrieved string not null")
154  retrieval_string(4:6) = "LKJ"
155  call assert_not_equals(test_string, retrieval_string, "To and from generic strings are different")
156 
157  generic_data => conv_to_generic(test_string, .false.)
158  retrieval_string => conv_to_string(generic_data, .true., 100)
159  call assert_true(associated(generic_data), "Generic data not null")
160  retrieval_string(4:6) = "LKJ"
161  call assert_not_equals(test_string, retrieval_string, "To and from generic strings are different")
162  end subroutine test_string_to_generic_copy
163 
164  ! Tests generic conversion from integer and back, ensuring that with false supplied as copy arguments then both point to the same
165  ! bit of memory and hence changing one will change the other
166  subroutine test_integer_to_generic()
167  integer :: test_int
168  integer, pointer :: retrieval_int
169  class(*), pointer :: generic_data
170 
171  test_int = 72
172 
173  generic_data => conv_to_generic(test_int, .false.)
174  call assert_true(associated(generic_data), "Generic data not null")
175  retrieval_int => conv_to_integer(generic_data, .false.)
176  call assert_true(associated(retrieval_int), "Retrieved integer not null")
177  call assert_equals(test_int, retrieval_int, "To and from generic integers are equal")
178  retrieval_int = 13
179  call assert_equals(test_int, retrieval_int, "To and from generic integers are equal after modification")
180  end subroutine test_integer_to_generic
181 
182  ! Tests conversion from integer to generic and that specifying copy (in the to or from) will make a separate
183  ! copy of the memory so that changing one will not affect the other
184  subroutine test_integer_to_generic_copy()
185  integer :: test_int
186  integer, pointer :: retrieval_int
187  class(*), pointer :: generic_data
188 
189  test_int = 72
190 
191  generic_data => conv_to_generic(test_int, .true.)
192  call assert_true(associated(generic_data), "Generic data not null")
193  retrieval_int => conv_to_integer(generic_data, .false.)
194  call assert_true(associated(retrieval_int), "Retrieved integer not null")
195  retrieval_int = 19
196  call assert_not_equals(test_int, retrieval_int, "To and from generic integers are different")
197 
198  generic_data => conv_to_generic(test_int, .false.)
199  retrieval_int => conv_to_integer(generic_data, .true.)
200  call assert_true(associated(generic_data), "Generic data not null")
201  retrieval_int = 32
202  call assert_not_equals(test_int, retrieval_int, "To and from generic integers are different")
203  end subroutine test_integer_to_generic_copy
204 
205  ! Tests generic conversion from real and back, ensuring that with false supplied as copy arguments then both point to the same
206  ! bit of memory and hence changing one will change the other
207  subroutine test_real_to_generic()
208  real :: test_real
209  real, pointer :: retrieval_real
210  class(*), pointer :: generic_data
211 
212  test_real = 72.92
213 
214  generic_data => conv_to_generic(test_real, .false.)
215  call assert_true(associated(generic_data), "Generic data not null")
216  retrieval_real => conv_to_real(generic_data, .false.)
217  call assert_true(associated(retrieval_real), "Retrieved real not null")
218  call assert_equals(test_real, retrieval_real, "To and from generic reals are equal")
219  retrieval_real = 13.1
220  call assert_equals(test_real, retrieval_real, "To and from generic real are equal after modification")
221  end subroutine test_real_to_generic
222 
223  ! Tests conversion from real to generic and that specifying copy (in the to or from) will make a separate
224  ! copy of the memory so that changing one will not affect the other
225  subroutine test_real_to_generic_copy()
226  real :: test_real
227  real, pointer :: retrieval_real
228  class(*), pointer :: generic_data
229 
230  test_real = 72.92
231 
232  generic_data => conv_to_generic(test_real, .true.)
233  call assert_true(associated(generic_data), "Generic data not null")
234  retrieval_real => conv_to_real(generic_data, .false.)
235  call assert_true(associated(retrieval_real), "Retrieved real not null")
236  retrieval_real = 19
237  call assert_not_equals(test_real, retrieval_real, "To and from generic reals are different")
238 
239  generic_data => conv_to_generic(test_real, .false.)
240  call assert_true(associated(generic_data), "Generic data not null")
241  retrieval_real => conv_to_real(generic_data, .true.)
242  retrieval_real = 32
243  call assert_not_equals(test_real, retrieval_real, "To and from generic reals are different")
244  end subroutine test_real_to_generic_copy
245 
246  ! Tests generic conversion from logical and back, ensuring that with false supplied as copy arguments then both point to the same
247  ! bit of memory and hence changing one will change the other
248  subroutine test_logical_to_generic()
249  logical :: test_logical
250  logical, pointer :: retrieval_logical
251  class(*), pointer :: generic_data
252 
253  test_logical = .true.
254 
255  generic_data => conv_to_generic(test_logical, .false.)
256  call assert_true(associated(generic_data), "Generic data not null")
257  retrieval_logical => conv_to_logical(generic_data, .false.)
258  call assert_true(associated(retrieval_logical), "Retrieved logical not null")
259  call assert_equals(test_logical, retrieval_logical, "To and from generic logicals are equal")
260  retrieval_logical = .false.
261  call assert_equals(test_logical, retrieval_logical, "To and from generic logicals are equal after modification")
262  end subroutine test_logical_to_generic
263 
264  ! Tests conversion from logical to generic and that specifying copy (in the to or from) will make a separate
265  ! copy of the memory so that changing one will not affect the other
266  subroutine test_logical_to_generic_copy()
267  logical :: test_logical
268  logical, pointer :: retrieval_logical
269  class(*), pointer :: generic_data
270 
271  test_logical = .true.
272 
273  generic_data => conv_to_generic(test_logical, .true.)
274  call assert_true(associated(generic_data), "Generic data not null")
275  retrieval_logical => conv_to_logical(generic_data, .false.)
276  call assert_true(associated(retrieval_logical), "Retrieved logical not null")
277  retrieval_logical = .false.
278  call assert_not_equals(test_logical, retrieval_logical, "To and from generic logicals are different")
279 
280  generic_data => conv_to_generic(test_logical, .false.)
281  call assert_true(associated(generic_data), "Generic data not null")
282  retrieval_logical => conv_to_logical(generic_data, .true.)
283  retrieval_logical = .false.
284  call assert_not_equals(test_logical, retrieval_logical, "To and from generic logicals are different")
285  end subroutine test_logical_to_generic_copy
286 
287 end module test_conversions_mod
288 
289 ! Driver for conversion utility tests
291  use fruit, only : init_fruit, run_test_case, fruit_summary
296 
297  implicit none
298 
299  call init_fruit
300  call run_test_case(test_string_to_generic, "Test string generic conversion")
301  call run_test_case(test_string_to_generic_copy, "Test string generic conversion copy")
302  call run_test_case(test_integer_to_generic, "Test integer generic conversion")
303  call run_test_case(test_integer_to_generic_copy, "Test integer generic conversion copy")
304  call run_test_case(test_real_to_generic, "Test real generic conversion")
305  call run_test_case(test_real_to_generic_copy, "Test real generic conversion copy")
306  call run_test_case(test_logical_to_generic, "Test logical generic conversion")
307  call run_test_case(test_logical_to_generic_copy, "Test logical generic conversion copy")
308  call run_test_case(test_integer_to_string, "Test integer to string conversion")
309  call run_test_case(test_real_to_string, "Test real to string conversion")
310  call run_test_case(test_logical_to_string, "Test logical to string conversion")
311  call run_test_case(test_real_to_integer, "Test real to integer conversion")
312  call run_test_case(test_logical_to_integer, "Test logical to integer conversion")
313  call run_test_case(test_logical_to_real, "Test logical to real conversion")
314  call run_test_case(test_is_integer, "Test is string an integer")
315  call run_test_case(test_is_real, "Test is string a real")
316  call run_test_case(test_is_logical, "Test is string a logical")
317  call fruit_summary
318 end program test_conversion_driver
program test_conversion_driver
subroutine test_string_to_generic()
subroutine test_is_logical()
subroutine test_logical_to_string()
subroutine test_logical_to_generic()
Converts a data type into the generic (class *) form.
Definition: conversions.F90:23
Conversion between common inbuilt FORTRAN data types.
Definition: conversions.F90:5
subroutine test_real_to_generic()
Converts data types to strings.
Definition: conversions.F90:36
subroutine test_real_to_integer()
Converts data types to logical.
Definition: conversions.F90:69
subroutine test_string_to_generic_copy()
Determines whether a data item can be represented as a logical or not.
Definition: conversions.F90:98
subroutine test_integer_to_string()
subroutine test_is_integer()
subroutine test_logical_to_generic_copy()
Determines whether a data item can be represented as an integer or not.
Definition: conversions.F90:79
subroutine test_real_to_generic_copy()
Converts data types to real.
Definition: conversions.F90:58
subroutine test_logical_to_real()
Determines whether a data item can be represented as a real or not.
Definition: conversions.F90:89
Converts data types to integers.
Definition: conversions.F90:47
subroutine test_logical_to_integer()
subroutine test_real_to_string()
subroutine test_integer_to_generic()
subroutine test_integer_to_generic_copy()