]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/strtol.cc: Test 'strict_strtosi()'
authorJoao Eduardo Luis <joao.luis@inktank.com>
Fri, 23 May 2014 15:52:08 +0000 (16:52 +0100)
committerSage Weil <sage@redhat.com>
Sat, 16 Aug 2014 04:00:58 +0000 (21:00 -0700)
Signed-off-by: Joao Eduardo Luis <joao.luis@inktank.com>
(cherry picked from commit 40587d4792fd55db72d33870aae8b6a806c9baaf)

src/test/strtol.cc

index d3f0ae0c88a759158bee0c3c6f6b22220d382798..3c008b6035d1979d328cd7f3ab49b5b4962fd7c4 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "common/strtol.h"
 #include <string>
+#include <map>
 
 #include "gtest/gtest.h"
 
@@ -134,3 +135,77 @@ TEST(StrToL, Error1) {
 
   test_strict_strtof_err("0.05.0");
 }
+
+
+static void test_strict_sistrtoll(const char *str)
+{
+  std::string err;
+  strict_sistrtoll(str, &err);
+  ASSERT_EQ(err, "");
+}
+
+static void test_strict_sistrtoll_units(const std::string& foo,
+                                      char u, const int m)
+{
+  std::string s(foo);
+  s.push_back(u);
+  const char *str = s.c_str();
+  std::string err;
+  uint64_t r = strict_sistrtoll(str, &err);
+  ASSERT_EQ(err, "");
+
+  str = foo.c_str();
+  std::string err2;
+  long long tmp = strict_strtoll(str, 10, &err2);
+  ASSERT_EQ(err2, "");
+  tmp = (tmp << m);
+  ASSERT_EQ(tmp, r);
+}
+
+TEST(SIStrToLL, WithUnits) {
+  std::map<char,int> units;
+  units['B'] = 0;
+  units['K'] = 10;
+  units['M'] = 20;
+  units['G'] = 30;
+  units['T'] = 40;
+  units['P'] = 50;
+  units['E'] = 60;
+
+  for (std::map<char,int>::iterator p = units.begin();
+       p != units.end(); ++p) {
+    test_strict_sistrtoll_units("1024", p->first, p->second);
+    test_strict_sistrtoll_units("1", p->first, p->second);
+    test_strict_sistrtoll_units("0", p->first, p->second);
+  }
+}
+
+TEST(SIStrToLL, WithoutUnits) {
+  test_strict_sistrtoll("1024");
+  test_strict_sistrtoll("1152921504606846976");
+  test_strict_sistrtoll("0");
+}
+
+static void test_strict_sistrtoll_err(const char *str)
+{
+  std::string err;
+  strict_sistrtoll(str, &err);
+  ASSERT_NE(err, "");
+}
+
+TEST(SIStrToLL, Error) {
+  test_strict_sistrtoll_err("1024F");
+  test_strict_sistrtoll_err("QDDSA");
+  test_strict_sistrtoll_err("1b");
+  test_strict_sistrtoll_err("100k");
+  test_strict_sistrtoll_err("1000m");
+  test_strict_sistrtoll_err("1g");
+  test_strict_sistrtoll_err("20t");
+  test_strict_sistrtoll_err("100p");
+  test_strict_sistrtoll_err("1000e");
+  test_strict_sistrtoll_err("B");
+  test_strict_sistrtoll_err("M");
+  test_strict_sistrtoll_err("BM");
+  test_strict_sistrtoll_err("B0wef");
+  test_strict_sistrtoll_err("0m");
+}