|
1 | 1 | ! SPDX-Identifier: MIT
|
2 | 2 | module test_string_assignment
|
3 |
| - use stdlib_error, only : check |
| 3 | + use stdlib_test, only : new_unittest, unittest_type, error_type, check |
4 | 4 | use stdlib_kinds, only : int8, int16, int32, int64, lk, c_bool
|
5 | 5 | use stdlib_string_type, only : string_type, assignment(=), operator(==), len
|
6 | 6 | implicit none
|
7 | 7 |
|
8 | 8 | contains
|
9 | 9 |
|
10 |
| - subroutine test_assignment |
| 10 | + !> Collect all exported unit tests |
| 11 | + subroutine collect_string_assignment(testsuite) |
| 12 | + !> Collection of tests |
| 13 | + type(unittest_type), allocatable, intent(out) :: testsuite(:) |
| 14 | + |
| 15 | + testsuite = [ & |
| 16 | + new_unittest("assignment", test_assignment), & |
| 17 | + new_unittest("constructor", test_constructor) & |
| 18 | + ] |
| 19 | + end subroutine collect_string_assignment |
| 20 | + |
| 21 | + subroutine test_assignment(error) |
| 22 | + !> Error handling |
| 23 | + type(error_type), allocatable, intent(out) :: error |
11 | 24 | type(string_type) :: string
|
12 | 25 |
|
13 |
| - call check(len(string) == 0) |
| 26 | + call check(error, len(string) == 0) |
| 27 | + if (allocated(error)) return |
14 | 28 |
|
15 | 29 | string = "Sequence"
|
16 |
| - call check(len(string) == 8) |
| 30 | + call check(error, len(string) == 8) |
17 | 31 | end subroutine test_assignment
|
18 | 32 |
|
19 |
| - subroutine test_char_value |
20 |
| - character(len=128) :: flc |
| 33 | + subroutine test_constructor(error) |
| 34 | + !> Error handling |
| 35 | + type(error_type), allocatable, intent(out) :: error |
| 36 | + character(len=128) :: flc |
21 | 37 |
|
22 |
| - write(flc, '(g0)') -1026191 |
23 |
| - call check(string_type(-1026191) == trim(flc)) |
| 38 | + write(flc, '(g0)') -1026191 |
| 39 | + call check(error, string_type(-1026191) == trim(flc)) |
| 40 | + if (allocated(error)) return |
24 | 41 |
|
25 |
| - write(flc, '(g0)') 124787 |
26 |
| - call check(string_type(124787) == trim(flc)) |
| 42 | + write(flc, '(g0)') 124787 |
| 43 | + call check(error, string_type(124787) == trim(flc)) |
| 44 | + if (allocated(error)) return |
27 | 45 |
|
28 |
| - write(flc, '(g0)') -2_int8 |
29 |
| - call check(string_type(-2_int8) == trim(flc)) |
| 46 | + write(flc, '(g0)') -2_int8 |
| 47 | + call check(error, string_type(-2_int8) == trim(flc)) |
| 48 | + if (allocated(error)) return |
30 | 49 |
|
31 |
| - write(flc, '(g0)') 5_int8 |
32 |
| - call check(string_type(5_int8) == trim(flc)) |
| 50 | + write(flc, '(g0)') 5_int8 |
| 51 | + call check(error, string_type(5_int8) == trim(flc)) |
| 52 | + if (allocated(error)) return |
33 | 53 |
|
34 |
| - write(flc, '(g0)') -72_int16 |
35 |
| - call check(string_type(-72_int16) == trim(flc)) |
| 54 | + write(flc, '(g0)') -72_int16 |
| 55 | + call check(error, string_type(-72_int16) == trim(flc)) |
| 56 | + if (allocated(error)) return |
36 | 57 |
|
37 |
| - write(flc, '(g0)') -8924889_int32 |
38 |
| - call check(string_type(-8924889_int32) == trim(flc)) |
| 58 | + write(flc, '(g0)') -8924889_int32 |
| 59 | + call check(error, string_type(-8924889_int32) == trim(flc)) |
| 60 | + if (allocated(error)) return |
39 | 61 |
|
40 |
| - write(flc, '(g0)') 2378405_int32 |
41 |
| - call check(string_type(2378405_int32) == trim(flc)) |
| 62 | + write(flc, '(g0)') 2378405_int32 |
| 63 | + call check(error, string_type(2378405_int32) == trim(flc)) |
| 64 | + if (allocated(error)) return |
42 | 65 |
|
43 |
| - write(flc, '(g0)') 921092378411_int64 |
44 |
| - call check(string_type(921092378411_int64) == trim(flc)) |
| 66 | + write(flc, '(g0)') 921092378411_int64 |
| 67 | + call check(error, string_type(921092378411_int64) == trim(flc)) |
| 68 | + if (allocated(error)) return |
45 | 69 |
|
46 |
| - write(flc, '(g0)') -1272835761_int64 |
47 |
| - call check(string_type(-1272835761_int64) == trim(flc)) |
| 70 | + write(flc, '(g0)') -1272835761_int64 |
| 71 | + call check(error, string_type(-1272835761_int64) == trim(flc)) |
| 72 | + if (allocated(error)) return |
48 | 73 |
|
49 |
| - write(flc, '(g0)') .true. |
50 |
| - call check(string_type(.true.) == trim(flc)) |
| 74 | + write(flc, '(g0)') .true. |
| 75 | + call check(error, string_type(.true.) == trim(flc)) |
| 76 | + if (allocated(error)) return |
51 | 77 |
|
52 |
| - write(flc, '(g0)') .false. |
53 |
| - call check(string_type(.false.) == trim(flc)) |
| 78 | + write(flc, '(g0)') .false. |
| 79 | + call check(error, string_type(.false.) == trim(flc)) |
| 80 | + if (allocated(error)) return |
54 | 81 |
|
55 |
| - write(flc, '(g0)') .false._c_bool |
56 |
| - call check(string_type(.false._c_bool) == trim(flc)) |
| 82 | + write(flc, '(g0)') .false._c_bool |
| 83 | + call check(error, string_type(.false._c_bool) == trim(flc)) |
| 84 | + if (allocated(error)) return |
57 | 85 |
|
58 |
| - write(flc, '(g0)') .true._lk |
59 |
| - call check(string_type(.true._lk) == trim(flc)) |
60 |
| - end subroutine test_char_value |
| 86 | + write(flc, '(g0)') .true._lk |
| 87 | + call check(error, string_type(.true._lk) == trim(flc)) |
| 88 | + end subroutine test_constructor |
61 | 89 |
|
62 | 90 | end module test_string_assignment
|
63 | 91 |
|
| 92 | + |
64 | 93 | program tester
|
65 |
| - use test_string_assignment |
| 94 | + use, intrinsic :: iso_fortran_env, only : error_unit |
| 95 | + use stdlib_test, only : run_testsuite, new_testsuite, testsuite_type |
| 96 | + use test_string_assignment, only : collect_string_assignment |
66 | 97 | implicit none
|
67 |
| - |
68 |
| - call test_assignment |
69 |
| - call test_char_value |
70 |
| - |
71 |
| -end program tester |
72 |
| - |
| 98 | + integer :: stat, is |
| 99 | + type(testsuite_type), allocatable :: testsuites(:) |
| 100 | + character(len=*), parameter :: fmt = '("#", *(1x, a))' |
| 101 | + |
| 102 | + stat = 0 |
| 103 | + |
| 104 | + testsuites = [ & |
| 105 | + new_testsuite("string-assignment", collect_string_assignment) & |
| 106 | + ] |
| 107 | + |
| 108 | + do is = 1, size(testsuites) |
| 109 | + write(error_unit, fmt) "Testing:", testsuites(is)%name |
| 110 | + call run_testsuite(testsuites(is)%collect, error_unit, stat) |
| 111 | + end do |
| 112 | + |
| 113 | + if (stat > 0) then |
| 114 | + write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" |
| 115 | + error stop |
| 116 | + end if |
| 117 | +end program |
0 commit comments