Skip to content

Commit cb53f85

Browse files
committed
Add character and int32 keytype tests
1 parent 1724a4f commit cb53f85

File tree

1 file changed

+140
-55
lines changed

1 file changed

+140
-55
lines changed

test/hashmaps/test_maps.fypp

Lines changed: 140 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module test_stdlib_chaining_maps
2424
integer, parameter :: test_size = rand_size*4
2525
integer, parameter :: test_16 = 2**4
2626
integer, parameter :: test_256 = 2**8
27-
27+
integer, parameter :: key_types = 3
28+
character(len=*), parameter :: char_type = ' '
2829
public :: collect_stdlib_chaining_maps
2930

3031
contains
@@ -53,10 +54,10 @@ contains
5354
type(error_type), allocatable, intent(out) :: error
5455

5556
type(chaining_hashmap_type) :: map
56-
integer(int8) :: test_8_bits(test_size)
57+
integer(int8) :: test_8_bits(test_size,key_types)
5758

5859
call generate_vector(test_8_bits)
59-
60+
6061
call map % init( ${hash_}$, slots_bits=10 )
6162

6263
call test_input_random_data(error, map, test_8_bits, test_${size_}$)
@@ -77,29 +78,32 @@ contains
7778

7879

7980
subroutine generate_vector(test_8_bits)
80-
integer(int8), intent(out) :: test_8_bits(test_size)
81+
integer(int8), intent(out) :: test_8_bits(test_size, key_types)
8182

82-
integer :: index
83+
integer :: index, key_type
8384
real(dp) :: rand2(2)
8485
integer(int32) :: rand_object(rand_size)
85-
86-
do index=1, rand_size
87-
call random_number(rand2)
88-
if (rand2(1) < 0.5_dp) then
89-
rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
90-
else
91-
rand_object(index) = floor(rand2(2)*hugep1, int32)
92-
end if
86+
integer :: key_type
87+
88+
do key_type = 1, key_types
89+
do index=1, rand_size
90+
call random_number(rand2)
91+
if (rand2(1) < 0.5_dp) then
92+
rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
93+
else
94+
rand_object(index) = floor(rand2(2)*hugep1, int32)
95+
end if
96+
end do
97+
98+
test_8_bits(:,key_type) = transfer( rand_object, 0_int8, test_size )
9399
end do
94100

95-
test_8_bits(:) = transfer( rand_object, 0_int8, test_size )
96-
97101
end subroutine
98102

99103
subroutine test_input_random_data(error, map, test_8_bits, test_block)
100104
type(error_type), allocatable, intent(out) :: error
101105
type(chaining_hashmap_type), intent(inout) :: map
102-
integer(int8), intent(in) :: test_8_bits(test_size)
106+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
103107
integer(int_index), intent(in) :: test_block
104108
class(*), allocatable :: dummy
105109
type(dummy_type) :: dummy_val
@@ -108,32 +112,54 @@ contains
108112
type(other_type) :: other
109113
logical :: conflict
110114

111-
do index2=1, size(test_8_bits), test_block
112-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
115+
do index2=1, test_size, test_block
116+
113117
if (allocated(dummy)) deallocate(dummy)
114-
dummy_val % value = test_8_bits( index2:index2+test_block-1 )
118+
dummy_val % value = test_8_bits( index2:index2+test_block-1, 1 )
115119
allocate( dummy, source=dummy_val )
116120
call set ( other, dummy )
121+
122+
! Test all key interfaces
123+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
124+
call map % map_entry( key, other, conflict )
125+
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
126+
127+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
117128
call map % map_entry( key, other, conflict )
118129
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
130+
131+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
132+
call map % map_entry( key, other, conflict )
133+
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
134+
119135
if (allocated(error)) return
136+
120137
end do
121138

122139
end subroutine
123140

124141
subroutine test_inquire_data(error, map, test_8_bits, test_block)
125142
type(error_type), allocatable, intent(out) :: error
126143
type(chaining_hashmap_type), intent(inout) :: map
127-
integer(int8), intent(in) :: test_8_bits(test_size)
144+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
128145
integer(int_index), intent(in) :: test_block
129146
integer :: index2
130147
logical :: present
131148
type(key_type) :: key
132149

