diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 8141edb..2442112 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -25,7 +25,7 @@ else() endif() # Build executable from sources and headers -add_executable(${exercise} ${file}_test.f90 ${exercise_f90} ) +add_executable(${exercise} ${exercise_f90} ${file}_test.f90 ) if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib) file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *) diff --git a/exercises/bob/CMakeLists.txt b/exercises/bob/CMakeLists.txt new file mode 100644 index 0000000..2442112 --- /dev/null +++ b/exercises/bob/CMakeLists.txt @@ -0,0 +1,45 @@ +# Get the exercise name from the current directory +get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) + +# Basic CMake project +cmake_minimum_required(VERSION 2.8.11) + +# Name the project after the exercise +project(${exercise} Fortran) + +# set debug build default +set(CMAKE_BIULD_TYPE Debug) + +# Configure to run all the tests? +if(${EXERCISM_RUN_ALL_TESTS}) + add_definitions(-DEXERCISM_RUN_ALL_TESTS) +endif() + +# Get a source filename from the exercise name by replacing -'s with _'s +string(REPLACE "-" "_" file ${exercise}) + +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.f90) + set(exercise_f90 ${file}.f90) +else() + set(exercise_f90 "") +endif() + +# Build executable from sources and headers +add_executable(${exercise} ${exercise_f90} ${file}_test.f90 ) + +if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib) + file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *) +endif(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/testlib) + +add_subdirectory(testlib) +include_directories(testlib ${CMAKE_CURRENT_BINARY_DIR}/testlib) + +target_link_libraries(${exercise} TesterMain) + +include(CTest) + +add_test (${exercise}_test ${exercise} ) + +# Run the tests on every build +#add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise}) + diff --git a/exercises/bob/bob_test.f90 b/exercises/bob/bob_test.f90 new file mode 100644 index 0000000..2dd0b61 --- /dev/null +++ b/exercises/bob/bob_test.f90 @@ -0,0 +1,91 @@ + +! This test was created from ..\..\exercism\problem-specifications\exercises\bob\canonical-data.json +! version: 1.4.0. +! +! This is the main test program to test your implementaion which is done in +! bob.f90 +! +! If the file does not exist create it. +! +! To build your program first create a build directory, eg. Debug and change into it +! > mkdir Debug +! > cd Debug +! Now, to configure the build you run +! > cmake .. +! Which creates the build files (Makefiles etc.) needed to build and test your program. +! Now build your program with +! > make +! If your build is successfull run the test +! > ctest -V +! If the build fails or the tests, modify the implementation in hello_world.f90 and rerun +! the "make" and the "ctest -V" commands. +! +! Now proceed to the test section and one test after the other, ensure your implementation +! passes this test. + + +program bob_test_main + use TesterMain + use bob + implicit none + + + ! Test 1: stating something + call assert_equal("Whatever.", hey("Tom-ay-to, tom-aaaah-to."), "stating something") + ! Test 2: shouting + call assert_equal("Whoa, chill out!", hey("WATCH OUT!"), "shouting") + ! Test 3: shouting gibberish + call assert_equal("Whoa, chill out!", hey("FCECDFCAAB"), "shouting gibberish") + ! Test 4: asking a question + call assert_equal("Sure.", hey("Does this cryogenic chamber make me look fat?"), "asking a question") + ! Test 5: asking a numeric question + call assert_equal("Sure.", hey("You are, what, like 15?"), "asking a numeric question") + ! Test 6: asking gibberish + call assert_equal("Sure.", hey("fffbbcbeab?"), "asking gibberish") + ! Test 7: talking forcefully + call assert_equal("Whatever.", hey("Let's go make out behind the gym!"), "talking forcefully") + ! Test 8: using acronyms in regular speech + call assert_equal("Whatever.", hey("It's OK if you don't want to go to the DMV."), "using acronyms in regular speech") + ! Test 9: forceful question + call assert_equal("Calm down, I know what I'm doing!", hey("WHAT THE HELL WERE YOU THINKING?"), "forceful question") + ! Test 10: shouting numbers + call assert_equal("Whoa, chill out!", hey("1, 2, 3 GO!"), "shouting numbers") + ! Test 11: no letters + call assert_equal("Whatever.", hey("1, 2, 3"), "no letters") + ! Test 12: question with no letters + call assert_equal("Sure.", hey("4?"), "question with no letters") + ! Test 13: shouting with special characters + call assert_equal("Whoa, chill out!", hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"), "shouting with special characters") + ! Test 14: shouting with no exclamation mark + call assert_equal("Whoa, chill out!", hey("I HATE THE DMV"), "shouting with no exclamation mark") + ! Test 15: statement containing question mark + call assert_equal("Whatever.", hey("Ending with ? means a question."), "statement containing question mark") + ! Test 16: non-letters with question + call assert_equal("Sure.", hey(":) ?"), "non-letters with question") + ! Test 17: prattling on + call assert_equal("Sure.", hey("Wait! Hang on. Are you going to be OK?"), "prattling on") + ! Test 18: silence + call assert_equal("Fine. Be that way!", hey(""), "silence") + ! Test 19: prolonged silence + call assert_equal("Fine. Be that way!", hey(" "), "prolonged silence") + ! Test 20: alternate silence + !call assert_equal("Fine. Be that way!", hey(" "), "alternate silence") + ! Test 21: multiple line question + call assert_equal("Whatever.", hey(""// & + & "Does this cryogenic chamber make me look fat?"// & + & "No."), "multiple line question") + ! Test 22: starting with whitespace + call assert_equal("Whatever.", hey(" hmmmmmmm..."), "starting with whitespace") + ! Test 23: ending with whitespace + call assert_equal("Sure.", hey("Okay if like my spacebar quite a bit? "), "ending with whitespace") + ! Test 24: other whitespace + !call assert_equal("Fine. Be that way!", hey(""// & + ! & " + ! "), "other whitespace") + ! Test 25: non-question ending with whitespace + call assert_equal("Whatever.", hey("This is a statement ending with whitespace "), "non-question ending with whitespace") + + call test_report() + +end program + diff --git a/exercises/bob/example.f90 b/exercises/bob/example.f90 index 9270f84..5ac858a 100644 --- a/exercises/bob/example.f90 +++ b/exercises/bob/example.f90 @@ -1,21 +1,20 @@ module bob + implicit none contains function is_uppercase(str) logical :: is_uppercase - character(*) :: str + character(len=*),intent(in) :: str character :: chr - + integer :: i is_uppercase = .FALSE. - - do i = 1,len(str) + do i = 1,len_trim(str) chr = str(i:i) if (chr >= 'a' .AND. chr <= 'z') then - is_uppercase = .FALSE. - return + is_uppercase = .FALSE. + return else if (chr >= 'A' .AND. chr <= 'Z') then - is_uppercase = .TRUE. + is_uppercase = .TRUE. end if - end do end function is_uppercase @@ -23,32 +22,26 @@ function is_question(str) logical :: is_question character(*) :: str character :: chr - - chr = str(len(str):len(str)) + integer :: i + i = len_trim(str) + chr = str(i:i) is_question = (chr .EQ. '?') end function is_question function is_blank(str) logical :: is_blank character(*) :: str - integer :: i - is_blank = .TRUE. - - do i = 1,len(str) - if ( str(i:i) .NE. ' ' ) then - is_blank = .FALSE. - return - end if - end do - + is_blank = len_trim(adjustl(str)) == 0 end function is_blank function hey(statement) character(100) :: hey - character(*) :: statement + character(len=*), intent(in) :: statement - if ( is_uppercase(statement) ) then - hey = 'Whoa chill out!' + if ( is_uppercase(statement) .and.is_question(statement) ) then + hey = "Calm down, I know what I'm doing!" + else if ( is_uppercase(statement) ) then + hey = 'Whoa, chill out!' else if ( is_question(statement) ) then hey = 'Sure.' else if ( is_blank(statement) ) then