JSON/jf_test_9 [ Unittest ]

[ Top ] [ Unittest ]

NAME

jf_test_9

DESCRIPTION

Ninth unit test.

AUTHOR

Jacob Williams : 3/2/3015

LICENSE

JSON-FORTRAN: A Fortran 2008 JSON API

https://github.com/jacobwilliams/json-fortran

Copyright (c) 2014, Jacob Williams

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SOURCE

 45 module jf_test_9_mod
 46 
 47     use json_module
 48     use, intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp => real64
 49 
 50     implicit none
 51 
 52     !small file - 0.0 sec : http://www.json-generator.com
 53     !character(len=*),parameter :: filename = 'random1.json'
 54 
 55     !7 MB - 5.4 sec : http://www.json-generator.com
 56     character(len=*),parameter :: filename = 'big.json'
 57 
 58     !13 MB - 7.6 sec : http://mtgjson.com
 59     !character(len=*),parameter :: filename = 'AllSets.json'
 60 
 61     !....WARNING: this file is causing some error.... (bug in code?)
 62     !100 MB - takes forever... : https://github.com/seductiveapps/largeJSON
 63     !character(len=*),parameter :: filename = '100mb.json'
 64 
 65     !small file that contains unicode characters:
 66     !character(len=*),parameter :: filename = 'hello-world-ucs4.json'  !!!! test !!!!
 67 
 68     character(len=*),parameter :: dir = '../files/inputs/' !working directory
 69 
 70 contains
 71 
 72     subroutine test_9(error_cnt)
 73 
 74 !   Open a random JSON file generated by http://www.json-generator.com
 75 
 76     implicit none
 77 
 78     integer,intent(out) :: error_cnt
 79 
 80     type(json_file) :: f
 81     real :: tstart, tend
 82     character(len=:),allocatable :: str
 83 
 84     error_cnt = 0
 85     call json_initialize()
 86     if (json_failed()) then
 87         call json_print_error_message(error_unit)
 88         error_cnt = error_cnt + 1
 89     end if
 90 
 91     write(error_unit,'(A)') ''
 92     write(error_unit,'(A)') '================================='
 93     write(error_unit,'(A)') '   EXAMPLE 9a '
 94     write(error_unit,'(A)') '================================='
 95 
 96     write(error_unit,'(A)') ''
 97     write(error_unit,'(A)') '  Load a file using json_file%load_file'
 98     write(error_unit,'(A)') ''
 99     write(error_unit,'(A)') 'Loading file: '//trim(filename)
100 
101     call cpu_time(tstart)
102     call f%load_file(dir//filename)
103     call cpu_time(tend)
104     write(error_unit,'(A,1X,F10.3,1X,A)') 'Elapsed time: ',tend-tstart,' sec'
105 
106     if (json_failed()) then
107         call json_print_error_message(error_unit)
108         error_cnt = error_cnt + 1
109     else
110         write(error_unit,'(A)') 'File successfully read'
111     end if
112     write(error_unit,'(A)') ''
113 
114     !cleanup:
115     call f%destroy()
116 
117     write(error_unit,'(A)') ''
118     write(error_unit,'(A)') '================================='
119     write(error_unit,'(A)') '   EXAMPLE 9b '
120     write(error_unit,'(A)') '================================='
121 
122     write(error_unit,'(A)') ''
123     write(error_unit,'(A)') '  Load a file using json_file%load_from_string'
124     write(error_unit,'(A)') ''
125     write(error_unit,'(A)') 'Loading file: '//trim(filename)
126 
127     call cpu_time(tstart)
128     call read_file(dir//filename, str)
129 
130     if (allocated(str)) then
131         call f%load_from_string(str)
132         call cpu_time(tend)
133         write(error_unit,'(A,1X,F10.3,1X,A)') 'Elapsed time to parse: ',tend-tstart,' sec'
134         if (json_failed()) then
135             call json_print_error_message(error_unit)
136             error_cnt = error_cnt + 1
137         else
138             write(error_unit,'(A)') 'File successfully read'
139         end if
140         write(error_unit,'(A)') ''
141         !write(error_unit,'(A)') str   !!!! test !!!!
142         !write(error_unit,'(A)') ''    !!!! test !!!!
143     else
144         write(error_unit,'(A)') 'Error loading file'
145     end if
146 
147     !cleanup:
148     call f%destroy()
149 
150     end subroutine test_9
151 
152     subroutine read_file(filename,str)
153     !
154     ! Reads the contents of the file into the allocatable string str.
155     ! If there are any problems, str will be returned unallocated.
156     !
157 
158     ! Will this routine work if the file contains unicode characters??
159 
160     implicit none
161 
162     character(len=*),intent(in) :: filename
163     character(len=:),allocatable,intent(out) :: str
164 
165     integer :: iunit,istat,filesize
166 
167     open( newunit = iunit,&
168           file    = filename,&
169           status  = 'OLD',&
170           form    = 'UNFORMATTED',&
171           access  = 'STREAM',&
172           iostat  = istat )
173 
174     if (istat==0) then
175         inquire(file=filename, size=filesize)
176         if (filesize>0) then
177             allocate( character(len=filesize) :: str )
178             read(iunit,pos=1,iostat=istat) str
179             if (istat/=0) deallocate(str)
180             close(iunit, iostat=istat)
181         end if
182     end if
183 
184     end subroutine read_file
185 
186 end module jf_test_9_mod
187 
188 program jf_test_9
189     use jf_test_9_mod , only: test_9
190     implicit none
191     integer :: n_errors
192     n_errors = 0
193     call test_9(n_errors)
194     if (n_errors /= 0) stop 1
195 end program jf_test_9