[Groonga-commit] groonga/grnxx [master] Add comparison operators (<, >, <=, >=) to grnxx::Slice.

Back to archive index

susumu.yata null+****@clear*****
Tue Apr 9 10:34:23 JST 2013


susumu.yata	2013-04-09 10:34:23 +0900 (Tue, 09 Apr 2013)

  New Revision: 1998090d6ad809ff86dd0e1c63f52d389a197eb3
  https://github.com/groonga/grnxx/commit/1998090d6ad809ff86dd0e1c63f52d389a197eb3

  Message:
    Add comparison operators (<, >, <=, >=) to grnxx::Slice.

  Modified files:
    lib/grnxx/slice.hpp
    test/test_slice.cpp

  Modified: lib/grnxx/slice.hpp (+18 -0)
===================================================================
--- lib/grnxx/slice.hpp    2013-04-08 11:22:55 +0900 (e692711)
+++ lib/grnxx/slice.hpp    2013-04-09 10:34:23 +0900 (96513cd)
@@ -134,6 +134,24 @@ inline bool operator!=(const Slice &lhs, const Slice &rhs) {
   return !(lhs == rhs);
 }
 
+inline bool operator<(const Slice &lhs, const Slice &rhs) {
+  const size_t min_size = (lhs.size() < rhs.size()) ? lhs.size() : rhs.size();
+  int result = std::memcmp(lhs.ptr(), rhs.ptr(), min_size);
+  return (result < 0) || ((result == 0) && (lhs.size() < rhs.size()));
+}
+
+inline bool operator>(const Slice &lhs, const Slice &rhs) {
+  return rhs < lhs;
+}
+
+inline bool operator<=(const Slice &lhs, const Slice &rhs) {
+  return !(lhs > rhs);
+}
+
+inline bool operator>=(const Slice &lhs, const Slice &rhs) {
+  return rhs <= lhs;
+}
+
 std::ostream &operator<<(std::ostream &stream, const Slice &s);
 
 }  // namespace grnxx

  Modified: test/test_slice.cpp (+77 -0)
===================================================================
--- test/test_slice.cpp    2013-04-08 11:22:55 +0900 (ca2b5d4)
+++ test/test_slice.cpp    2013-04-09 10:34:23 +0900 (57e5c83)
@@ -164,6 +164,77 @@ void test_starts_with() {
   assert(!slice.starts_with("abc"));
 }
 
+void test_equal_to() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abc2("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(abc == abc);
+  assert(!(abc == abcde));
+  assert(!(abcde == abc));
+  assert(!(abc == cde));
+}
+
+void test_not_equal_to() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(!(abc != abc));
+  assert(abc != abcde);
+  assert(abcde != abc);
+  assert(abc != cde);
+}
+
+void test_less() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(!(abc < abc));
+  assert(abc < abcde);
+  assert(!(abcde < abc));
+  assert(abc < cde);
+  assert(!(cde < abc));
+}
+
+void test_greater() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(!(abc > abc));
+  assert(!(abc > abcde));
+  assert(abcde > abc);
+  assert(!(abc > cde));
+  assert(cde > abc);
+}
+
+void test_less_equal() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(abc <= abc);
+  assert(abc <= abcde);
+  assert(!(abcde <= abc));
+  assert(abc <= cde);
+  assert(!(cde <= abc));
+}
+
+void test_greater_equal() {
+  grnxx::Slice abc("abc");
+  grnxx::Slice abcde("abcde");
+  grnxx::Slice cde("cde");
+
+  assert(abc >= abc);
+  assert(!(abc >= abcde));
+  assert(abcde >= abc);
+  assert(!(abc >= cde));
+  assert(cde >= abc);
+}
+
 void test_ends_with() {
   grnxx::Slice slice("cde");
 
@@ -187,6 +258,12 @@ int main() {
   test_remove_prefix();
   test_remove_suffix();
   test_compare();
+  test_equal_to();
+  test_not_equal_to();
+  test_less();
+  test_greater();
+  test_less_equal();
+  test_greater_equal();
   test_starts_with();
   test_ends_with();
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index