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
|
#:include "common.fypp"
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES
#:set NRANK = 4
module test_stats_mean
use testdrive, only : new_unittest, unittest_type, error_type, check
use stdlib_stats, only: mean
use stdlib_kinds, only : int8, int16, int32, int64, sp, dp, xdp, qp
use, intrinsic :: ieee_arithmetic, only : ieee_is_nan
implicit none
private
public :: collect_stats_mean
real(sp), parameter :: sptol = 1000 * epsilon(1._sp)
real(dp), parameter :: dptol = 2000 * epsilon(1._dp)
#:if WITH_XDP
real(xdp), parameter :: xdptol = 2000 * epsilon(1._xdp)
#:endif
#:if WITH_QP
real(qp), parameter :: qptol = 2000 * epsilon(1._qp)
#:endif
#:for k1,t1 in IR_KINDS_TYPES
${t1}$ , parameter :: d1_${k1}$(18) = [-10, 2, 3, 4, -6, 6, -7, 8, 9, 4, 1, -20, 9, 10, 14, 15, 40, 30]
${t1}$ :: d2_${k1}$(3, 6) = reshape(d1_${k1}$, [3, 6])
${t1}$ :: d3_${k1}$(3, 2, 3) = reshape(d1_${k1}$, [3, 2, 3])
${t1}$ :: d4_${k1}$(3, 2, 3, 2) = reshape(d1_${k1}$, [3, 2, 3, 2], [${t1}$ :: 3])
#:endfor
#:for k1,t1 in CMPLX_KINDS_TYPES
${t1}$ , parameter :: d1_c${k1}$(18) = d1_${k1}$
${t1}$ :: d2_c${k1}$(3, 6) = reshape(d1_c${k1}$, [3, 6])
${t1}$ :: d3_c${k1}$(3, 2, 3) = reshape(d1_c${k1}$, [3, 2, 3])
${t1}$ :: d4_c${k1}$(3, 2, 3, 2) = reshape(d1_c${k1}$, [3, 2, 3, 2], [${t1}$ :: (3, -2)] )
#:endfor
contains
!> Collect all exported unit tests
subroutine collect_stats_mean(testsuite)
!> Collection of tests
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest("test_stats_mean_all_int8", test_stats_mean_all_int8) &
#:for k1,t1 in IR_KINDS_TYPES
,new_unittest("test_stats_mean_all_${k1}$", test_stats_mean_all_${k1}$) &
, new_unittest("test_stats_mean_all_optmask_${k1}$", test_stats_mean_all_optmask_${k1}$) &
, new_unittest("test_stats_mean_${k1}$", test_stats_mean_${k1}$) &
, new_unittest("test_stats_mean_optmask_${k1}$", test_stats_mean_optmask_${k1}$) &
, new_unittest("test_stats_mean_mask_all_${k1}$", test_stats_mean_mask_all_${k1}$) &
, new_unittest("test_stats_mean_mask_${k1}$", test_stats_mean_mask_${k1}$) &
#:endfor
#:for k1,t1 in CMPLX_KINDS_TYPES
,new_unittest("test_stats_mean_all_c${k1}$", test_stats_mean_all_c${k1}$) &
, new_unittest("test_stats_mean_all_optmask_c${k1}$", test_stats_mean_all_optmask_c${k1}$) &
, new_unittest("test_stats_mean_c${k1}$", test_stats_mean_c${k1}$) &
, new_unittest("test_stats_mean_optmask_c${k1}$", test_stats_mean_optmask_c${k1}$) &
, new_unittest("test_stats_mean_mask_all_c${k1}$", test_stats_mean_mask_all_c${k1}$) &
, new_unittest("test_stats_mean_mask_c${k1}$", test_stats_mean_mask_c${k1}$) &
#:endfor
]
end subroutine collect_stats_mean
#:for k1,t1 in INT_KINDS_TYPES
subroutine test_stats_mean_all_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK + 1)
call check(error, mean(d${rank}$_${k1}$), sum(real(d${rank}$_${k1}$, dp))/real(size(d${rank}$_${k1}$), dp)&
, 'mean(d${rank}$_${k1}$): uncorrect answer'&
, thr = dptol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_all_optmask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK + 1)
call check(error, ieee_is_nan(mean(d${rank}$_${k1}$, .false.))&
, 'mean(d${rank}$_${k1}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_${k1}$, 1) -&
sum(real(d1_${k1}$, dp), 1)/real(size(d1_${k1}$, 1), dp)) < dptol&
, 'mean(d1_${k1}$, 1): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_${k1}$, ${dim}$) -&
sum(real(d${rank}$_${k1}$, dp), ${dim}$)/real(size(d${rank}$_${k1}$, ${dim}$), dp))) < dptol&
, 'mean(d${rank}$_${k1}$, ${dim}$): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_optmask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, ieee_is_nan(mean(d1_${k1}$, 1, .false.))&
, 'mean(d1_${k1}$, 1, .false.): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error, any(ieee_is_nan(mean(d${rank}$_${k1}$, ${dim}$, .false.)))&
, 'mean(d${rank}$_${k1}$, ${dim}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_mask_all_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, mean(d${rank}$_${k1}$, d${rank}$_${k1}$ > 0)&
, sum(real(d${rank}$_${k1}$, dp), d${rank}$_${k1}$ > 0)/real(count(d${rank}$_${k1}$ > 0), dp)&
, 'mean(d${rank}$_${k1}$, d${rank}$_${k1}$ > 0): uncorrect answer'&
, thr = dptol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_mask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_${k1}$, 1, d1_${k1}$ > 0) -&
sum(real(d1_${k1}$, dp), 1, d1_${k1}$ > 0)/real(count(d1_${k1}$ > 0, 1), dp)) < dptol&
, 'mean(d1_${k1}$, 1, d1_${k1}$ > 0): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_${k1}$, ${dim}$, d${rank}$_${k1}$ > 0) -&
sum(real(d${rank}$_${k1}$, dp), ${dim}$, d${rank}$_${k1}$ > 0)/real(count(d${rank}$_${k1}$ > 0, ${dim}$), dp))) < dptol&
, 'mean(d${rank}$_${k1}$, ${dim}$, d${rank}$_${k1}$ > 0): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
#:endfor
#:for k1,t1 in REAL_KINDS_TYPES
subroutine test_stats_mean_all_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, mean(d${rank}$_${k1}$), sum(d${rank}$_${k1}$)/real(size(d${rank}$_${k1}$), ${k1}$)&
, 'mean(d${rank}$_${k1}$): uncorrect answer'&
, thr = ${k1}$tol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_all_optmask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, ieee_is_nan(mean(d${rank}$_${k1}$, .false.))&
, 'mean(d${rank}$_${k1}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_${k1}$, 1) - sum(d1_${k1}$, 1)/real(size(d1_${k1}$, 1), ${k1}$)) <${k1}$tol&
, 'mean(d1_${k1}$, 1): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_${k1}$, ${dim}$) -&
sum(d${rank}$_${k1}$, ${dim}$)/real(size(d${rank}$_${k1}$, ${dim}$), ${k1}$))) < ${k1}$tol&
, 'mean(d${rank}$_${k1}$, ${dim}$): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_optmask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, ieee_is_nan(mean(d1_${k1}$, 1, .false.))&
, 'mean(d1_${k1}$, 1, .false.): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error, any(ieee_is_nan(mean(d${rank}$_${k1}$, ${dim}$, .false.)))&
, 'mean(d${rank}$_${k1}$, ${dim}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_mask_all_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, mean(d${rank}$_${k1}$, d${rank}$_${k1}$ > 0)&
, sum(d${rank}$_${k1}$, d${rank}$_${k1}$ > 0)/real(count(d${rank}$_${k1}$ > 0), ${k1}$)&
, 'mean(d${rank}$_${k1}$, d${rank}$_${k1}$ > 0): uncorrect answer'&
, thr = ${k1}$tol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_mask_${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_${k1}$, 1, d1_${k1}$ > 0) -&
sum(d1_${k1}$, 1, d1_${k1}$ > 0)/real(count(d1_${k1}$ > 0, 1), ${k1}$)) < ${k1}$tol&
, 'mean(d1_${k1}$, 1, d1_${k1}$ > 0): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_${k1}$, ${dim}$, d${rank}$_${k1}$ > 0) -&
sum(d${rank}$_${k1}$, ${dim}$, d${rank}$_${k1}$ > 0)/real(count(d${rank}$_${k1}$ > 0, ${dim}$), ${k1}$))) < ${k1}$tol&
, 'mean(d${rank}$_${k1}$, ${dim}$, d${rank}$_${k1}$ > 0): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
#:endfor
#:for k1,t1 in CMPLX_KINDS_TYPES
subroutine test_stats_mean_all_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, mean(d${rank}$_c${k1}$), sum(d${rank}$_c${k1}$)/real(size(d${rank}$_c${k1}$), ${k1}$)&
, 'mean(d${rank}$_c${k1}$): uncorrect answer'&
, thr = ${k1}$tol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_all_optmask_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, ieee_is_nan(real(mean(d${rank}$_c${k1}$, .false.)))&
, 'mean(d${rank}$_c${k1}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_c${k1}$, 1) - sum(d1_c${k1}$, 1)/real(size(d1_c${k1}$, 1), ${k1}$)) <${k1}$tol&
, 'mean(d1_c${k1}$, 1): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_c${k1}$, ${dim}$) -&
sum(d${rank}$_c${k1}$, ${dim}$)/real(size(d${rank}$_c${k1}$, ${dim}$), ${k1}$))) < ${k1}$tol&
, 'mean(d${rank}$_c${k1}$, ${dim}$): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_optmask_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error, ieee_is_nan(real(mean(d1_c${k1}$, 1, .false.)))&
, 'mean(d1_c${k1}$, 1, .false.): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error, any(ieee_is_nan(real(mean(d${rank}$_c${k1}$, ${dim}$, .false.))))&
, 'mean(d${rank}$_c${k1}$, ${dim}$, .false.): uncorrect answer')
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
subroutine test_stats_mean_mask_all_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
#:for rank in range(1, NRANK+1)
call check(error, mean(d${rank}$_c${k1}$, d${rank}$_c${k1}$%re > 0)&
, sum(d${rank}$_c${k1}$, d${rank}$_c${k1}$%re > 0)/real(count(d${rank}$_c${k1}$%re > 0), ${k1}$)&
, 'mean(d${rank}$_c${k1}$, d${rank}$_c${k1}$%re > 0): uncorrect answer'&
, thr = ${k1}$tol)
if (allocated(error)) return
#:endfor
end subroutine
subroutine test_stats_mean_mask_c${k1}$(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
call check(error&
, abs(mean(d1_c${k1}$, 1, d1_c${k1}$%re > 0) -&
sum(d1_c${k1}$, 1, d1_c${k1}$%re > 0)/real(count(d1_c${k1}$%re > 0, 1), ${k1}$)) < ${k1}$tol&
, 'mean(d1_c${k1}$, 1, d1_c${k1}$%re > 0): uncorrect answer'&
)
if (allocated(error)) return
#:for rank in range(2, NRANK+1)
#:for dim in range(1, rank+1)
call check(error&
, sum(abs(mean(d${rank}$_c${k1}$, ${dim}$, d${rank}$_c${k1}$%re > 0) -&
sum(d${rank}$_c${k1}$, ${dim}$, d${rank}$_c${k1}$%re > 0)/real(count(d${rank}$_c${k1}$%re > 0, ${dim}$), ${k1}$))) < ${k1}$tol&
, 'mean(d${rank}$_c${k1}$, ${dim}$, d${rank}$_c${k1}$%re > 0): uncorrect answer'&
)
if (allocated(error)) return
#:endfor
#:endfor
end subroutine
#:endfor
end module test_stats_mean
program tester
use, intrinsic :: iso_fortran_env, only : error_unit
use testdrive, only : run_testsuite, new_testsuite, testsuite_type
use test_stats_mean, only : collect_stats_mean
implicit none
integer :: stat, is
type(testsuite_type), allocatable :: testsuites(:)
character(len=*), parameter :: fmt = '("#", *(1x, a))'
stat = 0
testsuites = [ &
new_testsuite("stats_mean", collect_stats_mean) &
]
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
|