From 338ef4aa6cf39be4761b39b25ce5dec829af535b Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 8 Dec 2015 17:48:17 -0800 Subject: fix bug MIN_VALUE bug in ltoa and int64toa and add tests --- test/core/support/string_test.c | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'test') diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index f62cbe3435..b4024fb799 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -286,6 +286,53 @@ static void test_strsplit(void) { gpr_free(parts); } +test_ltoa() { + char *str; + char buf[GPR_LTOA_MIN_BUFSIZE]; + + LOG_TEST_NAME("test_ltoa"); + + /* zero */ + GPR_ASSERT(1 == gpr_ltoa(0, buf)); + GPR_ASSERT(0 == strcmp("0", buf)); + + /* positive number */ + GPR_ASSERT(3 == gpr_ltoa(123, buf)); + GPR_ASSERT(0 == strcmp("123", buf)); + + /* negative number */ + GPR_ASSERT(6 == gpr_ltoa(-12345, buf)); + GPR_ASSERT(0 == strcmp("-12345", buf)); + + /* large negative - we don't know the size of long in advance */ + GPR_ASSERT(gpr_asprintf(&str, "%lld", (long long)LONG_MIN)); + GPR_ASSERT(strlen(str) == gpr_ltoa(LONG_MIN, buf)); + GPR_ASSERT(0 == strcmp(str, buf)); + gpr_free(str); +} + +test_int64toa() { + char buf[GPR_INT64TOA_MIN_BUFSIZE]; + + LOG_TEST_NAME("test_int64toa"); + + /* zero */ + GPR_ASSERT(1 == gpr_int64toa(0, buf)); + GPR_ASSERT(0 == strcmp("0", buf)); + + /* positive */ + GPR_ASSERT(3 == gpr_int64toa(123, buf)); + GPR_ASSERT(0 == strcmp("123", buf)); + + /* large positive */ + GPR_ASSERT(19 == gpr_int64toa(9223372036854775807LL, buf)); + GPR_ASSERT(0 == strcmp("9223372036854775807", buf)); + + /* large negative */ + GPR_ASSERT(20 == gpr_int64toa(-9223372036854775808LL, buf)); + GPR_ASSERT(0 == strcmp("-9223372036854775808", buf)); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); test_strdup(); @@ -296,5 +343,7 @@ int main(int argc, char **argv) { test_strjoin(); test_strjoin_sep(); test_strsplit(); + test_ltoa(); + test_int64toa(); return 0; } -- cgit v1.2.3 From 26e190f3ee7c259165ce12106172f3bd25b1e323 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 8 Dec 2015 22:44:21 -0800 Subject: make test methods static void --- test/core/support/string_test.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index b4024fb799..0ae930fa61 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -286,7 +286,7 @@ static void test_strsplit(void) { gpr_free(parts); } -test_ltoa() { +static void test_ltoa() { char *str; char buf[GPR_LTOA_MIN_BUFSIZE]; @@ -311,7 +311,7 @@ test_ltoa() { gpr_free(str); } -test_int64toa() { +static void test_int64toa() { char buf[GPR_INT64TOA_MIN_BUFSIZE]; LOG_TEST_NAME("test_int64toa"); -- cgit v1.2.3 From 06ae5cd48c1e235b0980cfca631c94572c7777d8 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 9 Dec 2015 19:24:04 -0800 Subject: get rid of warnings on linux --- test/core/support/string_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index 0ae930fa61..c97d3176c5 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -33,6 +33,7 @@ #include "src/core/support/string.h" +#include #include #include #include @@ -306,7 +307,7 @@ static void test_ltoa() { /* large negative - we don't know the size of long in advance */ GPR_ASSERT(gpr_asprintf(&str, "%lld", (long long)LONG_MIN)); - GPR_ASSERT(strlen(str) == gpr_ltoa(LONG_MIN, buf)); + GPR_ASSERT(strlen(str) == (size_t)gpr_ltoa(LONG_MIN, buf)); GPR_ASSERT(0 == strcmp(str, buf)); gpr_free(str); } @@ -329,7 +330,7 @@ static void test_int64toa() { GPR_ASSERT(0 == strcmp("9223372036854775807", buf)); /* large negative */ - GPR_ASSERT(20 == gpr_int64toa(-9223372036854775808LL, buf)); + GPR_ASSERT(20 == gpr_int64toa(-9223372036854775807LL - 1, buf)); GPR_ASSERT(0 == strcmp("-9223372036854775808", buf)); } -- cgit v1.2.3