|
6 | 6 | #endif
|
7 | 7 | #include <stdexcept>
|
8 | 8 |
|
| 9 | +/* |
| 10 | +Problem spec commentary: |
| 11 | +
|
| 12 | + Pursuant to discussion in #202, we have decided NOT to test triangles |
| 13 | + where all side lengths are positive but a + b = c. e.g: |
| 14 | + (2, 4, 2, Isosceles), (1, 3, 4, Scalene). |
| 15 | + It's true that the triangle inequality admits such triangles.These |
| 16 | + triangles have zero area, however. |
| 17 | + They're degenerate triangles with all three vertices collinear. |
| 18 | + (In contrast, we will test (0, 0, 0, Illegal), as it is a point) |
| 19 | + The tests assert properties of the triangle are true or false. |
| 20 | + See: https://github.com/exercism/problem-specifications/issues/379 for |
| 21 | + disscussion of this approach How you handle invalid triangles is up to you. |
| 22 | + These tests suggest a triangle is returned, but all of its properties are |
| 23 | +false. But you could also have the creation of an invalid triangle return an |
| 24 | +error or exception. Choose what is idiomatic for your language." |
| 25 | +
|
| 26 | +The CPP track has chosen to throw a domain error for invalid triangles. |
| 27 | +
|
| 28 | +Tests, that check if an equilateral is also scalene (etc.) do not work with |
| 29 | +the chosen implementation, they are therefore not implemented for this track. |
| 30 | +
|
| 31 | +*/ |
| 32 | + |
9 | 33 | // improves error messages with triangle flavor enum text instead of integers:
|
10 |
| -CATCH_REGISTER_ENUM(triangle::flavor, |
11 |
| - triangle::flavor::equilateral, |
12 |
| - triangle::flavor::isosceles, |
13 |
| - triangle::flavor::scalene) |
| 34 | +CATCH_REGISTER_ENUM(triangle::flavor, triangle::flavor::equilateral, |
| 35 | + triangle::flavor::isosceles, triangle::flavor::scalene) |
14 | 36 |
|
15 |
| -TEST_CASE("equilateral_triangles_have_equal_sides") |
16 |
| -{ |
| 37 | +TEST_CASE("equilateral triangle -> all sides are equal", |
| 38 | + "[8b2c43ac-7257-43f9-b552-7631a91988af]") { |
17 | 39 | REQUIRE(triangle::flavor::equilateral == triangle::kind(2, 2, 2));
|
18 | 40 | }
|
19 | 41 |
|
20 | 42 | #if defined(EXERCISM_RUN_ALL_TESTS)
|
21 |
| -TEST_CASE("larger_equilateral_triangles_also_have_equal_sides") |
22 |
| -{ |
23 |
| - REQUIRE(triangle::flavor::equilateral == triangle::kind(10, 10, 10)); |
| 43 | + |
| 44 | +TEST_CASE("equilateral triangle -> any side is unequal", |
| 45 | + "[33eb6f87-0498-4ccf-9573-7f8c3ce92b7b]") { |
| 46 | + REQUIRE_FALSE(triangle::flavor::equilateral == triangle::kind(2, 3, 2)); |
| 47 | +} |
| 48 | + |
| 49 | +TEST_CASE("equilateral triangle -> no sides are equal", |
| 50 | + "[c6585b7d-a8c0-4ad8-8a34-e21d36f7ad87]") { |
| 51 | + REQUIRE_FALSE(triangle::flavor::equilateral == triangle::kind(5, 4, 6)); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_CASE("equilateral triangle -> sides may be floats", |
| 55 | + "[3022f537-b8e5-4cc1-8f12-fd775827a00c]") { |
| 56 | + REQUIRE(triangle::flavor::equilateral == triangle::kind(0.5, 0.5, 0.5)); |
24 | 57 | }
|
25 | 58 |
|
26 |
| -TEST_CASE("isosceles_triangles_have_last_two_sides_equal") |
27 |
| -{ |
| 59 | +TEST_CASE("isosceles triangle -> last two sides are equal", |
| 60 | + "[cbc612dc-d75a-4c1c-87fc-e2d5edd70b71]") { |
28 | 61 | REQUIRE(triangle::flavor::isosceles == triangle::kind(3, 4, 4));
|
29 | 62 | }
|
30 | 63 |
|
31 |
| -TEST_CASE("isosceles_triangles_have_first_and_last_sides_equal") |
32 |
| -{ |
| 64 | +TEST_CASE("isosceles triangle -> first two sides are equal", |
| 65 | + "[e388ce93-f25e-4daf-b977-4b7ede992217]") { |
| 66 | + REQUIRE(triangle::flavor::isosceles == triangle::kind(4, 4, 3)); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_CASE("isosceles triangle -> first and last sides are equal", |
| 70 | + "[d2080b79-4523-4c3f-9d42-2da6e81ab30f]") { |
33 | 71 | REQUIRE(triangle::flavor::isosceles == triangle::kind(4, 3, 4));
|
34 | 72 | }
|
35 | 73 |
|
36 |
| -TEST_CASE("isosceles_triangles_have_first_two_sides_equal") |
37 |
| -{ |
38 |
| - REQUIRE(triangle::flavor::isosceles == triangle::kind(4, 4, 3)); |
| 74 | +TEST_CASE("isosceles triangle -> no sides are equal", |
| 75 | + "[840ed5f8-366f-43c5-ac69-8f05e6f10bbb]") { |
| 76 | + REQUIRE_FALSE(triangle::flavor::isosceles == triangle::kind(2, 3, 4)); |
39 | 77 | }
|
40 | 78 |
|
41 |
| -TEST_CASE("isosceles_triangles_have_in_fact_exactly_two_sides_equal") |
42 |
| -{ |
43 |
| - REQUIRE(triangle::flavor::isosceles == triangle::kind(10, 10, 2)); |
| 79 | +TEST_CASE("isosceles triangle -> first triangle inequality violation", |
| 80 | + "[2eba0cfb-6c65-4c40-8146-30b608905eae]") { |
| 81 | + REQUIRE_THROWS_AS(triangle::kind(1, 1, 3), std::domain_error); |
44 | 82 | }
|
45 | 83 |
|
46 |
| -TEST_CASE("scalene_triangles_have_no_equal_sides") |
47 |
| -{ |
48 |
| - REQUIRE(triangle::flavor::scalene == triangle::kind(3, 4, 5)); |
| 84 | +TEST_CASE("isosceles triangle -> second triangle inequality violation", |
| 85 | + "[278469cb-ac6b-41f0-81d4-66d9b828f8ac]") { |
| 86 | + REQUIRE_THROWS_AS(triangle::kind(1, 3, 1), std::domain_error); |
49 | 87 | }
|
50 | 88 |
|
51 |
| -TEST_CASE("scalene_triangles_have_no_equal_sides_at_a_larger_scale_too") |
52 |
| -{ |
53 |
| - REQUIRE(triangle::flavor::scalene == triangle::kind(10, 11, 12)); |
| 89 | +TEST_CASE("isosceles triangle -> third triangle inequality violation", |
| 90 | + "[90efb0c7-72bb-4514-b320-3a3892e278ff]") { |
| 91 | + REQUIRE_THROWS_AS(triangle::kind(3, 1, 1), std::domain_error); |
54 | 92 | }
|
55 | 93 |
|
56 |
| -TEST_CASE("scalene_triangles_have_no_equal_sides_in_descending_order_either") |
57 |
| -{ |
58 |
| - REQUIRE(triangle::flavor::scalene == triangle::kind(5, 4, 2)); |
| 94 | +TEST_CASE("isosceles triangle -> sides may be floats", |
| 95 | + "[adb4ee20-532f-43dc-8d31-e9271b7ef2bc]") { |
| 96 | + REQUIRE(triangle::flavor::isosceles == triangle::kind(0.5, 0.4, 0.5)); |
59 | 97 | }
|
60 | 98 |
|
61 |
| -TEST_CASE("very_small_triangles_are_legal") |
62 |
| -{ |
63 |
| - REQUIRE(triangle::flavor::scalene == triangle::kind(0.4, 0.6, 0.3)); |
| 99 | +TEST_CASE("scalene triangle -> no sides are equal", |
| 100 | + "[e8b5f09c-ec2e-47c1-abec-f35095733afb]") { |
| 101 | + REQUIRE(triangle::flavor::scalene == triangle::kind(5, 4, 6)); |
64 | 102 | }
|
65 | 103 |
|
66 |
| -TEST_CASE("triangles_with_no_size_are_illegal") |
67 |
| -{ |
68 |
| - REQUIRE_THROWS_AS(triangle::kind(0, 0, 0), std::domain_error); |
| 104 | +TEST_CASE("scalene triangle -> may not violate triangle inequality", |
| 105 | + "[70ad5154-0033-48b7-af2c-b8d739cd9fdc]") { |
| 106 | + REQUIRE_THROWS_AS(triangle::kind(7, 3, 2), std::domain_error); |
69 | 107 | }
|
70 | 108 |
|
71 |
| -TEST_CASE("triangles_with_negative_sides_are_illegal") |
72 |
| -{ |
73 |
| - REQUIRE_THROWS_AS(triangle::kind(3, 4, -5), std::domain_error); |
| 109 | +TEST_CASE("scalene triangle -> sides may be floats", |
| 110 | + "[26d9d59d-f8f1-40d3-ad58-ae4d54123d7d]") { |
| 111 | + REQUIRE(triangle::flavor::scalene == triangle::kind(0.4, 0.6, 0.3)); |
74 | 112 | }
|
75 | 113 |
|
76 |
| -TEST_CASE("triangles_violating_triangle_inequality_are_illegal") |
77 |
| -{ |
78 |
| - REQUIRE_THROWS_AS(triangle::kind(1, 1, 3), std::domain_error); |
| 114 | +TEST_CASE("all zero sides is not a triangle", |
| 115 | + "[16e8ceb0-eadb-46d1-b892-c50327479251]") { |
| 116 | + REQUIRE_THROWS_AS(triangle::kind(0, 0, 0), std::domain_error); |
79 | 117 | }
|
80 | 118 |
|
81 |
| -TEST_CASE("larger_triangles_violating_triangle_inequality_are_illegal") |
82 |
| -{ |
83 |
| - REQUIRE_THROWS_AS(triangle::kind(7, 3, 2), std::domain_error); |
| 119 | +// Tests that are not in the problem spec |
| 120 | +// kept for legacy and specific cpp functionality |
| 121 | + |
| 122 | +TEST_CASE("triangles_with_negative_sides_are_illegal") { |
| 123 | + REQUIRE_THROWS_AS(triangle::kind(3, 4, -5), std::domain_error); |
84 | 124 | }
|
85 | 125 |
|
86 |
| -TEST_CASE("double_and_integer_arguments") |
87 |
| -{ |
| 126 | +TEST_CASE("double_and_integer_arguments") { |
88 | 127 | REQUIRE(triangle::flavor::scalene == triangle::kind(5.5, 4, 2));
|
89 | 128 | }
|
| 129 | + |
90 | 130 | #endif
|
0 commit comments