grade_school: initial commit
This commit is contained in:
parent
6cde462a4a
commit
6055b23a3a
3 changed files with 243 additions and 0 deletions
52
cpp/grade-school/CMakeLists.txt
Normal file
52
cpp/grade-school/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})
|
||||
71
cpp/grade-school/README.md
Normal file
71
cpp/grade-school/README.md
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# Grade School
|
||||
|
||||
Write a small archiving program that stores students' names along with the grade that they are in.
|
||||
|
||||
In the end, you should be able to:
|
||||
|
||||
- Add a student's name to the roster for a grade
|
||||
- "Add Jim to grade 2."
|
||||
- "OK."
|
||||
- Get a list of all students enrolled in a grade
|
||||
- "Which students are in grade 2?"
|
||||
- "We've only got Jim just now."
|
||||
- Get a sorted list of all students in all grades. Grades should sort
|
||||
as 1, 2, 3, etc., and students within a grade should be sorted
|
||||
alphabetically by name.
|
||||
- "Who all is enrolled in school right now?"
|
||||
- "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe.
|
||||
Grade 3…"
|
||||
|
||||
Note that all our students only have one name. (It's a small town, what
|
||||
do you want?)
|
||||
|
||||
|
||||
## For bonus points
|
||||
|
||||
Did you get the tests passing and the code clean? If you want to, these
|
||||
are some additional things you could try:
|
||||
|
||||
- If you're working in a language with mutable data structures and your
|
||||
implementation allows outside code to mutate the school's internal DB
|
||||
directly, see if you can prevent this. Feel free to introduce additional
|
||||
tests.
|
||||
|
||||
Then please share your thoughts in a comment on the submission. Did this
|
||||
experiment make the code better? Worse? Did you learn anything from it?
|
||||
|
||||
## 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
|
||||
|
||||
A pairing session with Phil Battos at gSchool [view source](http://gschool.it)
|
||||
120
cpp/grade-school/grade_school_test.cpp
Normal file
120
cpp/grade-school/grade_school_test.cpp
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
#include "grade_school.h"
|
||||
#define BOOST_TEST_MAIN
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
// teach Boost.Test how to print std::vector
|
||||
template <typename T>
|
||||
inline wrap_stringstream&
|
||||
operator<<(wrap_stringstream& wrapped, std::vector<T> const& item)
|
||||
{
|
||||
wrapped << '[';
|
||||
bool first = true;
|
||||
for (auto const& element : item) {
|
||||
wrapped << (!first ? "," : "") << element;
|
||||
first = false;
|
||||
}
|
||||
return wrapped << ']';
|
||||
}
|
||||
|
||||
// teach Boost.Test how to print std::pair
|
||||
template <typename K, typename V>
|
||||
inline wrap_stringstream&
|
||||
operator<<(wrap_stringstream& wrapped, std::pair<const K, V> 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())
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct school_fixture
|
||||
{
|
||||
grade_school::school school_;
|
||||
};
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(school_suite, school_fixture);
|
||||
|
||||
BOOST_AUTO_TEST_CASE(a_new_school_has_an_empty_roster)
|
||||
{
|
||||
BOOST_REQUIRE(school_.roster().empty());
|
||||
}
|
||||
|
||||
#if defined(EXERCISM_RUN_ALL_TESTS)
|
||||
BOOST_AUTO_TEST_CASE(adding_a_student_adds_them_to_the_roster_for_the_given_grade)
|
||||
{
|
||||
school_.add("Aimee", 2);
|
||||
|
||||
const auto actual = school_.roster();
|
||||
|
||||
const map<int, vector<string>> expected{{2, {"Aimee"}}};
|
||||
REQUIRE_EQUAL_CONTAINERS(expected, actual);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(adding_more_students_to_the_same_grade_adds_them_to_the_roster)
|
||||
{
|
||||
school_.add("Blair", 2);
|
||||
school_.add("James", 2);
|
||||
school_.add("Paul", 2);
|
||||
|
||||
const auto actual = school_.roster();
|
||||
|
||||
const map<int, vector<string>> expected{{2, {"Blair", "James", "Paul"}}};
|
||||
REQUIRE_EQUAL_CONTAINERS(expected, actual);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(adding_students_to_different_grades_adds_them_to_the_roster)
|
||||
{
|
||||
school_.add("Chelsea", 3);
|
||||
school_.add("Logan", 7);
|
||||
|
||||
const auto actual = school_.roster();
|
||||
|
||||
const map<int, vector<string>> expected{{3, {"Chelsea"}}, {7, {"Logan"}}};
|
||||
REQUIRE_EQUAL_CONTAINERS(expected, actual);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(grade_returns_the_students_in_that_grade_in_alphabetical_order)
|
||||
{
|
||||
school_.add("Franklin", 5);
|
||||
school_.add("Bradley", 5);
|
||||
school_.add("Jeff", 1);
|
||||
|
||||
const auto actual = school_.grade(5);
|
||||
|
||||
const vector<string> expected{"Bradley", "Franklin"};
|
||||
REQUIRE_EQUAL_CONTAINERS(expected, actual);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(grade_returns_an_empty_array_if_there_are_no_students_in_that_grade)
|
||||
{
|
||||
const auto actual = school_.grade(1);
|
||||
|
||||
BOOST_REQUIRE(actual.empty());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(the_student_names_in_each_grade_in_the_roster_are_sorted)
|
||||
{
|
||||
school_.add("Jennifer", 4);
|
||||
school_.add("Kareem", 6);
|
||||
school_.add("Christopher", 4);
|
||||
school_.add("Kyle", 3);
|
||||
|
||||
const auto actual = school_.roster();
|
||||
|
||||
const map<int, vector<string>> expected{
|
||||
{3, {"Kyle"}},
|
||||
{4, {"Christopher", "Jennifer"}},
|
||||
{6, {"Kareem"}}
|
||||
};
|
||||
REQUIRE_EQUAL_CONTAINERS(expected, actual);
|
||||
}
|
||||
#endif
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
Loading…
Add table
Add a link
Reference in a new issue