JSON/jf_test_11 [ Unittest ]

[ Top ] [ Unittest ]

NAME

jf_test_11

DESCRIPTION

11th unit test to test unicode support if enabled

USES

json_module iso_fortran_env (intrinsic)

HISTORY

Izaak Beekman : created : 3/13/2015

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

 49 module jf_test_11_mod
 50 
 51     use json_module
 52     use, intrinsic :: iso_fortran_env , only: error_unit, output_unit, wp => real64
 53 
 54     implicit none
 55 
 56     character(len=*),parameter :: dir = '../files/inputs/'               !working directory
 57 # ifdef USE_UCS4
 58     character(len=*),parameter :: unicode_file = 'hello-world-ucs4.json'
 59 #endif
 60     character(len=*),parameter :: ascii_equivalent = 'hello-world-ascii.json'
 61 
 62 contains
 63 
 64     subroutine test_11(error_cnt)
 65 
 66 !   Read the file generated in jf_test_2, and extract some data from it.
 67 
 68     implicit none
 69 
 70     integer,intent(out) :: error_cnt
 71     character(kind=CK,len=:),allocatable :: cval
 72     type(json_file) :: json    !the JSON structure read from the file:
 73 # ifdef USE_UCS4
 74     type(json_file) :: clone
 75 # endif
 76 
 77     error_cnt = 0
 78     call json_initialize()
 79     if (json_failed()) then
 80         call json_print_error_message(error_unit)
 81         error_cnt = error_cnt + 1
 82     end if
 83 
 84     write(error_unit,'(A)') ''
 85     write(error_unit,'(A)') '================================='
 86     write(error_unit,'(A)') '   EXAMPLE 11'
 87     write(error_unit,'(A)') '================================='
 88     write(error_unit,'(A)') ''
 89 
 90 # ifdef USE_UCS4
 91     ! parse the json file:
 92     write(error_unit,'(A)') ''
 93     write(error_unit,'(A)') 'parsing file: '//dir//unicode_file
 94 
 95     call json%load_file(filename = dir//unicode_file)
 96 
 97     if (json_failed()) then    !if there was an error reading the file
 98 
 99         call json_print_error_message(error_unit)
100         error_cnt = error_cnt + 1
101 
102     else
103 
104         write(error_unit,'(A)') ''
105         write(error_unit,'(A)') 'reading data from file...'
106 
107         write(error_unit,'(A)') ''
108         call json%get('UCS4 support?', cval)
109         if (json_failed()) then
110             call json_print_error_message(error_unit)
111             error_cnt = error_cnt + 1
112         else
113             write(error_unit,'(A)') 'UCS4 support? '//cval
114         end if
115 
116         write(error_unit,'(A)') ''
117         call json%get('hello world.Amharic', cval)
118         if (json_failed()) then
119             call json_print_error_message(error_unit)
120             error_cnt = error_cnt + 1
121         else
122             write(error_unit,'(A)') 'hello world.Amharic : '//cval
123         end if
124 
125         write(error_unit,'(A)') ''
126         call json%get('hello world.Portuguese', cval)
127         if (json_failed()) then
128             call json_print_error_message(error_unit)
129             error_cnt = error_cnt + 1
130         else
131             write(error_unit,'(A)') 'hello world.Portuguese : '//cval
132         end if
133 
134         write(error_unit,'(A)') ''
135         call json%get('hello world.Russian', cval)
136         if (json_failed()) then
137             call json_print_error_message(error_unit)
138             error_cnt = error_cnt + 1
139         else
140             write(error_unit,'(A)') 'hello world.Russian : '//cval
141         end if
142 
143         write(error_unit,'(A)') ''
144         call json%get('hello world.Hebrew', cval)
145         if (json_failed()) then
146             call json_print_error_message(error_unit)
147             error_cnt = error_cnt + 1
148         else
149             write(error_unit,'(A)') 'hello world.Hebrew : '//cval
150         end if
151 
152         write(error_unit,'(A)') ''
153         call json%get('hello world.Urdu', cval)
154         if (json_failed()) then
155             call json_print_error_message(error_unit)
156             error_cnt = error_cnt + 1
157         else
158             write(error_unit,'(A)') 'hello world.Urdu : '//cval
159         end if
160 
161         write(error_unit,'(A)') ''
162         call json%print_to_string(cval)
163         if (json_failed()) then
164            call json_print_error_message(error_unit)
165            error_cnt = error_cnt + 1
166         else
167            write(error_unit,'(A)') 'The contents of the file were:'
168            write(error_unit,'(A)') cval
169         end if
170 
171         write(error_unit,'(A)') ''
172         call clone%load_from_string(cval)
173         if ( json_failed()) then
174            call json_print_error_message(error_unit)
175            error_cnt = error_cnt + 1
176         end if
177 
178         write(error_unit,'(A)') ''
179         write(error_unit,'(A)') 'Printing same file, but now to stdout:'
180         call clone%print_file(output_unit)
181         if (json_failed()) then
182            call json_print_error_message(error_unit)
183            error_cnt = error_cnt + 1
184         end if
185 
186         write(error_unit,'(A)') ''
187         write(error_unit,'(A)') 'Writing json file object to "../files/'//unicode_file//'"'
188         call clone%print_file('../files/'//unicode_file)
189         if ( json_failed() ) then
190            call json_print_error_message(error_unit)
191            error_cnt = error_cnt + 1
192         end if
193 
194     end if
195 
196     ! clean up
197     write(error_unit,'(A)') ''
198     write(error_unit,'(A)') 'destroy...'
199     call json%destroy()
200     if (json_failed()) then
201         call json_print_error_message(error_unit)
202         error_cnt = error_cnt + 1
203     end if
204     call clone%destroy()
205     if (json_failed()) then
206         call json_print_error_message(error_unit)
207         error_cnt = error_cnt + 1
208     end if
209 
210 # endif
211     ! parse the json file:
212     write(error_unit,'(A)') ''
213     write(error_unit,'(A)') 'parsing file: '//dir//ascii_equivalent
214     write(error_unit,'(A)') 'This is the ascii equivalent of "../files/inputs/hello-world-ucs4.json"'
215 
216     call json%load_file(filename = dir//ascii_equivalent)
217 
218     if (json_failed()) then    !if there was an error reading the file
219 
220         call json_print_error_message(error_unit)
221         error_cnt = error_cnt + 1
222 
223     else
224 
225         write(error_unit,'(A)') ''
226         write(error_unit,'(A)') 'reading data from file...'
227 
228         write(error_unit,'(A)') ''
229         call json%get('UCS4 support?', cval)
230         if (json_failed()) then
231             call json_print_error_message(error_unit)
232             error_cnt = error_cnt + 1
233         else
234             write(error_unit,'(A)') 'UCS4 support? '//cval
235         end if
236 
237         write(error_unit,'(A)') ''
238         call json%get('hello world.Amharic', cval)
239         if (json_failed()) then
240             call json_print_error_message(error_unit)
241             error_cnt = error_cnt + 1
242         else
243             write(error_unit,'(A)') 'hello world.Amharic : '//cval
244         end if
245 
246         write(error_unit,'(A)') ''
247         call json%get('hello world.Portuguese', cval)
248         if (json_failed()) then
249             call json_print_error_message(error_unit)
250             error_cnt = error_cnt + 1
251         else
252             write(error_unit,'(A)') 'hello world.Portuguese : '//cval
253         end if
254 
255         write(error_unit,'(A)') ''
256         call json%get('hello world.Russian', cval)
257         if (json_failed()) then
258             call json_print_error_message(error_unit)
259             error_cnt = error_cnt + 1
260         else
261             write(error_unit,'(A)') 'hello world.Russian : '//cval
262         end if
263 
264         write(error_unit,'(A)') ''
265         call json%get('hello world.Hebrew', cval)
266         if (json_failed()) then
267             call json_print_error_message(error_unit)
268             error_cnt = error_cnt + 1
269         else
270             write(error_unit,'(A)') 'hello world.Hebrew : '//cval
271         end if
272 
273         write(error_unit,'(A)') ''
274         call json%get('hello world.Urdu', cval)
275         if (json_failed()) then
276             call json_print_error_message(error_unit)
277             error_cnt = error_cnt + 1
278         else
279             write(error_unit,'(A)') 'hello world.Urdu : '//cval
280         end if
281 
282         write(error_unit,'(A)') ''
283         call json%print_to_string(cval)
284         if (json_failed()) then
285            call json_print_error_message(error_unit)
286            error_cnt = error_cnt + 1
287         else
288            write(error_unit,'(A)') 'The contents of the file were:'
289            write(error_unit,'(A)') cval
290         end if
291 
292         write(error_unit,'(A)') ''
293         write(error_unit,'(A)') 'Printing same file, but now to stdout:'
294         call json%print_file(output_unit)
295         if (json_failed()) then
296            call json_print_error_message(error_unit)
297            error_cnt = error_cnt + 1
298         end if
299 
300         write(error_unit,'(A)') ''
301         write(error_unit,'(A)') 'Writing json file object to "../files/'//ascii_equivalent//'"'
302         call json%print_file('../files/'//ascii_equivalent)
303         if ( json_failed() ) then
304            call json_print_error_message(error_unit)
305            error_cnt = error_cnt + 1
306         end if
307 
308     end if
309 
310     ! clean up
311     write(error_unit,'(A)') ''
312     write(error_unit,'(A)') 'destroy...'
313     call json%destroy()
314     if (json_failed()) then
315         call json_print_error_message(error_unit)
316         error_cnt = error_cnt + 1
317     end if
318 
319     end subroutine test_11
320 
321 end module jf_test_11_mod
322 
323 program jf_test_11
324     use jf_test_11_mod , only: test_11
325     implicit none
326     integer :: n_errors
327     n_errors = 0
328     call test_11(n_errors)
329     if (n_errors /= 0) stop 1
330 end program jf_test_11