Skip to content

Commit 8af8207

Browse files
author
Damian Rouson
committed
feat(separated_values): (sub)module + tests
1 parent 8ae9e1b commit 8af8207

File tree

5 files changed

+143
-9
lines changed

5 files changed

+143
-9
lines changed

src/formats_m.F90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module formats_m
2+
!! Useful strings for formatting `print` and `write` statements
3+
implicit none
4+
5+
character(len=*), parameter :: csv = "(*(G0,:,','))" !! comma-separated values
6+
character(len=*), parameter :: cscv = "(*('(',G0,',',G0,')',:,',')))" !! comma-separated complex values
7+
8+
interface
9+
10+
pure module function separated_values(separator, mold) result(format_string)
11+
character(len=*), intent(in) :: separator
12+
#ifndef NAGFOR
13+
class(*), intent(in) :: mold(..)
14+
#else
15+
class(*), intent(in) :: mold(:)
16+
#endif
17+
character(len=:), allocatable :: format_string
18+
end function
19+
20+
end interface
21+
22+
end module

src/formats_m.f90

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/formats_s.F90

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
submodule(formats_m) formats_s
2+
!! Construct separated-value formats
3+
implicit none
4+
5+
contains
6+
7+
module procedure separated_values
8+
character(len=*), parameter :: prefix = "(*(G0,:,'"
9+
character(len=*), parameter :: suffix = "'))"
10+
character(len=*), parameter :: complex_prefix = "(*('(',G0,',',G0,')',:,'"
11+
12+
#ifndef NAGFOR
13+
select rank(mold)
14+
rank(1)
15+
#endif
16+
select type(mold)
17+
type is(complex)
18+
format_string = complex_prefix // separator // suffix
19+
type is(real)
20+
format_string = prefix // separator // suffix
21+
type is(integer)
22+
format_string = prefix // separator // suffix
23+
type is(character(len=*))
24+
format_string = prefix // separator // suffix
25+
class default
26+
error stop "format_s separated_values: unsupported type"
27+
end select
28+
#ifndef NAGFOR
29+
rank default
30+
error stop "formats_s separated_values: unsupported rank"
31+
end select
32+
#endif
33+
end procedure
34+
35+
end submodule formats_s

tests/formats_test.f90

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
module formats_test
2+
3+
!! author: Damian Rouson
4+
!!
5+
!! summary: verify that format strings provide the desired formatting
6+
use vegetables, only: &
7+
result_t, test_item_t, & ! types
8+
describe, it, assert_equals ! functions
9+
use formats_m, only : separated_values
10+
implicit none
11+
12+
private
13+
public :: test_object
14+
15+
contains
16+
17+
function test_object() result(tests)
18+
type(test_item_t) tests
19+
20+
tests = describe( &
21+
"csv format", &
22+
[it( &
23+
"yields a comma-separated list of real numbers", &
24+
check_csv_reals), &
25+
it( &
26+
"yields a space-separated list of complex numbers", &
27+
check_space_separated_complex), &
28+
it( &
29+
"yields a comma- and space-separated list of character values", &
30+
check_cssv_character), &
31+
it( &
32+
"yields a new-line-separated list of integer numbers", &
33+
check_new_line_separated_integers)])
34+
end function
35+
36+
function check_csv_reals() result(result_)
37+
type(result_t) result_
38+
character(len=*), parameter :: expected_output = "0.00000000,1.00000000,2.00000000"
39+
character(len=len(expected_output)) captured_output
40+
41+
write(captured_output, fmt = separated_values(separator=",", mold=[integer::])) [0.,1.,2.]
42+
43+
result_ = assert_equals(expected_output, captured_output)
44+
end function
45+
46+
function check_space_separated_complex() result(result_)
47+
type(result_t) result_
48+
49+
character(len=*), parameter :: expected_output = "(0.00000000,1.00000000) (1.00000000,0.00000000)"
50+
character(len=len(expected_output)) captured_output
51+
52+
write(captured_output, fmt = separated_values(separator=" ", mold=[complex::])) [(0.,1.),(1.,0.)]
53+
54+
result_ = assert_equals(expected_output, captured_output)
55+
end function
56+
57+
function check_new_line_separated_integers() result(result_)
58+
type(result_t) result_
59+
60+
character(len=*), parameter :: expected_output = ( "0" // new_line("") // "1" //new_line("") // "2")
61+
character(len=len(expected_output)) captured_output
62+
63+
write(captured_output, fmt = separated_values(separator=new_line(""), mold=[integer::])) [0,1,2]
64+
65+
result_ = assert_equals(captured_output, "0" // new_line("") // "1" //new_line("") // "2")
66+
end function
67+
68+
function check_cssv_character() result(result_)
69+
type(result_t) result_
70+
71+
integer, parameter :: num_spaces=3
72+
character(len=*), parameter :: expected_output = "Yodel, Ay, Hee, Hoo!"
73+
character(len=len(expected_output)+num_spaces) captured_output
74+
75+
write(captured_output, fmt = separated_values(separator=", ", mold=[integer::])) "Yodel", "Ay", "Hee", "Hoo!"
76+
77+
result_ = assert_equals(expected_output, captured_output)
78+
end function
79+
80+
end module formats_test

tests/main.f90

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@ program main
77
subroutine run()
88
use data_partition_test, only: &
99
data_partition_data_partition => test_data_partition
10+
use formats_test, only: &
11+
formats_object => test_object
1012
use object_interface_test, only: &
1113
object_interface_object => test_object
1214
use user_defined_collectives_test, only: &
1315
user_defined_collectives_co_all => test_co_all
1416
use vegetables, only: test_item_t, test_that, run_tests
1517

1618
type(test_item_t) :: tests
17-
type(test_item_t) :: individual_tests(3)
19+
type(test_item_t) :: individual_tests(4)
1820

1921
individual_tests(1) = data_partition_data_partition()
20-
individual_tests(2) = object_interface_object()
21-
individual_tests(3) = user_defined_collectives_co_all()
22+
individual_tests(2) = formats_object()
23+
individual_tests(3) = object_interface_object()
24+
individual_tests(4) = user_defined_collectives_co_all()
2225
tests = test_that(individual_tests)
2326

2427
call run_tests(tests)

0 commit comments

Comments
 (0)