hidl_string: relational operators

Fixes: 34524287
Test: libhidl_test
Change-Id: Id0731e6df1fc1831d8c1bdf35b3aa17abd5273d2
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 01590c3..3bd68af 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -250,10 +250,6 @@
     return *this;
 }
 
-bool hidl_string::operator< (const hidl_string &rhs) const {
-    return strcmp(mBuffer, rhs.mBuffer) < 0;
-}
-
 hidl_string::operator std::string() const {
     return std::string(mBuffer, mSize);
 }
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 17951f7..6c00c3d 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -157,8 +157,6 @@
     // to maintain this hidl_string alive.
     operator const char *() const;
 
-    bool operator< (const hidl_string &rhs) const;
-
     void clear();
 
     // Reference an external char array. Ownership is _not_ transferred.
@@ -181,29 +179,25 @@
     void moveFrom(hidl_string &&);
 };
 
-inline bool operator==(const hidl_string &hs1, const hidl_string &hs2) {
-    return strcmp(hs1.c_str(), hs2.c_str()) == 0;
-}
+#define HIDL_STRING_OPERATOR(OP)                                               \
+    inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) {  \
+        return strcmp(hs1.c_str(), hs2.c_str()) OP 0;                          \
+    }                                                                          \
+    inline bool operator OP(const hidl_string &hs, const char *s) {            \
+        return strcmp(hs.c_str(), s) OP 0;                                     \
+    }                                                                          \
+    inline bool operator OP(const char *s, const hidl_string &hs) {            \
+        return strcmp(hs.c_str(), s) OP 0;                                     \
+    }
 
-inline bool operator!=(const hidl_string &hs1, const hidl_string &hs2) {
-    return !(hs1 == hs2);
-}
+HIDL_STRING_OPERATOR(==)
+HIDL_STRING_OPERATOR(!=)
+HIDL_STRING_OPERATOR(<)
+HIDL_STRING_OPERATOR(<=)
+HIDL_STRING_OPERATOR(>)
+HIDL_STRING_OPERATOR(>=)
 
-inline bool operator==(const hidl_string &hs, const char *s) {
-    return strcmp(hs.c_str(), s) == 0;
-}
-
-inline bool operator!=(const hidl_string &hs, const char *s) {
-    return !(hs == s);
-}
-
-inline bool operator==(const char *s, const hidl_string &hs) {
-    return strcmp(hs.c_str(), s) == 0;
-}
-
-inline bool operator!=(const char *s, const hidl_string &hs) {
-    return !(s == hs);
-}
+#undef HIDL_STRING_OPERATOR
 
 // hidl_memory is a structure that can be used to transfer
 // pieces of shared memory between processes. The assumption
diff --git a/test_main.cpp b/test_main.cpp
index 6e9bf25..d663391 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -81,9 +81,6 @@
     s.clear(); // should not affect myCString
     EXPECT_STREQ(myCString, "myDString");
 
-    // operator <
-    EXPECT_LT("ab", "bcd");
-
     // casts
     s = "great";
     std::string myString = s;
@@ -101,18 +98,33 @@
     const char * cstrNE = "ABC";
     std::string stringNE(cstrNE);
     hidl_string hsNE(cstrNE);
+    const char * cstr2 = "def";
+    std::string string2(cstr2);
+    hidl_string hs2(cstr2);
+
     EXPECT_TRUE(hs1  == hsE);
-    EXPECT_FALSE(hs1 != hsE);
-    EXPECT_TRUE(hs1  != hsNE);
     EXPECT_FALSE(hs1 == hsNE);
     EXPECT_TRUE(hs1  == cstrE);
-    EXPECT_FALSE(hs1 != cstrE);
-    EXPECT_TRUE(hs1  != cstrNE);
     EXPECT_FALSE(hs1 == cstrNE);
     EXPECT_TRUE(hs1  == stringE);
+    EXPECT_FALSE(hs1 == stringNE);
+    EXPECT_FALSE(hs1 != hsE);
+    EXPECT_TRUE(hs1  != hsNE);
+    EXPECT_FALSE(hs1 != cstrE);
+    EXPECT_TRUE(hs1  != cstrNE);
     EXPECT_FALSE(hs1 != stringE);
     EXPECT_TRUE(hs1  != stringNE);
-    EXPECT_FALSE(hs1 == stringNE);
+
+    EXPECT_TRUE(hs1 < hs2);
+    EXPECT_FALSE(hs2 < hs1);
+    EXPECT_TRUE(hs2 > hs1);
+    EXPECT_FALSE(hs1 > hs2);
+    EXPECT_TRUE(hs1 <= hs1);
+    EXPECT_TRUE(hs1 <= hs2);
+    EXPECT_FALSE(hs2 <= hs1);
+    EXPECT_TRUE(hs1 >= hs1);
+    EXPECT_TRUE(hs2 >= hs1);
+    EXPECT_FALSE(hs2 <= hs1);
 }
 
 TEST_F(LibHidlTest, MemoryTest) {