Merge pull request #237 from pclausen/fortran-kata

First commit of Fortran version of GildedRose
This commit is contained in:
Emily Bache 2021-05-31 15:10:26 +02:00 committed by GitHub
commit 775d8758c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 307 additions and 0 deletions

33
fortran/CMakeLists.txt Normal file
View File

@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.0.0)
project(GildedRose Fortran)
include(CTest)
set(EXERCISM_RUN_ALL_TESTS 1)
# Activate Fortran compiler warnings
if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel") # Intel fortran
if(WIN32)
set (CMAKE_Fortran_FLAGS "/warn:all")
else()
set (CMAKE_Fortran_FLAGS "-warn all")
endif()
endif()
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU" ) # GFortran
# set (CMAKE_Fortran_FLAGS "-std=f2008 -W -Wall -Wextra -pedantic -fbacktrace -Wdo-subscript")
set (CMAKE_Fortran_FLAGS "-std=f2008 -W -Wall -Wextra -pedantic -fbacktrace ")
set (CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS} -o0 -ffpe-trap=zero,invalid,overflow,underflow")
endif()
add_executable(GildedRose_text_test
src/GildedRose.f90
test/GildedRose_text_test.f90
)
add_executable(GildedRose_unity_test
src/GildedRose.f90
test/GildedRose_unity_test.f90
)
add_test(GildedRose_text_test GildedRose_text_test)
add_test(GildedRose_unity_test GildedRose_unity_test)

57
fortran/README.md Normal file
View File

@ -0,0 +1,57 @@
# Fortran version of Gilded Rose refactoring kata
## Introduction
The Fortran90 version of the Gilded Rose refactoring kata.
## Prerequisites
* CMake version >= 3.13
* Fortran compiler
* Tested with:
* gfortran
* Intel Fortran
## How to build and run tests in a terminal
### Build tests
$ cd ${GIT_FOLDER}/GildedRose-Refactoring-Kata/fortran
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .
### Show available tests
$ cd ${GIT_FOLDER}/GildedRose-Refactoring-Kata/fortran/build
$ ctest -N
Test project ${GIT_FOLDER}/GildedRose-Refactoring-Kata/fortran/build
Test #1: GildedRose_text_test
Test #2: GildedRose_unity_test
Total Tests: 2
### Run all tests
$ ctest
### Run all tests with verbose output
$ ctest -VV
## How to build and run tests using the [CLion IDE](https://www.jetbrains.com/clion/)
1. Start CLion
2. Select menu `File - Open...`
3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/fortran`
4. Select menu `Build - Build Project`
4. Select menu `Run - Run...`
## How to build and run tests using Visual Studio 2019
1. Start Visual Studio 2019
2. Select `Open a local folder`
3. Select folder `${GIT_FOLDER}/GildedRose-Refactoring-Kata/fortran`
4. Wait for message `CMake generation finished.` in the CMake output window at the bottom
5. Select ALL_BUILD and build to build the source code
6. Select RUN_TEST and build which will execute the tests

144
fortran/src/GildedRose.f90 Normal file
View File

@ -0,0 +1,144 @@
module GildedRose
implicit none
private
save
public init_item
public print_item
public update_quality
public int2str
integer, parameter, public :: MAX_STR_LEN = 80
type, public :: Item
character(len=MAX_STR_LEN) :: name
integer :: sellIn
integer :: quality
end type
contains
subroutine init_item(it, name, sellIn, quality)
type(Item) :: it
character(len=*) :: name
integer :: sellIn
integer :: quality
it%name = trim(name)
it%sellIn = sellIn
it%quality = quality
end
function int2str( int_val )
implicit none
integer :: int_val
character( int(log10(real(max(abs(int_val),1)))) + 1 + &
(1-sign(1,int_val))/2 ) :: int2str
character(64) :: all_chars
write(all_chars,*) int_val
int2str = trim( adjustl( all_chars ) )
end function
subroutine print_item(it)
type(Item) :: it
write(*,*) trim(it%name), ', ', int2str(it%sellIn), ', ', int2str(it%quality)
end subroutine
subroutine update_quality(items, size)
type(Item), dimension(:) :: items
integer :: size
integer :: i
do i = 1, size
if (items(i)%name /= "Aged Brie" .and. items(i)%name /= "Backstage passes to a TAFKAL80ETC concert" ) then
if (items(i)%quality > 0) then
if (items(i)%name /= "Sulfuras, Hand of Ragnaros" ) then
items(i)%quality = items(i)%quality - 1
endif
endif
else
if (items(i)%quality < 50) then
items(i)%quality = items(i)%quality + 1
if ( items(i)%name =="Backstage passes to a TAFKAL80ETC concert") then
if (items(i)%sellIn < 11) then
if (items(i)%quality < 50) then
items(i)%quality = items(i)%quality + 1
endif
endif
if (items(i)%sellIn < 6) then
if (items(i)%quality < 50) then
items(i)%quality = items(i)%quality + 1
endif
endif
endif
endif
endif
if (items(i)%name /= "Sulfuras, Hand of Ragnaros") then
items(i)%sellIn = items(i)%sellIn - 1
endif
if (items(i)%sellIn < 0) then
if (items(i)%name /= "Aged Brie" ) then
if (items(i)%name /= "Backstage passes to a TAFKAL80ETC concert" ) then
if (items(i)%quality > 0 ) then
if (items(i)%name /= "Sulfuras, Hand of Ragnaros" )then
items(i)%quality = items(i)%quality - 1
endif
endif
else
items(i)%quality = items(i)%quality - items(i)%quality
end if
else
if (items(i)%quality < 50) then
items(i)%quality = items(i)%quality + 1
end if
end if
end if
end do
end subroutine
end module

View File

@ -0,0 +1,52 @@
program GildedRose_text_test
use GildedRose
implicit none
type(Item) :: items(9)
integer :: last
integer :: day
integer :: index
last = 1
call init_item(items(last), "+5 Dexterity Vest", 10, 20)
last=last+1
call init_item(items(last), "Aged Brie", 2, 0)
last=last+1
call init_item(items(last), "Elixir of the Mongoose", 5, 7)
last=last+1
call init_item(items(last), "Sulfuras, Hand of Ragnaros", 0, 80)
last=last+1
call init_item(items(last), "Sulfuras, Hand of Ragnaros", -1, 80)
last=last+1
call init_item(items(last), "Backstage passes to a TAFKAL80ETC concert", 15, 20)
last=last+1
call init_item(items(last), "Backstage passes to a TAFKAL80ETC concert", 10, 49)
last=last+1
call init_item(items(last), "Backstage passes to a TAFKAL80ETC concert", 5, 49)
! this Conjured item doesn't yet work properly
last=last+1
call init_item(items(last), "Conjured Mana Cake", 3, 6)
write(*,*) "OMGHAI!"
do day = 1, 2
write(*,*) "-------- day "//int2str(day)//" --------"
write(*,*) "name, sellIn, quality"
do index = 1, last
call print_item(items(index))
end do
write(*,*) " "
call update_quality(items, last)
enddo
end program

View File

@ -0,0 +1,21 @@
program GildedRose_unity_test
use GildedRose
implicit none
type(Item):: it(1)
call init_item(it(1), "foo", 10, 20)
call update_quality(it, 1)
if (trim(it(1)%name) /= "fixMe" ) then
write(*,*) "ERRORUnity test Failed"
stop 1
else
write(*,*) "Unity test OK"
endif
end program