Skip to content

Commit af6faab

Browse files
authored
chore: update test cases from problem specifications for prime-factors (#895)
- Updated all the test cases including new ones added - Updated examples to also use `long long` instead of `int` [no important files changed]
1 parent 987f21d commit af6faab

File tree

5 files changed

+94
-56
lines changed

5 files changed

+94
-56
lines changed

exercises/practice/prime-factors/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"jackhughesweb",
99
"KevinWMatthews",
1010
"kytrinyx",
11-
"patricksjackson"
11+
"patricksjackson",
12+
"JagritGumber"
1213
],
1314
"files": {
1415
"solution": [
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
#include "prime_factors.h"
21
#include <cmath>
32

4-
namespace prime_factors
5-
{
3+
#include "prime_factors.h"
4+
5+
namespace prime_factors {
66

7-
std::vector<int> of(int n)
8-
{
9-
std::vector<int> factors;
10-
const int end{static_cast<int>(std::sqrt(n))};
11-
for (int candidate{2}; candidate <= end; ++candidate) {
7+
std::vector<long long> of(long long n) {
8+
std::vector<long long> factors;
9+
const long long end{static_cast<long long>(std::sqrt(n))};
10+
for (long long candidate{2}; candidate <= end; ++candidate) {
1211
while (n % candidate == 0) {
1312
factors.push_back(candidate);
1413
n /= candidate;
@@ -21,4 +20,4 @@ std::vector<int> of(int n)
2120
return factors;
2221
}
2322

24-
}
23+
} // namespace prime_factors

exercises/practice/prime-factors/.meta/example.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
#include <vector>
55

6-
namespace prime_factors
7-
{
6+
namespace prime_factors {
87

9-
std::vector<int> of(int n);
8+
std::vector<long long> of(long long n);
109

1110
}
1211

exercises/practice/prime-factors/.meta/tests.toml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

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

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

18+
[238d57c8-4c12-42ef-af34-ae4929f94789]
19+
description = "another prime number"
20+
1121
[f59b8350-a180-495a-8fb1-1712fbee1158]
1222
description = "square of a prime"
1323

24+
[756949d3-3158-4e3d-91f2-c4f9f043ee70]
25+
description = "product of first prime"
26+
1427
[bc8c113f-9580-4516-8669-c5fc29512ceb]
1528
description = "cube of a prime"
1629

30+
[7d6a3300-a4cb-4065-bd33-0ced1de6cb44]
31+
description = "product of second prime"
32+
33+
[073ac0b2-c915-4362-929d-fc45f7b9a9e4]
34+
description = "product of third prime"
35+
36+
[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75]
37+
description = "product of first and second prime"
38+
1739
[00485cd3-a3fe-4fbe-a64a-a4308fc1f870]
1840
description = "product of primes and non-primes"
1941

exercises/practice/prime-factors/prime_factors_test.cpp

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,111 @@
55
#include "test/catch.hpp"
66
#endif
77

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

12-
const std::vector<int> actual{prime_factors::of(1)};
11+
const std::vector<long long> actual{prime_factors::of(1)};
1312

1413
REQUIRE(expected == actual);
1514
}
1615

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

22-
const std::vector<int> actual{prime_factors::of(2)};
20+
const std::vector<long long> actual{prime_factors::of(2)};
2321

2422
REQUIRE(expected == actual);
2523
}
2624

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

31-
const std::vector<int> actual{prime_factors::of(3)};
29+
const std::vector<long long> actual{prime_factors::of(3)};
3230

3331
REQUIRE(expected == actual);
3432
}
3533

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

40-
const std::vector<int> actual{prime_factors::of(4)};
38+
const std::vector<long long> actual{prime_factors::of(9)};
4139

4240
REQUIRE(expected == actual);
4341
}
4442

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

49-
const std::vector<int> actual{prime_factors::of(6)};
47+
const std::vector<long long> actual{prime_factors::of(4)};
5048

5149
REQUIRE(expected == actual);
5250
}
5351

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

58-
const std::vector<int> actual{prime_factors::of(8)};
56+
const std::vector<long long> actual{prime_factors::of(8)};
5957

6058
REQUIRE(expected == actual);
6159
}
6260

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

67-
const std::vector<int> actual{prime_factors::of(9)};
65+
const std::vector<long long> actual{prime_factors::of(27)};
6866

6967
REQUIRE(expected == actual);
7068
}
7169

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

76-
const std::vector<int> actual{prime_factors::of(27)};
74+
const std::vector<long long> actual{prime_factors::of(625)};
7775

7876
REQUIRE(expected == actual);
7977
}
8078

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

85-
const std::vector<int> actual{prime_factors::of(625)};
83+
const std::vector<long long> actual{prime_factors::of(6)};
8684

8785
REQUIRE(expected == actual);
8886
}
8987

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

94-
const std::vector<int> actual{prime_factors::of(901255)};
92+
const std::vector<long long> actual{prime_factors::of(12)};
9593

9694
REQUIRE(expected == actual);
9795
}
96+
97+
TEST_CASE("product of primes",
98+
"[factors][02251d54-3ca1-4a9b-85e1-b38f4b0ccb91]") {
99+
const std::vector<long long> expected{5, 17, 23, 461};
100+
101+
const std::vector<long long> actual{prime_factors::of(901255)};
102+
103+
REQUIRE(expected == actual);
104+
}
105+
106+
TEST_CASE("factors include a large prime",
107+
"[factors][070cf8dc-e202-4285-aa37-8d775c9cd473]") {
108+
const std::vector<long long> expected{11, 9539, 894119};
109+
110+
const std::vector<long long> actual{prime_factors::of(93819012551)};
111+
112+
REQUIRE(expected == actual);
113+
}
114+
98115
#endif

0 commit comments

Comments
 (0)