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