Skip to content

Commit

Permalink
implementing data conversion (ProAlgos#99)
Browse files Browse the repository at this point in the history
- Make upper case using bit manipulation with unit tests
- Make lower case using bit manipulation with unit tests
  • Loading branch information
0xWaleed committed Dec 14, 2020
1 parent 4b8b7ae commit 89c9c05
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,10 @@ add_executable(fenwick_tree
test/data_structure/tree/fenwick_tree.cpp)
target_link_libraries(fenwick_tree test_runner)

# ============================================================================
# Utilities
# ============================================================================

add_executable(data_conversion
test/utils/data_conversion.cpp)
target_link_libraries(data_conversion test_runner)
58 changes: 58 additions & 0 deletions cpp/include/utils/data_conversion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Data Conversion
-------------------
Upper/Lower case algorithm using bit manipulation.
Time complexity
---------------
O(N), where N is the length of the input.
Space complexity
----------------
O(N), where N is the result.
*/

#ifndef DATA_CONVERSION_HPP
#define DATA_CONVERSION_HPP

#include <string>

#define UPPER_CASE_BIT_MASK 0b11011111
#define LOWER_CASE_BIT_MASK 0b00100000

bool is_letter(const char& c) {
char lower_cased_char = (char)(c | LOWER_CASE_BIT_MASK);
return lower_cased_char >= 'a' && lower_cased_char <= 'z';
}

std::string make_upper_case(const std::string& input) {
std::string result;
for (auto& c: input) {

if (is_letter(c)) {
result += c & UPPER_CASE_BIT_MASK;
}
else {
result += c;
}

}
return result;
}

std::string make_lower_case(const std::string& input) {
std::string result;
for (auto& current_char: input) {

if (is_letter(current_char)) {
result += current_char | LOWER_CASE_BIT_MASK;
}
else {
result += current_char;
}

}
return result;
}

#endif //DATA_CONVERSION_HPP
31 changes: 31 additions & 0 deletions cpp/test/utils/data_conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "third_party/catch.hpp"
#include "utils/data_conversion.hpp"

#define NAME_SPACE "[utils][data-conversion][make_upper_case]"

TEST_CASE("Base cases", NAME_SPACE) {
REQUIRE(make_upper_case(" ") == " ");
REQUIRE(make_upper_case("1234567890") == "1234567890");
REQUIRE(make_upper_case("!@#$%^&*()_+±~<>.,|':;") == "!@#$%^&*()_+±~<>.,|':;");
REQUIRE(make_lower_case(" ") == " ");
REQUIRE(make_lower_case("1234567890") == "1234567890");
REQUIRE(make_lower_case("!@#$%^&*()_+±~<>.,|':;") == "!@#$%^&*()_+±~<>.,|':;");
}

TEST_CASE("Convert to upper case", NAME_SPACE) {
REQUIRE(make_upper_case("hello") == "HELLO");
REQUIRE(make_upper_case("world") == "WORLD");
REQUIRE(make_upper_case("GITHUB") == "GITHUB");
REQUIRE(make_upper_case("Cpp") == "CPP");
REQUIRE(make_upper_case("Hello World!") == "HELLO WORLD!");
REQUIRE(make_upper_case("Hello\nWorld!") == "HELLO\nWORLD!");
}

TEST_CASE("Convert to lower case", NAME_SPACE) {
REQUIRE(make_lower_case("HELLO") == "hello");
REQUIRE(make_lower_case("WORLD") == "world");
REQUIRE(make_lower_case("github") == "github");
REQUIRE(make_lower_case("Cpp") == "cpp");
REQUIRE(make_lower_case("Hello World!") == "hello world!");
REQUIRE(make_lower_case("Hello\nWorld!") == "hello\nworld!");
}

0 comments on commit 89c9c05

Please sign in to comment.