|
| 1 | +!***************************************************************************************** |
| 2 | +!> author: Jacob Williams |
| 3 | +! date: 09/02/2015 |
| 4 | +! |
| 5 | +! Module for the 14th unit test. |
| 6 | + |
| 7 | +module jf_test_14_mod |
| 8 | + |
| 9 | + use json_module |
| 10 | + use, intrinsic :: iso_fortran_env , only: error_unit,output_unit |
| 11 | + |
| 12 | + implicit none |
| 13 | + |
| 14 | + character(len=*),parameter :: dir = '../files/inputs/' !! working directory |
| 15 | + character(len=*),parameter :: filename1 = 'test1.json' !! the file to read |
| 16 | + integer :: icount = 0 !! a count of the number of "name" variables found |
| 17 | + |
| 18 | +contains |
| 19 | + |
| 20 | + subroutine test_14(error_cnt) |
| 21 | + |
| 22 | + !! Tests the traversal of a JSON structure |
| 23 | + !! |
| 24 | + !! It traverses the structure, looks for all "name" variables, and changes the name. |
| 25 | + |
| 26 | + implicit none |
| 27 | + |
| 28 | + integer,intent(out) :: error_cnt !! report number of errors to caller |
| 29 | + |
| 30 | + type(json_value),pointer :: json |
| 31 | + |
| 32 | + write(error_unit,'(A)') '' |
| 33 | + write(error_unit,'(A)') '=================================' |
| 34 | + write(error_unit,'(A)') ' TEST 14' |
| 35 | + write(error_unit,'(A)') '=================================' |
| 36 | + write(error_unit,'(A)') '' |
| 37 | + |
| 38 | + error_cnt = 0 |
| 39 | + icount = 0 !number of name changes (should be 2) |
| 40 | + |
| 41 | + call json_initialize() !initialize the module |
| 42 | + |
| 43 | + call json_parse(dir//filename1,json) !read the file |
| 44 | + if (json_failed()) then |
| 45 | + call json_print_error_message(error_unit) |
| 46 | + error_cnt = error_cnt + 1 |
| 47 | + end if |
| 48 | + |
| 49 | + call json_traverse(json,rename) !traverse all nodes in the structure |
| 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 (icount/=2) then |
| 56 | + write(error_unit,'(A)') 'Error: should be 2 "name" variables in this file: '//filename1 |
| 57 | + error_cnt = error_cnt + 1 |
| 58 | + end if |
| 59 | + |
| 60 | + if (error_cnt==0) then |
| 61 | + write(error_unit,'(A)') '' |
| 62 | + write(error_unit,'(A)') ' All names changed to Fred:' |
| 63 | + write(error_unit,'(A)') '' |
| 64 | + call json_print(json,output_unit) |
| 65 | + write(error_unit,'(A)') '' |
| 66 | + end if |
| 67 | + |
| 68 | + call json_destroy(json) !clean up |
| 69 | + if (json_failed()) then |
| 70 | + call json_print_error_message(error_unit) |
| 71 | + error_cnt = error_cnt + 1 |
| 72 | + end if |
| 73 | + |
| 74 | + end subroutine test_14 |
| 75 | + |
| 76 | + subroutine rename(p,finished) !! change all "name" variable values to "Fred" |
| 77 | + |
| 78 | + implicit none |
| 79 | + |
| 80 | + type(json_value),pointer,intent(in) :: p |
| 81 | + logical,intent(out) :: finished |
| 82 | + |
| 83 | + integer :: var_type |
| 84 | + character(kind=CK,len=:),allocatable :: str |
| 85 | + logical :: found |
| 86 | + |
| 87 | + !get info about this variable: |
| 88 | + call json_info(p,var_type=var_type,name=str) |
| 89 | + |
| 90 | + !it must be a string named "name": |
| 91 | + if (var_type==json_string .and. str=='name') then |
| 92 | + call json_get(p,'@',str) ! get original name |
| 93 | + call json_update(p,'@','Fred',found) !change it |
| 94 | + write(error_unit,'(A)') str//' name changed' |
| 95 | + icount = icount + 1 |
| 96 | + end if |
| 97 | + |
| 98 | + !cleanup: |
| 99 | + if (allocated(str)) deallocate(str) |
| 100 | + |
| 101 | + !always false, since we want to traverse all nodes: |
| 102 | + finished = .false. |
| 103 | + |
| 104 | + end subroutine rename |
| 105 | + |
| 106 | +end module jf_test_14_mod |
| 107 | +!***************************************************************************************** |
| 108 | + |
| 109 | +!***************************************************************************************** |
| 110 | +program jf_test_14 |
| 111 | + |
| 112 | + !! 14th unit test. |
| 113 | + |
| 114 | + use jf_test_14_mod, only: test_14 |
| 115 | + implicit none |
| 116 | + integer :: n_errors |
| 117 | + call test_14(n_errors) |
| 118 | + if ( n_errors /= 0) stop 1 |
| 119 | + |
| 120 | +end program jf_test_14 |
| 121 | +!***************************************************************************************** |
0 commit comments