-
Notifications
You must be signed in to change notification settings - Fork 160
/
test_sorting.f90
1019 lines (820 loc) · 33.1 KB
/
test_sorting.f90
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module test_sorting
use, intrinsic :: iso_fortran_env, only: compiler_version, error_unit
use stdlib_kinds, only: int32, int64, dp, sp
use stdlib_sorting
use stdlib_string_type, only: string_type, assignment(=), operator(>), &
operator(<), write(formatted)
use testdrive, only: new_unittest, unittest_type, error_type, check
implicit none
integer(int32), parameter :: test_power = 16
integer(int32), parameter :: char_set_size = 16
integer(int32), parameter :: test_size = 2_int32**test_power
integer(int32), parameter :: char_size = char_set_size**4
integer(int32), parameter :: string_size = char_set_size**3
integer(int32), parameter :: block_size = test_size/6
integer, parameter :: repeat = 1
integer(int32) :: &
blocks(0:test_size-1), &
decrease(0:test_size-1), &
identical(0:test_size-1), &
increase(0:test_size-1), &
rand0(0:test_size-1), &
rand1(0:test_size-1), &
rand2(0:test_size-1), &
rand3(0:test_size-1), &
rand10(0:test_size-1)
character(len=4) :: &
char_decrease(0:char_size-1), &
char_increase(0:char_size-1), &
char_rand(0:char_size-1)
type(string_type) :: &
string_decrease(0:string_size-1), &
string_increase(0:string_size-1), &
string_rand(0:string_size-1)
integer(int32) :: dummy(0:test_size-1)
character(len=4) :: char_dummy(0:char_size-1)
type(string_type) :: string_dummy(0:string_size-1)
integer(int_size) :: index(0:max(test_size, char_size, string_size)-1)
integer(int32) :: work(0:test_size/2-1)
character(len=4) :: char_work(0:char_size/2-1)
type(string_type) :: string_work(0:string_size/2-1)
integer(int_size) :: iwork(0:max(test_size, char_size, &
string_size)/2-1)
integer :: count, i, index1, index2, j, k, l, temp
real(sp) :: arand, brand
character(*), parameter :: filename = 'test_sorting.txt'
integer :: lun
character(len=4) :: char_temp
type(string_type) :: string_temp
logical :: ltest, ldummy
contains
!> Collect all exported unit tests
subroutine collect_sorting(testsuite)
!> Collection of tests
type(unittest_type), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
new_unittest('int_ord_sorts', test_int_ord_sorts), &
new_unittest('char_ord_sorts', test_char_ord_sorts), &
new_unittest('string_ord_sorts', test_string_ord_sorts), &
new_unittest('int_sorts', test_int_sorts), &
new_unittest('char_sorts', test_char_sorts), &
new_unittest('string_sorts', test_string_sorts), &
new_unittest('int_sort_indexes', test_int_sort_indexes), &
new_unittest('char_sort_indexes', test_char_sort_indexes), &
new_unittest('string_sort_indexes', test_string_sort_indexes) &
]
end subroutine collect_sorting
subroutine initialize_tests()
! Create the test arrays
identical(:) = 10
do i=0, test_size-1
increase(i) = i
decrease(i) = test_size - 1 - i
call random_number( arand )
rand0(i) = int( floor( 4 * arand * test_size ), kind=int32 )
rand1(i) = int( floor( arand * test_size / 4 ), kind=int32 )
end do
blocks(:) = increase(:)
blocks(0:block_size-1) = increase(4*block_size:5*block_size-1)
blocks(block_size:2*block_size-1) = increase(0:block_size-1)
blocks(2*block_size:3*block_size-1) = increase(2*block_size:3*block_size-1)
blocks(3*block_size:4*block_size-1) = increase(block_size:2*block_size-1)
blocks(4*block_size:5*block_size-1) = increase(3*block_size:4*block_size-1)
rand2(:) = increase(:)
do i=0, test_size-1
call random_number( arand )
index1 = int( floor( arand * test_size ), kind=int32 )
temp = rand2(i)
rand2(i) = rand2(index1)
rand2(index1) = temp
end do
rand3(:) = increase(:)
do i=0, 2
call random_number( arand )
call random_number( brand )
index1 = int( floor( arand * test_size ), kind=int32 )
index2 = int( floor( brand * test_size ), kind=int32 )
temp = rand3(index1)
rand3(index1) = rand3(index2)
rand3(index2) = temp
end do
rand10(:) = increase(:)
do i=test_size-10, test_size-1
call random_number( arand )
rand10(i) = int( floor( arand * test_size ), kind=int32 )
end do
count = 0
do i=0, char_set_size-1
do j=0, char_set_size-1
do k=0, char_set_size-1
do l=0, char_set_size-1
char_increase(count) = achar(97+i) // achar(97+j) // &
achar(97+k) // achar(97+l)
count = count + 1
end do
end do
end do
end do
do i=0, char_size-1
char_decrease(char_size-1-i) = char_increase(i)
end do
char_rand(:) = char_increase(:)
do i=0, char_size-1
call random_number( arand )
index1 = int( floor( arand * char_size ), kind=int32 )
char_temp = char_rand(i)
char_rand(i) = char_rand(index1)
char_rand(index1) = char_temp
end do
count = 0
do i=0, char_set_size-1
do j=0, char_set_size-1
do k=0, char_set_size-1
string_increase(count) = achar(97+i) // achar(97+j) // &
achar(97+k)
count = count + 1
end do
end do
end do
do i=0, string_size-1
string_decrease(string_size - 1 - i) = char_increase(i)
end do
string_rand(:) = string_increase(:)
do i=0, string_size-1
call random_number( arand )
index1 = int( floor( arand * string_size ), kind=int32 )
string_temp = string_rand(i)
string_rand(i) = string_rand(index1)
string_rand(index1) = string_temp
end do
! Create and intialize file to report the results of the sortings
open( newunit=lun, file=filename, access='sequential', action='write', &
form='formatted', status='replace' )
write( lun, '(a)' ) trim(compiler_version())
write( lun, * )
write( lun, '("| Type | Elements | Array Name | Method ' // &
' | Time (s) |")' )
write( lun, '("|-------------|----------|-----------------|-----------' // &
'--|-----------|")' )
end subroutine initialize_tests
subroutine test_int_ord_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer(int64) :: i
integer, allocatable :: d1(:)
logical :: ltest
call test_int_ord_sort( blocks, "Blocks", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( decrease, "Decreasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( identical, "Identical", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( increase, "Increasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( rand1, "Random dense", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( rand2, "Random order", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( rand0, "Random sparse", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( rand3, "Random 3", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_ord_sort( rand10, "Random 10", ltest )
call check(error, ltest)
if (allocated(error)) return
!triggered an issue in insertion_sort
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
call ord_sort( d1 )
call verify_sort( d1, ltest, i )
call check(error, ltest)
end subroutine test_int_ord_sorts
subroutine test_int_ord_sort( a, a_name, ltest )
integer(int32), intent(in) :: a(:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
dummy = a
call system_clock( t0, rate )
call ord_sort( dummy, work )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "ORD_SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a12, 2i7)') 'dummy(i-1:i) = ', dummy(i-1:i)
end if
write( lun, '("| Integer |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
test_size, a_name, "Ord_Sort", tdiff/rate
!reverse
dummy = a
call ord_sort( dummy, work, reverse = .true.)
call verify_reverse_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse + work ORD_SORT did not sort " // a_name // &
"."
write(*,*) 'i = ', i
write(*,'(a12, 2i7)') 'dummy(i-1:i) = ', dummy(i-1:i)
end if
dummy = a
call ord_sort( dummy, reverse = .true.)
call verify_reverse_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse ORD_SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a12, 2i7)') 'dummy(i-1:i) = ', dummy(i-1:i)
end if
end subroutine test_int_ord_sort
subroutine test_char_ord_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical :: ltest
call test_char_ord_sort( char_decrease, "Char. Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_ord_sort( char_increase, "Char. Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_ord_sort( char_rand, "Char. Random", ltest )
call check(error, ltest)
end subroutine test_char_ord_sorts
subroutine test_char_ord_sort( a, a_name, ltest )
character(len=4), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
char_dummy = a
call system_clock( t0, rate )
call ord_sort( char_dummy, char_work )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_char_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "ORD_SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
write( lun, '("| Character |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
char_size, a_name, "Ord_Sort", tdiff/rate
!reverse
char_dummy = a
call ord_sort( char_dummy, char_work, reverse = .true. )
call verify_char_reverse_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse + work ORD_SORT did not sort " // a_name // &
"."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
char_dummy = a
call ord_sort( char_dummy, reverse = .true. )
call verify_char_reverse_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse + work ORD_SORT did not sort " // a_name // &
"."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
end subroutine test_char_ord_sort
subroutine test_string_ord_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical:: ltest
call test_string_ord_sort( string_decrease, "String Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_ord_sort( string_increase, "String Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_ord_sort( string_rand, "String Random" , ltest)
call check(error, ltest)
end subroutine test_string_ord_sorts
subroutine test_string_ord_sort( a, a_name, ltest )
type(string_type), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
string_dummy = a
call system_clock( t0, rate )
call ord_sort( string_dummy, string_work )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_string_sort( string_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "ORD_SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
write( lun, '("| String_type |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
string_size, a_name, "Ord_Sort", tdiff/rate
!reverse
string_dummy = a
call ord_sort( string_dummy, string_work, reverse = .true. )
call verify_string_reverse_sort( string_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse + work ORD_SORT did not sort " // a_name // &
"."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
string_dummy = a
call ord_sort( string_dummy, reverse = .true. )
call verify_string_reverse_sort( string_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse ORD_SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a, 2(1x,a))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
end subroutine test_string_ord_sort
subroutine test_int_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer(int64) :: i
integer, allocatable :: d1(:)
logical :: ltest
call test_int_sort( blocks, "Blocks", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( decrease, "Decreasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( identical, "Identical", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( increase, "Increasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( rand1, "Random dense", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( rand2, "Random order", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( rand0, "Random sparse", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( rand3, "Random 3", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort( rand10, "Random 10", ltest )
call check(error, ltest)
if (allocated(error)) return
!triggered an issue in insertion
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
call sort( d1 )
call verify_sort( d1, ltest, i )
call check(error, ltest)
end subroutine test_int_sorts
subroutine test_int_sort( a, a_name, ltest )
integer(int32), intent(in) :: a(:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
dummy = a
call system_clock( t0, rate )
call sort( dummy )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a12, 2i7)') 'dummy(i-1:i) = ', dummy(i-1:i)
end if
write( lun, '("| Integer |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
test_size, a_name, "Sort", tdiff/rate
! reverse
dummy = a
call sort( dummy, .true.)
call verify_reverse_sort(dummy, valid, i)
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a12, 2i7)') 'dummy(i-1:i) = ', dummy(i-1:i)
end if
end subroutine test_int_sort
subroutine test_char_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical :: ltest
call test_char_sort( char_decrease, "Char. Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_sort( char_increase, "Char. Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_sort( char_rand, "Char. Random", ltest )
call check(error, ltest)
end subroutine test_char_sorts
subroutine test_char_sort( a, a_name, ltest )
character(len=4), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
char_dummy = a
call system_clock( t0, rate )
call sort( char_dummy )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_char_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
write( lun, '("| Character |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
char_size, a_name, "Sort", tdiff/rate
!reverse
char_dummy = a
call sort( char_dummy, .true.)
call verify_char_reverse_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
end subroutine test_char_sort
subroutine test_string_sorts(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical :: ltest
call test_string_sort( string_decrease, "String Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_sort( string_increase, "String Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_sort( string_rand, "String Random", ltest )
call check(error, ltest)
end subroutine test_string_sorts
subroutine test_string_sort( a, a_name, ltest )
type(string_type), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
string_dummy = a
call system_clock( t0, rate )
call sort( string_dummy )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_string_sort( string_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
write( lun, '("| String_type |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
string_size, a_name, "Sort", tdiff/rate
! reverse
string_dummy = a
call sort( string_dummy, .true.)
call verify_string_reverse_sort(string_dummy, valid, i)
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "reverse SORT did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
end subroutine test_string_sort
subroutine test_int_sort_indexes(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
integer(int64) :: i
integer(int32), allocatable :: d1(:)
integer(int64), allocatable :: index(:)
logical :: ltest
call test_int_sort_index( blocks, "Blocks", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( decrease, "Decreasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( identical, "Identical", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( increase, "Increasing", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( rand1, "Random dense", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( rand2, "Random order", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( rand0, "Random sparse", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( rand3, "Random 3", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_int_sort_index( rand10, "Random 10", ltest )
call check(error, ltest)
if (allocated(error)) return
d1 = [10, 2, -3, -4, 6, -6, 7, -8, 9, 0, 1, 20]
allocate( index(size(d1)) )
call sort_index( d1, index )
call verify_sort( d1, ltest, i )
call check(error, ltest)
end subroutine test_int_sort_indexes
subroutine test_int_sort_index( a, a_name, ltest )
integer(int32), intent(inout) :: a(:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
dummy = a
call system_clock( t0, rate )
call sort_index( dummy, index, work, iwork )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
dummy = a(index(0:size(a)-1))
call verify_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT_INDEX did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a18, 2i7)') 'a(index(i-1:i)) = ', a(index(i-1:i))
end if
write( lun, '("| Integer |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
test_size, a_name, "Sort_Index", tdiff/rate
dummy = a
call sort_index( dummy, index, work, iwork, reverse=.true. )
dummy = a(index(size(a)-1))
call verify_reverse_sort( dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT_INDEX did not reverse sort " // &
a_name // "."
write(*,*) 'i = ', i
write(*,'(a18, 2i7)') 'a(index(i-1:i)) = ', a(index(i-1:i))
end if
end subroutine test_int_sort_index
subroutine test_char_sort_indexes(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical :: ltest
call test_char_sort_index( char_decrease, "Char. Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_sort_index( char_increase, "Char. Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_char_sort_index( char_rand, "Char. Random", ltest )
call check(error, ltest)
end subroutine test_char_sort_indexes
subroutine test_char_sort_index( a, a_name, ltest )
character(len=4), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
char_dummy = a
call system_clock( t0, rate )
call sort_index( char_dummy, index, char_work, iwork )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_char_sort( char_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT_INDEX did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'char_dummy(i-1:i) = ', char_dummy(i-1:i)
end if
write( lun, '("| Character |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
char_size, a_name, "Sort_Index", tdiff/rate
end subroutine test_char_sort_index
subroutine test_string_sort_indexes(error)
!> Error handling
type(error_type), allocatable, intent(out) :: error
logical :: ltest
call test_string_sort_index( string_decrease, "String Decrease", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_sort_index( string_increase, "String Increase", ltest )
call check(error, ltest)
if (allocated(error)) return
call test_string_sort_index( string_rand, "String Random", ltest )
call check(error, ltest)
end subroutine test_string_sort_indexes
subroutine test_string_sort_index( a, a_name, ltest )
type(string_type), intent(in) :: a(0:)
character(*), intent(in) :: a_name
logical, intent(out) :: ltest
integer(int64) :: t0, t1, tdiff
real(dp) :: rate
integer(int64) :: i
logical :: valid
ltest = .true.
tdiff = 0
do i = 1, repeat
string_dummy = a
call system_clock( t0, rate )
call sort_index( string_dummy, index, string_work, iwork )
call system_clock( t1, rate )
tdiff = tdiff + t1 - t0
end do
tdiff = tdiff/repeat
call verify_string_sort( string_dummy, valid, i )
ltest = (ltest .and. valid)
if ( .not. valid ) then
write( *, * ) "SORT_INDEX did not sort " // a_name // "."
write(*,*) 'i = ', i
write(*,'(a17, 2(1x,a4))') 'string_dummy(i-1:i) = ', &
string_dummy(i-1:i)
end if
write( lun, '("| String_type |", 1x, i7, 2x, "|", 1x, a15, " |", ' // &
'a12, " |", F10.6, " |" )' ) &
string_size, a_name, "Sort_Index", tdiff/rate
end subroutine test_string_sort_index
subroutine verify_sort( a, valid, i )
integer(int32), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) > a(i) ) return
end do
valid = .true.
end subroutine verify_sort
subroutine verify_string_sort( a, valid, i )
type(string_type), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) > a(i) ) return
end do
valid = .true.
end subroutine verify_string_sort
subroutine verify_char_sort( a, valid, i )
character(len=4), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) > a(i) ) return
end do
valid = .true.
end subroutine verify_char_sort
subroutine verify_char_reverse_sort( a, valid, i )
character(len=4), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) < a(i) ) return
end do
valid = .true.
end subroutine verify_char_reverse_sort
subroutine verify_reverse_sort( a, valid, i )
integer(int32), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) < a(i) ) return
end do
valid = .true.
end subroutine verify_reverse_sort
subroutine verify_string_reverse_sort( a, valid, i )
type(string_type), intent(in) :: a(0:)
logical, intent(out) :: valid
integer(int64), intent(out) :: i
integer(int64) :: n
n = size( a, kind=int64 )
valid = .false.
do i=1, n-1
if ( a(i-1) < a(i) ) return
end do
valid = .true.
end subroutine verify_string_reverse_sort
end module test_sorting
program tester
use, intrinsic :: iso_fortran_env, only: compiler_version, error_unit
use testdrive, only: new_testsuite, run_testsuite, testsuite_type
use test_sorting, only: initialize_tests, collect_sorting
implicit none
integer :: stat, is
type(testsuite_type), allocatable :: testsuites(:)
character(len=*), parameter :: fmt = '("#", *(1x, a))'