From 75a9e236e2888230275ac68e2ef42fe019570836 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Fri, 8 Apr 2016 00:18:50 +0300 Subject: [PATCH] say: initial commit --- cpp/say/CMakeLists.txt | 59 +++++++++++++++++++++++++ cpp/say/README.md | 98 ++++++++++++++++++++++++++++++++++++++++++ cpp/say/say_test.cpp | 95 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 cpp/say/CMakeLists.txt create mode 100644 cpp/say/README.md create mode 100644 cpp/say/say_test.cpp diff --git a/cpp/say/CMakeLists.txt b/cpp/say/CMakeLists.txt new file mode 100644 index 0000000..56a3675 --- /dev/null +++ b/cpp/say/CMakeLists.txt @@ -0,0 +1,59 @@ +# 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() + +# Include a test helper header if it exists +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/require_equal_containers.h) + set(test_helper require_equal_containers.h) +else() + set(test_helper "") +endif() + +# Build executable from sources and headers +add_executable(${exercise} ${file}_test.cpp ${test_helper} ${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/say/README.md b/cpp/say/README.md new file mode 100644 index 0000000..b600509 --- /dev/null +++ b/cpp/say/README.md @@ -0,0 +1,98 @@ +# Say + +Write a program that will take a number from 0 to 999,999,999,999 and spell out that number in English. + +## Step 1 + +Handle the basic case of 0 through 99. + +If the input to the program is `22`, then the output should be +`'twenty-two'`. + +Your program should complain loudly if given a number outside the +blessed range. + +Some good test cases for this program are: + +- 0 +- 14 +- 50 +- 98 +- -1 +- 100 + +### Extension + +If you're on a Mac, shell out to Mac OS X's `say` program to talk out +loud. + +## Step 2 + +Implement breaking a number up into chunks of thousands. + +So `1234567890` should yield a list like 1, 234, 567, and 890, while the +far simpler `1000` should yield just 1 and 0. + +The program must also report any values that are out of range. + +## Step 3 + +Now handle inserting the appropriate scale word between those chunks. + +So `1234567890` should yield `'1 billion 234 million 567 thousand 890'` + +The program must also report any values that are out of range. It's +fine to stop at "trillion". + +## Step 4 + +Put it all together to get nothing but plain English. + +`12345` should give `twelve thousand three hundred forty-five`. + +The program must also report any values that are out of range. + +### Extensions + +Use _and_ (correctly) when spelling out the number in English: + +- 14 becomes "fourteen". +- 100 becomes "one hundred". +- 120 becomes "one hundred and twenty". +- 1002 becomes "one thousand and two". +- 1323 becomes "one thousand three hundred and twenty-three". + +## Getting Started + +Make sure you have read [the C++ page](http://exercism.io/languages/cpp) on +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 + +A variation on JavaRanch CattleDrive, exercise 4a [view source](http://www.javaranch.com/say.jsp) diff --git a/cpp/say/say_test.cpp b/cpp/say/say_test.cpp new file mode 100644 index 0000000..f76a46f --- /dev/null +++ b/cpp/say/say_test.cpp @@ -0,0 +1,95 @@ +#include "say.h" +#define BOOST_TEST_MAIN +#include + +BOOST_AUTO_TEST_CASE(zero) +{ + BOOST_REQUIRE_EQUAL("zero", say::in_english(0ULL)); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +BOOST_AUTO_TEST_CASE(one) +{ + BOOST_REQUIRE_EQUAL("one", say::in_english(1ULL)); +} + +BOOST_AUTO_TEST_CASE(fourteen) +{ + BOOST_REQUIRE_EQUAL("fourteen", say::in_english(14ULL)); +} + +BOOST_AUTO_TEST_CASE(twenty) +{ + BOOST_REQUIRE_EQUAL("twenty", say::in_english(20ULL)); +} + +BOOST_AUTO_TEST_CASE(twenty_two) +{ + BOOST_REQUIRE_EQUAL("twenty-two", say::in_english(22ULL)); +} + +BOOST_AUTO_TEST_CASE(sixty_nine) +{ + BOOST_REQUIRE_EQUAL("sixty-nine", say::in_english(69ULL)); +} + +BOOST_AUTO_TEST_CASE(one_hundred) +{ + BOOST_REQUIRE_EQUAL("one hundred", say::in_english(100ULL)); +} + +BOOST_AUTO_TEST_CASE(one_hundred_twenty_three) +{ + BOOST_REQUIRE_EQUAL("one hundred twenty-three", say::in_english(123ULL)); +} + +BOOST_AUTO_TEST_CASE(one_thousand) +{ + BOOST_REQUIRE_EQUAL("one thousand", say::in_english(1000ULL)); +} + +BOOST_AUTO_TEST_CASE(one_thousand_two_hundred_thirty_four) +{ + BOOST_REQUIRE_EQUAL("one thousand two hundred thirty-four", say::in_english(1234ULL)); +} + +BOOST_AUTO_TEST_CASE(one_million) +{ + BOOST_REQUIRE_EQUAL("one million", say::in_english(1000ULL*1000ULL)); +} + +BOOST_AUTO_TEST_CASE(one_million_two) +{ + BOOST_REQUIRE_EQUAL("one million two", say::in_english(1000ULL*1000ULL + 2ULL)); +} + +BOOST_AUTO_TEST_CASE(one_million_two_thousand_three_hundred_forty_five) +{ + BOOST_REQUIRE_EQUAL("one million two thousand three hundred forty-five", say::in_english(1002345ULL)); +} + +BOOST_AUTO_TEST_CASE(one_billion) +{ + BOOST_REQUIRE_EQUAL("one billion", say::in_english(1000ULL*1000ULL*1000ULL)); +} + +BOOST_AUTO_TEST_CASE(a_really_big_number) +{ + BOOST_REQUIRE_EQUAL( + "nine hundred eighty-seven billion" + " six hundred fifty-four million" + " three hundred twenty-one thousand" + " one hundred twenty-three", + say::in_english(987654321123ULL)); +} + +BOOST_AUTO_TEST_CASE(raises_an_error_below_zero) +{ + BOOST_REQUIRE_THROW(say::in_english(-1), std::domain_error); +} + +BOOST_AUTO_TEST_CASE(raises_an_error_for_one_trillion) +{ + BOOST_REQUIRE_THROW(say::in_english(1000ULL*1000ULL*1000ULL*1000ULL), std::domain_error); +} +#endif