Skip to content

Commit d69bfb7

Browse files
authored
[Enhancement] Add Unit Test for character Procedures (#11)
* Update doxygen.cfg * Update .justfile * Update .justfile * Update lib.f08 * Update type_character.f08 * Update README.md * Update .justfile * Update README.md * Create test_character.f08 * Update test_character.f08 * Create feature:unit-test-character.rst
1 parent f4da165 commit d69bfb7

File tree

7 files changed

+159
-8
lines changed

7 files changed

+159
-8
lines changed

.justfile

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,39 @@ alias build := library
3737
alias clr := clear
3838
alias d := doxygen
3939
alias l := library
40+
alias v := valgrind
4041
alias ver := bump
4142
alias version := bump
4243

4344

4445

4546
# Compiler flags.
47+
exe := '-fPIE'
4648
f18 := '-std=f2018'
4749
flags := '-Wall -Werror -Wextra -Wpedantic'
4850
lib := '-c -fPIC'
4951

50-
# Targets.
51-
library := 'libcndmem.a'
52+
# Linker flags.
53+
lflags := '-L. -lf18cndmem'
5254

5355
# Settings for the supported language modes.
56+
f18-exe := f18 + ' ' + exe + ' ' + flags
5457
f18-lib := f18 + ' ' + lib + ' ' + flags
58+
lnk-f18 := '-I. ' + lflags
59+
60+
# Targets.
61+
library := 'libf18cndmem.a'
62+
63+
# Valgrind settings.
64+
vflags := '--leak-check=full --redzone-size=512 --show-leak-kinds=all'
5565

5666

5767

5868
# The default recipe to execute.
5969
@default: all
6070

6171
# Execute all configured recipes.
62-
@all: clear doxygen library
72+
@all: clear doxygen valgrind
6373

6474
# Increment the version numbers.
6575
@bump part:
@@ -80,6 +90,10 @@ f18-lib := f18 + ' ' + lib + ' ' + flags
8090
ar rsv {{library}} *.o
8191
rm -rf *.o
8292

93+
# Create the required directories.
94+
@directories:
95+
mkdir -p target/
96+
8397
# Create the Doxygen documentation for this project.
8498
@doxygen:
8599
doxygen doxygen.cfg
@@ -93,4 +107,14 @@ f18-lib := f18 + ' ' + lib + ' ' + flags
93107
# Compile the target library.
94108
@library: character
95109

110+
# Compile and run a single unit test.
111+
@test name: directories library
112+
gfortran {{f18-exe}} tests/test_{{name}}.f08 -o \
113+
target/test_{{name}} {{lnk-f18}}
114+
valgrind {{vflags}} target/test_{{name}}
115+
116+
# Analyse the memory management of the unit tests.
117+
@valgrind:
118+
just test character
119+
96120
################################################################################

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ version is as follows:
7575
| Latexmk | application | LaTeX compilation of Doxygen manual |
7676
| Scriv | Python CLI | changelog management |
7777
| TeX Live (full) | package | LaTeX environment for Doxygen manual |
78+
| Valgrind | application | memory leak detection |
7879

7980
This library is written in Fortran 2018 such that its build requires a compiler
8081
supporting this standard. This project relies on **GFortran** therefore.
@@ -94,6 +95,10 @@ In order to finalise the LaTeX documentation, an appropriate LaTeX distribution
9495
is required. A *full* installation of **TeX Live** is recommended. This
9596
project employs **Latexmk** as LaTeX build manager for the manual finalisation.
9697

98+
There are unit tests for the library logic written in plain Fortran 2018 without
99+
requiring any further libraries. When running the tests, **Valgrind** will
100+
analyse their memory management.
101+
97102
In order to simplify the maintenance, **bump2version** as well as **Scriv** are
98103
used to automate the release generation. Scriv will compile the changelog on
99104
release after all version numbers were incremented by bump2version.
@@ -155,8 +160,18 @@ just d
155160
just doxygen
156161
```
157162

163+
The correctness of this library is ensured by a set of unit tests. They are
164+
furthermore analysed for their memory management by Valgrind. By calling for
165+
the Valgrind analysis, the unit tests are executed implicitly. This can be
166+
done with one of the following commands.
167+
168+
```bash
169+
just v
170+
just valgrind
171+
```
172+
158173
If the compilation of both the library and its documentation is wished, the
159-
following instructions can be executed.
174+
following instructions can be executed. This will also run all unit tests.
160175

161176
```bash
162177
just
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Added
2+
.....
3+
4+
- unit test: conditional allocation (type ``character``, default)
5+
6+
- unit test: conditional deallocation (type ``character``, default)

doxygen.cfg

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,8 @@ WARN_LOGFILE =
907907
# Note: If this tag is empty the current directory is searched.
908908

909909
INPUT = src/ \
910-
src/type_character/
910+
src/type_character/ \
911+
tests/
911912

912913
# This tag can be used to specify the character encoding of the source files
913914
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses

src/lib.f08

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
!!
4444
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4545

46-
module libcndmem
46+
module libf18cndmem
4747
implicit none
4848
private
4949
!> This library's version.
@@ -66,6 +66,6 @@ pure module subroutine cnddel_character (tgt)
6666
character (:), allocatable, intent (inout) :: tgt
6767
end subroutine cnddel_character
6868
end interface cnddel
69-
end module libcndmem
69+
end module libf18cndmem
7070

7171
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

src/type_character.f08

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
!!
4040
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
4141

42-
submodule (libcndmem) type_character
42+
submodule (libf18cndmem) type_character
4343
implicit none
4444
contains
4545
include 'type_character/cndall_character.f08'

tests/test_character.f08

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
!!!!!!!!!!!!!!!!!!!!!!!! GNU General Public License 2.0 !!!!!!!!!!!!!!!!!!!!!!!!
2+
!! !!
3+
!! Copyright (C) 2022 Kevin Matthes !!
4+
!! !!
5+
!! This program is free software; you can redistribute it and/or modify !!
6+
!! it under the terms of the GNU General Public License as published by !!
7+
!! the Free Software Foundation; either version 2 of the License, or !!
8+
!! (at your option) any later version. !!
9+
!! !!
10+
!! This program is distributed in the hope that it will be useful, !!
11+
!! but WITHOUT ANY WARRANTY; without even the implied warranty of !!
12+
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the !!
13+
!! GNU General Public License for more details. !!
14+
!! !!
15+
!! You should have received a copy of the GNU General Public License along !!
16+
!! with this program; if not, write to the Free Software Foundation, Inc., !!
17+
!! 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. !!
18+
!! !!
19+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20+
21+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22+
!!
23+
!> \file test_character.f08
24+
!!
25+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
26+
27+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
28+
!!
29+
!> \author Kevin Matthes
30+
!> \copyright GPL-2.0
31+
!> \date 2022
32+
!> \note See `LICENSE' for full license.
33+
!> See `README.md' for project details.
34+
!>
35+
!> \brief A set of simple tests for the `character` type (default).
36+
!> \return Whether this test succeeds.
37+
!>
38+
!> This unit test will check whether
39+
!>
40+
!> * an unallocated allocatable object can be deallocated.
41+
!> * an unallocated allocatable object can be allocated and receive a constant.
42+
!> * an allocated allocatable object can receive another constant by conditional
43+
!> reallocation.
44+
!> * an allocated allocatable object can be deallocated.
45+
!!
46+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47+
48+
program test_character
49+
use, non_intrinsic :: libf18cndmem, only: cndall
50+
use, non_intrinsic :: libf18cndmem, only: cnddel
51+
implicit none
52+
character (:), allocatable :: object
53+
character (*), parameter :: constant &
54+
= 'constant'
55+
character (*), parameter :: error_allocated &
56+
= 'Allocatable object should not be allocated!'
57+
character (*), parameter :: error_not_allocated &
58+
= 'Allocatable object should be allocated!'
59+
character (*), parameter :: error_wrong_value &
60+
= 'Allocatable object does not have the expect&
61+
&ed value!'
62+
character (*), parameter :: hello &
63+
= 'Hello'
64+
character (*), parameter :: world &
65+
= ', World!'
66+
intrinsic :: allocated
67+
68+
if (allocated (object)) then
69+
error stop error_allocated
70+
else
71+
call cnddel (object)
72+
end if
73+
74+
if (allocated (object)) then
75+
error stop error_allocated
76+
else
77+
call cndall (object, constant)
78+
end if
79+
80+
if (.not. allocated (object)) then
81+
error stop error_not_allocated
82+
else if (object /= constant) then
83+
error stop error_wrong_value
84+
end if
85+
86+
call cndall (object, hello)
87+
88+
if (.not. allocated (object)) then
89+
error stop error_not_allocated
90+
else if (object // world /= hello // world) then
91+
error stop error_wrong_value
92+
end if
93+
94+
if (.not. allocated (object)) then
95+
error stop error_not_allocated
96+
else
97+
call cnddel (object)
98+
end if
99+
100+
if (allocated (object)) then
101+
error stop error_allocated
102+
end if
103+
end program test_character
104+
105+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0 commit comments

Comments
 (0)