File: test_maps.fypp

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 (608 lines) | stat: -rw-r--r-- 25,933 bytes parent folder | download
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#: include "common.fypp"
#:set HASH_NAME = ["fnv_1_hasher", "fnv_1a_hasher", "seeded_nmhash32_hasher", "seeded_nmhash32x_hasher", "seeded_water_hasher"]
#:set SIZE_NAME = ["16", "256"]
module test_stdlib_chaining_maps
!! Test various aspects of the runtime system.
!! Running this program may require increasing the stack size to above 48 MBytes
!! or decreasing rand_power to 20 or less
    use testdrive, only : new_unittest, unittest_type, error_type, check
    use :: stdlib_kinds, only : dp, int8, int32
    use stdlib_hashmaps, only : chaining_hashmap_type, int_depth, int_index
    use stdlib_hashmap_wrappers

    implicit none
    private

    type dummy_type
        integer(int8), allocatable :: value(:)
    end type dummy_type

    integer(int32), parameter :: huge32 = huge(0_int32)
    real(dp), parameter       :: hugep1 = real(huge32, dp) + 1.0_dp
    integer, parameter        :: rand_power = 18
    integer, parameter        :: rand_size = 2**rand_power
    integer, parameter        :: test_size = rand_size*4
    integer, parameter        :: test_16 = 2**4
    integer, parameter        :: test_256 = 2**8
    ! key_type = 5 to support int8 and int32 key types tested.  Can be 
    ! increased to generate additional unique int8 vectors additional key types.   
    integer, parameter        :: key_types = 5
    character(len=16)         :: char_size 
    public :: collect_stdlib_chaining_maps


