diff --git a/cpp/word-count/CMakeLists.txt b/cpp/word-count/CMakeLists.txt new file mode 100644 index 0000000..95c7011 --- /dev/null +++ b/cpp/word-count/CMakeLists.txt @@ -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}) diff --git a/cpp/word-count/README.md b/cpp/word-count/README.md new file mode 100644 index 0000000..9fb5655 --- /dev/null +++ b/cpp/word-count/README.md @@ -0,0 +1,49 @@ +# Word Count + +Write a program that given a phrase can count the occurrences of each word in that phrase. + +For example for the input `"olly olly in come free"` + +```plain +olly: 2 +in: 1 +come: 1 +free: 1 +``` + + +## 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 + +This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour. [view source]() diff --git a/cpp/word-count/word_count.cpp b/cpp/word-count/word_count.cpp new file mode 100644 index 0000000..aa37f4b --- /dev/null +++ b/cpp/word-count/word_count.cpp @@ -0,0 +1,12 @@ +#include "word_count.h" + +namespace word_count { + +using namespace std; + +Words words(const string &str) +{ + return Words(); +} + +} diff --git a/cpp/word-count/word_count.h b/cpp/word-count/word_count.h new file mode 100644 index 0000000..8043872 --- /dev/null +++ b/cpp/word-count/word_count.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +namespace word_count { + +typedef std::map Words; + +Words words(const std::string&); + +} diff --git a/cpp/word-count/word_count_test.cpp b/cpp/word-count/word_count_test.cpp new file mode 100644 index 0000000..0891466 --- /dev/null +++ b/cpp/word-count/word_count_test.cpp @@ -0,0 +1,136 @@ +#include "word_count.h" +#define BOOST_TEST_MAIN +#include +#include + +using namespace std; + +namespace boost +{ + +// teach Boost.Test how to print std::pair +template +inline wrap_stringstream& +operator<<(wrap_stringstream& wrapped, std::pair const& item) +{ + return wrapped << '<' << item.first << ',' << item.second << '>'; +} + +} + +#define REQUIRE_EQUAL_CONTAINERS(left_, right_) \ + BOOST_REQUIRE_EQUAL_COLLECTIONS(left_.begin(), left_.end(), right_.begin(), right_.end()) + +BOOST_AUTO_TEST_CASE(counts_one_word) +{ + const map expected{{"word", 1}}; + + const auto actual = word_count::words("word"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +BOOST_AUTO_TEST_CASE(counts_one_of_each) +{ + const map expected{{"one", 1}, {"of", 1}, {"each", 1}}; + + const auto actual = word_count::words("one of each"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(counts_multiple_occurrences) +{ + const map expected{{"one", 1}, {"fish", 4}, {"two", 1}, {"red", 1}, {"blue", 1}}; + + const auto actual = word_count::words("one fish two fish red fish blue fish"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(ignores_punctuation) +{ + const map expected{{"car", 1}, {"carpet", 1}, {"as", 1}, {"java", 1}, {"javascript", 1}}; + + const auto actual = word_count::words("car : carpet as java : javascript!!&@$%^&"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(includes_numbers) +{ + const map expected{{"testing", 2}, {"1", 1}, {"2", 1}}; + + const auto actual = word_count::words("testing, 1, 2 testing"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(normalizes_case) +{ + const map expected{{"go", 3}}; + + const auto actual = word_count::words("go Go GO"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(counts_constructor) +{ + const map expected{{"constructor", 2}}; + + const auto actual = word_count::words("constructor Constructor"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(counts_multiline) +{ + const map expected{{"hello", 1}, {"world", 1}}; + + const auto actual = word_count::words("hello\nworld"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(count_everything_just_once) +{ + const map expected{{"all", 2}, {"the", 2}, {"kings", 2}, {"horses", 1}, {"and", 1}, {"men", 1}}; + const auto actual = word_count::words("all the kings horses and all the kings men"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(handles_cramped_list) +{ + const map expected{{"one", 1}, {"two", 1}, {"three", 1}}; + const auto actual = word_count::words("one,two,three"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(with_apostrophes) +{ + const map expected{{"first", 1}, {"don't", 2}, {"laugh", 1}, {"then", 1}, {"cry", 1}}; + const auto actual = word_count::words("First: don't laugh. Then: don't cry."); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(with_free_standing_apostrophes) +{ + const map expected{{ "go", 3 }}; + const auto actual = word_count::words("go ' Go '' GO"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(with_apostrophes_as_quotes) +{ + const map expected{{"she", 1}, {"said", 1}, {"let's", 1}, {"meet", 1}, {"at", 1}, {"twelve", 1}, {"o'clock", 1}}; + const auto actual = word_count::words("She said, 'let's meet at twelve o'clock'"); + + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} +#endif