Skip to content

Commit

Permalink
tests: update perfect numbers exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
vaeng committed Feb 26, 2024
1 parent 563f99c commit 0b34c08
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 56 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/perfect-numbers/.meta/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PERFECT_NUMBERS_H

namespace perfect_numbers {
enum classification { deficient, perfect, abundant };
enum class classification { deficient, perfect, abundant };

classification classify(int n);

Expand Down
61 changes: 40 additions & 21 deletions exercises/practice/perfect-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e46c542b-31fc-4506-bcae-6b62b3268537]
description = "square of sum 1"
[163e8e86-7bfd-4ee2-bd68-d083dc3381a3]
description = "Perfect numbers -> Smallest perfect number is classified correctly"

[9b3f96cb-638d-41ee-99b7-b4f9c0622948]
description = "square of sum 5"
[169a7854-0431-4ae0-9815-c3b6d967436d]
description = "Perfect numbers -> Medium perfect number is classified correctly"

[54ba043f-3c35-4d43-86ff-3a41625d5e86]
description = "square of sum 100"
[ee3627c4-7b36-4245-ba7c-8727d585f402]
description = "Perfect numbers -> Large perfect number is classified correctly"

[01d84507-b03e-4238-9395-dd61d03074b5]
description = "sum of squares 1"
[80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e]
description = "Abundant numbers -> Smallest abundant number is classified correctly"

[c93900cd-8cc2-4ca4-917b-dd3027023499]
description = "sum of squares 5"
[3e300e0d-1a12-4f11-8c48-d1027165ab60]
description = "Abundant numbers -> Medium abundant number is classified correctly"

[94807386-73e4-4d9e-8dec-69eb135b19e4]
description = "sum of squares 100"
[ec7792e6-8786-449c-b005-ce6dd89a772b]
description = "Abundant numbers -> Large abundant number is classified correctly"

