Fruit could be use along with Ruby pre-processor as part of the simple standalone FORTRAN approach. The advantage of using the pre-processor is it removes the need to maintain the basket and driver files and keep them up to date.
IMPORTANT: Notice all file code blocks have first line commented with their location. This will help you understanding where should they be placed.
Example project structure
. ├── mod ├── src │ └── <your_module>.f90 ├── test │ ├── <your_module>_test.f90 │ ├── fruit_generator.rb | ├── fruit.f90 | └── Makefile └── rake_base.rb
- fruit.f90 - fruit source code
- mod - directory used to store .mod files
- <your_module>.f90 - your actual module
- <your_module>_test.f90 - file with unit tests for your module
- fruit_geneator.rb - script used to generate test driver and basket files
- Makefile - used to compile and run all tests
Prepare environment
You will need to load module 'ruby' to use auto-generating scripts.
module load ruby module load itm-gcc/7.3.0 #gcc is needed to build ruby libraries
Install requirements
FRUIT source code
cd <project_dir>/test wget https://raw.githubusercontent.com/mortele/FRUIT/master/src/fruit.f90
IMPORTANT
While compiling fruit.f90 an error "Logicals must be compared with .eqv. instead of .eq." may occure. In this case you will need to modify fruit.f90 lines 912 and 913 editing ".eq." to ".eqv.". This step is needed to be done only once, during first run.
You can try to compile fruit.f90 at this step by calling gfortran compiler:
gfortran fruit.f90 -o fruit
Rake
gem install rake --user-install
You will need to add gem to PATH in order to run gem packages.
cd ~ nano .bashrc #add line at the end of file export PATH="~/.gem/ruby/3.1.0/bin:$PATH" #ctrl + O to save file #ctrl + X to exit nano source ~/.bashrc
Bundler
gem install bundler --user-install gem install json --user-install
Fruit_processor_gem
Download fruit_processor_gem
You will need to download and install ruby gem. It is used to auto-detect test files and generate test drivers.
You will install it only once and it will apply to any project where you use it.
cd <somewhere outside project> git clone --recursive https://github.com/mortele/FRUIT
Install fruit_processor_gemcd fruit_pr
cd .FRUIT/fruit_processor_gem rake install
rake_base.rb
You will need to download rake_base.rb file from FRUIT repository in order to run fruit_generator script. You will never modify it's content.
cd <project_dir> wget https://raw.githubusercontent.com/mortele/FRUIT/master/rake_base.rb
Create project files
calculator_test.f90
This file contains your tests, setup and teardown subroutines.
!file <project_directory>/test/calculator_test.f90 module calculator_test use fruit contains subroutine setup print *, "setup subroutine ran" end subroutine setup subroutine teardown print *, "teardown subroutine ran" end subroutine teardown subroutine test_calculator use calculator integer :: result !test add subroutine call add (2,2,result) call assert_equals(4,result) end subroutine test_calculator end module calculator_test
NOTE: in order to autogenerate test_driver and test_basket files, you need to follow naming convention:
- files containing tests must be named: *_test.f90
- test module must be named: *_test
- test subroutines must be named: test_*
calculator.f90
This is your module.
!file <project_directory>/src/calculator.f90 module calculator implicit none contains subroutine add(a,b,output) integer, intent (in) :: a,b integer, intent (out) :: output output=a+b end subroutine add end module calculator
Create driving files
fruit_generator.rb
This is script needed to run FRUIT processor. You create it once and never modify in basic usage scenario.
#file <project_dir>/test/fruit_generator.rb require 'rubygems' require 'fruit_processor' load "../rake_base.rb" $build_dir = "" $goal = "fruit_driver_dummy" fp = FruitProcessor.new fp.pre_process
Makefile
This is file with build instruction. You will not have nanoto modify it unless you stick to project structure. Just copy it's content and save as Makefile in <project_dir>/test directory.
#file <project_dir>/test/Makefile compiler=gfortran option=-Wall -Wtabs -Wextra -Wno-tabs -pedantic -fbounds-check -Wuninitialized -O -g \ -Wno-unused-parameter -cpp fruit_code = ./fruit.f90 code = ../src/*.f90 ./*_test.f90 code_gen = fruit_basket_gen.f90 fruit_driver_gen.f90 all_code = $(fruit_code) $(code) $(code_gen) driver = fruit_driver all : build run build : $(driver) fruit_basket_gen.f90 : $(code) ruby fruit_generator.rb fruit_driver_gen.f90 : $(code) ruby fruit_generator.rb fruit_driver : $(all_code) $(compiler) $(option) $(all_code) -J ../mod -o fruit_driver clean : rm -f *.o *.mod *.obj fruit_driver rm -f fruit_driver_gen.f90 rm -f fruit_basket_gen.f90 rm -f result.xml result_tmp.xml run : $(driver) ./$(driver) .PHONY : all clean run
Run tests
Run commands listed below.
cd <project_dir>/test make
NOTE: While compiling fruit.f90 an error "Logicals must be compared with .eqv. instead of .eq." may occure. In this case you will need to modify fruit.f90 lines 912 and 913 editing ".eq." to ".eqv.". This step is needed to be done only once, during first run.
Example output
The scientific work is published for the realization of the international project co-financed by Polish Ministry of Science and Higher Education in 2019 from financial resources of the program entitled "PMW"; Agreement No. 5040/H2020/Euratom/2019/2
This work has been carried out within the framework of the EUROfusion Consortium and has received funding from the Euratom research and training programme 2014–2020 under grant agreement No 633053. The views and opinions expressed herein do not necessarily reflect those of the European Commission or ITER