]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
TestEncoding: add templated encode-then-decode fn
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Nov 2010 19:14:21 +0000 (12:14 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Nov 2010 19:19:03 +0000 (12:19 -0700)
TestEncoding: add a templated encode-then-decode fn that can be used to
test encoding followed by decoding of any type. Test encoding and
decoding of a std::multimap.

Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
src/test/TestEncoding.cc

index 6d9feed89cabce3884b368d308863fa6a69f7845..5e4a83bcaef5edf83efc08a1b43bfa1621e05d86 100644 (file)
@@ -11,27 +11,43 @@ using std::cerr;
 using std::string;
 using std::vector;
 
-// Test string serialization
-static int test_string_sz(const char *str)
+typedef std::multimap < int, std::string > multimap_t;
+typedef multimap_t::value_type val_ty;
+
+static std::ostream& operator<<(std::ostream& oss, const multimap_t &multimap)
 {
-  bufferlist bl(100000);
+  for (multimap_t::const_iterator m = multimap.begin();
+       m != multimap.end();
+       ++m)
+  {
+    oss << m->first << "->" << m->second << " ";
+  }
+  return oss;
+}
+
+template < typename T >
+static int test_encode_and_decode(const T& src)
+{
+  bufferlist bl(1000000);
 
   try {
     // source
-    string src(str);
     encode(src, bl);
 
     // dest
-    string dst;
+    T dst;
     bufferlist::iterator i(bl.begin());
     decode(dst, i);
     if (src != dst) {
       cout << "src = " << src << ", but dst = " << dst << std::endl;
       return 1;
     }
+//    else {
+//      cout << "src = " << src << ", and dst = " << dst << std::endl;
+//    }
   }
-  catch (const ceph::buffer::malformed_input &i) {
-    cout << "got exception " << i.what() << std::endl;
+  catch (const ceph::buffer::malformed_input &e) {
+    cout << "got exception " << e.what() << std::endl;
     return 1;
   }
   catch (const std::exception &e) {
@@ -45,6 +61,24 @@ static int test_string_sz(const char *str)
   return 0;
 }
 
+static int test_string_sz(const char *str)
+{
+  string my_str(str);
+  return test_encode_and_decode < std::string >(my_str);
+}
+
+static int multimap_tests(void)
+{
+  multimap_t multimap;
+  multimap.insert( val_ty(1, "foo") );
+  multimap.insert( val_ty(2, "bar") );
+  multimap.insert( val_ty(2, "baz") );
+  multimap.insert( val_ty(3, "lucky number 3") );
+  multimap.insert( val_ty(10000, "large number") );
+
+  return test_encode_and_decode < multimap_t >(multimap);
+}
+
 int main(int argc, const char **argv)
 {
   int ret = 0;
@@ -69,6 +103,10 @@ int main(int argc, const char **argv)
   if (ret)
     goto done;
 
+  ret = multimap_tests();
+  if (ret)
+    goto done;
+
 done:
   if (ret) {
     cout << "FAILURE" << std::endl;