From 3ecb7aab28ee627744b39db370f266a2d887e4c6 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sat, 30 May 2020 03:10:08 +0530 Subject: [PATCH 1/8] Data validation utility function --- C++/include/utils/data_validation.hpp | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 C++/include/utils/data_validation.hpp diff --git a/C++/include/utils/data_validation.hpp b/C++/include/utils/data_validation.hpp new file mode 100644 index 00000000..f36c6632 --- /dev/null +++ b/C++/include/utils/data_validation.hpp @@ -0,0 +1,64 @@ +/* + Data validation + --------------- + This utility function contains two sub-funtions : + + 1) make_upper_case() : converts the characters in a string to upper case . + + 2) make_lower_case() : converts the characters in a string to lower case . + + Time complexity + --------------- + O(n), where n is the length of the string . +*/ + +#ifndef DATA_VALIDATION_HPP +#define DATA_VALIDATION_HPP + +#include + +using std::string; + +/* + make_upper_case() + ----------------- + This function returns a given string into upper case . + Loop invariant : Selects each character and converts it to upper case . + + Return value + ------------ + string having all the characters in upper case . +*/ + +string make_upper_case(string str) +{ + //converting each character to upper case using for loop + for (auto &c : str) + { + c = std::toupper(c); // using the function toupper() for conversion + } + return str; // returning the converted string +} + +/* + make_lower_case() + ----------------- + This function returns a give string into lower case . + Loop invariant : Selects each character and converts it to lower case . + + Return value + ------------ + string having all the characters in lower case . +*/ + +string make_lower_case(string str) +{ + // converting each character to lower case using for loop + for (auto &c : str) + { + c = std::tolower(c); //using the function tolower() for conversion + } + return str; //returning the converted string +} + +#endif // DATA_VALIDATION_HPP \ No newline at end of file From be5bc1c1f4d2a3676add853d776bd15094733ac3 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sat, 30 May 2020 03:18:23 +0530 Subject: [PATCH 2/8] Tests for the data validation function --- C++/test/utils/data_validation.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 C++/test/utils/data_validation.cpp diff --git a/C++/test/utils/data_validation.cpp b/C++/test/utils/data_validation.cpp new file mode 100644 index 00000000..6e8c16e2 --- /dev/null +++ b/C++/test/utils/data_validation.cpp @@ -0,0 +1,20 @@ +#include "third_party/catch.hpp" +#include "utils/data_validation.hpp" + +TEST_CASE("Base cases", "[string][data_validation]") +{ + REQUIRE(make_upper_case("cats") == "CATS"); + REQUIRE(make_upper_case("dogs") == "DOGS"); + REQUIRE(make_lower_case("CATS") == "cats"); + REQUIRE(make_lower_case("dogs") == "DOGS"); +} + +TEST_CASE("Mixed cases", "[string][data-validation]") +{ + REQUIRE(make_upper_case("cAts") == "CATS"); + REQUIRE(make_upper_case("dOgs") == "DOGS"); + REQUIRE(make_upper_case("dOGS") == "DOGS"); + REQUIRE(make_lower_case("CaTS") == "cats"); + REQUIRE(make_lower_case("DoGS") == "dogs"); + REQUIRE(make_lower_case("dogS") == "DOGS"); +} From e532cb2df910c0cdbe13b9c0f358ef82ddf248b0 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sat, 30 May 2020 20:12:21 +0530 Subject: [PATCH 3/8] Modified the code with the given changes --- C++/include/utils/data_validation.hpp | 42 +++++++++++++++++++-------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/C++/include/utils/data_validation.hpp b/C++/include/utils/data_validation.hpp index f36c6632..bc8a0ab4 100644 --- a/C++/include/utils/data_validation.hpp +++ b/C++/include/utils/data_validation.hpp @@ -16,6 +16,11 @@ #define DATA_VALIDATION_HPP #include +#define UP_A 65 +#define UP_Z 91 + +#define LOW_A 97 +#define LOW_Z 122 using std::string; @@ -23,23 +28,29 @@ using std::string; make_upper_case() ----------------- This function returns a given string into upper case . - Loop invariant : Selects each character and converts it to upper case . + Loop invariant : Iterates through every charachter of the string and convert it to upper case Return value ------------ string having all the characters in upper case . */ -string make_upper_case(string str) +std::string make_upper_case(string str) { - //converting each character to upper case using for loop - for (auto &c : str) + std::string upper; // defined a string variable to store the convereted string + for (size_t i = 0; i < str.length(); i++) { - c = std::toupper(c); // using the function toupper() for conversion + if (str.at(i) >= LOW_A && str.at(i) <= LOW_Z) // checking for the characters that lie between the ASCII values + { + upper += (str.at(i) - 32); // subtracting 32 to get the character in the range 65 - 97 + } + else + { + upper += str.at(i); + } } - return str; // returning the converted string + return upper; // returning the converted string to upper case } - /* make_lower_case() ----------------- @@ -51,14 +62,21 @@ string make_upper_case(string str) string having all the characters in lower case . */ -string make_lower_case(string str) +std::string make_lower_case(string str) { - // converting each character to lower case using for loop - for (auto &c : str) + std::string lower; // defined a variable to store the converted the string + for (size_t i = 0; i < str.length(); i++) { - c = std::tolower(c); //using the function tolower() for conversion + if (str.at(i) >= UP_A && str.at(i) <= UP_Z) // condition to check if the characters lie in the given ASCII range + { + lower += (str.at(i) + 32); // adding 32 to convert the characters in lower case + } + else + { + lower += str.at(i); + } } - return str; //returning the converted string + return lower; // returning the given string in lower case } #endif // DATA_VALIDATION_HPP \ No newline at end of file From 5eff67732f8df02d4a49ecd6bb3af6acca631d39 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sat, 30 May 2020 23:48:03 +0530 Subject: [PATCH 4/8] Added executables to run uit tests on data_conversion.hpp --- C++/CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/C++/CMakeLists.txt b/C++/CMakeLists.txt index 7c76ad26..1db39eef 100644 --- a/C++/CMakeLists.txt +++ b/C++/CMakeLists.txt @@ -192,3 +192,12 @@ target_link_libraries(disjoint_set test_runner) add_executable(binary_search_tree test/data_structure/tree/binary_search_tree.cpp) target_link_libraries(binary_search_tree test_runner) + +# ============================================================================ +# Utilities +# ============================================================================ + +# Data conversion +add_executable(data_conversion + test/utils/data_conversion.cpp) +target_link_libraries(data_conversion test_runner) \ No newline at end of file From 04266957a7ccb4fb2aba50004dbcbe33253ffae3 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sat, 30 May 2020 23:50:37 +0530 Subject: [PATCH 5/8] Changed the name of the file from data_validation to data_conversion --- C++/include/utils/data_conversion.hpp | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 C++/include/utils/data_conversion.hpp diff --git a/C++/include/utils/data_conversion.hpp b/C++/include/utils/data_conversion.hpp new file mode 100644 index 00000000..bc8a0ab4 --- /dev/null +++ b/C++/include/utils/data_conversion.hpp @@ -0,0 +1,82 @@ +/* + Data validation + --------------- + This utility function contains two sub-funtions : + + 1) make_upper_case() : converts the characters in a string to upper case . + + 2) make_lower_case() : converts the characters in a string to lower case . + + Time complexity + --------------- + O(n), where n is the length of the string . +*/ + +#ifndef DATA_VALIDATION_HPP +#define DATA_VALIDATION_HPP + +#include +#define UP_A 65 +#define UP_Z 91 + +#define LOW_A 97 +#define LOW_Z 122 + +using std::string; + +/* + make_upper_case() + ----------------- + This function returns a given string into upper case . + Loop invariant : Iterates through every charachter of the string and convert it to upper case + + Return value + ------------ + string having all the characters in upper case . +*/ + +std::string make_upper_case(string str) +{ + std::string upper; // defined a string variable to store the convereted string + for (size_t i = 0; i < str.length(); i++) + { + if (str.at(i) >= LOW_A && str.at(i) <= LOW_Z) // checking for the characters that lie between the ASCII values + { + upper += (str.at(i) - 32); // subtracting 32 to get the character in the range 65 - 97 + } + else + { + upper += str.at(i); + } + } + return upper; // returning the converted string to upper case +} +/* + make_lower_case() + ----------------- + This function returns a give string into lower case . + Loop invariant : Selects each character and converts it to lower case . + + Return value + ------------ + string having all the characters in lower case . +*/ + +std::string make_lower_case(string str) +{ + std::string lower; // defined a variable to store the converted the string + for (size_t i = 0; i < str.length(); i++) + { + if (str.at(i) >= UP_A && str.at(i) <= UP_Z) // condition to check if the characters lie in the given ASCII range + { + lower += (str.at(i) + 32); // adding 32 to convert the characters in lower case + } + else + { + lower += str.at(i); + } + } + return lower; // returning the given string in lower case +} + +#endif // DATA_VALIDATION_HPP \ No newline at end of file From ab2f0b45d3674fb0629f6f1225e8ee3560090ea9 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sun, 31 May 2020 00:32:43 +0530 Subject: [PATCH 6/8] Added new line at the end of the file --- C++/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C++/CMakeLists.txt b/C++/CMakeLists.txt index 1db39eef..29fc3985 100644 --- a/C++/CMakeLists.txt +++ b/C++/CMakeLists.txt @@ -200,4 +200,4 @@ target_link_libraries(binary_search_tree test_runner) # Data conversion add_executable(data_conversion test/utils/data_conversion.cpp) -target_link_libraries(data_conversion test_runner) \ No newline at end of file +target_link_libraries(data_conversion test_runner) From 0c2d9bcebaec7f27906818efb531ec084c6860bc Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sun, 31 May 2020 00:38:28 +0530 Subject: [PATCH 7/8] Added and modeified test cases --- C++/test/utils/data_conversion.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 C++/test/utils/data_conversion.cpp diff --git a/C++/test/utils/data_conversion.cpp b/C++/test/utils/data_conversion.cpp new file mode 100644 index 00000000..3d068b4b --- /dev/null +++ b/C++/test/utils/data_conversion.cpp @@ -0,0 +1,22 @@ +#include "third_party/catch.hpp" +#include "utils/data_conversion.hpp" + +TEST_CASE("Make upper case", "[string][data_conversion][make-upper-case]") +{ + REQUIRE(make_upper_case("cats") == "CATS"); + REQUIRE(make_upper_case("dogs") == "DOGS"); + REQUIRE(make_upper_case("s3Cret") == "S3CRET"); + REQUIRE(make_upper_case(" ") == " "); + REQUIRE(make_upper_case("c") == "C"); + REQUIRE(make_upper_case("#er$T") == ("#ER$T")); +} + +TEST_CASE("Make lower case", "[string][data_conversion][make-lower-case]") +{ + REQUIRE(make_lower_case("CaTS") == "cats"); + REQUIRE(make_lower_case("DoGS") == "dogs"); + REQUIRE(make_lower_case("dogS") == "DOGS"); + REQUIRE(make_lower_case("A@#B$%C") == "a@#b$%c"); + REQUIRE(make_lower_case(" ") == " "); + REQUIRE(make_lower_case("F") == "f"); +} From a106f3f00ca5ebe7a7f1f6480574828689c838c4 Mon Sep 17 00:00:00 2001 From: maverick12345678 Date: Sun, 31 May 2020 00:44:01 +0530 Subject: [PATCH 8/8] Added newline at the eof --- C++/include/utils/data_conversion.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C++/include/utils/data_conversion.hpp b/C++/include/utils/data_conversion.hpp index bc8a0ab4..e2a6abbc 100644 --- a/C++/include/utils/data_conversion.hpp +++ b/C++/include/utils/data_conversion.hpp @@ -79,4 +79,4 @@ std::string make_lower_case(string str) return lower; // returning the given string in lower case } -#endif // DATA_VALIDATION_HPP \ No newline at end of file +#endif // DATA_VALIDATION_HPP