From 3d0e13aedeef052387bca51b2cca9b9c69ac7088 Mon Sep 17 00:00:00 2001 From: Ayden Marshall Date: Fri, 2 Dec 2022 16:24:48 -0500 Subject: [PATCH] Run Length Encoding Extra Credit --- src/include/run_length_encode_string.h | 11 ++++++ src/run_length_encode_string.cc | 39 +++++++++++++++++++ test/unit/run_length_encode_test.cc | 54 ++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/include/run_length_encode_string.h create mode 100644 src/run_length_encode_string.cc create mode 100644 test/unit/run_length_encode_test.cc diff --git a/src/include/run_length_encode_string.h b/src/include/run_length_encode_string.h new file mode 100644 index 0000000..6473cfc --- /dev/null +++ b/src/include/run_length_encode_string.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include +#include + +namespace buzzdb{ + +std::string encode_string(std::string str); + +} // namespace buzzdb \ No newline at end of file diff --git a/src/run_length_encode_string.cc b/src/run_length_encode_string.cc new file mode 100644 index 0000000..e993dd8 --- /dev/null +++ b/src/run_length_encode_string.cc @@ -0,0 +1,39 @@ +#include +#include + +namespace buzzdb { + +#define NULL_CHAR '\0' + +std::string encode_string(std::string str) { + if (str.size() == 0) { + return str; + } + + std::string str_encoded; + + size_t curr_run_size = 0; + char curr_run_char = NULL_CHAR; + + for (size_t i = 0; i < str.size(); i++) { + if (curr_run_size == 0 || str[i] != curr_run_char) { + + // If not just starting to encode, add current run size to back of string and end current run + if (curr_run_size != NULL_CHAR) str_encoded += std::to_string(curr_run_size); + + // Start new run and add the new run char to back of string + curr_run_char = str[i]; + curr_run_size = 0; + str_encoded.push_back(curr_run_char); + } + + curr_run_size++; + } + + // We have reached end of string. Add the curr run size to back and return! + str_encoded += std::to_string(curr_run_size); + + return str_encoded; +} + +} // namespace buzzdb \ No newline at end of file diff --git a/test/unit/run_length_encode_test.cc b/test/unit/run_length_encode_test.cc new file mode 100644 index 0000000..266dc1c --- /dev/null +++ b/test/unit/run_length_encode_test.cc @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "run_length_encode_string.h" + +namespace { + +TEST(RunLengthEncodeStringTest, EmptyString) { + EXPECT_EQ(buzzdb::encode_string(""), ""); +} + +TEST(RunLengthEncodeStringTest, SingleCharOne) { + EXPECT_EQ(buzzdb::encode_string("A"), "A1"); +} + +TEST(RunLengthEncodeStringTest, SingleCharTwo) { + EXPECT_EQ(buzzdb::encode_string("AA"), "A2"); +} + +TEST(RunLengthEncodeStringTest, DoubleCharOne) { + EXPECT_EQ(buzzdb::encode_string("AB"), "A1B1"); +} + +TEST(RunLengthEncodeStringTest, DoubleCharTwo) { + EXPECT_EQ(buzzdb::encode_string("AABB"), "A2B2"); +} + +TEST(RunLengthEncodeStringTest, FlipFlop) { + EXPECT_EQ(buzzdb::encode_string("ABAB"), "A1B1A1B1"); +} + +TEST(RunLengthEncodeStringTest, LongRun) { + std::string long_run ("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW"); + std::string long_run_exp ("W12B1W12B3W24B1W14"); + EXPECT_EQ(buzzdb::encode_string(long_run), long_run_exp); +} + +TEST(RunLengthEncodeStringTest, MultipleCharLong) { + EXPECT_EQ(buzzdb::encode_string("aaaabbcddddd"), "a4b2c1d5"); +} + + +} // namespace + +int main(int argc, char *argv[]) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file