Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cut down unit test code using destructor #298

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 24 additions & 66 deletions test/unit/test_tensor_constructors.pf
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,15 @@ contains
! Check that the tensor values are all zero
expected(:,:) = 0.0
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_zeros")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_zeros subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_tensor_zeros

! Unit test for the torch_tensor_ones subroutine
Expand Down Expand Up @@ -187,22 +180,15 @@ contains
! Check that the tensor values are all one
expected(:,:) = 1.0
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_ones")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_ones subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_tensor_ones

! Unit test for the torch_tensor_from_array subroutine in the 1D case
Expand Down Expand Up @@ -244,22 +230,15 @@ contains
! Compare the data in the tensor to the input data
expected(:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_1d

! Unit test for the torch_tensor_from_array subroutine in the 2D case
Expand Down Expand Up @@ -301,22 +280,15 @@ contains
! Compare the data in the tensor to the input data
expected(:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_2d

! Unit test for the torch_tensor_from_array subroutine in the 3D case
Expand Down Expand Up @@ -357,22 +329,15 @@ contains
! Compare the data in the tensor to the input data
expected(:,:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_3d

! Unit test for the torch_tensor_from_blob subroutine
Expand Down Expand Up @@ -417,22 +382,15 @@ contains
! Compare the data in the tensor to the input data
expected(:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_blob")

! Nullify pointers used in the unit test
nullify(out_data)

if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_blob

end module test_tensor_constructors
81 changes: 16 additions & 65 deletions test/unit/test_tensor_interrogation.pf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
! file for details.
module test_tensor_interrogation
use funit
use ftorch, only: torch_kFloat32, torch_kCPU, torch_tensor, torch_tensor_delete, &
torch_tensor_empty
use ftorch, only: torch_kFloat32, torch_kCPU, torch_tensor, torch_tensor_empty
use iso_c_binding, only: c_int64_t

implicit none
Expand All @@ -33,12 +32,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_rank can correctly
! identify its rank
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (ndims /= tensor%get_rank()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_rank() returned incorrect rank for 1D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(ndims, tensor%get_rank())

end subroutine test_torch_tensor_get_rank_1D

Expand All @@ -56,12 +50,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_rank can correctly
! identify its rank
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (ndims /= tensor%get_rank()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_rank() returned incorrect rank for 2D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(ndims, tensor%get_rank())

end subroutine test_torch_tensor_get_rank_2D

Expand All @@ -79,12 +68,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_rank can correctly
! identify its rank
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (ndims /= tensor%get_rank()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_rank() returned incorrect rank for 3D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(ndims, tensor%get_rank())

end subroutine test_torch_tensor_get_rank_3D

Expand All @@ -102,12 +86,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_shape can correctly
! identify its shape
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (.not. all(tensor_shape == tensor%get_shape())) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_shape() returned incorrect shape for 1D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertTrue(all(tensor_shape == tensor%get_shape()))

end subroutine test_torch_tensor_get_shape_1D

Expand All @@ -125,12 +104,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_shape can correctly
! identify its shape
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (.not. all(tensor_shape == tensor%get_shape())) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_shape() returned incorrect shape for 2D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertTrue(all(tensor_shape == tensor%get_shape()))

end subroutine test_torch_tensor_get_shape_2D

Expand All @@ -148,12 +122,7 @@ contains
! Create a tensor with uninitialised values and check torch_tensor_get_shape can correctly
! identify its shape
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)
if (.not. all(tensor_shape == tensor%get_shape())) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_shape() returned incorrect shape for 3D tensor"
stop 999
end if
call torch_tensor_delete(tensor)
@assertTrue(all(tensor_shape == tensor%get_shape()))

end subroutine test_torch_tensor_get_shape_3D

Expand All @@ -169,16 +138,10 @@ contains
integer, parameter :: dtype = torch_kFloat32
integer, parameter :: expected = torch_kFloat32

! Create an empty tensor for 32-bit floats
! Create an empty tensor for 32-bit floats and check that torch_tensor_get_dtype can get the
! device type
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)

! Check that torch_tensor_get_dtype can get the device type
if (expected /= tensor%get_dtype()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_dtype() returned incorrect dtype"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(expected, tensor%get_dtype())

end subroutine test_torch_tensor_get_dtype

Expand All @@ -194,16 +157,10 @@ contains
integer, parameter :: dtype = torch_kFloat32
integer, parameter :: expected = torch_kCPU

! Create an empty tensor on the CPU with the default device type
! Create an empty tensor on the CPU with the default device type and check that
! torch_tensor_get_device_type can get the device type
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)

! Check that torch_tensor_get_device_type can get the device type
if (expected /= tensor%get_device_type()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_device_type() returned incorrect device type"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(expected, tensor%get_device_type())

end subroutine test_torch_tensor_get_device_type

Expand All @@ -219,16 +176,10 @@ contains
integer, parameter :: dtype = torch_kFloat32
integer, parameter :: expected = -1

! Create an empty tensor on the CPU with the default device index
! Create an empty tensor on the CPU with the default device index and check that
! torch_tensor_get_device_index can get the device index
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)

! Check that torch_tensor_get_device_index can get the device index
if (expected /= tensor%get_device_index()) then
call torch_tensor_delete(tensor)
print *, "Error :: tensor%get_device_index() returned incorrect CPU device index"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(expected, tensor%get_device_index())

end subroutine test_torch_tensor_get_device_index_default

Expand Down
27 changes: 7 additions & 20 deletions test/unit/test_tensor_interrogation_cuda.pf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
! file for details.
module test_tensor_interrogation_cuda
use funit
use ftorch, only: torch_kFloat32, torch_kCUDA, torch_tensor, torch_tensor_delete, &
torch_tensor_empty
use ftorch, only: torch_kFloat32, torch_kCUDA, torch_tensor, torch_tensor_empty
use iso_c_binding, only: c_int64_t

implicit none
Expand All @@ -31,16 +30,10 @@ contains
integer, parameter :: dtype = torch_kFloat32
integer, parameter :: expected = torch_kCUDA

! Create an empty tensor on the CUDAD device
! Create an empty tensor on the CUDA device and check that torch_tensor_get_device_type can get
! the device type
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)

! Check that torch_tensor_get_device_type can get the device type
if (expected /= tensor%get_device_type()) then
call torch_tensor_delete(tensor)
print *, "Error :: torch_tensor_get_device_type returned incorrect device type"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(expected, tensor%get_device_type())

end subroutine test_torch_tensor_get_device_type

Expand All @@ -57,16 +50,10 @@ contains
integer, parameter :: dtype = torch_kFloat32
integer, parameter :: expected = 0

! Create an empty tensor on the CUDA device with the default device index
! Create an empty tensor on the CUDA device with the default device index and check that
! torch_tensor_get_device_index can get the device index
call torch_tensor_empty(tensor, ndims, tensor_shape, dtype, device_type)

! Check that torch_tensor_get_device_index can get the device index
if (expected /= tensor%get_device_index()) then
call torch_tensor_delete(tensor)
print *, "Error :: torch_tensor_get_device_index returned incorrect CUDA device index"
stop 999
end if
call torch_tensor_delete(tensor)
@assertEqual(expected, tensor%get_device_index())

end subroutine test_torch_tensor_get_device_index_default

Expand Down
Loading