Skip to content

Commit a7b3f63

Browse files
authored
Exercise bob (#40)
* Exercise bob with ctest Exercise bob added with cmake and ctest * Exercise hello world with ctest Exercise hello world added with cmake and ctest * Exercise bob with ctest Exercise bob added with cmake and ctest
1 parent cff1dc3 commit a7b3f63

File tree

4 files changed

+153
-24
lines changed

4 files changed

+153
-24
lines changed

config/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ else()
2525
endif()
2626

2727
# Build executable from sources and headers
28-
add_executable(${exercise} ${file}_test.f90 ${exercise_f90} )
28+
add_executable(${exercise} ${exercise_f90} ${file}_test.f90 )
2929

3030
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
3131
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *)

exercises/bob/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Get the exercise name from the current directory
2+
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)
3+
4+
# Basic CMake project
5+
cmake_minimum_required(VERSION 2.8.11)
6+
7+
# Name the project after the exercise
8+
project(${exercise} Fortran)
9+
10+
# set debug build default
11+
set(CMAKE_BIULD_TYPE Debug)
12+
13+
# Configure to run all the tests?
14+
if(${EXERCISM_RUN_ALL_TESTS})
15+
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
16+
endif()
17+
18+
# Get a source filename from the exercise name by replacing -'s with _'s
19+
string(REPLACE "-" "_" file ${exercise})
20+
21+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.f90)
22+
set(exercise_f90 ${file}.f90)
23+
else()
24+
set(exercise_f90 "")
25+
endif()
26+
27+
# Build executable from sources and headers
28+
add_executable(${exercise} ${exercise_f90} ${file}_test.f90 )
29+
30+
if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
31+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *)
32+
endif(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
33+
34+
add_subdirectory(testlib)
35+
include_directories(testlib ${CMAKE_CURRENT_BINARY_DIR}/testlib)
36+
37+
target_link_libraries(${exercise} TesterMain)
38+
39+
include(CTest)
40+
41+
add_test (${exercise}_test ${exercise} )
42+
43+
# Run the tests on every build
44+
#add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
45+

exercises/bob/bob_test.f90

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
! This test was created from ..\..\exercism\problem-specifications\exercises\bob\canonical-data.json
3+
! version: 1.4.0.
4+
!
5+
! This is the main test program to test your implementaion which is done in
6+
! bob.f90
7+
!
8+
! If the file does not exist create it.
9+
!
10+
! To build your program first create a build directory, eg. Debug and change into it
11+
! > mkdir Debug
12+
! > cd Debug
13+
! Now, to configure the build you run
14+
! > cmake ..
15+
! Which creates the build files (Makefiles etc.) needed to build and test your program.
16+
! Now build your program with
17+
! > make
18+
! If your build is successfull run the test
19+
! > ctest -V
20+
! If the build fails or the tests, modify the implementation in hello_world.f90 and rerun
21+
! the "make" and the "ctest -V" commands.
22+
!
23+
! Now proceed to the test section and one test after the other, ensure your implementation
24+
! passes this test.
25+
26+
27+
program bob_test_main
28+
use TesterMain
29+
use bob
30+
implicit none
31+
32+
33+
! Test 1: stating something
34+
call assert_equal("Whatever.", hey("Tom-ay-to, tom-aaaah-to."), "stating something")
35+
! Test 2: shouting
36+
call assert_equal("Whoa, chill out!", hey("WATCH OUT!"), "shouting")
37+
! Test 3: shouting gibberish
38+
call assert_equal("Whoa, chill out!", hey("FCECDFCAAB"), "shouting gibberish")
39+
! Test 4: asking a question
40+
call assert_equal("Sure.", hey("Does this cryogenic chamber make me look fat?"), "asking a question")
41+
! Test 5: asking a numeric question
42+
call assert_equal("Sure.", hey("You are, what, like 15?"), "asking a numeric question")
43+
! Test 6: asking gibberish
44+
call assert_equal("Sure.", hey("fffbbcbeab?"), "asking gibberish")
45+
! Test 7: talking forcefully
46+
call assert_equal("Whatever.", hey("Let's go make out behind the gym!"), "talking forcefully")
47+
! Test 8: using acronyms in regular speech
48+
call assert_equal("Whatever.", hey("It's OK if you don't want to go to the DMV."), "using acronyms in regular speech")
49+
! Test 9: forceful question
50+
call assert_equal("Calm down, I know what I'm doing!", hey("WHAT THE HELL WERE YOU THINKING?"), "forceful question")
51+
! Test 10: shouting numbers
52+
call assert_equal("Whoa, chill out!", hey("1, 2, 3 GO!"), "shouting numbers")
53+
! Test 11: no letters
54+
call assert_equal("Whatever.", hey("1, 2, 3"), "no letters")
55+
! Test 12: question with no letters
56+
call assert_equal("Sure.", hey("4?"), "question with no letters")
57+
! Test 13: shouting with special characters
58+
call assert_equal("Whoa, chill out!", hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"), "shouting with special characters")
59+
! Test 14: shouting with no exclamation mark
60+
call assert_equal("Whoa, chill out!", hey("I HATE THE DMV"), "shouting with no exclamation mark")
61+
! Test 15: statement containing question mark
62+
call assert_equal("Whatever.", hey("Ending with ? means a question."), "statement containing question mark")
63+
! Test 16: non-letters with question
64+
call assert_equal("Sure.", hey(":) ?"), "non-letters with question")
65+
! Test 17: prattling on
66+
call assert_equal("Sure.", hey("Wait! Hang on. Are you going to be OK?"), "prattling on")
67+
! Test 18: silence
68+
call assert_equal("Fine. Be that way!", hey(""), "silence")
69+
! Test 19: prolonged silence
70+
call assert_equal("Fine. Be that way!", hey(" "), "prolonged silence")
71+
! Test 20: alternate silence
72+
!call assert_equal("Fine. Be that way!", hey(" "), "alternate silence")
73+
! Test 21: multiple line question
74+
call assert_equal("Whatever.", hey(""// &
75+
& "Does this cryogenic chamber make me look fat?"// &
76+
& "No."), "multiple line question")
77+
! Test 22: starting with whitespace
78+
call assert_equal("Whatever.", hey(" hmmmmmmm..."), "starting with whitespace")
79+
! Test 23: ending with whitespace
80+
call assert_equal("Sure.", hey("Okay if like my spacebar quite a bit? "), "ending with whitespace")
81+
! Test 24: other whitespace
82+
!call assert_equal("Fine. Be that way!", hey(""// &
83+
! & "
84+
! "), "other whitespace")
85+
! Test 25: non-question ending with whitespace
86+
call assert_equal("Whatever.", hey("This is a statement ending with whitespace "), "non-question ending with whitespace")
87+
88+
call test_report()
89+
90+
end program
91+

exercises/bob/example.f90

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,47 @@
11
module bob
2+
implicit none
23
contains
34
function is_uppercase(str)
45
logical :: is_uppercase
5-
character(*) :: str
6+
character(len=*),intent(in) :: str
67
character :: chr
7-
8+
integer :: i
89
is_uppercase = .FALSE.
9-
10-
do i = 1,len(str)
10+
do i = 1,len_trim(str)
1111
chr = str(i:i)
1212
if (chr >= 'a' .AND. chr <= 'z') then
13-
is_uppercase = .FALSE.
14-
return
13+
is_uppercase = .FALSE.
14+
return
1515
else if (chr >= 'A' .AND. chr <= 'Z') then
16-
is_uppercase = .TRUE.
16+
is_uppercase = .TRUE.
1717
end if
18-
1918
end do
2019
end function is_uppercase
2120

2221
function is_question(str)
2322
logical :: is_question
2423
character(*) :: str
2524
character :: chr
26-
27-
chr = str(len(str):len(str))
25+
integer :: i
26+
i = len_trim(str)
27+
chr = str(i:i)
2828
is_question = (chr .EQ. '?')
2929
end function is_question
3030

3131
function is_blank(str)
3232
logical :: is_blank
3333
character(*) :: str
34-
integer :: i
35-
is_blank = .TRUE.
36-
37-
do i = 1,len(str)
38-
if ( str(i:i) .NE. ' ' ) then
39-
is_blank = .FALSE.
40-
return
41-
end if
42-
end do
43-
34+
is_blank = len_trim(adjustl(str)) == 0
4435
end function is_blank
4536

4637
function hey(statement)
4738
character(100) :: hey
48-
character(*) :: statement
39+
character(len=*), intent(in) :: statement
4940

50-
if ( is_uppercase(statement) ) then
51-
hey = 'Whoa chill out!'
41+
if ( is_uppercase(statement) .and.is_question(statement) ) then
42+
hey = "Calm down, I know what I'm doing!"
43+
else if ( is_uppercase(statement) ) then
44+
hey = 'Whoa, chill out!'
5245
else if ( is_question(statement) ) then
5346
hey = 'Sure.'
5447
else if ( is_blank(statement) ) then

0 commit comments

Comments
 (0)