Skip to content

Commit

Permalink
Added "basic" unit tests in Fortran.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 21, 2017
1 parent 758bdd1 commit e8afba7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
13 changes: 10 additions & 3 deletions tests/fortran/Makefile
Expand Up @@ -21,29 +21,36 @@ F90_SOURCES := \
$(SRC_DIR)/curve$(F90) \
$(SRC_DIR)/surface$(F90) \
$(SRC_DIR)/curve_intersection$(F90)
TEST_SOURCES = $(wildcard $(CURR_DIR)/test_*$(F90))
F77_OBJS := $(patsubst $(QUADPACK_DIR)/%$(F77), $(QUADPACK_DIR)/%$(OBJ), $(F77_SOURCES))
F90_OBJS := $(patsubst $(SRC_DIR)/%$(F90), $(SRC_DIR)/%$(OBJ), $(F90_SOURCES))
TEST_OBJS := $(patsubst $(CURR_DIR)/%$(F90), $(CURR_DIR)/%$(OBJ), $(TEST_SOURCES))

test: $(CURR_DIR)/test-bin
$(CURR_DIR)/test-bin

$(QUADPACK_DIR)/%$(OBJ): $(QUADPACK_DIR)/%$(F77)
$(FC) $(FCFLAGS) -c $< -o $@

$(SRC_DIR)/%$(OBJ): $(SRC_DIR)/%$(F90)
$(FC) $(FCFLAGS) -c $< -o $@

$(QUADPACK_DIR)/%$(OBJ): $(QUADPACK_DIR)/%$(F77)
$(CURR_DIR)/%$(OBJ): $(CURR_DIR)/%$(F90)
$(FC) $(FCFLAGS) -c $< -o $@

$(CURR_DIR)/test-bin: $(F77_OBJS) $(F90_OBJS) $(CURR_DIR)/test.f90
$(CURR_DIR)/test-bin: $(F77_OBJS) $(F90_OBJS) $(TEST_OBJS) $(CURR_DIR)/test.f90
$(FC) $(FCFLAGS) \
-o $(CURR_DIR)/test-bin \
$(F77_OBJS) \
$(F90_OBJS) \
$(TEST_OBJS) \
$(CURR_DIR)/test.f90

clean:
rm -f $(CURR_DIR)/*.mod
rm -f $(SRC_DIR)/*$(OBJ)
rm -f $(QUADPACK_DIR)/*$(OBJ)
rm -f $(SRC_DIR)/*$(OBJ)
rm -f $(CURR_DIR)/*$(OBJ)
rm -f $(CURR_DIR)/test-bin

.PHONY: all test clean
23 changes: 21 additions & 2 deletions tests/fortran/test.f90
@@ -1,8 +1,27 @@
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! https://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.

program test

use types, only: dp
use iso_c_binding, only: c_bool
use test_helpers, only: test_vector_close
implicit none

print *, dp
logical(c_bool) :: success

success = .TRUE.
call test_vector_close(success)
if (.NOT. success) then
call exit(1)
end if

end program test
60 changes: 60 additions & 0 deletions tests/fortran/test_helpers.f90
@@ -0,0 +1,60 @@
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
!
! https://www.apache.org/licenses/LICENSE-2.0
!
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.

module test_helpers

use iso_c_binding, only: c_double, c_bool
use helpers, only: vector_close
use types, only: dp
implicit none
private
public test_vector_close

contains

subroutine test_vector_close(success)
logical(c_bool), intent(inout) :: success
! Variables outside of signature.
logical(c_bool) :: is_close
real(c_double) :: eps
real(c_double) :: vec1(1, 2)
real(c_double) :: vec2(1, 2)

eps = 0.5_dp**40

! CASE 1: Identical vector.
vec1(1, 1) = 0.5_dp
vec1(1, 2) = 4.0_dp
is_close = vector_close(2, vec1, vec1, eps)
if (is_close) then
write (*, "(A)") "vector_close: Case 1 success"
else
write (*, "(A)") "vector_close: Case 1 failure"
success = .FALSE.
end if

! CASE 2: Far apart vectors.
vec1(1, 1) = 0.0_dp
vec1(1, 2) = 6.0_dp
vec2(1, 1) = 1.0_dp
vec2(1, 2) = -4.0_dp
is_close = vector_close(2, vec1, vec2, eps)
if (.NOT. is_close) then
write (*, "(A)") "vector_close: Case 2 success"
else
write (*, "(A)") "vector_close: Case 2 failure"
success = .FALSE.
end if

end subroutine test_vector_close

end module test_helpers

0 comments on commit e8afba7

Please sign in to comment.