(git:98357aa)
Loading...
Searching...
No Matches
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-2026 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
9 USE kinds, ONLY: dp
11
12 IMPLICIT NONE
13
15 CALL check_real_rank1_unallocated()
16
17 CALL check_real_rank2_allocated()
18 CALL check_real_rank2_unallocated()
19
20 CALL check_string_rank1_allocated()
21 CALL check_string_rank1_unallocated()
22CONTAINS
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)])) THEN
36 error stop "check_real_rank1_allocated: reallocating changed the initial values"
37 END IF
38
39 IF (.NOT. all(real_arr(11:20) == 0.)) THEN
40 error stop "check_real_rank1_allocated: reallocation failed to initialise new values with 0."
41 END IF
42
43 DEALLOCATE (real_arr)
44
45 print *, "check_real_rank1_allocated: OK"
46 END SUBROUTINE check_real_rank1_allocated
47
48! **************************************************************************************************
49!> \brief Check that an unallocated and unassociated (null) r1 array can be extended
50! **************************************************************************************************
51 SUBROUTINE check_real_rank1_unallocated()
52 REAL(KIND=dp), DIMENSION(:), POINTER :: real_arr
53
54 NULLIFY (real_arr)
55
56 CALL reallocate(real_arr, 1, 20)
57
58 IF (.NOT. all(real_arr(1:20) == 0.)) THEN
59 error stop "check_real_rank1_unallocated: reallocation failed to initialise new values with 0."
60 END IF
61
62 DEALLOCATE (real_arr)
63
64 print *, "check_real_rank1_unallocated: OK"
65 END SUBROUTINE check_real_rank1_unallocated
66
67! **************************************************************************************************
68!> \brief Check that an allocated r2 array can be extended
69! **************************************************************************************************
70 SUBROUTINE check_real_rank2_allocated()
71 INTEGER :: idx
72 REAL(KIND=dp), DIMENSION(:, :), POINTER :: real_arr
73
74 ALLOCATE (real_arr(5, 2))
75 real_arr = reshape([(idx, idx=1, 10)], [5, 2])
76
77 CALL reallocate(real_arr, 1, 10, 1, 5)
78
79 IF (.NOT. (all(real_arr(1:5, 1) == [(idx, idx=1, 5)]) .AND. all(real_arr(1:5, 2) == [(idx, idx=6, 10)]))) THEN
80 error stop "check_real_rank2_allocated: reallocating changed the initial values"
81 END IF
82
83 IF (.NOT. (all(real_arr(6:10, 1:2) == 0.) .AND. all(real_arr(1:10, 3:5) == 0.))) THEN
84 error stop "check_real_rank2_allocated: reallocation failed to initialise new values with 0."
85 END IF
86
87 DEALLOCATE (real_arr)
88
89 print *, "check_real_rank1_allocated: OK"
90 END SUBROUTINE check_real_rank2_allocated
91
92! **************************************************************************************************
93!> \brief Check that an unallocated and unassociated (null) r2 array can be extended
94! **************************************************************************************************
95 SUBROUTINE check_real_rank2_unallocated()
96 REAL(KIND=dp), DIMENSION(:, :), POINTER :: real_arr
97
98 NULLIFY (real_arr)
99
100 CALL reallocate(real_arr, 1, 10, 1, 5)
101
102 IF (.NOT. all(real_arr(1:10, 1:5) == 0.)) THEN
103 error stop "check_real_rank2_unallocated: reallocation failed to initialise new values with 0."
104 END IF
105
106 DEALLOCATE (real_arr)
107
108 print *, "check_real_rank2_unallocated: OK"
109 END SUBROUTINE check_real_rank2_unallocated
110
111! **************************************************************************************************
112!> \brief Check that an allocated string array can be extended
113! **************************************************************************************************
114 SUBROUTINE check_string_rank1_allocated()
115 CHARACTER(LEN=12), DIMENSION(:), POINTER :: str_arr
116 INTEGER :: idx
117
118 ALLOCATE (str_arr(10))
119 str_arr = [("hello, there", idx=1, 10)]
120
121 CALL reallocate(str_arr, 1, 20)
122
123 IF (.NOT. all(str_arr(1:10) == [("hello, there", idx=1, 10)])) THEN
124 error stop "check_string_rank1_allocated: reallocating changed the initial values"
125 END IF
126
127 IF (.NOT. all(str_arr(11:20) == "")) THEN
128 error stop "check_string_rank1_allocated: reallocation failed to initialise new values with ''."
129 END IF
130
131 DEALLOCATE (str_arr)
132
133 print *, "check_string_rank1_allocated: OK"
134 END SUBROUTINE check_string_rank1_allocated
135
136! **************************************************************************************************
137!> \brief Check that an unallocated string array can be extended
138! **************************************************************************************************
139 SUBROUTINE check_string_rank1_unallocated()
140 CHARACTER(LEN=12), DIMENSION(:), POINTER :: str_arr
141
142 NULLIFY (str_arr)
143
144 CALL reallocate(str_arr, 1, 20)
145
146 IF (.NOT. all(str_arr(1:20) == "")) THEN
147 error stop "check_string_rank1_allocated: reallocation failed to initialise new values with ''."
148 END IF
149
150 DEALLOCATE (str_arr)
151
152 print *, "check_string_rank1_unallocated: OK"
153 END SUBROUTINE check_string_rank1_unallocated
154
155END PROGRAM memory_utilities_test
156! vim: set ts=3 sw=3 tw=132 :
program memory_utilities_test
subroutine check_real_rank1_allocated()
Check that an allocated 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.