aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/support
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-12-16 19:25:58 -0800
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-12-16 19:25:58 -0800
commit86e8ad8ddf6bbbb59327cee2383d7ec414e06c71 (patch)
tree1eacf02d534f6c246597c800988f7091306d558b /test/core/support
parentc8b7013be30f003e501e743cec856c739db3e160 (diff)
parent788767a18f918131268ca88985b3547a8257e973 (diff)
Merge github.com:grpc/grpc into big_data
Diffstat (limited to 'test/core/support')
-rw-r--r--test/core/support/alloc_test.c20
-rw-r--r--test/core/support/cmdline_test.c187
-rw-r--r--test/core/support/string_test.c50
-rw-r--r--test/core/support/sync_test.c3
-rw-r--r--test/core/support/thd_test.c14
-rw-r--r--test/core/support/time_test.c4
6 files changed, 257 insertions, 21 deletions
diff --git a/test/core/support/alloc_test.c b/test/core/support/alloc_test.c
index 289b2b0c87..b950f0ab49 100644
--- a/test/core/support/alloc_test.c
+++ b/test/core/support/alloc_test.c
@@ -35,17 +35,11 @@
#include <grpc/support/alloc.h>
#include "test/core/util/test_config.h"
-static void *fake_malloc(size_t size) {
- return (void*)size;
-}
+static void *fake_malloc(size_t size) { return (void *)size; }
-static void *fake_realloc(void *addr, size_t size) {
- return (void*)size;
-}
+static void *fake_realloc(void *addr, size_t size) { return (void *)size; }
-static void fake_free(void *addr) {
- *((gpr_intptr*)addr) = 0xdeadd00d;
-}
+static void fake_free(void *addr) { *((gpr_intptr *)addr) = 0xdeadd00d; }
static void test_custom_allocs() {
const gpr_allocation_functions default_fns = gpr_get_allocation_functions();
@@ -54,16 +48,16 @@ static void test_custom_allocs() {
gpr_allocation_functions fns = {fake_malloc, fake_realloc, fake_free};
gpr_set_allocation_functions(fns);
- GPR_ASSERT((void*)0xdeadbeef == gpr_malloc(0xdeadbeef));
- GPR_ASSERT((void*)0xcafed00d == gpr_realloc(0, 0xcafed00d));
+ GPR_ASSERT((void *)0xdeadbeef == gpr_malloc(0xdeadbeef));
+ GPR_ASSERT((void *)0xcafed00d == gpr_realloc(0, 0xcafed00d));
gpr_free(&addr_to_free);
GPR_ASSERT(addr_to_free == 0xdeadd00d);
/* Restore and check we don't get funky values and that we don't leak */
gpr_set_allocation_functions(default_fns);
- GPR_ASSERT((void*)1 != (i = gpr_malloc(sizeof(*i))));
- GPR_ASSERT((void*)2 != (i = gpr_realloc(i, 2)));
+ GPR_ASSERT((void *)1 != (i = gpr_malloc(sizeof(*i))));
+ GPR_ASSERT((void *)2 != (i = gpr_realloc(i, 2)));
gpr_free(i);
}
diff --git a/test/core/support/cmdline_test.c b/test/core/support/cmdline_test.c
index 1c77c15233..4730fcc1b5 100644
--- a/test/core/support/cmdline_test.c
+++ b/test/core/support/cmdline_test.c
@@ -40,7 +40,7 @@
#include <grpc/support/useful.h>
#include "test/core/util/test_config.h"
-#define LOG_TEST() gpr_log(GPR_INFO, "%s", __FILE__)
+#define LOG_TEST() gpr_log(GPR_INFO, "test at %s:%d", __FILE__, __LINE__)
static void test_simple_int(void) {
int x = 1;
@@ -273,6 +273,44 @@ static void test_many(void) {
gpr_cmdline_destroy(cl);
}
+static void extra_arg_cb(void *user_data, const char *arg) {
+ int *count = user_data;
+ GPR_ASSERT(arg != NULL);
+ GPR_ASSERT(strlen(arg) == 1);
+ GPR_ASSERT(arg[0] == 'a' + *count);
+ ++*count;
+}
+
+static void test_extra(void) {
+ gpr_cmdline *cl;
+ int count = 0;
+ char *args[] = {(char *)__FILE__, "a", "b", "c"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ &count);
+ gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
+ GPR_ASSERT(count == 3);
+ gpr_cmdline_destroy(cl);
+}
+
+static void test_extra_dashdash(void) {
+ gpr_cmdline *cl;
+ int count = 0;
+ char *args[] = {(char *)__FILE__, "--", "a", "b", "c"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ &count);
+ gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args);
+ GPR_ASSERT(count == 3);
+ gpr_cmdline_destroy(cl);
+}
+
static void test_usage(void) {
gpr_cmdline *cl;
char *usage;
@@ -281,20 +319,154 @@ static void test_usage(void) {
int x = 0;
int flag = 2;
+ LOG_TEST();
+
cl = gpr_cmdline_create(NULL);
gpr_cmdline_add_string(cl, "str", NULL, &str);
gpr_cmdline_add_int(cl, "x", NULL, &x);
gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
usage = gpr_cmdline_usage_string(cl, "test");
- GPR_ASSERT(
- 0 == strcmp(usage,
- "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n"));
+ GPR_ASSERT(0 == strcmp(usage,
+ "Usage: test [--str=string] [--x=int] "
+ "[--flag|--no-flag] [file...]\n"));
+ gpr_free(usage);
+
+ usage = gpr_cmdline_usage_string(cl, "/foo/test");
+ GPR_ASSERT(0 == strcmp(usage,
+ "Usage: test [--str=string] [--x=int] "
+ "[--flag|--no-flag] [file...]\n"));
gpr_free(usage);
gpr_cmdline_destroy(cl);
}
+static void test_help(void) {
+ gpr_cmdline *cl;
+
+ char *str = NULL;
+ int x = 0;
+ int flag = 2;
+
+ char *help[] = {(char *)__FILE__, "-h"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_set_survive_failure(cl);
+ gpr_cmdline_add_string(cl, "str", NULL, &str);
+ gpr_cmdline_add_int(cl, "x", NULL, &x);
+ gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
+
+ GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(help), help));
+
+ gpr_cmdline_destroy(cl);
+}
+
+static void test_badargs1(void) {
+ gpr_cmdline *cl;
+
+ char *str = NULL;
+ int x = 0;
+ int flag = 2;
+
+ char *bad_arg_name[] = {(char *)__FILE__, "--y"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_set_survive_failure(cl);
+ gpr_cmdline_add_string(cl, "str", NULL, &str);
+ gpr_cmdline_add_int(cl, "x", NULL, &x);
+ gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
+
+ GPR_ASSERT(0 ==
+ gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_arg_name), bad_arg_name));
+
+ gpr_cmdline_destroy(cl);
+}
+
+static void test_badargs2(void) {
+ gpr_cmdline *cl;
+
+ char *str = NULL;
+ int x = 0;
+ int flag = 2;
+
+ char *bad_int_value[] = {(char *)__FILE__, "--x", "henry"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_set_survive_failure(cl);
+ gpr_cmdline_add_string(cl, "str", NULL, &str);
+ gpr_cmdline_add_int(cl, "x", NULL, &x);
+ gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
+
+ GPR_ASSERT(
+ 0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_int_value), bad_int_value));
+
+ gpr_cmdline_destroy(cl);
+}
+
+static void test_badargs3(void) {
+ gpr_cmdline *cl;
+
+ char *str = NULL;
+ int x = 0;
+ int flag = 2;
+
+ char *bad_bool_value[] = {(char *)__FILE__, "--flag=henry"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_set_survive_failure(cl);
+ gpr_cmdline_add_string(cl, "str", NULL, &str);
+ gpr_cmdline_add_int(cl, "x", NULL, &x);
+ gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
+
+ GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
+ bad_bool_value));
+
+ gpr_cmdline_destroy(cl);
+}
+
+static void test_badargs4(void) {
+ gpr_cmdline *cl;
+
+ char *str = NULL;
+ int x = 0;
+ int flag = 2;
+
+ char *bad_bool_value[] = {(char *)__FILE__, "--no-str"};
+
+ LOG_TEST();
+
+ cl = gpr_cmdline_create(NULL);
+ gpr_cmdline_set_survive_failure(cl);
+ gpr_cmdline_add_string(cl, "str", NULL, &str);
+ gpr_cmdline_add_int(cl, "x", NULL, &x);
+ gpr_cmdline_add_flag(cl, "flag", NULL, &flag);
+ gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
+ NULL);
+
+ GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
+ bad_bool_value));
+
+ gpr_cmdline_destroy(cl);
+}
+
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_simple_int();
@@ -312,6 +484,13 @@ int main(int argc, char **argv) {
test_flag_val_true();
test_flag_val_false();
test_many();
+ test_extra();
+ test_extra_dashdash();
test_usage();
+ test_help();
+ test_badargs1();
+ test_badargs2();
+ test_badargs3();
+ test_badargs4();
return 0;
}
diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c
index f62cbe3435..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 <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
@@ -286,6 +287,53 @@ static void test_strsplit(void) {
gpr_free(parts);
}
+static void 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) == (size_t)gpr_ltoa(LONG_MIN, buf));
+ GPR_ASSERT(0 == strcmp(str, buf));
+ gpr_free(str);
+}
+
+static void 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(-9223372036854775807LL - 1, buf));
+ GPR_ASSERT(0 == strcmp("-9223372036854775808", buf));
+}
+
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
test_strdup();
@@ -296,5 +344,7 @@ int main(int argc, char **argv) {
test_strjoin();
test_strjoin_sep();
test_strsplit();
+ test_ltoa();
+ test_int64toa();
return 0;
}
diff --git a/test/core/support/sync_test.c b/test/core/support/sync_test.c
index 6bc5f792e5..73f68e4dbd 100644
--- a/test/core/support/sync_test.c
+++ b/test/core/support/sync_test.c
@@ -272,7 +272,7 @@ static void test(const char *name, void (*body)(void *m),
test_destroy(m);
}
time_taken = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start);
- fprintf(stderr, " done %ld.%09d s\n", (long)time_taken.tv_sec,
+ fprintf(stderr, " done %lld.%09d s\n", (long long)time_taken.tv_sec,
(int)time_taken.tv_nsec);
}
@@ -430,7 +430,6 @@ static void refinc(void *v /*=m*/) {
mark_thread_done(m);
}
-
/* Wait until m->event is set to (void *)1, then decrement m->refcount by 1
(m->threads * m->iterations * m->incr_step) times, and ensure that the last
decrement caused the counter to reach zero, then mark thread as done. */
diff --git a/test/core/support/thd_test.c b/test/core/support/thd_test.c
index faba33c5e8..f7807d280a 100644
--- a/test/core/support/thd_test.c
+++ b/test/core/support/thd_test.c
@@ -62,6 +62,19 @@ static void thd_body(void *v) {
static void thd_body_joinable(void *v) {}
+/* Test thread options work as expected */
+static void test_options(void) {
+ gpr_thd_options options = gpr_thd_options_default();
+ GPR_ASSERT(!gpr_thd_options_is_joinable(&options));
+ GPR_ASSERT(gpr_thd_options_is_detached(&options));
+ gpr_thd_options_set_joinable(&options);
+ GPR_ASSERT(gpr_thd_options_is_joinable(&options));
+ GPR_ASSERT(!gpr_thd_options_is_detached(&options));
+ gpr_thd_options_set_detached(&options);
+ GPR_ASSERT(!gpr_thd_options_is_joinable(&options));
+ GPR_ASSERT(gpr_thd_options_is_detached(&options));
+}
+
/* Test that we can create a number of threads and wait for them. */
static void test(void) {
int i;
@@ -96,6 +109,7 @@ static void test(void) {
int main(int argc, char *argv[]) {
grpc_test_init(argc, argv);
+ test_options();
test();
return 0;
}
diff --git a/test/core/support/time_test.c b/test/core/support/time_test.c
index ce35edd83c..b921052e7b 100644
--- a/test/core/support/time_test.c
+++ b/test/core/support/time_test.c
@@ -98,7 +98,7 @@ static void test_values(void) {
fprintf(stderr, "far future ");
i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
fprintf(stderr, "\n");
- GPR_ASSERT(x.tv_sec >= INT_MAX);
+ GPR_ASSERT(x.tv_sec == INT64_MAX);
fprintf(stderr, "far future ");
ts_to_s(x, &to_fp, stderr);
fprintf(stderr, "\n");
@@ -107,7 +107,7 @@ static void test_values(void) {
fprintf(stderr, "far past ");
i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
fprintf(stderr, "\n");
- GPR_ASSERT(x.tv_sec <= INT_MIN);
+ GPR_ASSERT(x.tv_sec == INT64_MIN);
fprintf(stderr, "far past ");
ts_to_s(x, &to_fp, stderr);
fprintf(stderr, "\n");