Skip to content

Commit fd3cdc4

Browse files
committed
Fixed a bug in the print_to_string feature. Fixes #380.
Expanded the unit test for this routine.
1 parent 939dddc commit fd3cdc4

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
lines changed

src/json_value_module.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5761,7 +5761,7 @@ subroutine write_it(advance,comma,space_after_comma)
57615761
str = str // repeat(space, print_str_chunk_size*n_chunks_to_add)
57625762
end if
57635763
! append s to str:
5764-
str(iloc+1:iloc+n-1) = s
5764+
str(iloc+1:iloc+n) = s
57655765
iloc = iloc + n
57665766

57675767
end if

src/tests/jf_test_36.F90

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ subroutine test_36(error_cnt)
2626

2727
type(json_file) :: my_file
2828
character(kind=json_CK,len=:),allocatable :: str_in, str_out
29+
integer :: i !! counter for number of test cases
30+
integer :: j !! counter
31+
integer :: n !! size of strings to append
2932

3033
write(error_unit,'(A)') ''
3134
write(error_unit,'(A)') '================================='
@@ -35,24 +38,78 @@ subroutine test_36(error_cnt)
3538

3639
error_cnt = 0
3740

38-
! create a long string:
39-
str_in = '{"long_string": "' // repeat('a', 10000) //'"}'
40-
41-
call my_file%initialize()
42-
43-
call my_file%load_from_string(str_in)
44-
45-
if (my_file%failed()) then
46-
call my_file%print_error_message(error_unit)
47-
error_cnt = error_cnt + 1
48-
end if
49-
call my_file%print_to_string(str_out)
50-
if (my_file%failed()) then
51-
call my_file%print_error_message(error_unit)
52-
error_cnt = error_cnt + 1
53-
end if
54-
55-
call my_file%destroy()
41+
do i = 1, 2
42+
43+
do n = 0, 200
44+
45+
str_in = ''
46+
str_out = ''
47+
48+
! create some long strings:
49+
! [the idea here is to check the chunk
50+
! code for different sized strings]
51+
select case (i)
52+
case (1)
53+
! one big string
54+
str_in = '{"long_string":"' // repeat('a', 10000) //'"}'
55+
case (2:)
56+
! a lot of little strings
57+
str_in = '{"big_array":['
58+
do j = 1, 1000
59+
str_in = str_in // '"' // repeat('a', n) // '"'
60+
if (j<1000) str_in = str_in // ','
61+
end do
62+
str_in = str_in//']}'
63+
end select
64+
65+
! don't print extra spaces, so the result will match the input exactly
66+
call my_file%initialize(no_whitespace=.true.)
67+
68+
! load from the original string:
69+
call my_file%load_from_string(str_in)
70+
if (my_file%failed()) then
71+
call my_file%print_error_message(error_unit)
72+
error_cnt = error_cnt + 1
73+
end if
74+
75+
! now, write it to a new string:
76+
call my_file%print_to_string(str_out)
77+
if (my_file%failed()) then
78+
call my_file%print_error_message(error_unit)
79+
error_cnt = error_cnt + 1
80+
end if
81+
call my_file%destroy()
82+
83+
! verify that the strings are the same:
84+
if (str_in /= str_out) then
85+
write(error_unit,'(A,1X,I2,1X,I4)') ' Error: the strings are not the same for case', i, n
86+
error_cnt = error_cnt + 1
87+
if (n==2) then
88+
write(error_unit,'(A)') ''
89+
write(error_unit,'(A)') '----str_in:'
90+
write(error_unit,'(A)') str_in
91+
write(error_unit,'(A)') ''
92+
write(error_unit,'(A)') '----str_out:'
93+
write(error_unit,'(A)') str_out
94+
write(error_unit,'(A)') ''
95+
write(error_unit,'(A)') ''
96+
error stop
97+
end if
98+
end if
99+
100+
! now load the string again to verify that it
101+
! printed correctly without errors:
102+
call my_file%load_from_string(str_out)
103+
if (my_file%failed()) then
104+
call my_file%print_error_message(error_unit)
105+
error_cnt = error_cnt + 1
106+
end if
107+
108+
if (i==1) exit ! only one for this case
109+
110+
end do
111+
112+
end do
56113

57114
if (error_cnt==0) write(error_unit,'(A)') ' Success!'
58115

0 commit comments

Comments
 (0)