triangle: initial commit

This commit is contained in:
Dmitry Kokorin 2016-03-17 00:20:00 +03:00
parent 1baf38d8bc
commit e74c3dd04f
3 changed files with 189 additions and 0 deletions

View file

@ -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})

49
cpp/triangle/README.md Normal file
View file

@ -0,0 +1,49 @@
# Triangle
Write a program that can tell you if a triangle is equilateral, isosceles, or scalene.
The program should raise an error if the triangle cannot exist.
Tests are provided, delete one `skip` at a time.
## Hint
The sum of the lengths of any two sides of a triangle always exceeds the
length of the third side, a principle known as the _triangle
inequality_.
## 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 Ruby Koans triangle project, parts 1 & 2 [view source](http://rubykoans.com)

View file

@ -0,0 +1,81 @@
#include "triangle.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <stdexcept>
BOOST_AUTO_TEST_CASE(equilateral_triangles_have_equal_sides)
{
BOOST_REQUIRE_EQUAL(triangle::equilateral, triangle::kind(2, 2, 2));
}
#if defined(EXERCISM_RUN_ALL_TESTS)
BOOST_AUTO_TEST_CASE(larger_equilateral_triangles_also_have_equal_sides)
{
BOOST_REQUIRE_EQUAL(triangle::equilateral, triangle::kind(10, 10, 10));
}
BOOST_AUTO_TEST_CASE(isosceles_triangles_have_last_two_sides_equal)
{
BOOST_REQUIRE_EQUAL(triangle::isosceles, triangle::kind(3, 4, 4));
}
BOOST_AUTO_TEST_CASE(isosceles_triangles_have_first_and_last_sides_equal)
{
BOOST_REQUIRE_EQUAL(triangle::isosceles, triangle::kind(4, 3, 4));
}
BOOST_AUTO_TEST_CASE(isosceles_triangles_have_first_two_sides_equal)
{
BOOST_REQUIRE_EQUAL(triangle::isosceles, triangle::kind(4, 4, 3));
}
BOOST_AUTO_TEST_CASE(isosceles_triangles_have_in_fact_exactly_two_sides_equal)
{
BOOST_REQUIRE_EQUAL(triangle::isosceles, triangle::kind(10, 10, 2));
}
BOOST_AUTO_TEST_CASE(scalene_triangles_have_no_equal_sides)
{
BOOST_REQUIRE_EQUAL(triangle::scalene, triangle::kind(3, 4, 5));
}
BOOST_AUTO_TEST_CASE(scalene_triangles_have_no_equal_sides_at_a_larger_scale_too)
{
BOOST_REQUIRE_EQUAL(triangle::scalene, triangle::kind(10, 11, 12));
}
BOOST_AUTO_TEST_CASE(scalene_triangles_have_no_equal_sides_in_descending_order_either)
{
BOOST_REQUIRE_EQUAL(triangle::scalene, triangle::kind(5, 4, 2));
}
BOOST_AUTO_TEST_CASE(very_small_triangles_are_legal)
{
BOOST_REQUIRE_EQUAL(triangle::scalene, triangle::kind(0.4, 0.6, 0.3));
}
BOOST_AUTO_TEST_CASE(triangles_with_no_size_are_illegal)
{
BOOST_REQUIRE_THROW(triangle::kind(0, 0, 0), std::domain_error);
}
BOOST_AUTO_TEST_CASE(triangles_with_negative_sides_are_illegal)
{
BOOST_REQUIRE_THROW(triangle::kind(3, 4, -5), std::domain_error);
}
BOOST_AUTO_TEST_CASE(triangles_violating_triangle_inequality_are_illegal)
{
BOOST_REQUIRE_THROW(triangle::kind(1, 1, 3), std::domain_error);
}
BOOST_AUTO_TEST_CASE(medium_triangles_violating_triangle_inequality_are_illegal)
{
BOOST_REQUIRE_THROW(triangle::kind(2, 4, 2), std::domain_error);
}
BOOST_AUTO_TEST_CASE(larger_triangles_violating_triangle_inequality_are_illegal)
{
BOOST_REQUIRE_THROW(triangle::kind(7, 3, 2), std::domain_error);
}
#endif