@@ -16,7 +16,7 @@ module formats_test_m
1616
1717 pure function subject () result(specimen)
1818 character (len= :), allocatable :: specimen
19- specimen = " The csv format"
19+ specimen = " A format string "
2020 end function
2121
2222 function results () result(test_results)
@@ -32,68 +32,54 @@ function results() result(test_results)
3232
3333 function check_csv_reals () result(test_passes)
3434 logical test_passes
35- character (len=* ), parameter :: longest_expected_output = " 0.00000000,1.00000000,2.00000000"
36- character (len= len (longest_expected_output)) captured_output
37- character (len= :), allocatable :: substring
35+ character (len= 100 ) captured_output
36+ real zero, one, two
3837
3938 write (captured_output, fmt = separated_values(separator= " ," , mold= [integer :: ])) [0 .,1 .,2 .]
4039
41- substring = adjustl (trim (captured_output))
42- associate(leading_real_zero = > index (substring, " 0." ) == 1 )
43- substring = adjustl (substring(index (substring, " ," )+ 1 :)) ! get remaining string after fierst comma
44- associate(followed_by_real_one = > index (substring, " 1." ) == 1 )
45- substring = adjustl (substring(index (substring, " ," )+ 1 :)) ! get remaining string after fierst comma
46- associate(followed_by_real_two = > index (substring, " 2." ) == 1 )
47- test_passes = leading_real_zero .and. followed_by_real_one .and. followed_by_real_two
48- end associate
40+ associate(first_comma = > index (captured_output, ' ,' ))
41+ associate(second_comma = > first_comma + index (captured_output(first_comma+1 :), ' ,' ))
42+ read (captured_output(:first_comma-1 ), * ) zero
43+ read (captured_output(first_comma+1 :second_comma-1 ), * ) one
44+ read (captured_output(second_comma+1 :), * ) two
45+ test_passes = (zero== 0 .) .and. (one== 1 .) .and. (two== 2 .)
4946 end associate
5047 end associate
5148 end function
5249
5350 function check_space_separated_complex () result(test_passes)
5451 logical test_passes
55- character (len=* ), parameter :: longest_expected_output = " (0.00000000,1.00000000) (1.00000000,0.00000000) "
56- character (len= len (longest_expected_output)) captured_output
52+ character (len= 100 ) captured_output
53+ character (len= :), allocatable :: i_string, one_string
5754 complex , parameter :: i = (0 .,1 .), one = (1 .,0 .)
58- character (len = :), allocatable :: i_imag_part, one_imag_part, i_string, one_string
55+ complex i_read, one_read
5956
6057 write (captured_output, fmt = separated_values(separator= " " , mold= [complex :: ])) i,one
6158
62- i_string = adjustl (trim (captured_output(:index (captured_output," )" ))))
63- one_string = adjustl (trim (captured_output(len (i_string)+ 1 :)))
64-
65- associate( &
66- i_real_part_zero = > index (i_string," (0." ) == 1 , &
67- one_real_part_one = > index (one_string," (1." ) == 1 &
68- )
69- i_imag_part = adjustl (i_string(index (i_string," ," )+ 1 :index (i_string," )" )- 1 ))
70- one_imag_part = adjustl (one_string(index (one_string," ," )+ 1 :index (one_string," )" )- 1 ))
71- associate( &
72- i_imag_part_one = > index (i_imag_part," 1." ) == 1 , &
73- one_imag_part_zero = > index (one_imag_part," 0." ) == 1 &
74- )
75- test_passes = i_real_part_zero .and. i_imag_part_one .and. one_real_part_one .and. one_imag_part_zero
76- end associate
77- end associate
59+ i_string = captured_output(:index (captured_output," )" ))
60+ one_string = captured_output(len (i_string)+ 1 :)
61+
62+ read (i_string,* ) i_read
63+ read (one_string,* ) one_read
64+
65+ test_passes = i_read == i .and. one_read == one
7866 end function
7967
80- pure function check_new_line_separated_integers () result(test_passes)
68+ function check_csv_character () result(test_passes)
8169 logical test_passes
82- character (len=* ), parameter :: expected_output = ( " 0 " // new_line( " " ) // " 1 " // new_line( " " ) // " 2 " )
83- character (len= len (expected_output)) captured_output
70+ character (len= 200 ) captured_output
71+ character (len=* ), parameter :: expected_output = " Yodel, Ay, Hee, Hoo! "
8472
85- write (captured_output, fmt = separated_values(separator= new_line( " " ) , mold= [integer :: ])) [ 0 , 1 , 2 ]
86- test_passes = captured_output == " 0 " // new_line( " " ) // " 1 " // new_line( " " ) // " 2 "
73+ write (captured_output, fmt = separated_values(separator= " , " , mold= [integer :: ])) " Yodel " , " Ay " , " Hee " , " Hoo! "
74+ test_passes = expected_output == captured_output
8775 end function
8876
89- pure function check_csv_character () result(test_passes)
77+ function check_new_line_separated_integers () result(test_passes)
9078 logical test_passes
91- integer , parameter :: num_spaces= 3
92- character (len=* ), parameter :: expected_output = " Yodel, Ay, Hee, Hoo!"
93- character (len= len (expected_output)+ num_spaces) captured_output
79+ character (len= 100 ) captured_output
9480
95- write (captured_output, fmt = separated_values(separator= " , " , mold= [integer :: ])) " Yodel " , " Ay " , " Hee " , " Hoo! "
96- test_passes= expected_output == captured_output
81+ write (captured_output, fmt = separated_values(separator= new_line( " " ) , mold= [integer :: ])) [ 0 , 1 , 2 ]
82+ test_passes = captured_output == " 0 " // new_line( " " ) // " 1 " // new_line( " " ) // " 2 "
9783 end function
9884
9985end module formats_test_m
0 commit comments