File: test_linspace.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (422 lines) | stat: -rw-r--r-- 14,743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
module test_linspace
    use testdrive, only : new_unittest, unittest_type, error_type, check
    use stdlib_kinds, only: sp, dp, int8, int16
    use stdlib_math, only: linspace, DEFAULT_LINSPACE_LENGTH

    implicit none
    private

    public :: collect_linspace

    real(sp), parameter :: TOLERANCE_SP = 1000 * epsilon(1.0_sp)
    real(dp), parameter :: TOLERANCE_DP = 1000 * epsilon(1.0_dp) ! Percentage of the range for which the actual gap must not exceed

    ! Testing linspace.
    !
    ! For single and double precision, check if the beginning and end values are properly recorded
    ! and make sure that the size of the result array is as expected.
    !
    ! This testing suite makes use of the a repeated section of code that will check to make
    ! sure that every element is linearly spaced (i.e., call check(|array(i+1) - array(i)| < |expected_value| * TOLERANCE)).
    ! I would convert this repeated code into a subroutine but that would require the implementation of a
    ! generic procedure given that each linear space will have a different expected_value type and kind.

contains

    !> Collect all exported unit tests
    subroutine collect_linspace(testsuite)
        !> Collection of tests
        type(unittest_type), allocatable, intent(out) :: testsuite(:)

        testsuite = [ &
            new_unittest("linspace_sp", test_linspace_sp), &
            new_unittest("linspace_dp", test_linspace_dp), &
            new_unittest("linspace_neg_index", test_linspace_neg_index), &
            new_unittest("linspace_cmplx", test_linspace_cmplx), &
            new_unittest("linspace_cmplx_2", test_linspace_cmplx_2), &
            new_unittest("linspace_cmplx_3", test_linspace_cmplx_3), &
            new_unittest("linspace_cmplx_sp", test_linspace_cmplx_sp), &
            new_unittest("linspace_cmplx_sp_2", test_linspace_cmplx_sp_2), &
            new_unittest("linspace_int16", test_linspace_int16), &
            new_unittest("linspace_int8", test_linspace_int8) &
            ]

    end subroutine collect_linspace


    subroutine test_linspace_sp(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        integer, parameter :: n = 20
        real(sp), parameter :: start = 1.0_sp
        real(sp), parameter :: end = 10.0_sp
        real(sp) :: expected_interval
        real(sp) :: true_difference

        integer :: i
        real(sp), allocatable :: x(:)

        x = linspace(start, end, n)

        expected_interval =( end - start ) / real(( n - 1 ), sp)

        call check(error, x(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, x(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return
        call check(error, size(x), n, "Array not allocated to appropriate size")
        if (allocated(error)) return

        print *, "Made it through first round of tests"

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            true_difference = x(i + 1) - x(i)
            call check(error, abs(true_difference - expected_interval) < abs(expected_interval) * TOLERANCE_SP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_dp(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        real(dp), parameter :: start = 1.0_dp
        real(dp), parameter :: end = 10.0_dp
        integer, parameter :: n = DEFAULT_LINSPACE_LENGTH
        real(dp) :: expected_interval
        real(dp) :: true_difference

        real(dp), allocatable :: x(:)
        integer :: i

        x = linspace(start, end)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(x), n, "Array not allocated to default size")
        if (allocated(error)) return
        call check(error, x(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, x(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            true_difference = x(i + 1) - x(i)
            call check(error, true_difference, expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_neg_index(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        real(dp), parameter :: start = 1.0_dp
        real(dp), parameter :: end = 10.0_dp

        real(dp), allocatable :: x(:)

        x = linspace(start, end, -15)

        call check(error, size(x), 0, "Allocated array is not empty")

    end subroutine

    subroutine test_linspace_cmplx(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        complex(dp), parameter :: start = (0.0_dp, 10.0_dp)
        complex(dp), parameter :: end = (1.0_dp, 0.0_dp)
        complex(dp) :: expected_interval
        integer, parameter :: n = 10

        complex(dp), allocatable :: z(:)

        integer :: i

        z = linspace(start, end, n)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_cmplx_2(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        complex(dp), parameter :: start = (10.0_dp, 10.0_dp)
        complex(dp), parameter :: end = (1.0_dp, 1.0_dp)
        complex(dp) :: expected_interval

        integer, parameter :: n = 5

        complex(dp), allocatable :: z(:)

        integer :: i

        z = linspace(start, end, n)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_cmplx_3(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        complex(dp), parameter :: start = (-5.0_dp, 100.0_dp)
        complex(dp), parameter :: end = (20.0_dp, 13.0_dp)
        complex(dp) :: expected_interval

        integer, parameter :: n = 20

        complex(dp), allocatable :: z(:)

        integer :: i

        z = linspace(start, end, n)

        expected_interval = ( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_cmplx_sp(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        complex(sp), parameter :: start = (0.5_sp, 5.0_sp)
        complex(sp), parameter :: end = (1.0_sp, -30.0_sp)
        complex(sp) :: expected_interval

        integer, parameter :: n = 10

        complex(sp), allocatable :: z(:)

        integer :: i

        z = linspace(start, end, n)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_SP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_cmplx_sp_2(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        complex(sp), parameter :: start = (50.0_sp, 500.0_sp)
        complex(sp), parameter :: end = (-100.0_sp, 2000.0_sp)
        complex(sp) :: expected_interval
        complex(sp) :: true_interval
        real(sp) :: offset

        integer, parameter :: n = DEFAULT_LINSPACE_LENGTH

        complex(sp), allocatable :: z(:)

        integer :: i

        z = linspace(start, end)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to default size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            true_interval = (z(i + 1) - z(i))
            offset = abs(true_interval - expected_interval)
            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_SP)
            if (allocated(error)) return
            ! print *, i

        end do

    end subroutine

    subroutine test_linspace_int16(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        integer(int16), parameter :: start = 5
        integer(int16), parameter :: end = 10
        real(dp) :: expected_interval

        integer, parameter :: n = 6

        integer(int16), allocatable :: z(:)

        integer :: i

        z = linspace(start, end, n)

        expected_interval =( end - start ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), start, "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), end, "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, real(z(i + 1) - z(i), dp), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine

    subroutine test_linspace_int8(error)
        !> Error handling
        type(error_type), allocatable, intent(out) :: error

        integer(int8), parameter :: start = 20
        integer(int8), parameter :: end = 50

        real(dp) :: expected_interval

        integer, parameter :: n = 10

        real(dp), allocatable :: z(:)
        integer(int8) :: z_int(n)

        integer :: i

        z = linspace(start, end, n)
        z_int = linspace(start, end, n)

        expected_interval =real( end - start, dp ) / ( n - 1 )

        call check(error, size(z), n, "Array not allocated to correct size")
        if (allocated(error)) return
        call check(error, z(1), real(start, dp), "Initial value of array is not equal to the passed start parameter")
        if (allocated(error)) return
        call check(error, z(n), real(end, dp), "Final array value is not equal to end parameter")
        if (allocated(error)) return

        ! Due to roundoff error, it is possible that the jump from x(n-1) to x(n) is slightly different than the expected interval
        do i = 1, n-1

            call check(error, z(i + 1) - z(i), expected_interval, &
                & thr=abs(expected_interval) * TOLERANCE_DP)
            if (allocated(error)) return

        end do

    end subroutine



end module

program tester
    use, intrinsic :: iso_fortran_env, only : error_unit
    use testdrive, only : run_testsuite, new_testsuite, testsuite_type
    use test_linspace, only : collect_linspace
    implicit none
    integer :: stat, is
    type(testsuite_type), allocatable :: testsuites(:)
    character(len=*), parameter :: fmt = '("#", *(1x, a))'

    stat = 0

    testsuites = [ &
        new_testsuite("linspace", collect_linspace) &
        ]

    do is = 1, size(testsuites)
        write(error_unit, fmt) "Testing:", testsuites(is)%name
        call run_testsuite(testsuites(is)%collect, error_unit, stat)
    end do

    if (stat > 0) then
        write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
        error stop
    end if
end program tester