contains

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

        testsuite = [ &
            new_unittest("chaining-maps-fnv_1_hasher-16-byte-words", test_fnv_1_hasher_16_byte_words) &
            #:for hash_ in HASH_NAME
              #:for size_ in SIZE_NAME
                , new_unittest("chaining-maps-${hash_}$-${size_}$-byte-words", test_${hash_}$_${size_}$_byte_words) &
              #:endfor
            #:endfor
            , new_unittest("chaining-maps-removal-spec", test_removal_spec) &
            ]

    end subroutine collect_stdlib_chaining_maps

    #:for hash_ in HASH_NAME
      #:for size_ in SIZE_NAME
        subroutine test_${hash_}$_${size_}$_byte_words(error)
            !> Error handling
            type(error_type), allocatable, intent(out) :: error

            type(chaining_hashmap_type)   :: map
            integer(int8)             :: test_8_bits(test_size,key_types)

            call generate_vector(test_8_bits)
            call map % init( ${hash_}$, slots_bits=10 )

            call test_input_random_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_inquire_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_get_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_removal(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

        end subroutine
      #:endfor
    #:endfor


    subroutine generate_vector(test_8_bits)
        integer(int8), intent(out) :: test_8_bits(test_size, key_types)

        integer                   :: index, key_type
        real(dp)                  :: rand2(2)
        integer(int32)            :: rand_object(rand_size)

        ! Generate a unique int8 vector for each key type tested to avoid
        ! dupilcate keys and mapping conflicts.   
        do key_type = 1, key_types        
            do index=1, rand_size
                call random_number(rand2)
                if (rand2(1) < 0.5_dp) then
                    rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
                else
                    rand_object(index) = floor(rand2(2)*hugep1, int32)
                end if
            end do

            test_8_bits(:,key_type) = transfer( rand_object, 0_int8, test_size )
        end do

    end subroutine

    subroutine test_input_random_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(chaining_hashmap_type), intent(inout) :: map
        integer(int8), intent(in) :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in) :: test_block
        type(dummy_type) :: dummy_val
        integer :: index2
        type(key_type) :: key
    
        logical :: conflict

        do index2=1, test_size, test_block
                       
            ! Test base int8 key interface
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % map_entry( key, dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map int8 entry because of a key conflict.")
            
            ! Test int32 key interface
            ! Use transfer to create int32 vector from generated int8 vector.  
            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % map_entry( key, dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map chaining int32 entry because of a key conflict.")

            ! Test int8 key generic interface
            call map % map_entry( test_8_bits( index2:index2+test_block-1, 3 ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map chaining int8 generic interface")

            ! Test int32 key generic interface
            call map % map_entry( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map chaining int32 generic interface")

            ! Test char key generic interface
            call map % map_entry( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map chaining character generic interface")

            if (allocated(error)) return
        end do

    end subroutine

    subroutine test_inquire_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(chaining_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        integer :: index2
        logical :: present
        type(key_type) :: key

        do index2=1, test_size, test_block
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % key_test( key, present )
            call check(error, present, "Int8 KEY not found in map KEY_TEST.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % key_test( key, present )
            call check(error, present, "Int32 KEY not found in map KEY_TEST.")

            call map % key_test( test_8_bits( index2:index2+test_block-1, 3 ), present )
            call check(error, present, "Int8 KEY generic interface not found in map KEY_TEST.")

            call map % key_test( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), present )
            call check(error, present, "Int32 KEY generic interface not found in map KEY_TEST.")

            call map % key_test( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), present )
            call check(error, present, "Char KEY generic interface not found in map KEY_TEST.")

            if (allocated(error)) return
        end do

    end subroutine

    subroutine test_get_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(chaining_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        integer :: index2
        type(key_type) :: key
        class(*), allocatable :: data
        logical :: exists

        do index2=1, test_size, test_block
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % get_other_data( key, data, exists )
            call check(error, exists, "Unable to get data because int8 key not found in map.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % get_other_data( key, data, exists )
            call check(error, exists, "Unable to get data because int32 key not found in map.")

            call map % get_other_data( test_8_bits( index2:index2+test_block-1, 3 ), data, exists )
            call check(error, exists, "Unable to get data because int8 generic interface key not found in map.")

            call map % get_other_data( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ) , data, exists )
            call check(error, exists, "Unable to get data because int32 generic interface key not found in map.")

            call map % get_other_data( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ) , data, exists )
            call check(error, exists, "Unable to get data because character generic interface key not found in map.")
        end do

    end subroutine

    subroutine test_removal(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(chaining_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        type(key_type) :: key
        integer(int_index) :: index2
        logical :: existed

        do index2=1, test_size, test_block
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % remove(key, existed)
            call check(error, existed,  "Int8 Key not found in entry removal.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % remove(key, existed)
            call check(error, existed,  "Int32 Key not found in entry removal.")

            call map % remove(test_8_bits( index2:index2+test_block-1, 3 ), existed)
            call check(error, existed,  "Int8 Key generic interface not found in entry removal.")

            call map % remove(transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), existed)
            call check(error, existed,  "Int32 Key generic interface not found in entry removal.")

            call map % remove(transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), existed)
            call check(error, existed,  "Character Key generic interface not found in entry removal.")
        end do

    end subroutine

    subroutine test_removal_spec(error)
        !! Test following code provided by @jannisteunissen
        !! https://github.com/fortran-lang/stdlib/issues/785
        type(error_type), allocatable, intent(out) :: error

        type(chaining_hashmap_type) :: map
        type(key_type) :: key
        integer, parameter :: n_max = 500
        integer :: n
        integer, allocatable :: key_counts(:)
        integer, allocatable :: seed(:)
        integer(int8) :: int32_int8(4)
        integer(int32) :: keys(n_max)
        real(dp) :: r_uniform(n_max)
        logical :: existed, present

        call random_seed(size = n)
        allocate(seed(n), source = 123456)
        call random_seed(put = seed)

        call random_number(r_uniform)
        keys = nint(r_uniform * n_max * 0.25_dp)

        call map%init(fnv_1_hasher, slots_bits=10)

        do n = 1, n_max
            call set(key, transfer(keys(n), int32_int8))
            call map%key_test(key, present)
            if (present) then
                call map%remove(key, existed)
                call check(error, existed, "chaining-removal-spec: Key not found in entry removal.")
                return
            else
                call map%map_entry(key)
            end if
        end do

        ! Count number of keys that occur an odd number of times
        allocate(key_counts(minval(keys):maxval(keys)), source = 0)
        do n = 1, n_max
            key_counts(keys(n)) = key_counts(keys(n)) + 1
        end do
        n = sum(iand(key_counts, 1))

        call check(error, map%entries(), n, & 
                   "chaining-removal-spec: Number of expected keys and entries are different.")
        return

    end subroutine

end module

module test_stdlib_open_maps
!! Test various aspects of the runtime system.
!! Running this program may require increasing the stack size to above 48 MBytes
!! or decreasing rand_power to 20 or less
    use testdrive, only : new_unittest, unittest_type, error_type, check
    use :: stdlib_kinds, only : dp, int8, int32
    use stdlib_hashmaps, only : open_hashmap_type, int_depth, int_index
    use stdlib_hashmap_wrappers

    implicit none
    private

    type dummy_type
        integer(int8), allocatable :: value(:)
    end type dummy_type

    integer(int32), parameter :: huge32 = huge(0_int32)
    real(dp), parameter       :: hugep1 = real(huge32, dp) + 1.0_dp
    integer, parameter        :: rand_power = 18
    integer, parameter        :: rand_size = 2**rand_power
    integer, parameter        :: test_size = rand_size*4
    integer, parameter        :: test_16 = 2**4
    integer, parameter        :: test_256 = 2**8
    ! key_type = 5 to support int8 and int32 key types tested.  Can be 
    ! increased to generate additional unique int8 vectors additional key types.  
    integer, parameter        :: key_types = 5
    character(len=16)         :: char_size

    public :: collect_stdlib_open_maps

contains

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

        testsuite = [ &
            new_unittest("open-maps-fnv_1_hasher-16-byte-words", test_fnv_1_hasher_16_byte_words) &
            #:for hash_ in HASH_NAME
              #:for size_ in SIZE_NAME
                , new_unittest("open-maps-${hash_}$-${size_}$-byte-words", test_${hash_}$_${size_}$_byte_words) &
              #:endfor
            #:endfor
            , new_unittest("open-maps-removal-spec", test_removal_spec) &
            ]

    end subroutine collect_stdlib_open_maps

    #:for hash_ in HASH_NAME
      #:for size_ in SIZE_NAME
        subroutine test_${hash_}$_${size_}$_byte_words(error)
            !> Error handling
            type(error_type), allocatable, intent(out) :: error

            type(open_hashmap_type)   :: map
            integer(int8)             :: test_8_bits(test_size,key_types)

            call generate_vector(test_8_bits)

            call map % init( ${hash_}$, slots_bits=10 )

            call test_input_random_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_inquire_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_get_data(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

            call test_removal(error, map, test_8_bits, test_${size_}$)
            if (allocated(error)) return

        end subroutine
      #:endfor
    #:endfor


    subroutine generate_vector(test_8_bits)
        integer(int8), intent(out) :: test_8_bits(test_size, key_types)

        integer                   :: index, key_type
        real(dp)                  :: rand2(2)
        integer(int32)            :: rand_object(rand_size)
        
        ! Generate a unique int8 vector for each key type tested to avoid
        ! dupilcate keys and mapping conflicts. 
        do key_type = 1, key_types
            do index=1, rand_size
                call random_number(rand2)
                if (rand2(1) < 0.5_dp) then
                    rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
                else
                    rand_object(index) = floor(rand2(2)*hugep1, int32)
                end if
            end do

            test_8_bits(:,key_type) = transfer( rand_object, 0_int8, test_size )
        enddo

    end subroutine

    subroutine test_input_random_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(open_hashmap_type), intent(inout) :: map
        integer(int8), intent(in) :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in) :: test_block
        type(dummy_type) :: dummy_val
        integer :: index2
        type(key_type) :: key
        logical :: conflict

        do index2=1, test_size, test_block
            
            ! Test base int8 key interface
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % map_entry( key, dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map int8 entry because of a key conflict.")
            
            ! Test int32 key interface
            ! Use transfer to create int32 vector from generated int8 vector.
            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % map_entry( key, dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map int32 entry because of a key conflict.")

            ! Test int8 generic key interface
            call map % map_entry( test_8_bits( index2:index2+test_block-1, 3 ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map int8 generic key interface entry because of a key conflict.")

            ! Test int32 key generic interface
            call map % map_entry( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map open int32 generic key interface entry because of a key conflict.")

            ! Test character key generic interface
            call map % map_entry( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), dummy_val, conflict )
            call check(error, .not.conflict, "Unable to map open character generic key interface entry because of a key conflict.")

            if (allocated(error)) return
        end do

    end subroutine

    subroutine test_inquire_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(open_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        integer :: index2
        logical :: present
        type(key_type) :: key

        do index2=1, test_size, test_block

            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % key_test( key, present )
            call check(error, present, "Int8 KEY not found in map KEY_TEST.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % key_test( key, present )
            call check(error, present, "Int32 KEY not found in map KEY_TEST.")

            call map % key_test( test_8_bits( index2:index2+test_block-1, 3 ), present )
            call check(error, present, "Int8 KEY generic interface not found in map KEY_TEST.")

            call map % key_test( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), present )
            call check(error, present, "Int32 KEY generic interface not found in map KEY_TEST.")

            call map % key_test( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), present )
            call check(error, present, "Character KEY generic interface not found in map KEY_TEST.")

            if (allocated(error)) return            
        end do

    end subroutine

    subroutine test_get_data(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(open_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        integer :: index2
        type(key_type) :: key
        class(*), allocatable :: data
        logical :: exists

        do index2=1, test_size, test_block
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % get_other_data( key, data, exists )
            call check(error, exists, "Unable to get data because int8 key not found in map.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % get_other_data( key, data, exists )
            call check(error, exists, "Unable to get data because int32 key not found in map.")

            call map % get_other_data( test_8_bits( index2:index2+test_block-1, 3 ), data, exists )
            call check(error, exists, "Unable to get data because int8 generic interface key not found in map.")

            call map % get_other_data( transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), data, exists )
            call check(error, exists, "Unable to get data because int32 generic interface key not found in map.")

            call map % get_other_data( transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), data, exists )
            call check(error, exists, "Unable to get data because character generic interface key not found in map.")
        end do

    end subroutine

    subroutine test_removal(error, map, test_8_bits, test_block)
        type(error_type), allocatable, intent(out) :: error
        type(open_hashmap_type), intent(inout)  :: map
        integer(int8), intent(in)               :: test_8_bits(test_size, key_types)
        integer(int_index), intent(in)          :: test_block
        type(key_type) :: key
        integer(int_index) :: index2
        logical :: existed

        do index2=1, test_size, test_block
            call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
            call map % remove(key, existed)
            call check(error, existed,  "Int8 Key not found in entry removal.")

            call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), [0_int32] ) )
            call map % remove(key, existed)
            call check(error, existed,  "Int32 Key not found in entry removal.")

            call map % remove( test_8_bits( index2:index2+test_block-1, 3 ), existed)
            call check(error, existed,  "Int8 Key generic interface not found in entry removal.")

            call map % remove(transfer( test_8_bits( index2:index2+test_block-1, 4 ), [0_int32] ), existed)
            call check(error, existed,  "Int32 Key generic interface not found in entry removal.")

            call map % remove(transfer( test_8_bits( index2:index2+test_block-1, 5 ), char_size ), existed)
            call check(error, existed,  "Character Key generic interface not found in entry removal.")
        end do

    end subroutine

    subroutine test_removal_spec(error)
        !! Test following code provided by @jannisteunissen
        !! https://github.com/fortran-lang/stdlib/issues/785
        type(error_type), allocatable, intent(out) :: error

        type(open_hashmap_type) :: map
        type(key_type) :: key
        integer, parameter :: n_max = 500
        integer :: n
        integer, allocatable :: key_counts(:)
        integer, allocatable :: seed(:)
        integer(int8) :: int32_int8(4)
        integer(int32) :: keys(n_max)
        real(dp) :: r_uniform(n_max)
        logical :: existed, present

        call random_seed(size = n)
        allocate(seed(n), source = 123456)
        call random_seed(put = seed)

        call random_number(r_uniform)
        keys = nint(r_uniform * n_max * 0.25_dp)

        call map%init(fnv_1_hasher, slots_bits=10)

        do n = 1, n_max
            call set(key, transfer(keys(n), int32_int8))
            call map%key_test(key, present)
            if (present) then
                call map%remove(key, existed)
                call check(error, existed, "open-removal-spec: Key not found in entry removal.")
                return
            else
                call map%map_entry(key)
            end if
        end do

        ! Count number of keys that occur an odd number of times
        allocate(key_counts(minval(keys):maxval(keys)), source = 0)
        do n = 1, n_max
            key_counts(keys(n)) = key_counts(keys(n)) + 1
        end do
        n = sum(iand(key_counts, 1))

        call check(error, map%entries(), n, & 
                   "open-removal-spec: Number of expected keys and entries are different.")
        return

    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_stdlib_open_maps, only : collect_stdlib_open_maps
    use test_stdlib_chaining_maps, only : collect_stdlib_chaining_maps
    implicit none
    integer :: stat, is
    type(testsuite_type), allocatable :: testsuites(:)
    character(len=*), parameter :: fmt = '("#", *(1x, a))'

    stat = 0

    testsuites = [ &
        new_testsuite("stdlib-open-maps", collect_stdlib_open_maps) &
        , new_testsuite("stdlib-chaining-maps", collect_stdlib_chaining_maps) &
        ]

    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