From 0a0618baac618f68a1e780e5c926d359e4eeec65 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 29 Jan 2015 23:28:47 +0900 Subject: [PATCH] nghttpx: Add test for util::duration_str, rename util::parse_duration_with_unit --- src/shrpx-unittest.cc | 6 ++++-- src/shrpx_config.cc | 2 +- src/util.cc | 5 ++++- src/util.h | 2 +- src/util_test.cc | 35 +++++++++++++++++++++-------------- src/util_test.h | 3 ++- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/shrpx-unittest.cc b/src/shrpx-unittest.cc index f513a630..b3c624ef 100644 --- a/src/shrpx-unittest.cc +++ b/src/shrpx-unittest.cc @@ -135,8 +135,10 @@ int main(int argc, char *argv[]) { !CU_add_test(pSuite, "util_parse_uint_with_unit", shrpx::test_util_parse_uint_with_unit) || !CU_add_test(pSuite, "util_parse_uint", shrpx::test_util_parse_uint) || - !CU_add_test(pSuite, "util_parse_time_with_unit", - shrpx::test_util_parse_time_with_unit) || + !CU_add_test(pSuite, "util_parse_duration_with_unit", + shrpx::test_util_parse_duration_with_unit) || + !CU_add_test(pSuite, "util_duration_str", + shrpx::test_util_duration_str) || !CU_add_test(pSuite, "gzip_inflate", test_nghttp2_gzip_inflate) || !CU_add_test(pSuite, "ringbuf_write", nghttp2::test_ringbuf_write) || !CU_add_test(pSuite, "ringbuf_iovec", nghttp2::test_ringbuf_iovec) || diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 1316109f..a00184fa 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -502,7 +502,7 @@ std::vector parse_log_format(const char *optarg) { namespace { int parse_duration(ev_tstamp *dest, const char *opt, const char *optarg) { - auto t = util::parse_time_with_unit(optarg); + auto t = util::parse_duration_with_unit(optarg); if (t == std::numeric_limits::infinity()) { LOG(ERROR) << opt << ": bad value: '" << optarg << "'"; return -1; diff --git a/src/util.cc b/src/util.cc index 74a54d8b..c5fce569 100644 --- a/src/util.cc +++ b/src/util.cc @@ -990,7 +990,7 @@ int64_t parse_uint(const uint8_t *s, size_t len) { return n; } -double parse_time_with_unit(const char *s) { +double parse_duration_with_unit(const char *s) { int64_t n; size_t i; auto len = strlen(s); @@ -1021,6 +1021,9 @@ fail: } std::string duration_str(double t) { + if (t == 0.) { + return "0"; + } auto frac = static_cast(t * 1000) % 1000; if (frac > 0) { return utos(static_cast(t * 1000)) + "ms"; diff --git a/src/util.h b/src/util.h index 059fb0d3..f653efcf 100644 --- a/src/util.h +++ b/src/util.h @@ -514,7 +514,7 @@ int64_t parse_uint(const std::string &s); // is millisecond. If none of them are given, the unit is second. // This function returns std::numeric_limits::infinity() if // error occurs. -double parse_time_with_unit(const char *s); +double parse_duration_with_unit(const char *s); // Returns string representation of time duration |t|. If t has // fractional part (at least more than or equal to 1e-3), |t| is diff --git a/src/util_test.cc b/src/util_test.cc index c6a47e50..4b406dfc 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -223,24 +223,31 @@ void test_util_parse_uint(void) { CU_ASSERT(-1 == util::parse_uint("")); } -void test_util_parse_time_with_unit(void) { - CU_ASSERT(0. == util::parse_time_with_unit("0")); - CU_ASSERT(123. == util::parse_time_with_unit("123")); - CU_ASSERT(123. == util::parse_time_with_unit("123s")); - CU_ASSERT(0.500 == util::parse_time_with_unit("500ms")); - CU_ASSERT(123. == util::parse_time_with_unit("123S")); - CU_ASSERT(0.500 == util::parse_time_with_unit("500MS")); +void test_util_parse_duration_with_unit(void) { + CU_ASSERT(0. == util::parse_duration_with_unit("0")); + CU_ASSERT(123. == util::parse_duration_with_unit("123")); + CU_ASSERT(123. == util::parse_duration_with_unit("123s")); + CU_ASSERT(0.500 == util::parse_duration_with_unit("500ms")); + CU_ASSERT(123. == util::parse_duration_with_unit("123S")); + CU_ASSERT(0.500 == util::parse_duration_with_unit("500MS")); auto err = std::numeric_limits::infinity(); // check overflow case - CU_ASSERT(err == util::parse_time_with_unit("9223372036854775808")); + CU_ASSERT(err == util::parse_duration_with_unit("9223372036854775808")); // bad characters - CU_ASSERT(err == util::parse_time_with_unit("0u")); - CU_ASSERT(err == util::parse_time_with_unit("0xs")); - CU_ASSERT(err == util::parse_time_with_unit("0mt")); - CU_ASSERT(err == util::parse_time_with_unit("0mss")); - CU_ASSERT(err == util::parse_time_with_unit("s")); - CU_ASSERT(err == util::parse_time_with_unit("ms")); + CU_ASSERT(err == util::parse_duration_with_unit("0u")); + CU_ASSERT(err == util::parse_duration_with_unit("0xs")); + CU_ASSERT(err == util::parse_duration_with_unit("0mt")); + CU_ASSERT(err == util::parse_duration_with_unit("0mss")); + CU_ASSERT(err == util::parse_duration_with_unit("s")); + CU_ASSERT(err == util::parse_duration_with_unit("ms")); +} + +void test_util_duration_str(void) { + CU_ASSERT("0" == util::duration_str(0.)); + CU_ASSERT("1s" == util::duration_str(1.)); + CU_ASSERT("500ms" == util::duration_str(0.5)); + CU_ASSERT("1500ms" == util::duration_str(1.5)); } } // namespace shrpx diff --git a/src/util_test.h b/src/util_test.h index b741ab2a..1c256ec1 100644 --- a/src/util_test.h +++ b/src/util_test.h @@ -40,7 +40,8 @@ void test_util_ipv6_numeric_addr(void); void test_util_utos_with_unit(void); void test_util_parse_uint_with_unit(void); void test_util_parse_uint(void); -void test_util_parse_time_with_unit(void); +void test_util_parse_duration_with_unit(void); +void test_util_duration_str(void); } // namespace shrpx