Skip to content

Commit

Permalink
#17 #18 New branch for extra-libs and first stab at adding abseil
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 committed May 21, 2018
1 parent 0b423d8 commit d4d25b2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "abseil-cpp"]
path = abseil-cpp
url = git@github.com:abseil/abseil-cpp.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ target_include_directories(mylib PRIVATE tests)
target_link_libraries(mytest PRIVATE mylib)
add_test(test1 mytest)

# configure abseil-cpp from submodule
add_subdirectory(abseil-cpp)
target_link_libraries(mylib PRIVATE absl::base absl::strings)

# boost as a required dependency for mylib
find_package(Boost REQUIRED)
target_link_libraries(mylib PRIVATE "${Boost_LIBRARIES}")
Expand Down
1 change: 1 addition & 0 deletions abseil-cpp
Submodule abseil-cpp added at 59ae4d
27 changes: 27 additions & 0 deletions src/MyLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Exception.hpp"

#include <algorithm>

#include <absl/strings/str_join.h>
#include <absl/strings/str_split.h>
#include <boost/math/special_functions/prime.hpp>

namespace cpp_template {
Expand All @@ -55,4 +59,27 @@ int get_nth_prime(int n) {
return static_cast<int>(boost::math::prime(static_cast<unsigned>(n)));
}

std::string rotate_substrings_left(
const std::string& original,
const std::string delimiter,
const long n) {

std::vector<absl::string_view> split = absl::StrSplit(original, delimiter);

if (split.size() < 2) {
return original;
}

const auto pivot = std::abs(n) % split.size();

if (n > 0) {
std::rotate(std::begin(split), std::begin(split) + pivot, std::end(split));
} else {
std::rotate(std::rbegin(split), std::rbegin(split) + pivot, std::rend(split));
}

return absl::StrJoin(split, delimiter);
}


} // namespace cpp_template
4 changes: 4 additions & 0 deletions src/MyLibrary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_

#include <string>

namespace cpp_template {
int get_nth_prime(int n);
std::string rotate_substrings_left(
const std::string& original, std::string delimiter, long n);
}

#endif // MYLIBRARY_H_
17 changes: 17 additions & 0 deletions tests/MyTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,20 @@ TEST_CASE("correct out of range exceptions", "[primes]") {
CHECK_THROWS_AS(get_nth_prime(-1), Exception);
CHECK_THROWS_AS(get_nth_prime(std::numeric_limits<int>::max()), Exception);
}

// This tests the output of the `rotate_substrings_left` function
TEST_CASE("correct strings are returned", "[substrings]") {
const std::string input = "a,b,c,d,e,f,g,h,i,j";

CHECK(rotate_substrings_left(input, ",", 0) == input); // no rotation
CHECK(rotate_substrings_left(input, ",", 10) == input); // complete rotation left
CHECK(rotate_substrings_left(input, ",", -10) == input); // complete rotation right

CHECK(rotate_substrings_left(input, ",", 1) == "b,c,d,e,f,g,h,i,j,a"); // one left
CHECK(rotate_substrings_left(input, ",", -1) == "j,a,b,c,d,e,f,g,h,i"); // one right
CHECK(rotate_substrings_left(rotate_substrings_left(input, ",", 1), ",", -1) == input); // left then right
CHECK(rotate_substrings_left(rotate_substrings_left(input, ",", -1), ",", 1) == input); // right then left

CHECK(rotate_substrings_left(input, ",", 15) == "f,g,h,i,j,a,b,c,d,e"); // 15 left (=5 left)
CHECK(rotate_substrings_left(input, ",", -13) == "h,i,j,a,b,c,d,e,f,g"); // 13 right (=3 right)
}

0 comments on commit d4d25b2

Please sign in to comment.