Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Length Encoding Extra Credit #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/include/run_length_encode_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cstdint>
#include <iostream>
#include <string>

namespace buzzdb{

std::string encode_string(std::string str);

} // namespace buzzdb
39 changes: 39 additions & 0 deletions src/run_length_encode_string.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <string>

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
54 changes: 54 additions & 0 deletions test/unit/run_length_encode_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <exception>
#include <random>
#include <utility>
#include <vector>

#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();
}