Skip to content

Commit c6b743a

Browse files
committed
Add choose() tests
1 parent 6b1fe7b commit c6b743a

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

OpenSim/Common/Test/testCommonUtilities.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* -------------------------------------------------------------------------- */
2323

2424
#include <OpenSim/Common/CommonUtilities.h>
25+
#include <OpenSim/Common/Exception.h>
2526

2627
#include <algorithm> // std::transform
2728
#include <limits> // std::numeric_limits
@@ -309,24 +310,43 @@ TEST_CASE("detectDelimiter") {
309310
}
310311

311312
TEST_CASE("choose") {
312-
REQUIRE(choose(5, 0) == 1);
313-
REQUIRE(choose(5, 1) == 5);
314-
REQUIRE(choose(5, 2) == 10);
315-
REQUIRE(choose(5, 3) == 10);
316-
REQUIRE(choose(5, 4) == 5);
317-
REQUIRE(choose(5, 5) == 1);
318-
319-
REQUIRE(choose(10, 0) == 1);
320-
REQUIRE(choose(10, 1) == 10);
321-
REQUIRE(choose(10, 2) == 45);
322-
REQUIRE(choose(10, 3) == 120);
323-
REQUIRE(choose(10, 4) == 210);
324-
REQUIRE(choose(10, 5) == 252);
325-
326-
REQUIRE(choose(50, 0) == 1);
327-
REQUIRE(choose(50, 1) == 50);
328-
REQUIRE(choose(50, 2) == 1225);
329-
REQUIRE(choose(50, 3) == 19600);
330-
REQUIRE(choose(50, 4) == 230300);
331-
REQUIRE(choose(50, 5) == 2118760);
313+
314+
using Catch::Matchers::ContainsSubstring;
315+
316+
SECTION("Invalid inputs") {
317+
CHECK_THROWS_WITH(choose(-1, 0),
318+
ContainsSubstring("n must be non-negative"));
319+
CHECK_THROWS_WITH(choose(5, -1),
320+
ContainsSubstring("k must be between 0 and n"));
321+
CHECK_THROWS_WITH(choose(5, 6),
322+
ContainsSubstring("k must be between 0 and n"));
323+
}
324+
325+
SECTION("Valid inputs") {
326+
CHECK(choose(5, 0) == 1);
327+
CHECK(choose(5, 1) == 5);
328+
CHECK(choose(5, 2) == 10);
329+
CHECK(choose(5, 3) == 10);
330+
CHECK(choose(5, 4) == 5);
331+
CHECK(choose(5, 5) == 1);
332+
333+
CHECK(choose(10, 0) == 1);
334+
CHECK(choose(10, 1) == 10);
335+
CHECK(choose(10, 2) == 45);
336+
CHECK(choose(10, 3) == 120);
337+
CHECK(choose(10, 4) == 210);
338+
CHECK(choose(10, 5) == 252);
339+
340+
CHECK(choose(25, 0) == 1);
341+
CHECK(choose(25, 1) == 25);
342+
CHECK(choose(25, 2) == 300);
343+
CHECK(choose(25, 3) == 2300);
344+
CHECK(choose(25, 4) == 12650);
345+
CHECK(choose(25, 5) == 53130);
346+
}
347+
348+
SECTION("Overflow checking") {
349+
CHECK_THROWS_WITH(choose(150, 100), ContainsSubstring("overflow"));
350+
CHECK_THROWS_WITH(choose(1000, 500), ContainsSubstring("overflow"));
351+
}
332352
}

0 commit comments

Comments
 (0)