[44f72ae6-31a7-437f-858d-2c0837adabb6]
description = "difference of squares 1"
[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
description = "Deficient numbers -> Smallest prime deficient number is classified correctly"

[005cb2bf-a0c8-46f3-ae25-924029f8b00b]
description = "difference of squares 5"
[0beb7f66-753a-443f-8075-ad7fbd9018f3]
description = "Deficient numbers -> Smallest non-prime deficient number is classified correctly"

[b1bf19de-9a16-41c0-a62b-1f02ecc0b036]
description = "difference of squares 100"
[1c802e45-b4c6-4962-93d7-1cad245821ef]
description = "Deficient numbers -> Medium deficient number is classified correctly"

[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]
description = "Deficient numbers -> Large deficient number is classified correctly"

[a696dec8-6147-4d68-afad-d38de5476a56]
description = "Deficient numbers -> Edge case (no factors other than itself) is classified correctly"

[72445cee-660c-4d75-8506-6c40089dc302]
description = "Invalid inputs -> Zero is rejected (as it is not a positive integer)"

[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
description = "Invalid inputs -> Negative integer is rejected (as it is not a positive integer)"
79 changes: 45 additions & 34 deletions exercises/practice/perfect-numbers/perfect_numbers_test.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@

#include "perfect_numbers.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif
#include <exception>
#include <string>
#include <vector>

#include "perfect_numbers.h"
// improves error messages with enum text instead of integers:
CATCH_REGISTER_ENUM(perfect_numbers::classification,
perfect_numbers::classification::perfect,
perfect_numbers::classification::abundant,
perfect_numbers::classification::deficient)

using namespace std;
TEST_CASE("Smallest perfect number is classified correctly") {
REQUIRE(perfect_numbers::classify(6) == perfect_numbers::perfect);
}
TEST_CASE("Smallest abundant number is classified correctly") {
REQUIRE(perfect_numbers::classify(12) == perfect_numbers::abundant);
}
TEST_CASE("Smallest prime deficient number is classified correctly") {
REQUIRE(perfect_numbers::classify(2) == perfect_numbers::deficient);
TEST_CASE("Smallest perfect number is classified correctly", "[163e8e86-7bfd-4ee2-bd68-d083dc3381a3]") {
REQUIRE(perfect_numbers::classification::perfect == perfect_numbers::classify(6));
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("Medium perfect number is classified correctly") {
REQUIRE(perfect_numbers::classify(28) == perfect_numbers::perfect);
TEST_CASE("Medium perfect number is classified correctly", "[169a7854-0431-4ae0-9815-c3b6d967436d]") {
REQUIRE(perfect_numbers::classification::perfect == perfect_numbers::classify(28));
}

TEST_CASE("Large perfect number is classified correctly", "[ee3627c4-7b36-4245-ba7c-8727d585f402]") {
REQUIRE(perfect_numbers::classification::perfect == perfect_numbers::classify(33550336));
}

TEST_CASE("Smallest abundant number is classified correctly", "[80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e]") {
REQUIRE(perfect_numbers::classification::abundant == perfect_numbers::classify(12));
}
TEST_CASE("Large perfect number is classified correctly") {
REQUIRE(perfect_numbers::classify(33550336) == perfect_numbers::perfect);

TEST_CASE("Medium abundant number is classified correctly", "[3e300e0d-1a12-4f11-8c48-d1027165ab60]") {
REQUIRE(perfect_numbers::classification::abundant == perfect_numbers::classify(30));
}
TEST_CASE("Medium abundant number is classified correctly") {
REQUIRE(perfect_numbers::classify(30) == perfect_numbers::abundant);

TEST_CASE("Large abundant number is classified correctly", "[ec7792e6-8786-449c-b005-ce6dd89a772b]") {
REQUIRE(perfect_numbers::classification::abundant == perfect_numbers::classify(33550335));
}
TEST_CASE("Large abundant number is classified correctly") {
REQUIRE(perfect_numbers::classify(33550335) == perfect_numbers::abundant);

TEST_CASE("Smallest prime deficient number is classified correctly", "[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]") {
REQUIRE(perfect_numbers::classification::deficient == perfect_numbers::classify(2));
}
TEST_CASE("Smallest non-prime deficient number is classified correctly") {
REQUIRE(perfect_numbers::classify(4) == perfect_numbers::deficient);

TEST_CASE("Smallest non-prime deficient number is classified correctly", "[0beb7f66-753a-443f-8075-ad7fbd9018f3]") {
REQUIRE(perfect_numbers::classification::deficient == perfect_numbers::classify(4));
}
TEST_CASE("Medium deficient number is classified correctly") {
REQUIRE(perfect_numbers::classify(32) == perfect_numbers::deficient);

TEST_CASE("Medium deficient number is classified correctly", "[1c802e45-b4c6-4962-93d7-1cad245821ef]") {
REQUIRE(perfect_numbers::classification::deficient == perfect_numbers::classify(32));
}
TEST_CASE("Large deficient number is classified correctly") {
REQUIRE(perfect_numbers::classify(33550337) == perfect_numbers::deficient);

TEST_CASE("Large deficient number is classified correctly", "[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]") {
REQUIRE(perfect_numbers::classification::deficient == perfect_numbers::classify(33550337));
}
TEST_CASE("Edge case (no factors other than itself) is classified correctly") {
REQUIRE(perfect_numbers::classify(1) == perfect_numbers::deficient);

TEST_CASE("Edge case (no factors other than itself) is classified correctly", "[a696dec8-6147-4d68-afad-d38de5476a56]") {
REQUIRE(perfect_numbers::classification::deficient == perfect_numbers::classify(1));
}
TEST_CASE("Zero is rejected (as it is not a positive integer)") {
REQUIRE_THROWS_AS(perfect_numbers::classify(0), std::domain_error);

TEST_CASE("Zero is rejected (as it is not a positive integer)", "[72445cee-660c-4d75-8506-6c40089dc302]") {
REQUIRE_THROWS_AS(perfect_numbers::classify(0),std::domain_error);
}
TEST_CASE("Negative numbers are rejected") {
REQUIRE_THROWS_AS(perfect_numbers::classify(-1), std::domain_error);

TEST_CASE("Negative integer is rejected (as it is not a positive integer)", "[2d72ce2c-6802-49ac-8ece-c790ba3dae13]") {
REQUIRE_THROWS_AS(perfect_numbers::classify(-1),std::domain_error);
}

#endif

0 comments on commit 0b34c08

Please sign in to comment.