Skip to content

Commit

Permalink
c++/prime-factors: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Jul 22, 2023
1 parent 5b6125f commit d51d4fd
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 50 deletions.
1 change: 1 addition & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
- [trinary](./trinary/README.md)
- [sieve](./sieve/README.md)
- [nth-prime](./nth-prime/README.md)
- [prime-factors](./prime-factors/README.md)
56 changes: 30 additions & 26 deletions cpp/prime-factors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,57 @@ string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h
$<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already includes a
# pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different header,
# we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h
test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)
set_target_properties(
${exercise}
PROPERTIES CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
set_target_properties(
${exercise} PROPERTIES COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror")
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
set_target_properties(${exercise} PROPERTIES COMPILE_DEFINITIONS_DEBUG
_SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
add_custom_target(
test_${exercise} ALL
DEPENDS ${exercise}
COMMAND ${exercise})
23 changes: 23 additions & 0 deletions cpp/prime-factors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.PHONY: clean
clean:
rm -rf ./build

.PHONY: test
test: clean
mkdir -pv ./build

@printf "\n"
@# each line is executed in a subshell, we have to daisy chain them so they
@# run in the build directory
cd ./build; export LDFLAGS="-lgcov --coverage" CXXFLAGS="--coverage"; cmake -DEXERCISM_RUN_ALL_TESTS=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G 'Unix Makefiles' ../; if make; then printf "\n=== All Tests Passed ===\n"; else printf "\n=== Test Failure ===\n"; false; fi

.PHONY: coverage
coverage: test
@printf "\n"
find . -regextype posix-egrep -regex "^.*(tests-main|CompilerId).*[.](gcda|gcno)$$" -print -delete

@printf "\n"
find . -regextype posix-egrep -regex "^.*[.](gcda|gcno)2169"

@printf "\n"
gcovr --print-summary
7 changes: 6 additions & 1 deletion cpp/prime-factors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ You can check this yourself:

### Based on

The Prime Factors Kata by Uncle Bob - http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata
The Prime Factors Kata by Uncle Bob - http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
17 changes: 17 additions & 0 deletions cpp/prime-factors/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/prime-factors.dir/prime_factors_test.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/prime_factors_test.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/prime_factors_test.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/prime-factors.dir/prime_factors.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/prime_factors.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/prime_factors.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/prime-factors.dir/test/tests-main.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/test/tests-main.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/prime-factors/test/tests-main.cpp"
}
]
8 changes: 8 additions & 0 deletions cpp/prime-factors/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-std=c++17
-stdlib=libstdc++
-g
-Wall
-Wextra
-Wpedantic
-Werror
-Wno-unused-parameter
37 changes: 36 additions & 1 deletion cpp/prime-factors/prime_factors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,39 @@

namespace prime_factors {

} // namespace prime_factors
// number should be unsigned
std::vector<int> of(int number) {
std::vector<int> factors{};

switch (number) {
case 0:
[[fallthrough]];
case 1:
return factors;
case 2:
factors.push_back(2);
return factors;
}

int factor{0};

factor = 2;
while (number % factor == 0 and number > 1) {
number /= factor;
factors.push_back(factor);
}

factor = 3;
while (number > 1) {
while (number % factor == 0 and number > 1) {
number /= factor;
factors.push_back(factor);
}

factor += 2;
}

return factors;
}

} // namespace prime_factors
10 changes: 8 additions & 2 deletions cpp/prime-factors/prime_factors.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#if !defined(PRIME_FACTORS_H)
#define PRIME_FACTORS_H

// this is not a c++ header file (use *.hpp)

#include <vector>

namespace prime_factors {

} // namespace prime_factors
std::vector<int> of(int number);

} // namespace prime_factors

#endif // PRIME_FACTORS_H
#endif // PRIME_FACTORS_H
14 changes: 14 additions & 0 deletions cpp/prime-factors/prime_factors.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<key>diagnostics</key>
<array>
</array>
<key>files</key>
<array>
</array>
</dict>
</plist>
30 changes: 10 additions & 20 deletions cpp/prime-factors/prime_factors_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include "test/catch.hpp"
#endif

TEST_CASE("_1_yields_empty")
{
TEST_CASE("_1_yields_empty") {
const std::vector<int> expected{};

const std::vector<int> actual{prime_factors::of(1)};
Expand All @@ -15,80 +14,71 @@ TEST_CASE("_1_yields_empty")
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("_2_yields_2")
{
TEST_CASE("_2_yields_2") {
const std::vector<int> expected{2};

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

REQUIRE(expected == actual);
}

TEST_CASE("_3_yields_3")
{
TEST_CASE("_3_yields_3") {
const std::vector<int> expected{3};

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

REQUIRE(expected == actual);
}

TEST_CASE("_4_yields_2_2")
{
TEST_CASE("_4_yields_2_2") {
const std::vector<int> expected{2, 2};

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

REQUIRE(expected == actual);
}

TEST_CASE("_6_yields_2_3")
{
TEST_CASE("_6_yields_2_3") {
const std::vector<int> expected{2, 3};

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

REQUIRE(expected == actual);
}

TEST_CASE("_8_yields_2_2_2")
{
TEST_CASE("_8_yields_2_2_2") {
const std::vector<int> expected{2, 2, 2};

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

REQUIRE(expected == actual);
}

TEST_CASE("_9_yields_3_3")
{
TEST_CASE("_9_yields_3_3") {
const std::vector<int> expected{3, 3};

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

REQUIRE(expected == actual);
}

TEST_CASE("_27_yields_3_3_3")
{
TEST_CASE("_27_yields_3_3_3") {
const std::vector<int> expected{3, 3, 3};

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

REQUIRE(expected == actual);
}

TEST_CASE("_625_yields_5_5_5_5")
{
TEST_CASE("_625_yields_5_5_5_5") {
const std::vector<int> expected{5, 5, 5, 5};

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

REQUIRE(expected == actual);
}

TEST_CASE("_901255_yields_5_17_23_461")
{
TEST_CASE("_901255_yields_5_17_23_461") {
const std::vector<int> expected{5, 17, 23, 461};

const std::vector<int> actual{prime_factors::of(901255)};
Expand Down
14 changes: 14 additions & 0 deletions cpp/prime-factors/prime_factors_test.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<key>diagnostics</key>
<array>
</array>
<key>files</key>
<array>
</array>
</dict>
</plist>
Loading

0 comments on commit d51d4fd

Please sign in to comment.