|
| 1 | +!***************************************************************************************** |
| 2 | +!> |
| 3 | +! Unit test for [[json_value_reverse]]. |
| 4 | +! |
| 5 | +!@note This uses Fortran 2008 auto LHS assignments. |
| 6 | + |
| 7 | + program jf_test_28 |
| 8 | + |
| 9 | + use json_module |
| 10 | + use iso_fortran_env |
| 11 | + |
| 12 | + implicit none |
| 13 | + |
| 14 | + type(json_core) :: json |
| 15 | + type(json_value),pointer :: p,vec |
| 16 | + integer(json_IK),dimension(:),allocatable :: ivec |
| 17 | + integer(json_IK),dimension(:),allocatable :: ivec_value,ivec_value_reversed |
| 18 | + character(kind=json_CK,len=:),allocatable :: str |
| 19 | + integer :: i !! counter |
| 20 | + |
| 21 | + write(error_unit,'(A)') '' |
| 22 | + write(error_unit,'(A)') '=================================' |
| 23 | + write(error_unit,'(A)') ' TEST 28' |
| 24 | + write(error_unit,'(A)') '=================================' |
| 25 | + write(error_unit,'(A)') '' |
| 26 | + |
| 27 | + call json%initialize(compress_vectors=.true.) |
| 28 | + |
| 29 | + do i=1,4 |
| 30 | + |
| 31 | + ! all the cases: |
| 32 | + select case (i) |
| 33 | + case(1) |
| 34 | + str = json_CK_'{"vec":[1,2,3,4,5]}' |
| 35 | + ivec_value = [1,2,3,4,5] |
| 36 | + ivec_value_reversed = [5,4,3,2,1] |
| 37 | + case(2) |
| 38 | + str = json_CK_'{"vec":[1]}' |
| 39 | + ivec_value = [1] |
| 40 | + ivec_value_reversed = [1] |
| 41 | + case(3) |
| 42 | + str = json_CK_'{"vec":[1,2]}' |
| 43 | + ivec_value = [1,2] |
| 44 | + ivec_value_reversed = [2,1] |
| 45 | + case(4) |
| 46 | + str = json_CK_'{"vec":[]}' |
| 47 | + !ivec_value = [] |
| 48 | + !ivec_value_reversed = [] |
| 49 | + end select |
| 50 | + |
| 51 | + call json%parse(p,str) |
| 52 | + call json%get(p,'vec',vec) |
| 53 | + |
| 54 | + write(output_unit,'(A)') '' |
| 55 | + write(output_unit,'(A)') 'Original:' |
| 56 | + write(output_unit,'(A)') '' |
| 57 | + call json%print(vec,output_unit) |
| 58 | + |
| 59 | + call json%reverse(vec) |
| 60 | + |
| 61 | + write(output_unit,'(A)') '' |
| 62 | + write(output_unit,'(A)') 'Reversed:' |
| 63 | + write(output_unit,'(A)') '' |
| 64 | + call json%print(vec,output_unit) |
| 65 | + |
| 66 | + call json%get(vec,ivec) |
| 67 | + call json%destroy(p) |
| 68 | + |
| 69 | + if (json%failed()) then |
| 70 | + call json%print_error_message(error_unit) |
| 71 | + stop 1 |
| 72 | + else |
| 73 | + |
| 74 | + if (allocated(ivec)) then |
| 75 | + if (i/=4) then |
| 76 | + if (all(ivec==ivec_value_reversed)) then |
| 77 | + write(output_unit,'(A)') 'reverse test passed' |
| 78 | + else |
| 79 | + write(output_unit,'(A,*(I3,1X))') 'reverse test failed: ', ivec |
| 80 | + stop 1 |
| 81 | + end if |
| 82 | + else |
| 83 | + if (size(ivec)==0) then |
| 84 | + write(output_unit,'(A)') 'reverse test passed' |
| 85 | + else |
| 86 | + write(output_unit,'(A,*(I3,1X))') 'reverse test failed: ', ivec |
| 87 | + stop 1 |
| 88 | + end if |
| 89 | + end if |
| 90 | + else |
| 91 | + write(output_unit,'(A)') 'reverse test failed: error getting ivec' |
| 92 | + stop 1 |
| 93 | + end if |
| 94 | + |
| 95 | + end if |
| 96 | + |
| 97 | + end do |
| 98 | + |
| 99 | + end program jf_test_28 |
| 100 | +!***************************************************************************************** |
0 commit comments