133-
do index2=1, size(test_8_bits), test_block
134-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
150+
do index2=1, test_size, test_block
151+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
152+
call map % key_test( key, present )
153+
call check(error, present, "KEY not found in map KEY_TEST.")
154+
155+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
156+
call map % key_test( key, present )
157+
call check(error, present, "KEY not found in map KEY_TEST.")
158+
159+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
135160
call map % key_test( key, present )
136161
call check(error, present, "KEY not found in map KEY_TEST.")
162+
137163
if (allocated(error)) return
138164
end do
139165

@@ -142,15 +168,23 @@ contains
142168
subroutine test_get_data(error, map, test_8_bits, test_block)
143169
type(error_type), allocatable, intent(out) :: error
144170
type(chaining_hashmap_type), intent(inout) :: map
145-
integer(int8), intent(in) :: test_8_bits(test_size)
171+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
146172
integer(int_index), intent(in) :: test_block
147173
integer :: index2
148174
type(key_type) :: key
149175
type(other_type) :: other
150176
logical :: exists
151177

152-
do index2=1, size(test_8_bits), test_block
153-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
178+
do index2=1, test_size, test_block
179+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
180+
call map % get_other_data( key, other, exists )
181+
call check(error, exists, "Unable to get data because key not found in map.")
182+
183+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
184+
call map % get_other_data( key, other, exists )
185+
call check(error, exists, "Unable to get data because key not found in map.")
186+
187+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
154188
call map % get_other_data( key, other, exists )
155189
call check(error, exists, "Unable to get data because key not found in map.")
156190
end do
@@ -160,14 +194,22 @@ contains
160194
subroutine test_removal(error, map, test_8_bits, test_block)
161195
type(error_type), allocatable, intent(out) :: error
162196
type(chaining_hashmap_type), intent(inout) :: map
163-
integer(int8), intent(in) :: test_8_bits(test_size)
197+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
164198
integer(int_index), intent(in) :: test_block
165199
type(key_type) :: key
166200
integer(int_index) :: index2
167201
logical :: existed
168202

169-
do index2=1, size(test_8_bits), test_block
170-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
203+
do index2=1, test_size, test_block
204+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
205+
call map % remove(key, existed)
206+
call check(error, existed, "Key not found in entry removal.")
207+
208+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
209+
call map % remove(key, existed)
210+
call check(error, existed, "Key not found in entry removal.")
211+
212+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
171213
call map % remove(key, existed)
172214
call check(error, existed, "Key not found in entry removal.")
173215
end do
@@ -249,6 +291,7 @@ module test_stdlib_open_maps
249291
integer, parameter :: test_size = rand_size*4
250292
integer, parameter :: test_16 = 2**4
251293
integer, parameter :: test_256 = 2**8
294+
integer, parameter :: key_types = 3
252295

253296
public :: collect_stdlib_open_maps
254297

@@ -278,7 +321,7 @@ contains
278321
type(error_type), allocatable, intent(out) :: error
279322

280323
type(open_hashmap_type) :: map
281-
integer(int8) :: test_8_bits(test_size)
324+
integer(int8) :: test_8_bits(test_size,key_types)
282325

283326
call generate_vector(test_8_bits)
284327

@@ -302,29 +345,31 @@ contains
302345

303346

304347
subroutine generate_vector(test_8_bits)
305-
integer(int8), intent(out) :: test_8_bits(test_size)
348+
integer(int8), intent(out) :: test_8_bits(test_size, key_types)
306349

307-
integer :: index
350+
integer :: index, key_type
308351
real(dp) :: rand2(2)
309352
integer(int32) :: rand_object(rand_size)
310-
311-
do index=1, rand_size
312-
call random_number(rand2)
313-
if (rand2(1) < 0.5_dp) then
314-
rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
315-
else
316-
rand_object(index) = floor(rand2(2)*hugep1, int32)
317-
end if
318-
end do
319-
320-
test_8_bits(:) = transfer( rand_object, 0_int8, test_size )
353+
354+
do key_type = 1, key_types
355+
do index=1, rand_size
356+
call random_number(rand2)
357+
if (rand2(1) < 0.5_dp) then
358+
rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
359+
else
360+
rand_object(index) = floor(rand2(2)*hugep1, int32)
361+
end if
362+
end do
363+
364+
test_8_bits(:,key_type) = transfer( rand_object, 0_int8, test_size )
365+
enddo
321366

322367
end subroutine
323368

