|
| 1 | +!***************************************************************************************** |
| 2 | +!> author: Jacob Williams |
| 3 | +! date: 7/17/2016 |
| 4 | +! |
| 5 | +! Test the `insert` routine. |
| 6 | + |
| 7 | +module jf_test_20_mod |
| 8 | + |
| 9 | + use json_module, lk => json_lk, rk => json_rk, ik => json_ik,& |
| 10 | + ck => json_ck, cdk => json_cdk |
| 11 | + use, intrinsic :: iso_fortran_env , only: error_unit,output_unit |
| 12 | + |
| 13 | + implicit none |
| 14 | + |
| 15 | +contains |
| 16 | + |
| 17 | + subroutine test_20(error_cnt) |
| 18 | + |
| 19 | + implicit none |
| 20 | + |
| 21 | + integer,intent(out) :: error_cnt !! report number of errors to caller |
| 22 | + |
| 23 | + type(json_core) :: json |
| 24 | + type(json_value),pointer :: p,new,element |
| 25 | + logical(lk) :: found,is_valid |
| 26 | + integer(IK),dimension(:),allocatable :: iarray |
| 27 | + character(kind=CK,len=:),allocatable :: error_msg |
| 28 | + |
| 29 | + character(kind=CK,len=*),parameter :: json_example = '{"x":[1,2,3,4]}' |
| 30 | + |
| 31 | + write(error_unit,'(A)') '' |
| 32 | + write(error_unit,'(A)') '=================================' |
| 33 | + write(error_unit,'(A)') ' TEST 20' |
| 34 | + write(error_unit,'(A)') '=================================' |
| 35 | + write(error_unit,'(A)') '' |
| 36 | + |
| 37 | + error_cnt = 0 |
| 38 | + |
| 39 | + call json%parse(p,json_example) |
| 40 | + if (json%failed()) then |
| 41 | + call json%print_error_message(error_unit) |
| 42 | + error_cnt = error_cnt + 1 |
| 43 | + else |
| 44 | + |
| 45 | + !insert one in the middle: |
| 46 | + nullify(element) |
| 47 | + call json%get(p,'x(3)',element) ! get pointer to an array element in the file |
| 48 | + if (json%failed()) then |
| 49 | + call json%print_error_message(error_unit) |
| 50 | + error_cnt = error_cnt + 1 |
| 51 | + else |
| 52 | + call json%create_integer(new,33,'') ! create a new element |
| 53 | + call json%insert_after(element,new) ! insert new element after x(3) |
| 54 | + if (json%failed()) then |
| 55 | + call json%print_error_message(error_unit) |
| 56 | + error_cnt = error_cnt + 1 |
| 57 | + else |
| 58 | + call json%get(p,'x',iarray) |
| 59 | + if (.not. all(iarray==[1,2,3,33,4])) then |
| 60 | + write(error_unit,'(A,1x,*(I2,1X))') 'Error: unexpected output:',iarray |
| 61 | + error_cnt = error_cnt + 1 |
| 62 | + else |
| 63 | + write(error_unit,'(A,1x,*(I2,1X))') 'Success:',iarray |
| 64 | + end if |
| 65 | + end if |
| 66 | + end if |
| 67 | + |
| 68 | + !insert one at the end: |
| 69 | + nullify(element) |
| 70 | + call json%get(p,'x(5)',element) ! get pointer to an array element in the file |
| 71 | + if (json%failed()) then |
| 72 | + call json%print_error_message(error_unit) |
| 73 | + error_cnt = error_cnt + 1 |
| 74 | + else |
| 75 | + call json%create_integer(new,44,'') ! create a new element |
| 76 | + call json%insert_after(element,new) ! insert new element after x(3) |
| 77 | + if (json%failed()) then |
| 78 | + call json%print_error_message(error_unit) |
| 79 | + error_cnt = error_cnt + 1 |
| 80 | + else |
| 81 | + call json%get(p,'x',iarray) |
| 82 | + if (.not. all(iarray==[1,2,3,33,4,44])) then |
| 83 | + write(error_unit,'(A,1x,*(I2,1X))') 'Error: unexpected output:',iarray |
| 84 | + error_cnt = error_cnt + 1 |
| 85 | + else |
| 86 | + write(error_unit,'(A,1x,*(I2,1X))') 'Success:',iarray |
| 87 | + end if |
| 88 | + end if |
| 89 | + end if |
| 90 | + |
| 91 | + call json%validate(p,is_valid,error_msg) |
| 92 | + if (.not. is_valid) then |
| 93 | + write(error_unit,'(A)') trim(error_msg) |
| 94 | + error_cnt = error_cnt + 1 |
| 95 | + end if |
| 96 | + |
| 97 | + end if |
| 98 | + |
| 99 | + ! cleanup: |
| 100 | + call json%destroy(p) |
| 101 | + |
| 102 | + write(error_unit,'(A)') '' |
| 103 | + write(error_unit,'(A)') '=================================' |
| 104 | + write(error_unit,'(A)') '' |
| 105 | + |
| 106 | + end subroutine test_20 |
| 107 | + |
| 108 | +end module jf_test_20_mod |
| 109 | +!***************************************************************************************** |
| 110 | + |
| 111 | +!***************************************************************************************** |
| 112 | +program jf_test_20 |
| 113 | + |
| 114 | + !! 20th unit test. |
| 115 | + |
| 116 | + use jf_test_20_mod, only: test_20 |
| 117 | + |
| 118 | + implicit none |
| 119 | + |
| 120 | + integer :: n_errors |
| 121 | + call test_20(n_errors) |
| 122 | + if ( n_errors /= 0) stop 1 |
| 123 | + |
| 124 | +end program jf_test_20 |
| 125 | +!***************************************************************************************** |
0 commit comments