nghttpx: Add test for util::duration_str, rename util::parse_duration_with_unit

This commit is contained in:
Tatsuhiro Tsujikawa 2015-01-29 23:28:47 +09:00
parent e03f36eeeb
commit 0a0618baac
6 changed files with 33 additions and 20 deletions

View File

@ -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) ||

View File

@ -502,7 +502,7 @@ std::vector<LogFragment> 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<double>::infinity()) {
LOG(ERROR) << opt << ": bad value: '" << optarg << "'";
return -1;

View File

@ -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<int64_t>(t * 1000) % 1000;
if (frac > 0) {
return utos(static_cast<int64_t>(t * 1000)) + "ms";

View File

@ -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<double>::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

View File

@ -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<double>::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

View File

@ -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