Skip to content

Commit

Permalink
Add lower and upper string operations to stringify header.
Browse files Browse the repository at this point in the history
Signed-off-by: fruffy <fruffy@nyu.edu>
  • Loading branch information
fruffy committed Sep 30, 2024
1 parent 7dec522 commit 217816f
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/stringify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include <sstream>
#include <string>

#include "absl/strings/ascii.h"
#include "big_int.h"
#include "cstring.h"
#include "exceptions.h"
Expand Down Expand Up @@ -128,4 +129,12 @@ cstring toString(cstring value) {

cstring toString(std::string_view value) { return cstring(value); }

void lowerInPlace(std::string &s) { absl::AsciiStrToLower(&s); }

std::string toLower(std::string_view s) { return absl::AsciiStrToLower(s); }

void upperInPlace(std::string &s) { absl::AsciiStrToUpper(&s); }

std::string toUpper(std::string_view s) { return absl::AsciiStrToUpper(s); }

} // namespace P4::Util
15 changes: 15 additions & 0 deletions lib/stringify.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ cstring toString(const void *value);

char DigitToChar(int digit);

/// Convert a string to lower case. Using non-ASCII strings might lead to unexpected results..
/// Performs the lowering operation in place.
void lowerInPlace(std::string &s);

/// Convert a string to lower case. Using non-ASCII strings might lead to unexpected results..
/// Returns a copy of the string.
std::string toLower(std::string_view s);

/// Convert a string to upper case. Using non-ASCII strings might lead to unexpected results..
/// Performs the up operation in place.
void upperInPlace(std::string &s);

/// Convert a string to upper case. Using non-ASCII strings might lead to unexpected results..
std::string toUpper(std::string_view s);

} // namespace Util
} // namespace P4

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set (GTEST_UNITTEST_SOURCES
gtest/source_file_test.cpp
gtest/strength_reduction.cpp
gtest/string_map.cpp
gtest/string_test.cpp
gtest/transforms.cpp
gtest/rtti_test.cpp
gtest/nethash.cpp
Expand Down
165 changes: 165 additions & 0 deletions test/gtest/string_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include <gtest/gtest.h>

#include <string>

#include "lib/stringify.h"

namespace P4::Test {

TEST(StringCaseConversionTests, MixedCase) {
std::string input = "HeLLo WoRLd!";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "hello world!");

EXPECT_EQ(Util::toLower(input), "hello world!");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "HELLO WORLD!");

EXPECT_EQ(Util::toUpper(input), "HELLO WORLD!");
}

TEST(StringCaseConversionTests, DigitsAndSpecialChars) {
std::string input = "123ABC$%^";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "123abc$%^");

EXPECT_EQ(Util::toLower(input), "123abc$%^");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "123ABC$%^");

EXPECT_EQ(Util::toUpper(input), "123ABC$%^");
}

TEST(StringCaseConversionTests, AlreadyLowercase) {
std::string input = "already lowercase";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "already lowercase");

EXPECT_EQ(Util::toLower(input), "already lowercase");

Util::upperInPlace(input);
EXPECT_EQ(input, "ALREADY LOWERCASE");

EXPECT_EQ(Util::toUpper("already lowercase"), "ALREADY LOWERCASE");
}

TEST(StringCaseConversionTests, AlreadyUppercase) {
std::string input = "ALREADY UPPERCASE";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "already uppercase");

EXPECT_EQ(Util::toLower(input), "already uppercase");

Util::upperInPlace(input);
EXPECT_EQ(input, "ALREADY UPPERCASE");

EXPECT_EQ(Util::toUpper(input), "ALREADY UPPERCASE");
}

TEST(StringCaseConversionTests, EmptyString) {
std::string input;

Util::lowerInPlace(input);
EXPECT_EQ(input, "");

EXPECT_EQ(Util::toLower(""), "");

Util::upperInPlace(input);
EXPECT_EQ(input, "");

EXPECT_EQ(Util::toUpper(""), "");
}

TEST(StringCaseConversionTests, SingleCharacter) {
std::string input = "A";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "a");

EXPECT_EQ(Util::toLower(input), "a");

std::string upperInput = "b";
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "B");

EXPECT_EQ(Util::toUpper("b"), "B");
}

TEST(StringCaseConversionTests, WhitespaceString) {
std::string input = " HeLlO wOrLd ";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, " hello world ");

EXPECT_EQ(Util::toLower(input), " hello world ");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, " HELLO WORLD ");

EXPECT_EQ(Util::toUpper(input), " HELLO WORLD ");
}

TEST(StringCaseConversionTests, UnicodeCharacters) {
std::string input = "áÉíÓú";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "áéíóú");

EXPECT_EQ(Util::toLower(input), "áéíóú");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "ÁÉÍÓÚ");

EXPECT_EQ(Util::toUpper(input), "ÁÉÍÓÚ");
}

TEST(StringCaseConversionTests, SymbolsOnly) {
std::string input = "@#$%^&*";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "@#$%^&*");

EXPECT_EQ(Util::toLower(input), "@#$%^&*");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "@#$%^&*");

EXPECT_EQ(Util::toUpper(input), "@#$%^&*");
}

TEST(StringCaseConversionTests, LongString) {
std::string input = "ThisIsAVeryLongStringToTestTheEfficiencyOfTheFunctions";

std::string lowerInput = input;
Util::lowerInPlace(lowerInput);
EXPECT_EQ(lowerInput, "thisisaverylongstringtotesttheefficiencyofthefunctions");

EXPECT_EQ(Util::toLower(input), "thisisaverylongstringtotesttheefficiencyofthefunctions");

std::string upperInput = input;
Util::upperInPlace(upperInput);
EXPECT_EQ(upperInput, "THISISAVERYLONGSTRINGTOTESTTHEEFFICIENCYOFTHEFUNCTIONS");

EXPECT_EQ(Util::toUpper(input), "THISISAVERYLONGSTRINGTOTESTTHEEFFICIENCYOFTHEFUNCTIONS");
}

} // namespace P4::Test

0 comments on commit 217816f

Please sign in to comment.