diff options
author | Matt Jacobson <mhjacobson@me.com> | 2022-01-14 22:24:34 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-16 11:08:23 +0100 |
commit | 47e8d585538ace0ef4b7a52ab303f2385327e199 (patch) | |
tree | e98bd7b9871c3694512b6c0b7c5eb9199d18f856 | |
parent | c74f75b910b7f8724b765e19e94048db2e3efcea (diff) | |
download | serenity-47e8d585538ace0ef4b7a52ab303f2385327e199.zip |
AK: Fix logic in String::operator>(const String&)
Null strings should not compare greater than non-null strings.
Add tests for >, <, >=, and <= comparison involving null strings.
-rw-r--r-- | AK/String.cpp | 6 | ||||
-rw-r--r-- | Tests/AK/TestString.cpp | 15 |
2 files changed, 17 insertions, 4 deletions
diff --git a/AK/String.cpp b/AK/String.cpp index 6dec4c1438..b975781de0 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -58,10 +58,10 @@ bool String::operator<(const String& other) const bool String::operator>(const String& other) const { - if (!m_impl) - return other.m_impl; - if (!other.m_impl) + return m_impl; + + if (!m_impl) return false; return strcmp(characters(), other.characters()) > 0; diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp index 0b3d859fa6..2c18f91cdb 100644 --- a/Tests/AK/TestString.cpp +++ b/Tests/AK/TestString.cpp @@ -42,7 +42,6 @@ TEST_CASE(construct_contents) TEST_CASE(compare) { - String test_string = "ABCDEF"; EXPECT("a" < String("b")); EXPECT(!("a" > String("b"))); EXPECT("b" > String("a")); @@ -51,6 +50,20 @@ TEST_CASE(compare) EXPECT(!("a" >= String("b"))); EXPECT("a" <= String("a")); EXPECT(!("b" <= String("a"))); + + EXPECT(String("a") > String()); + EXPECT(!(String() > String("a"))); + EXPECT(String() < String("a")); + EXPECT(!(String("a") < String())); + EXPECT(String("a") >= String()); + EXPECT(!(String() >= String("a"))); + EXPECT(String() <= String("a")); + EXPECT(!(String("a") <= String())); + + EXPECT(!(String() > String())); + EXPECT(!(String() < String())); + EXPECT(String() >= String()); + EXPECT(String() <= String()); } TEST_CASE(index_access) |