From 3cb7c2bff1d4afb93e73f168072dd8c57b40fbca Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Mon, 7 Mar 2016 21:17:32 +0300 Subject: [PATCH] etl: initial commit --- cpp/etl/CMakeLists.txt | 52 +++++++++++++++++++++++++++ cpp/etl/README.md | 81 ++++++++++++++++++++++++++++++++++++++++++ cpp/etl/etl_test.cpp | 76 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 cpp/etl/CMakeLists.txt create mode 100644 cpp/etl/README.md create mode 100644 cpp/etl/etl_test.cpp diff --git a/cpp/etl/CMakeLists.txt b/cpp/etl/CMakeLists.txt new file mode 100644 index 0000000..95c7011 --- /dev/null +++ b/cpp/etl/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/etl/README.md b/cpp/etl/README.md new file mode 100644 index 0000000..8b0079a --- /dev/null +++ b/cpp/etl/README.md @@ -0,0 +1,81 @@ +# Etl + +We are going to do the `Transform` step of an Extract-Transform-Load. + +### ETL +Extract-Transform-Load (ETL) is a fancy way of saying, "We have some crufty, legacy data over in this system, and now we need it in this shiny new system over here, so +we're going to migrate this." + +(Typically, this is followed by, "We're only going to need to run this +once." That's then typically followed by much forehead slapping and +moaning about how stupid we could possibly be.) + +### The goal +We're going to extract some scrabble scores from a legacy system. + +The old system stored a list of letters per score: + +- 1 point: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T", +- 2 points: "D", "G", +- 3 points: "B", "C", "M", "P", +- 4 points: "F", "H", "V", "W", "Y", +- 5 points: "K", +- 8 points: "J", "X", +- 10 points: "Q", "Z", + +The shiny new scrabble system instead stores the score per letter, which +makes it much faster and easier to calculate the score for a word. It +also stores the letters in lower-case regardless of the case of the +input letters: + +- "a" is worth 1 point. +- "b" is worth 3 points. +- "c" is worth 3 points. +- "d" is worth 2 points. +- Etc. + +Your mission, should you choose to accept it, is to write a program that +transforms the legacy data format to the shiny new format. + +### Notes + +A final note about scoring, Scrabble is played around the world in a +variety of languages, each with its own unique scoring table. For +example, an "A" is scored at 14 in the Basque-language version of the +game while being scored at 9 in the Latin-language version. + +## 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 + +The Jumpstart Lab team [view source](http://jumpstartlab.com) diff --git a/cpp/etl/etl_test.cpp b/cpp/etl/etl_test.cpp new file mode 100644 index 0000000..9babffd --- /dev/null +++ b/cpp/etl/etl_test.cpp @@ -0,0 +1,76 @@ +#include "etl.h" +#define BOOST_TEST_MAIN +#include + +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(transforms_one_value) +{ + const std::map> old{{1, {'A'}}}; + + const auto actual = etl::transform(old); + + const std::map expected{{'a', 1}}; + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +BOOST_AUTO_TEST_CASE(transforms_more_values) +{ + const std::map> old{{1, {'A', 'E', 'I', 'O', 'U'}}}; + + const auto actual = etl::transform(old); + + const std::map expected{{'a', 1}, {'e', 1}, {'i', 1}, {'o', 1}, {'u', 1}}; + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(transforms_more_keys) +{ + const std::map> old{{1, {'A', 'E'}}, {2, {'D', 'G'}}}; + + const auto actual = etl::transform(old); + + const std::map expected{{'a', 1}, {'e', 1}, {'d', 2}, {'g', 2}}; + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} + +BOOST_AUTO_TEST_CASE(transforms_a_full_dataset) +{ + const std::map> old{ + {1, {'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'}}, + {2, {'D', 'G'}}, + {3, {'B', 'C', 'M', 'P'}}, + {4, {'F', 'H', 'V', 'W', 'Y'}}, + {5, {'K'}}, + {8, {'J', 'X'}}, + {10, {'Q', 'Z'}} + }; + + const auto actual = etl::transform(old); + + const std::map expected{ + {'a', 1}, {'b', 3}, {'c', 3}, {'d', 2}, {'e', 1}, + {'f', 4}, {'g', 2}, {'h', 4}, {'i', 1}, {'j', 8}, + {'k', 5}, {'l', 1}, {'m', 3}, {'n', 1}, {'o', 1}, + {'p', 3}, {'q', 10}, {'r', 1}, {'s', 1}, {'t', 1}, + {'u', 1}, {'v', 4}, {'w', 4}, {'x', 8}, {'y', 4}, + {'z', 10} + }; + REQUIRE_EQUAL_CONTAINERS(expected, actual); +} +#endif