Skip to content

Commit fe971c5

Browse files
committed
feat(string_t): is_allocated type-bound procedure
1 parent 40b7720 commit fe971c5

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

src/string_m.f90

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module string_m
1010
character(len=:), allocatable :: string_
1111
contains
1212
procedure :: string
13+
procedure :: is_allocated
1314
end type
1415

1516
interface string_t
@@ -35,7 +36,13 @@ pure module function array_of_strings(delimited_strings, delimiter) result(strin
3536
character(len=*), intent(in) :: delimited_strings, delimiter
3637
type(string_t), allocatable :: strings_array(:)
3738
end function
38-
39+
40+
elemental module function is_allocated(self) result(string_allocated)
41+
implicit none
42+
class(string_t), intent(in) :: self
43+
logical string_allocated
44+
end function
45+
3946
end interface
4047

4148
end module string_m

src/string_s.f90

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
contains
55

6-
module procedure construct
7-
new_string%string_ = string
8-
end procedure
6+
module procedure construct
7+
new_string%string_ = string
8+
end procedure
9+
10+
module procedure string
11+
raw_string = self%string_
12+
end procedure
913

10-
module procedure string
11-
raw_string = self%string_
12-
end procedure
14+
module procedure is_allocated
15+
string_allocated = allocated(self%string_)
16+
end procedure
1317

1418
module procedure array_of_strings
1519
character(len=:), allocatable :: remainder, next_string

test/main.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ program main
55
use formats_test, only : formats_test_t
66
use test_result_test, only : test_result_test_t
77
use command_line_test, only : command_line_test_t
8+
use string_test, only : string_test_t
89
implicit none
910

1011
type(collectives_test_t) collectives_test
@@ -13,6 +14,7 @@ program main
1314
type(object_test_t) object_test
1415
type(test_result_test_t) test_result_test
1516
type(command_line_test_t) command_line_test
17+
type(string_test_t) string_test
1618

1719
integer :: passes=0, tests=0
1820

@@ -22,6 +24,7 @@ program main
2224
call formats_test%report(passes, tests)
2325
call test_result_test%report(passes, tests)
2426
call command_line_test%report(passes, tests)
27+
call string_test%report(passes, tests)
2528

2629
print *
2730
print *,"_________ In total, ",passes," of ",tests, " tests pass. _________"

test/string_test.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module string_test
2+
use test_m, only : test_t, test_result_t
3+
use string_m, only : string_t
4+
implicit none
5+
6+
private
7+
public :: string_test_t
8+
9+
type, extends(test_t) :: string_test_t
10+
contains
11+
procedure, nopass :: subject
12+
procedure, nopass :: results
13+
end type
14+
15+
contains
16+
17+
pure function subject() result(specimen)
18+
character(len=:), allocatable :: specimen
19+
specimen = "The string_t type"
20+
end function
21+
22+
function results() result(test_results)
23+
type(test_result_t), allocatable :: test_results(:)
24+
25+
test_results = [ &
26+
test_result_t("is_allocated() result .true. if & only if the string_t component(s) is/are allocated", check_allocation()) &
27+
]
28+
end function
29+
30+
pure function check_allocation() result(passed)
31+
type(string_t) :: scalar_not_allocated, scalar_allocated, array_allocated(2), array_not_allocated(2)
32+
logical passed
33+
34+
scalar_allocated = string_t("")
35+
array_allocated = [string_t("yada yada"), string_t("blah blah blah")]
36+
passed = (.not. any([scalar_not_allocated%is_allocated(), array_not_allocated%is_allocated()])) .and. &
37+
(all([scalar_allocated%is_allocated(), array_allocated%is_allocated()]))
38+
end function
39+
40+
end module string_test

0 commit comments

Comments
 (0)