From 40587d4792fd55db72d33870aae8b6a806c9baaf Mon Sep 17 00:00:00 2001 From: Joao Eduardo Luis Date: Fri, 23 May 2014 16:52:08 +0100 Subject: [PATCH] test/strtol.cc: Test 'strict_strtosi()' Signed-off-by: Joao Eduardo Luis --- src/test/strtol.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/test/strtol.cc b/src/test/strtol.cc index d3f0ae0c88a75..3c008b6035d19 100644 --- a/src/test/strtol.cc +++ b/src/test/strtol.cc @@ -14,6 +14,7 @@ #include "common/strtol.h" #include +#include #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 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::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"); +} -- 2.39.5