MONC
test_optionsdatabase.F90
Go to the documentation of this file.
1 ! Unit tests for the options database functionality
5  use collections_mod, only : map_type, c_get
7  use fruit, only : assert_equals, assert_true, assert_false
8  implicit none
9 
10  contains
11 
12  ! Tests the procedure that applies types to specific values
13  subroutine test_get_argument_value_type()
14 
15  call assert_equals(logical_type, get_argument_value_type("false"), "True is logical")
16  call assert_equals(logical_type, get_argument_value_type("false"), "False is logical")
17  call assert_equals(integer_type, get_argument_value_type("12345"), "Simple number is integer")
18  call assert_equals(integer_type, get_argument_value_type("+98"), "Positive number is integer")
19  call assert_equals(integer_type, get_argument_value_type("-55232"), "Negative number is integer")
20  call assert_equals(integer_type, get_argument_value_type("0"), "Zero is integer")
21  call assert_equals(real_type, get_argument_value_type("1.2"), "Simple floating point is real")
22  call assert_equals(real_type, get_argument_value_type("1.9867e6"), "Exponent floating point is real")
23  call assert_equals(real_type, get_argument_value_type("72.54e-6"), "Small exponent floating point is real")
24  call assert_equals(real_type, get_argument_value_type("-9983.2324"), "Negative floating point is real")
25  call assert_equals(string_type, get_argument_value_type("hello"), "Other characters are string")
26  call assert_equals(string_type, get_argument_value_type("1234abc"), "Number and then non numeric characters are string")
27  end subroutine test_get_argument_value_type
28 
29  ! Test the addition of logical k-v pairs
31  type(map_type) :: options_database
32  class(*), pointer :: raw_data
33 
34  call add_specific_option_key_value_pair(logical_type, options_database, "--test1")
35  call add_specific_option_key_value_pair(logical_type, options_database, "--test2=false")
36  call add_specific_option_key_value_pair(logical_type, options_database, "--test3=true")
37 
38  raw_data=>c_get(options_database, "test1")
39  call assert_true(conv_to_logical(raw_data, .false.), "Logical no value is true")
40  raw_data=>c_get(options_database, "test2")
41  call assert_false(conv_to_logical(raw_data, .false.), "Logical false values")
42  raw_data=>c_get(options_database, "test3")
43  call assert_true(conv_to_logical(raw_data, .false.), "Logical true values")
45 
46  ! Test the addition of integer k-v pairs
48  type(map_type) :: options_database
49  class(*), pointer :: raw_data
50 
51  call add_specific_option_key_value_pair(integer_type, options_database, "--test1=9542")
52  call add_specific_option_key_value_pair(integer_type, options_database, "--test2=-1234")
53  call add_specific_option_key_value_pair(integer_type, options_database, "--test3=+82")
54  call add_specific_option_key_value_pair(integer_type, options_database, "--test4=0")
55 
56  raw_data=>c_get(options_database, "test1")
57  call assert_equals(9542, conv_to_integer(raw_data, .false.), "Simple integer value added")
58  raw_data=>c_get(options_database, "test2")
59  call assert_equals(-1234, conv_to_integer(raw_data, .false.), "Negative integer value added")
60  raw_data=>c_get(options_database, "test3")
61  call assert_equals(82, conv_to_integer(raw_data, .false.), "Positive integer value added")
62  raw_data=>c_get(options_database, "test4")
63  call assert_equals(0, conv_to_integer(raw_data, .false.), "Zero integer value added")
64 
66 
67  ! Test the addition of real k-v pairs
69  type(map_type) :: options_database
70  class(*), pointer :: raw_data
71 
72  call add_specific_option_key_value_pair(real_type, options_database, "--test1=9542.342")
73  call add_specific_option_key_value_pair(real_type, options_database, "--test2=-1.33e9")
74  call add_specific_option_key_value_pair(real_type, options_database, "--test3=987.232e-6")
75  call add_specific_option_key_value_pair(real_type, options_database, "--test4=-98.72")
76 
77  raw_data=>c_get(options_database, "test1")
78  call assert_equals(9542.342, conv_to_real(raw_data, .false.), "Simple float value added")
79  raw_data=>c_get(options_database, "test2")
80  call assert_equals(-1.33e9, conv_to_real(raw_data, .false.), "Negative with exponent value added")
81  raw_data=>c_get(options_database, "test3")
82  call assert_equals(987.232e-6, conv_to_real(raw_data, .false.), &
83  "Positive with negative exponent value added")
84  raw_data=>c_get(options_database, "test4")
85  call assert_equals(-98.72, conv_to_real(raw_data, .false.), "Negative float value added")
86 
87  end subroutine test_add_real_key_value_pairs
88 
89  ! Tests the addition of string k-v pairs
91  type(map_type) :: options_database
92  class(*), pointer :: raw_data
93 
94  call add_specific_option_key_value_pair(string_type, options_database, "--test1=abc")
95  call add_specific_option_key_value_pair(string_type, options_database, "--test2=HeLlO")
96 
97  raw_data=>c_get(options_database, "test1")
98  call assert_equals("abc", conv_to_string(raw_data, .false., 44), "Simple string")
99  raw_data=>c_get(options_database, "test2")
100  call assert_equals("HeLlO", conv_to_string(raw_data, .false., 44), "Case sensitive string")
101 
102  end subroutine test_add_string_key_value_pairs
103 end module test_optionsdatabase_mod
104 
106  use fruit, only : init_fruit, run_test_case, fruit_summary
109 
110  implicit none
111 
112  call init_fruit
113  call run_test_case(test_get_argument_value_type, "Test argument value type")
114  call run_test_case(test_add_logical_key_value_pairs, "Test logical key value addition")
115  call run_test_case(test_add_integer_key_value_pairs, "Test integer key value addition")
116  call run_test_case(test_add_real_key_value_pairs, "Test real key value addition")
117  call run_test_case(test_add_string_key_value_pairs, "Test string key value addition")
118  call fruit_summary
119 end program test_optionsdatabase_driver
conversions_mod
Conversion between common inbuilt FORTRAN data types.
Definition: conversions.F90:5
collections_mod::map_type
Map data structure that holds string (length 20 maximum) key value pairs.
Definition: collections.F90:86
test_optionsdatabase_mod::test_add_logical_key_value_pairs
subroutine test_add_logical_key_value_pairs()
Definition: test_optionsdatabase.F90:31
conversions_mod::conv_to_integer
Converts data types to integers.
Definition: conversions.F90:49
test_optionsdatabase_mod
Definition: test_optionsdatabase.F90:2
collections_mod
Collection data structures.
Definition: collections.F90:7
optionsdatabase_mod::integer_type
integer, parameter integer_type
Type of integer value data.
Definition: optionsdatabase.F90:19
test_optionsdatabase_mod::test_add_string_key_value_pairs
subroutine test_add_string_key_value_pairs()
Definition: test_optionsdatabase.F90:91
conversions_mod::conv_to_logical
Converts data types to logical.
Definition: conversions.F90:71
optionsdatabase_mod::get_argument_value_type
integer function get_argument_value_type(specific_value)
Given a specific value this will determine the type of data.
Definition: optionsdatabase.F90:606
test_optionsdatabase_driver
program test_optionsdatabase_driver
Definition: test_optionsdatabase.F90:105
optionsdatabase_mod::real_type
integer, parameter real_type
Type of real value data.
Definition: optionsdatabase.F90:19
conversions_mod::conv_to_string
Converts data types to strings.
Definition: conversions.F90:38
optionsdatabase_mod::logical_type
integer, parameter logical_type
Type of logical value data.
Definition: optionsdatabase.F90:19
test_optionsdatabase_mod::test_add_integer_key_value_pairs
subroutine test_add_integer_key_value_pairs()
Definition: test_optionsdatabase.F90:48
optionsdatabase_mod::string_type
integer, parameter string_type
Type of string value data.
Definition: optionsdatabase.F90:19
optionsdatabase_mod::add_specific_option_key_value_pair
subroutine add_specific_option_key_value_pair(type_of_config, parse_options, specific_arg)
This will add a specific option key value pair to the options hashmap_type.
Definition: optionsdatabase.F90:641
test_optionsdatabase_mod::test_add_real_key_value_pairs
subroutine test_add_real_key_value_pairs()
Definition: test_optionsdatabase.F90:69
test_optionsdatabase_mod::test_get_argument_value_type
subroutine test_get_argument_value_type()
Definition: test_optionsdatabase.F90:14
conversions_mod::conv_to_real
Converts data types to real.
Definition: conversions.F90:60
optionsdatabase_mod
Manages the options database. Contains administration functions and deduce runtime options from the c...
Definition: optionsdatabase.F90:7