Skip to content

Commit

Permalink
Add finaliser for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
krystophny committed Nov 23, 2024
1 parent c4321d7 commit 2204311
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
17 changes: 11 additions & 6 deletions examples/issue41_abstract_classes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ FC = gfortran
FCFLAGS = -fPIC
PYTHON = python

%.o : %.f90
${FC} ${FCFLAGS} -c $< -o $@
all: wrapper test_abstract_classes.x

test: wrapper
$(PYTHON) run.py

all: myclass_base.o myclass_impl.o myclass_factory.o
test_abstract_classes.x: main.f90 myclass_base.o myclass_impl.o myclass_factory.o
$(FC) $(FCFLAGS) -o $@ $^

wrapper: myclass_base.o myclass_impl.o myclass_factory.o
f90wrap -m itest -P myclass_impl.f90 myclass_factory.f90 -v
f2py-f90wrap --build-dir . -c -m _itest \
f90wrap_myclass_impl.f90 f90wrap_myclass_factory.f90 \
myclass_base.o myclass_impl.o myclass_factory.o

test: all
$(PYTHON) run.py
%.o : %.f90
$(FC) $(FCFLAGS) -c $< -o $@

clean:
rm -f *.o f90wrap*.f90 *.so *.mod
rm -f *.o f90wrap*.f90 *.so *.mod *.x
rm -rf src.*/
rm -rf itest/
-rm -rf src.*/ .f2py_f2cmap .libs/ __pycache__/
24 changes: 24 additions & 0 deletions examples/issue41_abstract_classes/main.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
program main
use myclass_factory, only: create_myclass
use myclass_impl, only: myclass_impl_t
implicit none

print *, "Start"

call test()

print *, "Done"

contains

subroutine test
real :: x
class(myclass_impl_t), allocatable :: myobject

myobject = create_myclass("impl")
call myobject%get_value(x)

print *, "Value: ", x
end subroutine test

end program main
7 changes: 7 additions & 0 deletions examples/issue41_abstract_classes/myclass_impl.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module myclass_impl
type, extends(myclass_t) :: myclass_impl_t
contains
procedure :: get_value => get_value_impl
final :: myclass_impl_finalise
end type myclass_impl_t

contains
Expand All @@ -17,4 +18,10 @@ subroutine get_value_impl(self, value)
value = 1.0
end subroutine get_value_impl

subroutine myclass_impl_finalise(self)
type(myclass_impl_t), intent(inout) :: self

print *, "Finalizing myclass_impl_t"
end subroutine myclass_impl_finalise

end module myclass_impl
4 changes: 3 additions & 1 deletion examples/issue41_abstract_classes/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
TOL = 1.0e-14

obj = itest.myclass_factory.create_myclass("impl")
assert(abs(obj.get_value()-REF)<TOL)
output = obj.get_value()
assert(abs(output-REF)<TOL)
print(f"OK: {output} == {REF}")

0 comments on commit 2204311

Please sign in to comment.