Skip to content

Commit

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

@printf "\n"
gcovr --print-summary
7 changes: 6 additions & 1 deletion cpp/reverse-string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ output: "looc"

### Based on

Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
17 changes: 17 additions & 0 deletions cpp/reverse-string/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/reverse-string/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/reverse-string.dir/reverse_string_test.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/reverse_string_test.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/reverse_string_test.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/reverse-string.dir/reverse_string.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/reverse_string.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/reverse_string.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/reverse-string.dir/test/tests-main.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/test/tests-main.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/reverse-string/test/tests-main.cpp"
}
]
8 changes: 8 additions & 0 deletions cpp/reverse-string/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
17 changes: 16 additions & 1 deletion cpp/reverse-string/reverse_string.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#include "reverse_string.h"

#include <iterator>
#include <string>

namespace reverse_string {

} // namespace reverse_string
std::string reverse_string(std::string text) {
std::string reversed;

if (text.empty()) {
return reversed;
}

std::copy(text.rbegin(), text.rend(), std::back_inserter(reversed));

return reversed;
}

} // namespace reverse_string
11 changes: 9 additions & 2 deletions cpp/reverse-string/reverse_string.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#if !defined(REVERSE_STRING_H)
#define REVERSE_STRING_H

// Stop using C headers in C++ projects that have no C code!!!!!!

#include <iterator>
#include <string>

namespace reverse_string {

} // namespace reverse_string
std::string reverse_string(std::string text);

} // namespace reverse_string

#endif // REVERSE_STRING_H
#endif // REVERSE_STRING_H
14 changes: 14 additions & 0 deletions cpp/reverse-string/reverse_string.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: 6 additions & 11 deletions cpp/reverse-string/reverse_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,24 @@
#include "test/catch.hpp"
#endif

TEST_CASE("an_empty_string")
{
TEST_CASE("an_empty_string") {
REQUIRE("" == reverse_string::reverse_string(""));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("a_word")
{
TEST_CASE("a_word") {
REQUIRE("tobor" == reverse_string::reverse_string("robot"));
}

TEST_CASE("a_capitalized_word")
{
TEST_CASE("a_capitalized_word") {
REQUIRE("nemaR" == reverse_string::reverse_string("Ramen"));
}

TEST_CASE("a_sentence_with_punctuation")
{
TEST_CASE("a_sentence_with_punctuation") {
REQUIRE("!yrgnuh m'I" == reverse_string::reverse_string("I'm hungry!"));
}

TEST_CASE("a_palindrome")
{
REQUIRE("racecar" == reverse_string::reverse_string("racecar"));
TEST_CASE("a_palindrome") {
REQUIRE("racecar" == reverse_string::reverse_string("racecar"));
}
#endif
14 changes: 14 additions & 0 deletions cpp/reverse-string/reverse_string_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 9440c6a

Please sign in to comment.