|
| 1 | +!***************************************************************************************** |
| 2 | +!> |
| 3 | +! Module for the 44th unit test |
| 4 | + |
| 5 | +module jf_test_44_mod |
| 6 | + |
| 7 | + use json_module, CK => json_CK, IK => json_IK, RK => json_RK, LK => json_LK |
| 8 | + use, intrinsic :: iso_fortran_env , only: error_unit, output_unit |
| 9 | + |
| 10 | + implicit none |
| 11 | + |
| 12 | + private |
| 13 | + public :: test_44 |
| 14 | + |
| 15 | +contains |
| 16 | + |
| 17 | + subroutine test_44(error_cnt) |
| 18 | + |
| 19 | + !! testing recursive `json_get_array` |
| 20 | + |
| 21 | + implicit none |
| 22 | + |
| 23 | + integer,intent(out) :: error_cnt !! error counter |
| 24 | + |
| 25 | + character(kind=CK,len=*),parameter :: str = CK_'{ "x": [0, [1,2,3], [4,5,6] ] }' |
| 26 | + |
| 27 | + type(json_core) :: json |
| 28 | + type(json_value),pointer :: p, x |
| 29 | + logical :: found |
| 30 | + |
| 31 | + write(error_unit,'(A)') '' |
| 32 | + write(error_unit,'(A)') '=================================' |
| 33 | + write(error_unit,'(A)') ' TEST 44' |
| 34 | + write(error_unit,'(A)') '=================================' |
| 35 | + write(error_unit,'(A)') '' |
| 36 | + |
| 37 | + error_cnt = 0 |
| 38 | + |
| 39 | + call json%initialize(path_mode=1_IK) |
| 40 | + call json%deserialize(p,str) |
| 41 | + |
| 42 | + if (json%failed()) then |
| 43 | + call json%print_error_message(error_unit) |
| 44 | + error_cnt = error_cnt + 1 |
| 45 | + else |
| 46 | + call json%get(p,CK_'x',x) |
| 47 | + call json%get(x, callback) |
| 48 | + end if |
| 49 | + |
| 50 | + if (json%failed()) then |
| 51 | + call json%print_error_message(error_unit) |
| 52 | + error_cnt = error_cnt + 1 |
| 53 | + end if |
| 54 | + |
| 55 | + if (error_cnt==0) then |
| 56 | + write(error_unit,'(A)') 'Success!' |
| 57 | + else |
| 58 | + write(error_unit,'(A)') 'Failed!' |
| 59 | + end if |
| 60 | + write(error_unit,'(A)') '' |
| 61 | + |
| 62 | + end subroutine test_44 |
| 63 | + |
| 64 | + recursive subroutine callback(json, element, i, count) |
| 65 | + !! Array element callback function. Used by [[json_get_array]] |
| 66 | + |
| 67 | + implicit none |
| 68 | + |
| 69 | + class(json_core),intent(inout) :: json |
| 70 | + type(json_value),pointer,intent(in) :: element |
| 71 | + integer(IK),intent(in) :: i !! index |
| 72 | + integer(IK),intent(in) :: count !! size of array |
| 73 | + |
| 74 | + integer(IK) :: var_type |
| 75 | + character(kind=CK,len=:),allocatable :: path |
| 76 | + integer(IK) :: ival |
| 77 | + |
| 78 | + call json%get_path(element, path) |
| 79 | + |
| 80 | + call json%info(element,var_type=var_type) |
| 81 | + |
| 82 | + if (var_type == json_array) then |
| 83 | + write(output_unit,'(A)') 'array: '//trim(path) |
| 84 | + call json%get(element, callback) |
| 85 | + else if (var_type == json_integer) then |
| 86 | + call json%get(element,ival) |
| 87 | + !write(output_unit,'(A,1X,I2)') trim(path)//' = ', ival ! see Issue #452 |
| 88 | + write(output_unit,'(I2,A,I2)') i,' : ', ival |
| 89 | + end if |
| 90 | + |
| 91 | + end subroutine callback |
| 92 | + |
| 93 | +end module jf_test_44_mod |
| 94 | +!***************************************************************************************** |
| 95 | + |
| 96 | +#ifndef INTEGRATED_TESTS |
| 97 | +!***************************************************************************************** |
| 98 | +program jf_test_44 |
| 99 | + |
| 100 | + !! 44th unit test. |
| 101 | + |
| 102 | + use jf_test_44_mod , only: test_44 |
| 103 | + implicit none |
| 104 | + integer :: n_errors |
| 105 | + n_errors = 0 |
| 106 | + call test_44(n_errors) |
| 107 | + if (n_errors /= 0) stop 1 |
| 108 | + |
| 109 | +end program jf_test_44 |
| 110 | +!***************************************************************************************** |
| 111 | +#endif |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
0 commit comments