Skip to content

Commit 939dddc

Browse files
committed
bug fix in print to string code. it didn't work if the string being appended was larger than the chunk size.
added a unit test.
1 parent 4d53788 commit 939dddc

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/json_value_module.F90

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5702,7 +5702,9 @@ subroutine write_it(advance,comma,space_after_comma)
57025702
logical(LK) :: add_comma !! if a delimiter is to be added after string
57035703
logical(LK) :: add_line_break !! if a line break is to be added after string
57045704
logical(LK) :: add_space !! if a space is to be added after the comma
5705-
integer(IK) :: n !! length of actual string appended to `str`
5705+
integer(IK) :: n !! length of actual string `s` appended to `str`
5706+
integer(IK) :: room_left !! number of characters left in `str`
5707+
integer(IK) :: n_chunks_to_add !! number of chunks to add to `str` for appending `s`
57065708

57075709
if (present(comma)) then
57085710
add_comma = comma
@@ -5752,9 +5754,11 @@ subroutine write_it(advance,comma,space_after_comma)
57525754
if (add_line_break) s = s // newline
57535755

57545756
n = len(s)
5755-
if (len(str)-iloc < n) then
5757+
room_left = len(str)-iloc
5758+
if (room_left < n) then
57565759
! need to add another chunk to fit this string:
5757-
str = str // repeat(space, print_str_chunk_size)
5760+
n_chunks_to_add = max(1_IK, ceiling( real(len(s)-room_left,RK) / real(chunk_size,RK), IK ) )
5761+
str = str // repeat(space, print_str_chunk_size*n_chunks_to_add)
57585762
end if
57595763
! append s to str:
57605764
str(iloc+1:iloc+n-1) = s

src/tests/jf_test_36.F90

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
!*****************************************************************************************
2+
!> author: Jacob Williams
3+
! date: 1/15/2019
4+
!
5+
! Module for the 36th unit test.
6+
7+
module jf_test_36_mod
8+
9+
use json_module
10+
use, intrinsic :: iso_fortran_env , only: error_unit, output_unit
11+
12+
implicit none
13+
14+
private
15+
public :: test_36
16+
17+
contains
18+
19+
subroutine test_36(error_cnt)
20+
21+
!! Test writing a large JSON structure to a string.
22+
23+
implicit none
24+
25+
integer,intent(out) :: error_cnt !! report number of errors to caller
26+
27+
type(json_file) :: my_file
28+
character(kind=json_CK,len=:),allocatable :: str_in, str_out
29+
30+
write(error_unit,'(A)') ''
31+
write(error_unit,'(A)') '================================='
32+
write(error_unit,'(A)') ' TEST 36'
33+
write(error_unit,'(A)') '================================='
34+
write(error_unit,'(A)') ''
35+
36+
error_cnt = 0
37+
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()
56+
57+
if (error_cnt==0) write(error_unit,'(A)') ' Success!'
58+
59+
end subroutine test_36
60+
61+
end module jf_test_36_mod
62+
!*****************************************************************************************
63+
64+
#ifndef INTERGATED_TESTS
65+
!*****************************************************************************************
66+
program jf_test_36
67+
68+
!! 36th unit test.
69+
70+
use jf_test_36_mod, only: test_36
71+
implicit none
72+
integer :: n_errors
73+
call test_36(n_errors)
74+
if ( n_errors /= 0) stop 1
75+
76+
end program jf_test_36
77+
!*****************************************************************************************
78+
#endif

visual_studio/jsonfortrantest/jsonfortrantest.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ program jsonfortrantest
4242
use jf_test_33_mod , only: test_33
4343
use jf_test_34_mod , only: test_34
4444
use jf_test_35_mod , only: test_35
45+
use jf_test_36_mod , only: test_36
4546

4647
implicit none
4748

@@ -84,6 +85,7 @@ program jsonfortrantest
8485
call test_33(n_errors); if (n_errors /= 0) stop 1
8586
call test_34(n_errors); if (n_errors /= 0) stop 1
8687
call test_35(n_errors); if (n_errors /= 0) stop 1
88+
call test_36(n_errors); if (n_errors /= 0) stop 1
8789

8890
end program jsonfortrantest
8991
!*****************************************************************************************

visual_studio/jsonfortrantest/jsonfortrantest.vfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@
8181
<File RelativePath="..\..\src\tests\jf_test_33.F90"/>
8282
<File RelativePath="..\..\src\tests\jf_test_34.F90"/>
8383
<File RelativePath="..\..\src\tests\jf_test_35.F90"/>
84+
<File RelativePath="..\..\src\tests\jf_test_36.F90"/>
8485
<File RelativePath=".\jsonfortrantest.f90"/></Filter></Files>
8586
<Globals/></VisualStudioProject>

0 commit comments

Comments
 (0)