FMUTIL  0.1
Fortran Miscellaneous UTILities
test.f90
Go to the documentation of this file.
1 !##############################################################################
2 ! ________ _____ ______________
3 ! / ____/ |/ / / / /_ __/ _/ /
4 ! / /_ / /|_/ / / / / / / / // /
5 ! / __/ / / / / /_/ / / / _/ // /___
6 ! /_/ /_/ /_/\____/ /_/ /___/_____/
7 !
8 ! Copyright 2020 Bharat Mahajan
9 !
10 ! Licensed under the Apache License, Version 2.0 (the "License");
11 ! you may not use this file except in compliance with the License.
12 ! You may obtain a copy of the License at
13 !
14 ! http://www.apache.org/licenses/LICENSE-2.0
15 !
16 ! Unless required by applicable law or agreed to in writing, software
17 ! distributed under the License is distributed on an "AS IS" BASIS,
18 ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 ! See the License for the specific language governing permissions and
20 ! limitations under the License.
21 !
26 !
27 !##############################################################################
28 
29 
30  program testfmutil
31 
32 
33  use iso_fortran_env, only: output_unit
34  use fmutil
35  use testmodule
36 
37  implicit none
38 
39  ! Simulation Parameters
40  integer, parameter :: vecops = 10000
41 
42  complex(WP), dimension(:), allocatable :: coeffs, roots
43  integer :: error, ctr, status, itr
44  logical :: balanceon
45 
46  real(wp) :: starttime, endtime
47 
48  type(vector) :: rvec, rvec1
49  type(relem) :: rd1, rd2
50  class(vecelem), dimension(:), allocatable :: vecarr
51 
52  type(list) :: list1, list2
53  class(*), allocatable :: item1
54 
55 
56 
57  ! Polynomial Roots Test
58 
59  coeffs = [complex(WP):: 1e-200_wp, (0,-1e200_wp), 1, 100]
60 
61  balanceon = .true.
62 
63  call polyroots(coeffs, roots, error, balanceon = balanceon)
64 
65  print *, roots
66 
67  print *, 'Roots accuracy: ', &
68  sum(reshape([(coeffs(ctr)*(roots**(ubound(coeffs,1)-ctr)), &
69  ctr=1,ubound(coeffs,1))],[ubound(roots,1),ubound(coeffs,1)]),2)
70 
71 
72  ! Vector Tests
73 
74  ! Vector Init: set bucket size, capacity increments, and mold elems
75  print '(1A60,3I6)', 'Vector: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
76  call rvec%Init(1, 5, rd1)
77  print '(1A60,3I6)', 'Vector init: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
78 
79  ! Vector reserve and shrink
80  rd1%rdata = 1.01
81  call rvec%Reserve(49)
82  print '(1A60,3I6)', 'Vector reserved 49: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
83  ctr = rvec%PushBack(rd1)
84  ctr = rvec%PushBack(rd1)
85  rd2 = rvec%PopBack()
86  call rvec%ShrinkToFit()
87  print '(1A60,3I6)', 'added 1 & shrunk: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
88 
89  ! Vector insert and erase elements
90  rd1%rdata = 2.2
91  call rvec%Insert(2,[rd1],3)
92  rd1%rdata = 3.3
93  call rvec%Insert(5,[rd1],3)
94  print '(1A60,3I6)', 'Vector insert 6: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
95  rd1%rdata = 10.10
96  ctr = rvec%PushBack(rd1)
97  call rvec%Erase(3,4)
98  print '(1A60,3I6)', 'Vector push 1 & erase 2: capacity, size, Used Buckets: ', rvec%Capacity(), rvec%Size(), rvec%NUsedBkts()
99 
100  ! Vector element retrieval
101  do ctr = 1, rvec%Size()
102  rd2 = rvec%ElemAt(ctr)
103  print *, 'int(', ctr, ')=', rd2%rdata
104  end do
105 
106  ! Vector slicing
107  call cpu_time(starttime)
108  do ctr = 1, vecops
109  vecarr = rvec%Slice(1,6,1)
110  end do
111  call cpu_time(endtime)
112 
113  select type(vecarr)
114  type is (invalidvecelem)
115  print *, 'Invalid Elem'
116  type is (relem)
117  print *, vecarr
118  end select
119  print *, 'Slice time: ', (endtime-starttime)
120 
121  call cpu_time(starttime)
122  do ctr = 1, vecops
123  itr = 6
124  vecarr = rvec%BktSlice(1,itr)
125  end do
126  call cpu_time(endtime)
127 
128  select type(vecarr)
129  type is (invalidvecelem)
130  print *, 'Invalid Elem'
131  type is (relem)
132  print *, vecarr(1:size(vecarr))
133  end select
134  print *, 'Bkt Slice time: ', (endtime-starttime)
135 
136  ! Vector Assigment test
137  call rvec1%Init(3, 2, rd1)
138  rd1%rdata = 1.01
139  ctr = rvec1%PushBack(rd1)
140  print *, 'before assignment'
141  do ctr = 1, rvec1%Size()
142  rd2 = rvec1%ElemAt(ctr)
143  print *, 'int(', ctr, ')=', rd2%rdata
144  end do
145  rvec1 = rvec
146  print *, 'after assignment'
147  do ctr = 1, rvec1%Size()
148  rd2 = rvec1%ElemAt(ctr)
149  print *, 'int(', ctr, ')=', rd2%rdata
150  end do
151 
152  ! clear
153  call rvec1%Clear()
154  print *, 'after clear'
155  do ctr = 1, rvec1%Size()
156  rd2 = rvec1%ElemAt(ctr)
157  print *, 'int(', ctr, ')=', rd2%rdata
158  end do
159 
160 
161  ! List Data struture test
162 
163  ctr = list1%PushBack(1.8)
164  ctr = list1%PushBack(4)
165  ctr = list1%PushBack(rd1)
166  ctr = list1%PushBack('list item')
167  item1 = list1%PopBack()
168  select type (item1)
169  type is (character(len=*))
170  print *, item1
171  end select
172  call list1%Insert(4,40)
173  call list1%Erase(3)
174  call list1%ShrinktoFit()
175  list2 = list1
176  do ctr = 1, list2%Size()
177  item1 = list2%Item(ctr)
178  select type (item1)
179  type is (real)
180  print *, item1
181  type is (integer)
182  print *, item1
183  type is (relem)
184  print *, item1
185  end select
186  end do
187 
188 
189  contains
190 
191 
192 
193 
194  end program testfmutil
195 
testmodule::relem
Definition: testmod.f90:33
testfmutil
program testfmutil
TestFMUtil Main Program.
Definition: test.f90:30
fmutil
Definition: fmutil.F90:151
testmodule
TestMod Module.
Definition: testmod.f90:30