(git:34ef472)
memory_utilities_unittest.F
Go to the documentation of this file.
1 !--------------------------------------------------------------------------------------------------!
2 ! CP2K: A general program to perform molecular dynamics simulations !
3 ! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
4 ! !
5 ! SPDX-License-Identifier: GPL-2.0-or-later !
6 !--------------------------------------------------------------------------------------------------!
7 
9  USE kinds, ONLY: dp
10  USE memory_utilities, ONLY: reallocate
11 
12  IMPLICIT NONE
13 
16 
19 
22 CONTAINS
23 ! **************************************************************************************************
24 !> \brief Check that an allocated r1 array can be extended
25 ! **************************************************************************************************
27  INTEGER :: idx
28  REAL(KIND=dp), DIMENSION(:), POINTER :: real_arr
29 
30  ALLOCATE (real_arr(10))
31  real_arr = [(idx, idx=1, 10)]
32 
33  CALL reallocate(real_arr, 1, 20)
34 
35  IF (.NOT. all(real_arr(1:10) == [(idx, idx=1, 10)])) &
36  error stop "check_real_rank1_allocated: reallocating changed the initial values"
37 
38  IF (.NOT. all(real_arr(11:20) == 0.)) &
39  error stop "check_real_rank1_allocated: reallocation failed to initialise new values with 0."
40 
41  DEALLOCATE (real_arr)
42 
43  print *, "check_real_rank1_allocated: OK"
44  END SUBROUTINE
45 
46 ! **************************************************************************************************
47 !> \brief Check that an unallocated and unassociated (null) r1 array can be extended
48 ! **************************************************************************************************
50  REAL(KIND=dp), DIMENSION(:), POINTER :: real_arr
51 
52  NULLIFY (real_arr)
53 
54  CALL reallocate(real_arr, 1, 20)
55 
56  IF (.NOT. all(real_arr(1:20) == 0.)) &
57  error stop "check_real_rank1_unallocated: reallocation failed to initialise new values with 0."
58 
59  DEALLOCATE (real_arr)
60 
61  print *, "check_real_rank1_unallocated: OK"
62  END SUBROUTINE
63 
64 ! **************************************************************************************************
65 !> \brief Check that an allocated r2 array can be extended
66 ! **************************************************************************************************
68  INTEGER :: idx
69  REAL(KIND=dp), DIMENSION(:, :), POINTER :: real_arr
70 
71  ALLOCATE (real_arr(5, 2))
72  real_arr = reshape([(idx, idx=1, 10)], [5, 2])
73 
74  CALL reallocate(real_arr, 1, 10, 1, 5)
75 
76  IF (.NOT. (all(real_arr(1:5, 1) == [(idx, idx=1, 5)]) .AND. all(real_arr(1:5, 2) == [(idx, idx=6, 10)]))) &
77  error stop "check_real_rank2_allocated: reallocating changed the initial values"
78 
79  IF (.NOT. (all(real_arr(6:10, 1:2) == 0.) .AND. all(real_arr(1:10, 3:5) == 0.))) &
80  error stop "check_real_rank2_allocated: reallocation failed to initialise new values with 0."
81 
82  DEALLOCATE (real_arr)
83 
84  print *, "check_real_rank1_allocated: OK"
85  END SUBROUTINE
86 
87 ! **************************************************************************************************
88 !> \brief Check that an unallocated and unassociated (null) r2 array can be extended
89 ! **************************************************************************************************
91  REAL(KIND=dp), DIMENSION(:, :), POINTER :: real_arr
92 
93  NULLIFY (real_arr)
94 
95  CALL reallocate(real_arr, 1, 10, 1, 5)
96 
97  IF (.NOT. all(real_arr(1:10, 1:5) == 0.)) &
98  error stop "check_real_rank2_unallocated: reallocation failed to initialise new values with 0."
99 
100  DEALLOCATE (real_arr)
101 
102  print *, "check_real_rank2_unallocated: OK"
103  END SUBROUTINE
104 
105 ! **************************************************************************************************
106 !> \brief Check that an allocated string array can be extended
107 ! **************************************************************************************************
109  CHARACTER(LEN=12), DIMENSION(:), POINTER :: str_arr
110  INTEGER :: idx
111 
112  ALLOCATE (str_arr(10))
113  str_arr = [("hello, there", idx=1, 10)]
114 
115  CALL reallocate(str_arr, 1, 20)
116 
117  IF (.NOT. all(str_arr(1:10) == [("hello, there", idx=1, 10)])) &
118  error stop "check_string_rank1_allocated: reallocating changed the initial values"
119 
120  IF (.NOT. all(str_arr(11:20) == "")) &
121  error stop "check_string_rank1_allocated: reallocation failed to initialise new values with ''."
122 
123  DEALLOCATE (str_arr)
124 
125  print *, "check_string_rank1_allocated: OK"
126  END SUBROUTINE
127 
128 ! **************************************************************************************************
129 !> \brief Check that an unallocated string array can be extended
130 ! **************************************************************************************************
132  CHARACTER(LEN=12), DIMENSION(:), POINTER :: str_arr
133 
134  NULLIFY (str_arr)
135 
136  CALL reallocate(str_arr, 1, 20)
137 
138  IF (.NOT. all(str_arr(1:20) == "")) &
139  error stop "check_string_rank1_allocated: reallocation failed to initialise new values with ''."
140 
141  DEALLOCATE (str_arr)
142 
143  print *, "check_string_rank1_unallocated: OK"
144  END SUBROUTINE
145 
146 END PROGRAM
147 ! vim: set ts=3 sw=3 tw=132 :
program memory_utilities_test
subroutine check_string_rank1_allocated()
Check that an allocated string array can be extended.
subroutine check_real_rank2_allocated()
Check that an allocated r2 array can be extended.
subroutine check_real_rank2_unallocated()
Check that an unallocated and unassociated (null) r2 array can be extended.
subroutine check_string_rank1_unallocated()
Check that an unallocated string array can be extended.
subroutine check_real_rank1_allocated()
Check that an allocated r1 array can be extended.
subroutine check_real_rank1_unallocated()
Check that an unallocated and unassociated (null) r1 array can be extended.
Defines the basic variable types.
Definition: kinds.F:23
integer, parameter, public dp
Definition: kinds.F:34
Utility routines for the memory handling.