Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UPDATE test cases from problem specifications for prime-factors #895

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/practice/prime-factors/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"jackhughesweb",
"KevinWMatthews",
"kytrinyx",
"patricksjackson"
"patricksjackson",
"JagritGumber"
],
"files": {
"solution": [
Expand Down
17 changes: 8 additions & 9 deletions exercises/practice/prime-factors/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "prime_factors.h"
#include <cmath>

namespace prime_factors
{
#include "prime_factors.h"

namespace prime_factors {

std::vector<int> of(int n)
{
std::vector<int> factors;
const int end{static_cast<int>(std::sqrt(n))};
for (int candidate{2}; candidate <= end; ++candidate) {
std::vector<long long> of(long long n) {
std::vector<long long> factors;
const long long end{static_cast<long long>(std::sqrt(n))};
for (long long candidate{2}; candidate <= end; ++candidate) {
while (n % candidate == 0) {
factors.push_back(candidate);
n /= candidate;
Expand All @@ -21,4 +20,4 @@ std::vector<int> of(int n)
return factors;
}

}
} // namespace prime_factors
5 changes: 2 additions & 3 deletions exercises/practice/prime-factors/.meta/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

#include <vector>

namespace prime_factors
{
namespace prime_factors {

std::vector<int> of(int n);
std::vector<long long> of(long long n);

}

Expand Down
28 changes: 25 additions & 3 deletions exercises/practice/prime-factors/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
# 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.

[924fc966-a8f5-4288-82f2-6b9224819ccd]
description = "no factors"

[17e30670-b105-4305-af53-ddde182cb6ad]
description = "prime number"

[238d57c8-4c12-42ef-af34-ae4929f94789]
description = "another prime number"

[f59b8350-a180-495a-8fb1-1712fbee1158]
description = "square of a prime"

[756949d3-3158-4e3d-91f2-c4f9f043ee70]
description = "product of first prime"

[bc8c113f-9580-4516-8669-c5fc29512ceb]
description = "cube of a prime"

[7d6a3300-a4cb-4065-bd33-0ced1de6cb44]
description = "product of second prime"

[073ac0b2-c915-4362-929d-fc45f7b9a9e4]
description = "product of third prime"

[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]
description = "product of first and second prime"

[00485cd3-a3fe-4fbe-a64a-a4308fc1f870]
description = "product of primes and non-primes"

Expand Down
97 changes: 57 additions & 40 deletions exercises/practice/prime-factors/prime_factors_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,111 @@
#include "test/catch.hpp"
#endif

TEST_CASE("_1_yields_empty")
{
const std::vector<int> expected{};
TEST_CASE("no factors", "[factors][924fc966-a8f5-4288-82f2-6b9224819ccd]") {
const std::vector<long long> expected{};

const std::vector<int> actual{prime_factors::of(1)};
const std::vector<long long> actual{prime_factors::of(1)};

REQUIRE(expected == actual);
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("_2_yields_2")
{
const std::vector<int> expected{2};
TEST_CASE("prime number", "[factors][17e30670-b105-4305-af53-ddde182cb6ad]") {
const std::vector<long long> expected{2};

const std::vector<int> actual{prime_factors::of(2)};
const std::vector<long long> actual{prime_factors::of(2)};

REQUIRE(expected == actual);
}

TEST_CASE("_3_yields_3")
{
const std::vector<int> expected{3};
TEST_CASE("another prime number",
"[factors][238d57c8-4c12-42ef-af34-ae4929f94789]") {
const std::vector<long long> expected{3};

const std::vector<int> actual{prime_factors::of(3)};
const std::vector<long long> actual{prime_factors::of(3)};

REQUIRE(expected == actual);
}

TEST_CASE("_4_yields_2_2")
{
const std::vector<int> expected{2, 2};
TEST_CASE("square of a prime",
"[factors][f59b8350-a180-495a-8fb1-1712fbee1158]") {
const std::vector<long long> expected{3, 3};

const std::vector<int> actual{prime_factors::of(4)};
const std::vector<long long> actual{prime_factors::of(9)};

REQUIRE(expected == actual);
}

TEST_CASE("_6_yields_2_3")
{
const std::vector<int> expected{2, 3};
TEST_CASE("product of first prime",
"[factors][756949d3-3158-4e3d-91f2-c4f9f043ee70]") {
const std::vector<long long> expected{2, 2};

const std::vector<int> actual{prime_factors::of(6)};
const std::vector<long long> actual{prime_factors::of(4)};

REQUIRE(expected == actual);
}

TEST_CASE("_8_yields_2_2_2")
{
const std::vector<int> expected{2, 2, 2};
TEST_CASE("cube of a prime",
"[factors][bc8c113f-9580-4516-8669-c5fc29512ceb]") {
const std::vector<long long> expected{2, 2, 2};

const std::vector<int> actual{prime_factors::of(8)};
const std::vector<long long> actual{prime_factors::of(8)};

REQUIRE(expected == actual);
}

TEST_CASE("_9_yields_3_3")
{
const std::vector<int> expected{3, 3};
TEST_CASE("product of second prime",
"[factors][7d6a3300-a4cb-4065-bd33-0ced1de6cb44]") {
const std::vector<long long> expected{3, 3, 3};

const std::vector<int> actual{prime_factors::of(9)};
const std::vector<long long> actual{prime_factors::of(27)};

REQUIRE(expected == actual);
}

TEST_CASE("_27_yields_3_3_3")
{
const std::vector<int> expected{3, 3, 3};
TEST_CASE("product of third prime",
"[factors][073ac0b2-c915-4362-929d-fc45f7b9a9e4]") {
const std::vector<long long> expected{5, 5, 5, 5};

const std::vector<int> actual{prime_factors::of(27)};
const std::vector<long long> actual{prime_factors::of(625)};

REQUIRE(expected == actual);
}

TEST_CASE("_625_yields_5_5_5_5")
{
const std::vector<int> expected{5, 5, 5, 5};
TEST_CASE("product of first and second prime",
"[factors][6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]") {
const std::vector<long long> expected{2, 3};

const std::vector<int> actual{prime_factors::of(625)};
const std::vector<long long> actual{prime_factors::of(6)};

REQUIRE(expected == actual);
}

TEST_CASE("_901255_yields_5_17_23_461")
{
const std::vector<int> expected{5, 17, 23, 461};
TEST_CASE("product of primes and non-primes",
"[factors][00485cd3-a3fe-4fbe-a64a-a4308fc1f870]") {
const std::vector<long long> expected{2, 2, 3};

const std::vector<int> actual{prime_factors::of(901255)};
const std::vector<long long> actual{prime_factors::of(12)};

REQUIRE(expected == actual);
}

TEST_CASE("product of primes",
"[factors][02251d54-3ca1-4a9b-85e1-b38f4b0ccb91]") {
const std::vector<long long> expected{5, 17, 23, 461};

const std::vector<long long> actual{prime_factors::of(901255)};

REQUIRE(expected == actual);
}

TEST_CASE("factors include a large prime",
"[factors][070cf8dc-e202-4285-aa37-8d775c9cd473]") {
const std::vector<long long> expected{11, 9539, 894119};

const std::vector<long long> actual{prime_factors::of(93819012551)};

REQUIRE(expected == actual);
}

#endif