|
| 1 | +module bin_test_m |
| 2 | + !! Check data partitioning across bins |
| 3 | + use sourcery_m, only : bin_t, test_t, test_result_t, test_description_t, test_description_substring, test_function_i, string_t |
| 4 | + use assert_m, only : assert |
| 5 | + implicit none |
| 6 | + |
| 7 | + private |
| 8 | + public :: bin_test_t |
| 9 | + |
| 10 | + type, extends(test_t) :: bin_test_t |
| 11 | + contains |
| 12 | + procedure, nopass :: subject |
| 13 | + procedure, nopass :: results |
| 14 | + end type |
| 15 | + |
| 16 | +contains |
| 17 | + |
| 18 | + pure function subject() result(specimen) |
| 19 | + character(len=:), allocatable :: specimen |
| 20 | + specimen = "An array of bin_t objects (bins)" |
| 21 | + end function |
| 22 | + |
| 23 | + function results() result(test_results) |
| 24 | + type(test_result_t), allocatable :: test_results(:) |
| 25 | + type(test_description_t), allocatable :: test_descriptions(:) |
| 26 | + |
| 27 | +#ifndef __GFORTRAN__ |
| 28 | + test_descriptions = [ & |
| 29 | + test_description_t(string_t("partitioning items nearly evenly across bins"), check_block_partitioning), & |
| 30 | + test_description_t(string_t("partitioning all item across all bins without item loss"), check_all_items_partitioned) & |
| 31 | + ] |
| 32 | +#else |
| 33 | + ! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument: |
| 34 | + procedure(test_function_i), pointer :: check_block_partitioning_ptr, check_all_items_ptr |
| 35 | + check_block_partitioning_ptr => check_block_partitioning |
| 36 | + check_all_items_ptr => check_all_items_partitioned |
| 37 | + test_descriptions = [ & |
| 38 | + test_description_t(string_t("partitioning items nearly evenly across bins"), check_block_partitioning_ptr), & |
| 39 | + test_description_t(string_t("partitioning all item across all bins without item loss"), check_all_items_ptr) & |
| 40 | + ] |
| 41 | +#endif |
| 42 | + test_descriptions = pack(test_descriptions, test_descriptions%contains_text(string_t(test_description_substring))) |
| 43 | + test_results = test_descriptions%run() |
| 44 | + end function |
| 45 | + |
| 46 | + function check_block_partitioning() result(test_passes) |
| 47 | + !! Check that the items are partitioned across bins evenly to within a difference of one item per bin |
| 48 | + logical test_passes |
| 49 | + |
| 50 | + type(bin_t), allocatable :: bins(:) |
| 51 | + integer, parameter :: n_items=11, n_bins=7 |
| 52 | + integer b |
| 53 | + |
| 54 | + bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )] |
| 55 | + associate(in_bin => [(bins(b)%last() - bins(b)%first() + 1, b = 1, n_bins)]) |
| 56 | + associate(remainder => mod(n_items, n_bins), items_per_bin => n_items/n_bins) |
| 57 | + test_passes = all([(in_bin(1:remainder) == items_per_bin + 1)]) .and. all([(in_bin(remainder+1:) == items_per_bin)]) |
| 58 | + end associate |
| 59 | + end associate |
| 60 | + |
| 61 | + end function |
| 62 | + |
| 63 | + function check_all_items_partitioned() result(test_passes) |
| 64 | + !! Check that the number of items in each bin sums to the total number of items |
| 65 | + type(bin_t) partition |
| 66 | + logical test_passes |
| 67 | + |
| 68 | + type(bin_t), allocatable :: bins(:) |
| 69 | + integer, parameter :: n_items=11, n_bins=6 |
| 70 | + integer b |
| 71 | + |
| 72 | + bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )] |
| 73 | + test_passes = sum([(bins(b)%last() - bins(b)%first() + 1, b = 1, n_bins)]) == n_items |
| 74 | + |
| 75 | + end function |
| 76 | + |
| 77 | +end module bin_test_m |
0 commit comments