bob exercise, all tests are passed
This commit is contained in:
parent
f93f099cc6
commit
c252a1a93d
5 changed files with 273 additions and 0 deletions
52
cpp/bob/CMakeLists.txt
Normal file
52
cpp/bob/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
# 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} CXX)
|
||||||
|
|
||||||
|
# Locate Boost libraries: unit_test_framework, date_time and regex
|
||||||
|
set(Boost_USE_STATIC_LIBS ON)
|
||||||
|
set(Boost_USE_MULTITHREADED ON)
|
||||||
|
set(Boost_USE_STATIC_RUNTIME OFF)
|
||||||
|
find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework date_time regex)
|
||||||
|
|
||||||
|
# Enable C++11 features on gcc/clang
|
||||||
|
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
|
||||||
|
set(CMAKE_CXX_FLAGS "-std=c++11")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# 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})
|
||||||
|
|
||||||
|
# Implementation could be only a header
|
||||||
|
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
|
||||||
|
set(exercise_cpp ${file}.cpp)
|
||||||
|
else()
|
||||||
|
set(exercise_cpp "")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Build executable from sources and headers
|
||||||
|
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
|
||||||
|
|
||||||
|
# We need boost includes
|
||||||
|
target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS})
|
||||||
|
|
||||||
|
# We need boost libraries
|
||||||
|
target_link_libraries(${exercise} ${Boost_LIBRARIES})
|
||||||
|
|
||||||
|
# Tell MSVC not to warn us about unchecked iterators in debug builds
|
||||||
|
if(${MSVC})
|
||||||
|
set_target_properties(${exercise} PROPERTIES
|
||||||
|
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Run the tests on every build
|
||||||
|
add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise})
|
||||||
66
cpp/bob/README.md
Normal file
66
cpp/bob/README.md
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
# Bob
|
||||||
|
|
||||||
|
Bob is a lackadaisical teenager. In conversation, his responses are very limited.
|
||||||
|
|
||||||
|
Bob answers 'Sure.' if you ask him a question.
|
||||||
|
|
||||||
|
He answers 'Whoa, chill out!' if you yell at him.
|
||||||
|
|
||||||
|
He says 'Fine. Be that way!' if you address him without actually saying
|
||||||
|
anything.
|
||||||
|
|
||||||
|
He answers 'Whatever.' to anything else.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Run the test file, and fix each of the errors in turn. When you get the
|
||||||
|
first test to pass, go to the first pending or skipped test, and make
|
||||||
|
that pass as well. When all of the tests are passing, feel free to
|
||||||
|
submit.
|
||||||
|
|
||||||
|
Remember that passing code is just the first step. The goal is to work
|
||||||
|
towards a solution that is as readable and expressive as you can make
|
||||||
|
it.
|
||||||
|
|
||||||
|
Please make your solution as general as possible. Good code doesn't just
|
||||||
|
pass the test suite, it works with any input that fits the
|
||||||
|
specification.
|
||||||
|
|
||||||
|
Have fun!
|
||||||
|
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Make sure you have read the [getting started with C++](http://help.exercism.io/getting-started-with-cpp.html)
|
||||||
|
page on the [exercism help site](http://help.exercism.io/). This covers
|
||||||
|
the basic information on setting up the development environment expected
|
||||||
|
by the exercises.
|
||||||
|
|
||||||
|
## Passing the Tests
|
||||||
|
|
||||||
|
Get the first test compiling, linking and passing by following the [three
|
||||||
|
rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd).
|
||||||
|
Create just enough structure by declaring namespaces, functions, classes,
|
||||||
|
etc., to satisfy any compiler errors and get the test to fail. Then write
|
||||||
|
just enough code to get the test to pass. Once you've done that,
|
||||||
|
uncomment the next test by moving the following line past the next test.
|
||||||
|
|
||||||
|
```C++
|
||||||
|
#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||||
|
```
|
||||||
|
|
||||||
|
This may result in compile errors as new constructs may be invoked that
|
||||||
|
you haven't yet declared or defined. Again, fix the compile errors minimally
|
||||||
|
to get a failing test, then change the code minimally to pass the test,
|
||||||
|
refactor your implementation for readability and expressiveness and then
|
||||||
|
go on to the next test.
|
||||||
|
|
||||||
|
Try to use standard C++11 facilities in preference to writing your own
|
||||||
|
low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/)
|
||||||
|
is a wiki reference to the C++ language and standard library. If you
|
||||||
|
are new to C++, but have programmed in C, beware of
|
||||||
|
[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers).
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=06)
|
||||||
57
cpp/bob/bob.cpp
Normal file
57
cpp/bob/bob.cpp
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#include "bob.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <locale>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace bob {
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void trim_spaces(string &str)
|
||||||
|
{
|
||||||
|
str.erase(remove_if(str.begin(), str.end(),
|
||||||
|
[](char c){return isspace(static_cast<unsigned char>(c));}),
|
||||||
|
str.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
string to_uppercase(const string &str)
|
||||||
|
{
|
||||||
|
string result = str;
|
||||||
|
for (auto & c: result)
|
||||||
|
c = toupper(c);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool has_alpha(const string &str)
|
||||||
|
{
|
||||||
|
return find_if(str.begin(), str.end(),
|
||||||
|
[](char c){return isalpha(static_cast<unsigned char>(c));}) != str.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const char * hey(const char input[])
|
||||||
|
{
|
||||||
|
|
||||||
|
string str(input);
|
||||||
|
trim_spaces(str);
|
||||||
|
|
||||||
|
if (str.empty())
|
||||||
|
return "Fine. Be that way!";
|
||||||
|
|
||||||
|
if (has_alpha(str) && str == to_uppercase(str))
|
||||||
|
return "Whoa, chill out!";
|
||||||
|
|
||||||
|
if ('?' == str.back())
|
||||||
|
return "Sure.";
|
||||||
|
|
||||||
|
return "Whatever.";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
8
cpp/bob/bob.h
Normal file
8
cpp/bob/bob.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define EXERCISM_RUN_ALL_TESTS true
|
||||||
|
|
||||||
|
namespace bob
|
||||||
|
{
|
||||||
|
const char * hey(const char str[]);
|
||||||
|
}
|
||||||
90
cpp/bob/bob_test.cpp
Normal file
90
cpp/bob/bob_test.cpp
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
#include "bob.h"
|
||||||
|
#define BOOST_TEST_MAIN
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(stating_something)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey("Tom-ay-to, tom-aaaah-to."));
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||||
|
BOOST_AUTO_TEST_CASE(shouting)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whoa, chill out!", bob::hey("WATCH OUT!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(asking_a_question)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Sure.", bob::hey("Does this cryogenic chamber make me look fat?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(talking_forcefully)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey("Let's go make out behind the gym!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(using_acronyms_in_regular_speech)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey("It's OK if you don't want to go to the DMV."));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(forceful_questions)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whoa, chill out!", bob::hey("WHAT THE HELL WERE YOU THINKING?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(shouting_numbers)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whoa, chill out!", bob::hey("1, 2, 3 GO!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(only_numbers)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey("1, 2, 3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(question_with_only_numbers)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Sure.", bob::hey("4?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(shouting_with_special_characters)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whoa, chill out!", bob::hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(shouting_with_no_exclamation_mark)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whoa, chill out!", bob::hey("I HATE YOU"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(statement_containing_question_mark)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey("Ending with a ? means a question."));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(prattling_on)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Sure.", bob::hey("Wait! Hang on. Are you going to be OK?"));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(question_with_trailing_whitespace)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Sure.", bob::hey("Are you ok? "));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(silence)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Fine. Be that way!", bob::hey(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(prolonged_silence)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Fine. Be that way!", bob::hey(" "));
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(not_all_silence)
|
||||||
|
{
|
||||||
|
BOOST_REQUIRE_EQUAL("Whatever.", bob::hey(" A bit of silence can be nice. "));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue