Skip to content

Commit

Permalink
more testing for gbytec subroutines (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardhartnett authored Feb 5, 2024
1 parent a4e229b commit 1d230e5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/g2_gbytesc.F90
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
!> has more elements, use g2_sbytesc().
!>
!> @param[in] in Array input.
!> @param[out] iout Unpacked array output.
!> @param[inout] iout Unpacked array output.
!> @param[in] iskip Initial number of bits to skip.
!> @param[in] nbits Number of bits of each integer in IN to take.
!>
Expand All @@ -32,7 +32,7 @@ end subroutine g2_gbytec
!> This should be used when input array IN has only one element. If IN
!> has more elements, use G2_SBYTESC().
!>
!> @param[out] out packed array output
!> @param[inout] out packed array output
!> @param[in] in unpacked array input
!> @param[in] iskip initial number of bits to skip
!> @param[in] nbits Number of bits of each integer in OUT to fill.
Expand Down
142 changes: 114 additions & 28 deletions tests/test_gbytec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ program test_gbytec
character*1 :: out5(5)
character*1 :: out8(8)
character*1 :: out10(10)
character*1 :: out16(16)
character*1 :: out32(32)
integer :: in4(4), in1(1)
integer, parameter :: n = 1
integer :: in(n)
real :: r_in(n)
Expand All @@ -24,24 +27,30 @@ program test_gbytec
integer :: nskip = 0
integer :: i
integer :: num
integer(kind = 4) :: in44(4), in44_1(4)
integer(kind = 8) :: in8(1), in8_1(1), in84(4), in84_1(4)

print *, 'Testing g2_gbytesc.f subroutines.'
print *, 'Testing g2_gbytesc.F90 subroutines.'

print *, 'Testing g2_sbytec()...'
print *, ' testing g2_sbytec()...'
in(1) = 3
out(1) = char(0)
call g2_sbytec(out, in, iskip, nbits)
if (ichar(out(1)) .ne. in(1)) stop 10

print *, 'Testing g2_sbytesc()...'
print *, ' testing g2_gbytec()...'
call g2_gbytec(out, in, iskip, nbits)
if (ichar(out(1)) .ne. in(1)) stop 11

print *, ' testing g2_sbytesc()...'
in(1) = 3
out(1) = char(0)
call g2_sbytesc(out, in, iskip, nbits, nskip, n)
if (ichar(out(1)) .ne. in(1)) stop 20

! This will pack the numbers 1 and 2 into the first two chars of the
! buffer. The rest of the output buffer will remain zeros.
print *, 'Testing g2_sbytesc() packing 2 values...'
print *, ' testing g2_sbytesc() packing 2 values...'
in2(1) = 1
in2(2) = 2
do i = 1, 8
Expand All @@ -58,7 +67,7 @@ program test_gbytec
end do

! Now pack 5 values into the 5 character array out5.
print *, 'Testing g2_sbytesc() packing 5 values...'
print *, ' testing g2_sbytesc() packing 5 values...'
in5(1) = 1
in5(2) = 2
in5(3) = 3
Expand All @@ -76,7 +85,7 @@ program test_gbytec

! Now pack 5 values into the 10 character array out10. Skip every
! other byte in the output.
print *, 'Testing g2_sbytesc() packing 5 values, skipping every other byte...'
print *, ' testing g2_sbytesc() packing 5 values, skipping every other byte...'
nbits = 8
nskip = 0
do i = 1, 10
Expand All @@ -88,32 +97,45 @@ program test_gbytec
if (mod(i, 2) .gt. 0) then
if (ichar(out10(i)) .ne. in5(int(i/2) + 1)) stop 51;
else
if (ichar(out10(i)) .ne. 0) stop 50;
if (ichar(out10(i)) .ne. 0) stop 52;
endif
end do

print *, 'Testing g2_sbytec() with iskip of 1...'
print *, ' testing g2_sbytec() with iskip of 1...'
in(1) = 1
out(1) = char(0)
call g2_sbytec(out, in, 1, 6)
! print '(z2.2)', out(1)
if (ichar(out(1)) .ne. 2) stop 20
if (ichar(out(1)) .ne. 2) stop 53

print *, 'Testing g2_sbytesc() with a small array...'
print *, ' testing g2_sbytesc() with a size 4 output array...'
iskip = 0
nbits = 32
nskip = 0
num = 1
in(1) = 1
call g2_sbytesc(out4, in, iskip, nbits, nskip, num)
if (ichar(out4(1)) .ne. 0 .and. ichar(out4(2)) .ne. 0 .and. ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 1) stop 50
!print '(z2.2)', out4(1)
if (ichar(out4(1)) .ne. 0 .and. ichar(out4(2)) .ne. 0 .and. &
ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 1) stop 60

print *, ' now unpack into 4 ints with g2_gbytesc()...'
call g2_gbytesc(out4, in4, iskip, 8, 0, 4)
do i = 1, 4
if (i < 4) then
if (in4(i) .ne. 0) stop 61
else
if (in4(i) .ne. 1) stop 62
endif
end do

print *, ' now unpack into 1 int with g2_gbytesc()...'
call g2_gbytesc(out4, in1, iskip, 32, 0, 1)
if (in1(1) .ne. 1) stop 70

! For this test to pass the -fallow-argument-mismatch flag must be
! used, because I am passing in a real array instead of an int array
! for the in parameter. This is how g2_sbytesc() is called in
! addfield.F90.
print *, 'Testing g2_sbytesc() with a real array (size 1) instead of an int array...'
print *, ' testing g2_sbytesc() with a real array (size 1) instead of an int array...'
iskip = 0
nbits = 32
nskip = 0
Expand All @@ -122,14 +144,13 @@ program test_gbytec
call g2_sbytesc(out4, r_in, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 50
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. &
ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 80
! print '(z2.2)', out4(1)
! print '(z2.2)', out4(2)
! print '(z2.2)', out4(3)
! print '(z2.2)', out4(4)

! This test is the same as above, but does not require the -fallow-argument-mismatch flag.
print *, 'Testing g2_sbytesc() with a real array (size 1) instead of an int array, using transfer() intrinsic...'
! This test is the same as above, but does not require the
! -fallow-argument-mismatch flag.
print *, ' testing g2_sbytesc() with a size 1 real array instead of an int array, using transfer()...'
iskip = 0
nbits = 32
nskip = 0
Expand All @@ -139,14 +160,14 @@ program test_gbytec
call g2_sbytesc(out4, in, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 50
! print '(z2.2)', out4(1)
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. &
ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 90

! For this test to pass the -fallow-argument-mismatch flag must be
! used, because I am passing in a real array instead of an int array
! for the in parameter. This is how g2_sbytesc() is called in
! addfield.F90.
print *, 'Testing g2_sbytesc() with a real array instead of an int array...'
print *, ' testing g2_sbytesc() with a real array instead of an int array...'
iskip = 0
nbits = 32
nskip = 0
Expand All @@ -156,12 +177,14 @@ program test_gbytec
call g2_sbytesc(out8, r_in2, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (ichar(out8(1)) .ne. 63 .and. ichar(out8(2)) .ne. 128 .and. ichar(out8(3)) .ne. 0 .and. ichar(out8(4)) .ne. 0) stop 50
if (ichar(out8(5)) .ne. 63 .and. ichar(out8(6)) .ne. 128 .and. ichar(out8(7)) .ne. 0 .and. ichar(out8(8)) .ne. 0) stop 50
if (ichar(out8(1)) .ne. 63 .and. ichar(out8(2)) .ne. 128 .and. &
ichar(out8(3)) .ne. 0 .and. ichar(out8(4)) .ne. 0) stop 100
if (ichar(out8(5)) .ne. 63 .and. ichar(out8(6)) .ne. 128 .and. &
ichar(out8(7)) .ne. 0 .and. ichar(out8(8)) .ne. 0) stop 110
! print '(z2.2)', out8(1)

! This test is the same as above, but does not require the -fallow-argument-mismatch flag.
print *, 'Testing g2_sbytesc() with a real array instead of an int array, using transfer() intrinsic...'
print *, ' testing g2_sbytesc() with a real array instead of an int array, using transfer() intrinsic...'
iskip = 0
nbits = 32
nskip = 0
Expand All @@ -172,10 +195,73 @@ program test_gbytec
call g2_sbytesc(out8, in2, iskip, nbits, nskip, num)
! Note that the 32-bit IEEE representation of 1.0 is 3f800000. The
! decimal for 3f is 63, the decimal for 80 is 128.
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 50
if (ichar(out8(5)) .ne. 63 .and. ichar(out8(6)) .ne. 128 .and. ichar(out8(7)) .ne. 0 .and. ichar(out8(8)) .ne. 0) stop 50
if (ichar(out4(1)) .ne. 63 .and. ichar(out4(2)) .ne. 128 .and. &
ichar(out4(3)) .ne. 0 .and. ichar(out4(4)) .ne. 0) stop 120
if (ichar(out8(5)) .ne. 63 .and. ichar(out8(6)) .ne. 128 .and. &
ichar(out8(7)) .ne. 0 .and. ichar(out8(8)) .ne. 0) stop 130
! print '(z2.2)', out4(1)

print *, ' testing g2_sbytec() with 64-bit int...'
in8(1) = 1
do i = 1, 8
out8(i) = char(0)
end do
call g2_sbytec(out8, in8, iskip, 64)
do i = 1, 8
!print '(z2.2)', out8(i)
if (i .lt. 8) then
if (ichar(out8(i)) .ne. 0) stop 140
else
if (ichar(out8(i)) .ne. 1) stop 140
endif
end do

print *, ' now unpack into 1 64-bit int with g2_gbytesc()...'
in8_1(1) = 0
call g2_gbytesc(out8, in8_1, iskip, 64, 0, 1)
if (in8_1(1) .ne. in8(1)) stop 150

print *, ' testing g2_sbytec() with 32-bit int array of size 4...'
do i = 1, 4
in44(i) = 1
end do
do i = 1, 16
out16(i) = char(0)
end do
call g2_sbytesc(out16, in44, iskip, 32, 0, 4)
do i = 1, 16
print '(i3, x, z2.2)', i, out16(i)
end do

print *, ' now unpack into 4 32-bit ints with g2_gbytesc()...'
do i = 1, 4
in44_1(i) = 0
end do
call g2_gbytesc(out16, in44_1, iskip, 32, 0, 4)
do i = 1, 4
if (in44_1(i) .ne. in44(i)) stop 160
end do

print *, ' testing g2_sbytec() with 64-bit int array of size 4...'
do i = 1, 4
in84(i) = 1
end do
do i = 1, 32
out32(i) = char(0)
end do
print *, in84
call g2_sbytesc(out32, in84, iskip, 64, 0, 4)
do i = 1, 32
print '(i3, x, z2.2)', i, out32(i)
end do

print *, ' now unpack into 4 64-bit ints with g2_gbytesc()...'
do i = 1, 4
in84_1(i) = 0
end do
call g2_gbytesc(out32, in84_1, iskip, 64, 0, 4)
print *, in84_1

print *, 'SUCCESS!'

end program test_gbytec

0 comments on commit 1d230e5

Please sign in to comment.