]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/Formatter: add unit test
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 2 Aug 2011 18:08:52 +0000 (11:08 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 2 Aug 2011 18:08:52 +0000 (11:08 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/Makefile.am
src/test/formatter.cc [new file with mode: 0644]

index 30421e8651b4708062a6c1f9642acc7b5baf4d3b..e6ee312ba31951f4f4f907b4782459294091b1e7 100644 (file)
@@ -526,6 +526,12 @@ unittest_heartbeatmap_LDADD = ${UNITTEST_LDADD} $(LIBGLOBAL_LDA)
 unittest_heartbeatmap_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS}
 check_PROGRAMS += unittest_heartbeatmap
 
+unittest_formatter_SOURCES = test/formatter.cc
+unittest_formatter_LDFLAGS = -pthread ${AM_LDFLAGS}
+unittest_formatter_LDADD = ${UNITTEST_LDADD} $(LIBGLOBAL_LDA)
+unittest_formatter_CXXFLAGS = ${AM_CXXFLAGS} ${UNITTEST_CXXFLAGS}
+check_PROGRAMS += unittest_formatter
+
 if WITH_RADOSGW
 unittest_librgw_SOURCES = test/librgw.cc
 unittest_librgw_LDFLAGS = -lrt -pthread -lcurl ${AM_LDFLAGS}
diff --git a/src/test/formatter.cc b/src/test/formatter.cc
new file mode 100644 (file)
index 0000000..0a2bdba
--- /dev/null
@@ -0,0 +1,57 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include "test/unit.h"
+#include "common/Formatter.h"
+
+#include <sstream>
+#include <string>
+
+using std::ostringstream;
+
+TEST(JsonFormatter, Simple1) {
+  ostringstream oss;
+  JSONFormatter fmt(false);
+  fmt.open_object_section("foo");
+  fmt.dump_int("a", 1);
+  fmt.dump_int("b", 2);
+  fmt.dump_int("c", 3);
+  fmt.close_section();
+  fmt.flush(oss);
+  ASSERT_EQ(oss.str(), "{\"a\":1,\"b\":2,\"c\":3}");
+}
+
+TEST(JsonFormatter, Simple2) {
+  ostringstream oss;
+  JSONFormatter fmt(false);
+  fmt.open_object_section("foo");
+  fmt.open_object_section("bar");
+  fmt.dump_int("int", 0xf00000000000ll);
+  fmt.dump_unsigned("unsigned", 0x8000000000000001llu);
+  fmt.dump_float("float", 1.234);
+  fmt.close_section();
+  fmt.dump_string("string", "str");
+  fmt.close_section();
+  fmt.flush(oss);
+  ASSERT_EQ(oss.str(), "{\"bar\":{\"int\":263882790666240,\
+\"unsigned\":9223372036854775809,\"float\":\"1.234000\"},\
+\"string\":\"str\"}");
+}
+
+TEST(JsonFormatter, Empty) {
+  ostringstream oss;
+  JSONFormatter fmt(false);
+  fmt.flush(oss);
+  ASSERT_EQ(oss.str(), "");
+}