|
| 1 | +module bin_test_m |
| 2 | + !! verify data partitioning across bins |
| 3 | + use sourcery_m, only : bin_t, test_t, test_result_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 | + character(len=*), parameter :: longest_description = & |
| 26 | + "partitioning all item across all bins without item loss" |
| 27 | + |
| 28 | + associate( & |
| 29 | + descriptions => & |
| 30 | + [ character(len=len(longest_description)) :: & |
| 31 | + "partitioning items nearly evenly across bins", & |
| 32 | + "partitioning all item across all bins without item loss" & |
| 33 | + ], & |
| 34 | + outcomes => & |
| 35 | + [ verify_block_partitioning(), & |
| 36 | + verify_all_items_partitioned() & |
| 37 | + ] & |
| 38 | + ) |
| 39 | + call assert(size(descriptions) == size(outcomes), "bin_test_m(results): size(descriptions) == size(outcomes)") |
| 40 | + test_results = test_result_t(descriptions, outcomes) |
| 41 | + end associate |
| 42 | + |
| 43 | + end function |
| 44 | + |
| 45 | + function verify_block_partitioning() result(test_passes) |
| 46 | + !! Verify that the items are partitioned across bins evenly to within a difference of one item per bin |
| 47 | + logical test_passes |
| 48 | + |
| 49 | + type(bin_t), allocatable :: bins(:) |
| 50 | + integer, parameter :: n_items=11, n_bins=7 |
| 51 | + integer b |
| 52 | + |
| 53 | + bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )] |
| 54 | + associate(in_bin => [(bins(b)%last(b) - bins(b)%first(b) + 1, b = 1, n_bins)]) |
| 55 | + associate(remainder => mod(n_items, n_bins), items_per_bin => n_items/n_bins) |
| 56 | + test_passes = all([(in_bin(1:remainder) == items_per_bin + 1)]) .and. all([(in_bin(remainder+1:) == items_per_bin)]) |
| 57 | + end associate |
| 58 | + end associate |
| 59 | + |
| 60 | + end function |
| 61 | + |
| 62 | + function verify_all_items_partitioned() result(test_passes) |
| 63 | + !! Verify that the number of items in each bin sums to the total number of items |
| 64 | + type(bin_t) partition |
| 65 | + logical test_passes |
| 66 | + |
| 67 | + type(bin_t), allocatable :: bins(:) |
| 68 | + integer, parameter :: n_items=11, n_bins=6 |
| 69 | + integer b |
| 70 | + |
| 71 | + bins = [( bin_t(num_items=n_items, num_bins=n_bins, bin_number=b), b = 1,n_bins )] |
| 72 | + test_passes = sum([(bins(b)%last(b) - bins(b)%first(b) + 1, b = 1, n_bins)]) == n_items |
| 73 | + |
| 74 | + end function |
| 75 | + |
| 76 | +end module bin_test_m |
0 commit comments