Skip to content

Commit

Permalink
c++/armstrong-numbers: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Jul 17, 2023
1 parent 42f1655 commit b975189
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 49 deletions.
1 change: 1 addition & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
- [darts](./darts/README.md)
- [interest-is-interesting](./interest-is-interesting/README.md)
- [reverse-string](./reverse-string/README.md)
- [armstrong-numbers](./armstrong-numbers/README.md)
56 changes: 30 additions & 26 deletions cpp/armstrong-numbers/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/armstrong-numbers/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)27066"

@printf "\n"
gcovr --print-summary
7 changes: 6 additions & 1 deletion cpp/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ Write some code to determine whether a number is an Armstrong number.

### Based on

Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number
Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
26 changes: 25 additions & 1 deletion cpp/armstrong-numbers/armstrong_numbers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
#include "armstrong_numbers.h"
#include <cmath>

namespace armstrong_numbers {

} // namespace armstrong_numbers
bool is_armstrong_number(int candidate) {
if (candidate < 10) {
return true;
}

const int digit_count = (int)std::log10(candidate) + 1;
int num = candidate;
int pow_total = 0;

while (num > 0) {
const int pow_temp = num % 10;
int pow_temp_total = 1;

for (int i = 0; i < digit_count; i++) {
pow_temp_total *= pow_temp;
}
pow_total += pow_temp_total;
num /= 10;
}

return pow_total == candidate;
}

} // namespace armstrong_numbers
8 changes: 6 additions & 2 deletions cpp/armstrong-numbers/armstrong_numbers.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#if !defined(ARMSTRONG_NUMBERS_H)
#define ARMSTRONG_NUMBERS_H

// the C++ version is set to 17, why are you using C header files!

namespace armstrong_numbers {

} // namespace armstrong_numbers
bool is_armstrong_number(int candidate);

} // namespace armstrong_numbers

#endif // ARMSTRONG_NUMBERS_H
#endif // ARMSTRONG_NUMBERS_H
14 changes: 14 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers.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>
29 changes: 10 additions & 19 deletions cpp/armstrong-numbers/armstrong_numbers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,40 @@

// Armstrong-numbers exercise test case data version 1.1.0

TEST_CASE("zero_is_an_armstrong_number")
{
TEST_CASE("zero_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(0));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("single_digit_numbers_are_armstrong_numbers")
{
TEST_CASE("single_digit_numbers_are_armstrong_numbers") {
REQUIRE(armstrong_numbers::is_armstrong_number(5));
}

TEST_CASE("there_are_no_2_digit_armstrong_numbers")
{
TEST_CASE("there_are_no_2_digit_armstrong_numbers") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(10));
}

TEST_CASE("three_digit_number_that_is_an_armstrong_number")
{
TEST_CASE("three_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(153));
}

TEST_CASE("three_digit_number_that_is_not_an_armstrong_number")
{
TEST_CASE("three_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(100));
}

TEST_CASE("four_digit_number_that_is_an_armstrong_number")
{
TEST_CASE("four_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(9474));
}

TEST_CASE("four_digit_number_that_is_not_an_armstrong_number")
{
TEST_CASE("four_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9475));
}

TEST_CASE("seven_digit_number_that_is_an_armstrong_number")
{
TEST_CASE("seven_digit_number_that_is_an_armstrong_number") {
REQUIRE(armstrong_numbers::is_armstrong_number(9926315));
}

TEST_CASE("seven_digit_number_that_is_not_an_armstrong_number")
{
TEST_CASE("seven_digit_number_that_is_not_an_armstrong_number") {
REQUIRE_FALSE(armstrong_numbers::is_armstrong_number(9926314));
}
#endif // !EXERCISM_RUN_ALL_TESTS
#endif // !EXERCISM_RUN_ALL_TESTS
14 changes: 14 additions & 0 deletions cpp/armstrong-numbers/armstrong_numbers_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>
17 changes: 17 additions & 0 deletions cpp/armstrong-numbers/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/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/armstrong_numbers_test.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers_test.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers_test.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/armstrong_numbers.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/armstrong_numbers.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/armstrong-numbers.dir/test/tests-main.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/test/tests-main.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/armstrong-numbers/test/tests-main.cpp"
}
]
8 changes: 8 additions & 0 deletions cpp/armstrong-numbers/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
Loading

0 comments on commit b975189

Please sign in to comment.