324369
subroutine test_input_random_data(error, map, test_8_bits, test_block)
325370
type(error_type), allocatable, intent(out) :: error
326371
type(open_hashmap_type), intent(inout) :: map
327-
integer(int8), intent(in) :: test_8_bits(test_size)
372+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
328373
integer(int_index), intent(in) :: test_block
329374
class(*), allocatable :: dummy
330375
type(dummy_type) :: dummy_val
@@ -333,49 +378,81 @@ contains
333378
type(other_type) :: other
334379
logical :: conflict
335380

336-
do index2=1, size(test_8_bits), test_block
337-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
381+
do index2=1, test_size, test_block
382+
338383
if (allocated(dummy)) deallocate(dummy)
339-
dummy_val % value = test_8_bits( index2:index2+test_block-1 )
384+
dummy_val % value = test_8_bits( index2:index2+test_block-1, 1 )
340385
allocate( dummy, source=dummy_val )
341386
call set ( other, dummy )
387+
388+
! Test all key interfaces
389+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
390+
call map % map_entry( key, other, conflict )
391+
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
392+
393+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
394+
call map % map_entry( key, other, conflict )
395+
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
396+
397+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
342398
call map % map_entry( key, other, conflict )
343399
call check(error, .not.conflict, "Unable to map entry because of a key conflict.")
400+
344401
if (allocated(error)) return
402+
345403
end do
346404

347405
end subroutine
348406

349407
subroutine test_inquire_data(error, map, test_8_bits, test_block)
350408
type(error_type), allocatable, intent(out) :: error
351409
type(open_hashmap_type), intent(inout) :: map
352-
integer(int8), intent(in) :: test_8_bits(test_size)
410+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
353411
integer(int_index), intent(in) :: test_block
354412
integer :: index2
355413
logical :: present
356414
type(key_type) :: key
357415

358-
do index2=1, size(test_8_bits), test_block
359-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
416+
do index2=1, test_size, test_block
417+
418+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
419+
call map % key_test( key, present )
420+
call check(error, present, "KEY not found in map KEY_TEST.")
421+
422+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
360423
call map % key_test( key, present )
361424
call check(error, present, "KEY not found in map KEY_TEST.")
425+
426+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
427+
call map % key_test( key, present )
428+
call check(error, present, "KEY not found in map KEY_TEST.")
429+
362430
if (allocated(error)) return
431+
363432
end do
364433

365434
end subroutine
366435

367436
subroutine test_get_data(error, map, test_8_bits, test_block)
368437
type(error_type), allocatable, intent(out) :: error
369438
type(open_hashmap_type), intent(inout) :: map
370-
integer(int8), intent(in) :: test_8_bits(test_size)
439+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
371440
integer(int_index), intent(in) :: test_block
372441
integer :: index2
373442
type(key_type) :: key
374443
type(other_type) :: other
375444
logical :: exists
376445

377-
do index2=1, size(test_8_bits), test_block
378-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
446+
do index2=1, test_size, test_block
447+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
448+
call map % get_other_data( key, other, exists )
449+
call check(error, exists, "Unable to get data because key not found in map.")
450+
451+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
452+
call map % get_other_data( key, other, exists )
453+
call check(error, exists, "Unable to get data because key not found in map.")
454+
455+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
379456
call map % get_other_data( key, other, exists )
380457
call check(error, exists, "Unable to get data because key not found in map.")
381458
end do
@@ -385,14 +462,22 @@ contains
385462
subroutine test_removal(error, map, test_8_bits, test_block)
386463
type(error_type), allocatable, intent(out) :: error
387464
type(open_hashmap_type), intent(inout) :: map
388-
integer(int8), intent(in) :: test_8_bits(test_size)
465+
integer(int8), intent(in) :: test_8_bits(test_size, key_types)
389466
integer(int_index), intent(in) :: test_block
390467
type(key_type) :: key
391468
integer(int_index) :: index2
392469
logical :: existed
393470

394-
do index2=1, size(test_8_bits), test_block
395-
call set( key, test_8_bits( index2:index2+test_block-1 ) )
471+
do index2=1, test_size, test_block
472+
call set( key, test_8_bits( index2:index2+test_block-1, 1 ) )
473+
call map % remove(key, existed)
474+
call check(error, existed, "Key not found in entry removal.")
475+
476+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 2 ), 0_int32 ) )
477+
call map % remove(key, existed)
478+
call check(error, existed, "Key not found in entry removal.")
479+
480+
call set( key, transfer( test_8_bits( index2:index2+test_block-1, 3 ), char_type ) )
396481
call map % remove(key, existed)
397482
call check(error, existed, "Key not found in entry removal.")
398483
end do

0 commit comments

Comments
 (0)