From a82950e68318a6aab6fe894fa39f7fa616c4647b Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 22 Sep 2015 12:33:20 -0700 Subject: clang-format all core files --- test/core/support/cmdline_test.c | 366 ++++++++++------------ test/core/support/env_test.c | 24 +- test/core/support/file_test.c | 177 +++++------ test/core/support/histogram_test.c | 226 +++++++------- test/core/support/host_port_test.c | 47 ++- test/core/support/log_test.c | 30 +- test/core/support/murmur_hash_test.c | 49 ++- test/core/support/slice_buffer_test.c | 69 ++--- test/core/support/slice_test.c | 260 +++++++--------- test/core/support/stack_lockfree_test.c | 147 ++++----- test/core/support/string_test.c | 377 +++++++++++------------ test/core/support/sync_test.c | 529 ++++++++++++++------------------ test/core/support/thd_test.c | 79 ++--- test/core/support/time_test.c | 351 ++++++++++----------- test/core/support/tls_test.c | 47 ++- test/core/support/useful_test.c | 54 ++-- 16 files changed, 1285 insertions(+), 1547 deletions(-) (limited to 'test/core/support') diff --git a/test/core/support/cmdline_test.c b/test/core/support/cmdline_test.c index 5102beb519..1c77c15233 100644 --- a/test/core/support/cmdline_test.c +++ b/test/core/support/cmdline_test.c @@ -42,270 +42,238 @@ #define LOG_TEST() gpr_log(GPR_INFO, "%s", __FILE__) -static void -test_simple_int (void) -{ +static void test_simple_int(void) { int x = 1; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "-foo", "3" }; + char *args[] = {(char *)__FILE__, "-foo", "3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_int (cl, "foo", NULL, &x); - GPR_ASSERT (x == 1); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 3); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "foo", NULL, &x); + GPR_ASSERT(x == 1); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 3); + gpr_cmdline_destroy(cl); } -static void -test_eq_int (void) -{ +static void test_eq_int(void) { int x = 1; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "-foo=3" }; + char *args[] = {(char *)__FILE__, "-foo=3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_int (cl, "foo", NULL, &x); - GPR_ASSERT (x == 1); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 3); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "foo", NULL, &x); + GPR_ASSERT(x == 1); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 3); + gpr_cmdline_destroy(cl); } -static void -test_2dash_int (void) -{ +static void test_2dash_int(void) { int x = 1; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo", "3" }; + char *args[] = {(char *)__FILE__, "--foo", "3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_int (cl, "foo", NULL, &x); - GPR_ASSERT (x == 1); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 3); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "foo", NULL, &x); + GPR_ASSERT(x == 1); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 3); + gpr_cmdline_destroy(cl); } -static void -test_2dash_eq_int (void) -{ +static void test_2dash_eq_int(void) { int x = 1; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=3" }; + char *args[] = {(char *)__FILE__, "--foo=3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_int (cl, "foo", NULL, &x); - GPR_ASSERT (x == 1); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 3); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_int(cl, "foo", NULL, &x); + GPR_ASSERT(x == 1); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 3); + gpr_cmdline_destroy(cl); } -static void -test_simple_string (void) -{ +static void test_simple_string(void) { char *x = NULL; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "-foo", "3" }; + char *args[] = {(char *)__FILE__, "-foo", "3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_string (cl, "foo", NULL, &x); - GPR_ASSERT (x == NULL); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (0 == strcmp (x, "3")); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_string(cl, "foo", NULL, &x); + GPR_ASSERT(x == NULL); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(0 == strcmp(x, "3")); + gpr_cmdline_destroy(cl); } -static void -test_eq_string (void) -{ +static void test_eq_string(void) { char *x = NULL; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "-foo=3" }; + char *args[] = {(char *)__FILE__, "-foo=3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_string (cl, "foo", NULL, &x); - GPR_ASSERT (x == NULL); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (0 == strcmp (x, "3")); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_string(cl, "foo", NULL, &x); + GPR_ASSERT(x == NULL); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(0 == strcmp(x, "3")); + gpr_cmdline_destroy(cl); } -static void -test_2dash_string (void) -{ +static void test_2dash_string(void) { char *x = NULL; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo", "3" }; + char *args[] = {(char *)__FILE__, "--foo", "3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_string (cl, "foo", NULL, &x); - GPR_ASSERT (x == NULL); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (0 == strcmp (x, "3")); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_string(cl, "foo", NULL, &x); + GPR_ASSERT(x == NULL); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(0 == strcmp(x, "3")); + gpr_cmdline_destroy(cl); } -static void -test_2dash_eq_string (void) -{ +static void test_2dash_eq_string(void) { char *x = NULL; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=3" }; + char *args[] = {(char *)__FILE__, "--foo=3"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_string (cl, "foo", NULL, &x); - GPR_ASSERT (x == NULL); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (0 == strcmp (x, "3")); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_string(cl, "foo", NULL, &x); + GPR_ASSERT(x == NULL); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(0 == strcmp(x, "3")); + gpr_cmdline_destroy(cl); } -static void -test_flag_on (void) -{ +static void test_flag_on(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo" }; + char *args[] = {(char *)__FILE__, "--foo"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 1); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 1); + gpr_cmdline_destroy(cl); } -static void -test_flag_no (void) -{ +static void test_flag_no(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--no-foo" }; + char *args[] = {(char *)__FILE__, "--no-foo"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 0); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 0); + gpr_cmdline_destroy(cl); } -static void -test_flag_val_1 (void) -{ +static void test_flag_val_1(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=1" }; + char *args[] = {(char *)__FILE__, "--foo=1"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 1); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 1); + gpr_cmdline_destroy(cl); } -static void -test_flag_val_0 (void) -{ +static void test_flag_val_0(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=0" }; + char *args[] = {(char *)__FILE__, "--foo=0"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 0); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 0); + gpr_cmdline_destroy(cl); } -static void -test_flag_val_true (void) -{ +static void test_flag_val_true(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=true" }; + char *args[] = {(char *)__FILE__, "--foo=true"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 1); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 1); + gpr_cmdline_destroy(cl); } -static void -test_flag_val_false (void) -{ +static void test_flag_val_false(void) { int x = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--foo=false" }; + char *args[] = {(char *)__FILE__, "--foo=false"}; - LOG_TEST (); + LOG_TEST(); - cl = gpr_cmdline_create (NULL); - gpr_cmdline_add_flag (cl, "foo", NULL, &x); - GPR_ASSERT (x == 2); - gpr_cmdline_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 0); - gpr_cmdline_destroy (cl); + cl = gpr_cmdline_create(NULL); + gpr_cmdline_add_flag(cl, "foo", NULL, &x); + GPR_ASSERT(x == 2); + gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 0); + gpr_cmdline_destroy(cl); } -static void -test_many (void) -{ +static void test_many(void) { char *str = NULL; int x = 0; int flag = 2; gpr_cmdline *cl; - char *args[] = { (char *) __FILE__, "--str", "hello", "-x=4", "-no-flag" }; + char *args[] = {(char *)__FILE__, "--str", "hello", "-x=4", "-no-flag"}; - LOG_TEST (); + 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_parse (cl, GPR_ARRAY_SIZE (args), args); - GPR_ASSERT (x == 4); - GPR_ASSERT (0 == strcmp (str, "hello")); - GPR_ASSERT (flag == 0); - gpr_cmdline_destroy (cl); + 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_parse(cl, GPR_ARRAY_SIZE(args), args); + GPR_ASSERT(x == 4); + GPR_ASSERT(0 == strcmp(str, "hello")); + GPR_ASSERT(flag == 0); + gpr_cmdline_destroy(cl); } -static void -test_usage (void) -{ +static void test_usage(void) { gpr_cmdline *cl; char *usage; @@ -313,37 +281,37 @@ test_usage (void) int x = 0; int flag = 2; - 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); + 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); - usage = gpr_cmdline_usage_string (cl, "test"); - GPR_ASSERT (0 == strcmp (usage, "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n")); - gpr_free (usage); + usage = gpr_cmdline_usage_string(cl, "test"); + GPR_ASSERT( + 0 == strcmp(usage, + "Usage: test [--str=string] [--x=int] [--flag|--no-flag]\n")); + gpr_free(usage); - gpr_cmdline_destroy (cl); + gpr_cmdline_destroy(cl); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_simple_int (); - test_eq_int (); - test_2dash_int (); - test_2dash_eq_int (); - test_simple_string (); - test_eq_string (); - test_2dash_string (); - test_2dash_eq_string (); - test_flag_on (); - test_flag_no (); - test_flag_val_1 (); - test_flag_val_0 (); - test_flag_val_true (); - test_flag_val_false (); - test_many (); - test_usage (); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_simple_int(); + test_eq_int(); + test_2dash_int(); + test_2dash_eq_int(); + test_simple_string(); + test_eq_string(); + test_2dash_string(); + test_2dash_eq_string(); + test_flag_on(); + test_flag_no(); + test_flag_val_1(); + test_flag_val_0(); + test_flag_val_true(); + test_flag_val_false(); + test_many(); + test_usage(); return 0; } diff --git a/test/core/support/env_test.c b/test/core/support/env_test.c index f208253540..69aebcc918 100644 --- a/test/core/support/env_test.c +++ b/test/core/support/env_test.c @@ -43,26 +43,22 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x) -static void -test_setenv_getenv (void) -{ +static void test_setenv_getenv(void) { const char *name = "FOO"; const char *value = "BAR"; char *retrieved_value; - LOG_TEST_NAME ("test_setenv_getenv"); + LOG_TEST_NAME("test_setenv_getenv"); - gpr_setenv (name, value); - retrieved_value = gpr_getenv (name); - GPR_ASSERT (retrieved_value != NULL); - GPR_ASSERT (strcmp (value, retrieved_value) == 0); - gpr_free (retrieved_value); + gpr_setenv(name, value); + retrieved_value = gpr_getenv(name); + GPR_ASSERT(retrieved_value != NULL); + GPR_ASSERT(strcmp(value, retrieved_value) == 0); + gpr_free(retrieved_value); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_setenv_getenv (); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_setenv_getenv(); return 0; } diff --git a/test/core/support/file_test.c b/test/core/support/file_test.c index 600448d778..330b2173ef 100644 --- a/test/core/support/file_test.c +++ b/test/core/support/file_test.c @@ -46,63 +46,57 @@ static const char prefix[] = "file_test"; -static void -test_load_empty_file (void) -{ +static void test_load_empty_file(void) { FILE *tmp = NULL; gpr_slice slice; gpr_slice slice_with_null_term; int success; char *tmp_name; - LOG_TEST_NAME ("test_load_empty_file"); + LOG_TEST_NAME("test_load_empty_file"); - tmp = gpr_tmpfile (prefix, &tmp_name); - GPR_ASSERT (tmp_name != NULL); - GPR_ASSERT (tmp != NULL); - fclose (tmp); + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); + GPR_ASSERT(tmp != NULL); + fclose(tmp); - slice = gpr_load_file (tmp_name, 0, &success); - GPR_ASSERT (success == 1); - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == 0); + slice = gpr_load_file(tmp_name, 0, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); - slice_with_null_term = gpr_load_file (tmp_name, 1, &success); - GPR_ASSERT (success == 1); - GPR_ASSERT (GPR_SLICE_LENGTH (slice_with_null_term) == 1); - GPR_ASSERT (GPR_SLICE_START_PTR (slice_with_null_term)[0] == 0); + slice_with_null_term = gpr_load_file(tmp_name, 1, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1); + GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0); - remove (tmp_name); - gpr_free (tmp_name); - gpr_slice_unref (slice); - gpr_slice_unref (slice_with_null_term); + remove(tmp_name); + gpr_free(tmp_name); + gpr_slice_unref(slice); + gpr_slice_unref(slice_with_null_term); } -static void -test_load_failure (void) -{ +static void test_load_failure(void) { FILE *tmp = NULL; gpr_slice slice; int success; char *tmp_name; - LOG_TEST_NAME ("test_load_failure"); + LOG_TEST_NAME("test_load_failure"); - tmp = gpr_tmpfile (prefix, &tmp_name); - GPR_ASSERT (tmp_name != NULL); - GPR_ASSERT (tmp != NULL); - fclose (tmp); - remove (tmp_name); + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); + GPR_ASSERT(tmp != NULL); + fclose(tmp); + remove(tmp_name); - slice = gpr_load_file (tmp_name, 0, &success); - GPR_ASSERT (success == 0); - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == 0); - gpr_free (tmp_name); - gpr_slice_unref (slice); + slice = gpr_load_file(tmp_name, 0, &success); + GPR_ASSERT(success == 0); + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0); + gpr_free(tmp_name); + gpr_slice_unref(slice); } -static void -test_load_small_file (void) -{ +static void test_load_small_file(void) { FILE *tmp = NULL; gpr_slice slice; gpr_slice slice_with_null_term; @@ -110,33 +104,32 @@ test_load_small_file (void) char *tmp_name; const char *blah = "blah"; - LOG_TEST_NAME ("test_load_small_file"); - - tmp = gpr_tmpfile (prefix, &tmp_name); - GPR_ASSERT (tmp_name != NULL); - GPR_ASSERT (tmp != NULL); - GPR_ASSERT (fwrite (blah, 1, strlen (blah), tmp) == strlen (blah)); - fclose (tmp); - - slice = gpr_load_file (tmp_name, 0, &success); - GPR_ASSERT (success == 1); - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == strlen (blah)); - GPR_ASSERT (!memcmp (GPR_SLICE_START_PTR (slice), blah, strlen (blah))); - - slice_with_null_term = gpr_load_file (tmp_name, 1, &success); - GPR_ASSERT (success == 1); - GPR_ASSERT (GPR_SLICE_LENGTH (slice_with_null_term) == (strlen (blah) + 1)); - GPR_ASSERT (strcmp ((const char *) GPR_SLICE_START_PTR (slice_with_null_term), blah) == 0); - - remove (tmp_name); - gpr_free (tmp_name); - gpr_slice_unref (slice); - gpr_slice_unref (slice_with_null_term); + LOG_TEST_NAME("test_load_small_file"); + + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp_name != NULL); + GPR_ASSERT(tmp != NULL); + GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah)); + fclose(tmp); + + slice = gpr_load_file(tmp_name, 0, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah)); + GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah))); + + slice_with_null_term = gpr_load_file(tmp_name, 1, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1)); + GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term), + blah) == 0); + + remove(tmp_name); + gpr_free(tmp_name); + gpr_slice_unref(slice); + gpr_slice_unref(slice_with_null_term); } -static void -test_load_big_file (void) -{ +static void test_load_big_file(void) { FILE *tmp = NULL; gpr_slice slice; int success; @@ -145,40 +138,36 @@ test_load_big_file (void) unsigned char *current; size_t i; - LOG_TEST_NAME ("test_load_big_file"); - - for (i = 0; i < sizeof (buffer); i++) - { - buffer[i] = 42; - } - - tmp = gpr_tmpfile (prefix, &tmp_name); - GPR_ASSERT (tmp != NULL); - GPR_ASSERT (tmp_name != NULL); - GPR_ASSERT (fwrite (buffer, 1, sizeof (buffer), tmp) == sizeof (buffer)); - fclose (tmp); - - slice = gpr_load_file (tmp_name, 0, &success); - GPR_ASSERT (success == 1); - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == sizeof (buffer)); - current = GPR_SLICE_START_PTR (slice); - for (i = 0; i < sizeof (buffer); i++) - { - GPR_ASSERT (current[i] == 42); - } - - remove (tmp_name); - gpr_free (tmp_name); - gpr_slice_unref (slice); + LOG_TEST_NAME("test_load_big_file"); + + for (i = 0; i < sizeof(buffer); i++) { + buffer[i] = 42; + } + + tmp = gpr_tmpfile(prefix, &tmp_name); + GPR_ASSERT(tmp != NULL); + GPR_ASSERT(tmp_name != NULL); + GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer)); + fclose(tmp); + + slice = gpr_load_file(tmp_name, 0, &success); + GPR_ASSERT(success == 1); + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer)); + current = GPR_SLICE_START_PTR(slice); + for (i = 0; i < sizeof(buffer); i++) { + GPR_ASSERT(current[i] == 42); + } + + remove(tmp_name); + gpr_free(tmp_name); + gpr_slice_unref(slice); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_load_empty_file (); - test_load_failure (); - test_load_small_file (); - test_load_big_file (); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_load_empty_file(); + test_load_failure(); + test_load_small_file(); + test_load_big_file(); return 0; } diff --git a/test/core/support/histogram_test.c b/test/core/support/histogram_test.c index d13aeb651d..be935ede09 100644 --- a/test/core/support/histogram_test.c +++ b/test/core/support/histogram_test.c @@ -36,155 +36,143 @@ #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x); -static void -test_no_op (void) -{ - gpr_histogram_destroy (gpr_histogram_create (0.01, 60e9)); +static void test_no_op(void) { + gpr_histogram_destroy(gpr_histogram_create(0.01, 60e9)); } -static void -expect_percentile (gpr_histogram * h, double percentile, double min_expect, double max_expect) -{ - double got = gpr_histogram_percentile (h, percentile); - gpr_log (GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, max_expect); - GPR_ASSERT (min_expect <= got); - GPR_ASSERT (got <= max_expect); +static void expect_percentile(gpr_histogram *h, double percentile, + double min_expect, double max_expect) { + double got = gpr_histogram_percentile(h, percentile); + gpr_log(GPR_INFO, "@%f%%, expect %f <= %f <= %f", percentile, min_expect, got, + max_expect); + GPR_ASSERT(min_expect <= got); + GPR_ASSERT(got <= max_expect); } -static void -test_simple (void) -{ +static void test_simple(void) { gpr_histogram *h; - LOG_TEST ("test_simple"); + LOG_TEST("test_simple"); - h = gpr_histogram_create (0.01, 60e9); - gpr_histogram_add (h, 10000); - gpr_histogram_add (h, 10000); - gpr_histogram_add (h, 11000); - gpr_histogram_add (h, 11000); + h = gpr_histogram_create(0.01, 60e9); + gpr_histogram_add(h, 10000); + gpr_histogram_add(h, 10000); + gpr_histogram_add(h, 11000); + gpr_histogram_add(h, 11000); - expect_percentile (h, 50, 10001, 10999); - GPR_ASSERT (gpr_histogram_mean (h) == 10500); + expect_percentile(h, 50, 10001, 10999); + GPR_ASSERT(gpr_histogram_mean(h) == 10500); - gpr_histogram_destroy (h); + gpr_histogram_destroy(h); } -static void -test_percentile (void) -{ +static void test_percentile(void) { gpr_histogram *h; double last; double i; double cur; - LOG_TEST ("test_percentile"); - - h = gpr_histogram_create (0.05, 1e9); - gpr_histogram_add (h, 2.5); - gpr_histogram_add (h, 2.5); - gpr_histogram_add (h, 8); - gpr_histogram_add (h, 4); - - GPR_ASSERT (gpr_histogram_count (h) == 4); - GPR_ASSERT (gpr_histogram_minimum (h) == 2.5); - GPR_ASSERT (gpr_histogram_maximum (h) == 8); - GPR_ASSERT (gpr_histogram_sum (h) == 17); - GPR_ASSERT (gpr_histogram_sum_of_squares (h) == 92.5); - GPR_ASSERT (gpr_histogram_mean (h) == 4.25); - GPR_ASSERT (gpr_histogram_variance (h) == 5.0625); - GPR_ASSERT (gpr_histogram_stddev (h) == 2.25); - - expect_percentile (h, -10, 2.5, 2.5); - expect_percentile (h, 0, 2.5, 2.5); - expect_percentile (h, 12.5, 2.5, 2.5); - expect_percentile (h, 25, 2.5, 2.5); - expect_percentile (h, 37.5, 2.5, 2.8); - expect_percentile (h, 50, 3.0, 3.5); - expect_percentile (h, 62.5, 3.5, 4.5); - expect_percentile (h, 75, 5, 7.9); - expect_percentile (h, 100, 8, 8); - expect_percentile (h, 110, 8, 8); + LOG_TEST("test_percentile"); + + h = gpr_histogram_create(0.05, 1e9); + gpr_histogram_add(h, 2.5); + gpr_histogram_add(h, 2.5); + gpr_histogram_add(h, 8); + gpr_histogram_add(h, 4); + + GPR_ASSERT(gpr_histogram_count(h) == 4); + GPR_ASSERT(gpr_histogram_minimum(h) == 2.5); + GPR_ASSERT(gpr_histogram_maximum(h) == 8); + GPR_ASSERT(gpr_histogram_sum(h) == 17); + GPR_ASSERT(gpr_histogram_sum_of_squares(h) == 92.5); + GPR_ASSERT(gpr_histogram_mean(h) == 4.25); + GPR_ASSERT(gpr_histogram_variance(h) == 5.0625); + GPR_ASSERT(gpr_histogram_stddev(h) == 2.25); + + expect_percentile(h, -10, 2.5, 2.5); + expect_percentile(h, 0, 2.5, 2.5); + expect_percentile(h, 12.5, 2.5, 2.5); + expect_percentile(h, 25, 2.5, 2.5); + expect_percentile(h, 37.5, 2.5, 2.8); + expect_percentile(h, 50, 3.0, 3.5); + expect_percentile(h, 62.5, 3.5, 4.5); + expect_percentile(h, 75, 5, 7.9); + expect_percentile(h, 100, 8, 8); + expect_percentile(h, 110, 8, 8); /* test monotonicity */ last = 0.0; - for (i = 0; i < 100.0; i += 0.01) - { - cur = gpr_histogram_percentile (h, i); - GPR_ASSERT (cur >= last); - last = cur; - } - - gpr_histogram_destroy (h); + for (i = 0; i < 100.0; i += 0.01) { + cur = gpr_histogram_percentile(h, i); + GPR_ASSERT(cur >= last); + last = cur; + } + + gpr_histogram_destroy(h); } -static void -test_merge (void) -{ +static void test_merge(void) { gpr_histogram *h1, *h2; double last; double i; double cur; - LOG_TEST ("test_merge"); - - h1 = gpr_histogram_create (0.05, 1e9); - gpr_histogram_add (h1, 2.5); - gpr_histogram_add (h1, 2.5); - gpr_histogram_add (h1, 8); - gpr_histogram_add (h1, 4); - - h2 = gpr_histogram_create (0.01, 1e9); - GPR_ASSERT (gpr_histogram_merge (h1, h2) == 0); - gpr_histogram_destroy (h2); - - h2 = gpr_histogram_create (0.05, 1e10); - GPR_ASSERT (gpr_histogram_merge (h1, h2) == 0); - gpr_histogram_destroy (h2); - - h2 = gpr_histogram_create (0.05, 1e9); - GPR_ASSERT (gpr_histogram_merge (h1, h2) == 1); - GPR_ASSERT (gpr_histogram_count (h1) == 4); - GPR_ASSERT (gpr_histogram_minimum (h1) == 2.5); - GPR_ASSERT (gpr_histogram_maximum (h1) == 8); - GPR_ASSERT (gpr_histogram_sum (h1) == 17); - GPR_ASSERT (gpr_histogram_sum_of_squares (h1) == 92.5); - GPR_ASSERT (gpr_histogram_mean (h1) == 4.25); - GPR_ASSERT (gpr_histogram_variance (h1) == 5.0625); - GPR_ASSERT (gpr_histogram_stddev (h1) == 2.25); - gpr_histogram_destroy (h2); - - h2 = gpr_histogram_create (0.05, 1e9); - gpr_histogram_add (h2, 7.0); - gpr_histogram_add (h2, 17.0); - gpr_histogram_add (h2, 1.0); - GPR_ASSERT (gpr_histogram_merge (h1, h2) == 1); - GPR_ASSERT (gpr_histogram_count (h1) == 7); - GPR_ASSERT (gpr_histogram_minimum (h1) == 1.0); - GPR_ASSERT (gpr_histogram_maximum (h1) == 17.0); - GPR_ASSERT (gpr_histogram_sum (h1) == 42.0); - GPR_ASSERT (gpr_histogram_sum_of_squares (h1) == 431.5); - GPR_ASSERT (gpr_histogram_mean (h1) == 6.0); + LOG_TEST("test_merge"); + + h1 = gpr_histogram_create(0.05, 1e9); + gpr_histogram_add(h1, 2.5); + gpr_histogram_add(h1, 2.5); + gpr_histogram_add(h1, 8); + gpr_histogram_add(h1, 4); + + h2 = gpr_histogram_create(0.01, 1e9); + GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); + gpr_histogram_destroy(h2); + + h2 = gpr_histogram_create(0.05, 1e10); + GPR_ASSERT(gpr_histogram_merge(h1, h2) == 0); + gpr_histogram_destroy(h2); + + h2 = gpr_histogram_create(0.05, 1e9); + GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); + GPR_ASSERT(gpr_histogram_count(h1) == 4); + GPR_ASSERT(gpr_histogram_minimum(h1) == 2.5); + GPR_ASSERT(gpr_histogram_maximum(h1) == 8); + GPR_ASSERT(gpr_histogram_sum(h1) == 17); + GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 92.5); + GPR_ASSERT(gpr_histogram_mean(h1) == 4.25); + GPR_ASSERT(gpr_histogram_variance(h1) == 5.0625); + GPR_ASSERT(gpr_histogram_stddev(h1) == 2.25); + gpr_histogram_destroy(h2); + + h2 = gpr_histogram_create(0.05, 1e9); + gpr_histogram_add(h2, 7.0); + gpr_histogram_add(h2, 17.0); + gpr_histogram_add(h2, 1.0); + GPR_ASSERT(gpr_histogram_merge(h1, h2) == 1); + GPR_ASSERT(gpr_histogram_count(h1) == 7); + GPR_ASSERT(gpr_histogram_minimum(h1) == 1.0); + GPR_ASSERT(gpr_histogram_maximum(h1) == 17.0); + GPR_ASSERT(gpr_histogram_sum(h1) == 42.0); + GPR_ASSERT(gpr_histogram_sum_of_squares(h1) == 431.5); + GPR_ASSERT(gpr_histogram_mean(h1) == 6.0); /* test monotonicity */ last = 0.0; - for (i = 0; i < 100.0; i += 0.01) - { - cur = gpr_histogram_percentile (h1, i); - GPR_ASSERT (cur >= last); - last = cur; - } - - gpr_histogram_destroy (h1); - gpr_histogram_destroy (h2); + for (i = 0; i < 100.0; i += 0.01) { + cur = gpr_histogram_percentile(h1, i); + GPR_ASSERT(cur >= last); + last = cur; + } + + gpr_histogram_destroy(h1); + gpr_histogram_destroy(h2); } -int -main (void) -{ - test_no_op (); - test_simple (); - test_percentile (); - test_merge (); +int main(void) { + test_no_op(); + test_simple(); + test_percentile(); + test_merge(); return 0; } diff --git a/test/core/support/host_port_test.c b/test/core/support/host_port_test.c index 71a774191b..eccc39a2db 100644 --- a/test/core/support/host_port_test.c +++ b/test/core/support/host_port_test.c @@ -38,43 +38,36 @@ #include #include "test/core/util/test_config.h" -static void -join_host_port_expect (const char *host, int port, const char *expected) -{ +static void join_host_port_expect(const char *host, int port, + const char *expected) { char *buf; int len; - len = gpr_join_host_port (&buf, host, port); - GPR_ASSERT (len >= 0); - GPR_ASSERT (strlen (expected) == (size_t) len); - GPR_ASSERT (strcmp (expected, buf) == 0); - gpr_free (buf); + len = gpr_join_host_port(&buf, host, port); + GPR_ASSERT(len >= 0); + GPR_ASSERT(strlen(expected) == (size_t)len); + GPR_ASSERT(strcmp(expected, buf) == 0); + gpr_free(buf); } -static void -test_join_host_port (void) -{ - join_host_port_expect ("foo", 101, "foo:101"); - join_host_port_expect ("", 102, ":102"); - join_host_port_expect ("1::2", 103, "[1::2]:103"); - join_host_port_expect ("[::1]", 104, "[::1]:104"); +static void test_join_host_port(void) { + join_host_port_expect("foo", 101, "foo:101"); + join_host_port_expect("", 102, ":102"); + join_host_port_expect("1::2", 103, "[1::2]:103"); + join_host_port_expect("[::1]", 104, "[::1]:104"); } /* Garbage in, garbage out. */ -static void -test_join_host_port_garbage (void) -{ - join_host_port_expect ("[foo]", 105, "[foo]:105"); - join_host_port_expect ("[::", 106, "[:::106"); - join_host_port_expect ("::]", 107, "[::]]:107"); +static void test_join_host_port_garbage(void) { + join_host_port_expect("[foo]", 105, "[foo]:105"); + join_host_port_expect("[::", 106, "[:::106"); + join_host_port_expect("::]", 107, "[::]]:107"); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); - test_join_host_port (); - test_join_host_port_garbage (); + test_join_host_port(); + test_join_host_port_garbage(); return 0; } diff --git a/test/core/support/log_test.c b/test/core/support/log_test.c index c30ce6958f..b39b069913 100644 --- a/test/core/support/log_test.c +++ b/test/core/support/log_test.c @@ -37,27 +37,23 @@ #include "test/core/util/test_config.h" -static void -test_callback (gpr_log_func_args * args) -{ - GPR_ASSERT (0 == strcmp (__FILE__, args->file)); - GPR_ASSERT (args->severity == GPR_LOG_SEVERITY_INFO); - GPR_ASSERT (0 == strcmp (args->message, "hello 1 2 3")); +static void test_callback(gpr_log_func_args *args) { + GPR_ASSERT(0 == strcmp(__FILE__, args->file)); + GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO); + GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3")); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); /* test logging at various verbosity levels */ - gpr_log (GPR_DEBUG, "%s", "hello world"); - gpr_log (GPR_INFO, "%s", "hello world"); - gpr_log (GPR_ERROR, "%s", "hello world"); + gpr_log(GPR_DEBUG, "%s", "hello world"); + gpr_log(GPR_INFO, "%s", "hello world"); + gpr_log(GPR_ERROR, "%s", "hello world"); /* should succeed */ - GPR_ASSERT (1); - gpr_set_log_function (test_callback); - gpr_log_message (GPR_INFO, "hello 1 2 3"); - gpr_log (GPR_INFO, "hello %d %d %d", 1, 2, 3); + GPR_ASSERT(1); + gpr_set_log_function(test_callback); + gpr_log_message(GPR_INFO, "hello 1 2 3"); + gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3); /* TODO(ctiller): should we add a GPR_ASSERT failure test here */ return 0; } diff --git a/test/core/support/murmur_hash_test.c b/test/core/support/murmur_hash_test.c index 946a800c60..1762486776 100644 --- a/test/core/support/murmur_hash_test.c +++ b/test/core/support/murmur_hash_test.c @@ -38,58 +38,51 @@ #include -typedef gpr_uint32 (*hash_func) (const void *key, size_t len, gpr_uint32 seed); +typedef gpr_uint32 (*hash_func)(const void *key, size_t len, gpr_uint32 seed); /* From smhasher: This should hopefully be a thorough and uambiguous test of whether a hash is correctly implemented on a given platform */ -static void -verification_test (hash_func hash, gpr_uint32 expected) -{ +static void verification_test(hash_func hash, gpr_uint32 expected) { gpr_uint8 key[256]; gpr_uint32 hashes[256]; gpr_uint32 final = 0; size_t i; - memset (key, 0, sizeof (key)); - memset (hashes, 0, sizeof (hashes)); + memset(key, 0, sizeof(key)); + memset(hashes, 0, sizeof(hashes)); /* Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as the seed */ - for (i = 0; i < 256; i++) - { - key[i] = (gpr_uint8) i; - hashes[i] = hash (key, i, (gpr_uint32) (256u - i)); - } + for (i = 0; i < 256; i++) { + key[i] = (gpr_uint8)i; + hashes[i] = hash(key, i, (gpr_uint32)(256u - i)); + } /* Then hash the result array */ - final = hash (hashes, sizeof (hashes), 0); + final = hash(hashes, sizeof(hashes), 0); /* The first four bytes of that hash, interpreted as a little-endian integer, is our verification value */ - if (expected != final) - { - gpr_log (GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)", final, expected); - abort (); - } - else - { - gpr_log (GPR_INFO, "Verification value 0x%08X : Passed!", final); - } + if (expected != final) { + gpr_log(GPR_INFO, "Verification value 0x%08X : Failed! (Expected 0x%08x)", + final, expected); + abort(); + } else { + gpr_log(GPR_INFO, "Verification value 0x%08X : Passed!", final); + } } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); /* basic tests to verify that things don't crash */ - gpr_murmur_hash3 ("", 0, 0); - gpr_murmur_hash3 ("xyz", 3, 0); - verification_test (gpr_murmur_hash3, 0xB0F57EE3); + gpr_murmur_hash3("", 0, 0); + gpr_murmur_hash3("xyz", 3, 0); + verification_test(gpr_murmur_hash3, 0xB0F57EE3); return 0; } diff --git a/test/core/support/slice_buffer_test.c b/test/core/support/slice_buffer_test.c index 9254f709e3..a48278434f 100644 --- a/test/core/support/slice_buffer_test.c +++ b/test/core/support/slice_buffer_test.c @@ -35,46 +35,41 @@ #include #include "test/core/util/test_config.h" -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { gpr_slice_buffer buf; - gpr_slice aaa = gpr_slice_from_copied_string ("aaa"); - gpr_slice bb = gpr_slice_from_copied_string ("bb"); + gpr_slice aaa = gpr_slice_from_copied_string("aaa"); + gpr_slice bb = gpr_slice_from_copied_string("bb"); size_t i; - grpc_test_init (argc, argv); - gpr_slice_buffer_init (&buf); - for (i = 0; i < 10; i++) - { - gpr_slice_ref (aaa); - gpr_slice_ref (bb); - gpr_slice_buffer_add (&buf, aaa); - gpr_slice_buffer_add (&buf, bb); - } - GPR_ASSERT (buf.count > 0); - GPR_ASSERT (buf.length == 50); - gpr_slice_buffer_reset_and_unref (&buf); - GPR_ASSERT (buf.count == 0); - GPR_ASSERT (buf.length == 0); - for (i = 0; i < 10; i++) - { - gpr_slice_ref (aaa); - gpr_slice_ref (bb); - gpr_slice_buffer_add (&buf, aaa); - gpr_slice_buffer_add (&buf, bb); - } - GPR_ASSERT (buf.count > 0); - GPR_ASSERT (buf.length == 50); - for (i = 0; i < 10; i++) - { - gpr_slice_buffer_pop (&buf); - gpr_slice_unref (aaa); - gpr_slice_unref (bb); - } - GPR_ASSERT (buf.count == 0); - GPR_ASSERT (buf.length == 0); - gpr_slice_buffer_destroy (&buf); + grpc_test_init(argc, argv); + gpr_slice_buffer_init(&buf); + for (i = 0; i < 10; i++) { + gpr_slice_ref(aaa); + gpr_slice_ref(bb); + gpr_slice_buffer_add(&buf, aaa); + gpr_slice_buffer_add(&buf, bb); + } + GPR_ASSERT(buf.count > 0); + GPR_ASSERT(buf.length == 50); + gpr_slice_buffer_reset_and_unref(&buf); + GPR_ASSERT(buf.count == 0); + GPR_ASSERT(buf.length == 0); + for (i = 0; i < 10; i++) { + gpr_slice_ref(aaa); + gpr_slice_ref(bb); + gpr_slice_buffer_add(&buf, aaa); + gpr_slice_buffer_add(&buf, bb); + } + GPR_ASSERT(buf.count > 0); + GPR_ASSERT(buf.length == 50); + for (i = 0; i < 10; i++) { + gpr_slice_buffer_pop(&buf); + gpr_slice_unref(aaa); + gpr_slice_unref(bb); + } + GPR_ASSERT(buf.count == 0); + GPR_ASSERT(buf.length == 0); + gpr_slice_buffer_destroy(&buf); return 0; } diff --git a/test/core/support/slice_test.c b/test/core/support/slice_test.c index e041532c8b..1d202f0618 100644 --- a/test/core/support/slice_test.c +++ b/test/core/support/slice_test.c @@ -41,220 +41,188 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x); -static void -test_slice_malloc_returns_something_sensible (void) -{ +static void test_slice_malloc_returns_something_sensible(void) { /* Calls gpr_slice_create for various lengths and verifies the internals for consistency. */ size_t length; size_t i; gpr_slice slice; - LOG_TEST_NAME ("test_slice_malloc_returns_something_sensible"); - - for (length = 0; length <= 1024; length++) - { - slice = gpr_slice_malloc (length); - /* If there is a length, slice.data must be non-NULL. If length is zero - we don't care. */ - if (length) - { - GPR_ASSERT (GPR_SLICE_START_PTR (slice)); - } - /* Returned slice length must be what was requested. */ - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == length); - /* If the slice has a refcount, it must be destroyable. */ - if (slice.refcount) - { - GPR_ASSERT (slice.refcount->ref != NULL); - GPR_ASSERT (slice.refcount->unref != NULL); - } - /* We must be able to write to every byte of the data */ - for (i = 0; i < length; i++) - { - GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; - } - /* And finally we must succeed in destroying the slice */ - gpr_slice_unref (slice); + LOG_TEST_NAME("test_slice_malloc_returns_something_sensible"); + + for (length = 0; length <= 1024; length++) { + slice = gpr_slice_malloc(length); + /* If there is a length, slice.data must be non-NULL. If length is zero + we don't care. */ + if (length) { + GPR_ASSERT(GPR_SLICE_START_PTR(slice)); + } + /* Returned slice length must be what was requested. */ + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == length); + /* If the slice has a refcount, it must be destroyable. */ + if (slice.refcount) { + GPR_ASSERT(slice.refcount->ref != NULL); + GPR_ASSERT(slice.refcount->unref != NULL); + } + /* We must be able to write to every byte of the data */ + for (i = 0; i < length; i++) { + GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; } + /* And finally we must succeed in destroying the slice */ + gpr_slice_unref(slice); + } } -static void -do_nothing (void *ignored) -{ -} +static void do_nothing(void *ignored) {} -static void -test_slice_new_returns_something_sensible (void) -{ +static void test_slice_new_returns_something_sensible(void) { gpr_uint8 x; - gpr_slice slice = gpr_slice_new (&x, 1, do_nothing); - GPR_ASSERT (slice.refcount); - GPR_ASSERT (slice.data.refcounted.bytes == &x); - GPR_ASSERT (slice.data.refcounted.length == 1); - gpr_slice_unref (slice); + gpr_slice slice = gpr_slice_new(&x, 1, do_nothing); + GPR_ASSERT(slice.refcount); + GPR_ASSERT(slice.data.refcounted.bytes == &x); + GPR_ASSERT(slice.data.refcounted.length == 1); + gpr_slice_unref(slice); } static int do_nothing_with_len_1_calls = 0; -static void -do_nothing_with_len_1 (void *ignored, size_t len) -{ - GPR_ASSERT (len == 1); +static void do_nothing_with_len_1(void *ignored, size_t len) { + GPR_ASSERT(len == 1); do_nothing_with_len_1_calls++; } -static void -test_slice_new_with_len_returns_something_sensible (void) -{ +static void test_slice_new_with_len_returns_something_sensible(void) { gpr_uint8 x; - gpr_slice slice = gpr_slice_new_with_len (&x, 1, do_nothing_with_len_1); - GPR_ASSERT (slice.refcount); - GPR_ASSERT (slice.data.refcounted.bytes == &x); - GPR_ASSERT (slice.data.refcounted.length == 1); - GPR_ASSERT (do_nothing_with_len_1_calls == 0); - gpr_slice_unref (slice); - GPR_ASSERT (do_nothing_with_len_1_calls == 1); + gpr_slice slice = gpr_slice_new_with_len(&x, 1, do_nothing_with_len_1); + GPR_ASSERT(slice.refcount); + GPR_ASSERT(slice.data.refcounted.bytes == &x); + GPR_ASSERT(slice.data.refcounted.length == 1); + GPR_ASSERT(do_nothing_with_len_1_calls == 0); + gpr_slice_unref(slice); + GPR_ASSERT(do_nothing_with_len_1_calls == 1); } -static void -test_slice_sub_works (unsigned length) -{ +static void test_slice_sub_works(unsigned length) { gpr_slice slice; gpr_slice sub; unsigned i, j, k; - LOG_TEST_NAME ("test_slice_sub_works"); - gpr_log (GPR_INFO, "length=%d", length); + LOG_TEST_NAME("test_slice_sub_works"); + gpr_log(GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc (length); - for (i = 0; i < length; i++) - { - GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; - } + slice = gpr_slice_malloc(length); + for (i = 0; i < length; i++) { + GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) - { - for (j = i; j < length; j++) - { - sub = gpr_slice_sub (slice, i, j); - GPR_ASSERT (GPR_SLICE_LENGTH (sub) == j - i); - for (k = 0; k < j - i; k++) - { - GPR_ASSERT (GPR_SLICE_START_PTR (sub)[k] == (gpr_uint8) (i + k)); - } - gpr_slice_unref (sub); - } + for (i = 0; i < length; i++) { + for (j = i; j < length; j++) { + sub = gpr_slice_sub(slice, i, j); + GPR_ASSERT(GPR_SLICE_LENGTH(sub) == j - i); + for (k = 0; k < j - i; k++) { + GPR_ASSERT(GPR_SLICE_START_PTR(sub)[k] == (gpr_uint8)(i + k)); + } + gpr_slice_unref(sub); } - gpr_slice_unref (slice); + } + gpr_slice_unref(slice); } -static void -check_head_tail (gpr_slice slice, gpr_slice head, gpr_slice tail) -{ - GPR_ASSERT (GPR_SLICE_LENGTH (slice) == GPR_SLICE_LENGTH (head) + GPR_SLICE_LENGTH (tail)); - GPR_ASSERT (0 == memcmp (GPR_SLICE_START_PTR (slice), GPR_SLICE_START_PTR (head), GPR_SLICE_LENGTH (head))); - GPR_ASSERT (0 == memcmp (GPR_SLICE_START_PTR (slice) + GPR_SLICE_LENGTH (head), GPR_SLICE_START_PTR (tail), GPR_SLICE_LENGTH (tail))); +static void check_head_tail(gpr_slice slice, gpr_slice head, gpr_slice tail) { + GPR_ASSERT(GPR_SLICE_LENGTH(slice) == + GPR_SLICE_LENGTH(head) + GPR_SLICE_LENGTH(tail)); + GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice), GPR_SLICE_START_PTR(head), + GPR_SLICE_LENGTH(head))); + GPR_ASSERT(0 == memcmp(GPR_SLICE_START_PTR(slice) + GPR_SLICE_LENGTH(head), + GPR_SLICE_START_PTR(tail), GPR_SLICE_LENGTH(tail))); } -static void -test_slice_split_head_works (size_t length) -{ +static void test_slice_split_head_works(size_t length) { gpr_slice slice; gpr_slice head, tail; size_t i; - LOG_TEST_NAME ("test_slice_split_head_works"); - gpr_log (GPR_INFO, "length=%d", length); + LOG_TEST_NAME("test_slice_split_head_works"); + gpr_log(GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc (length); - for (i = 0; i < length; i++) - { - GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; - } + slice = gpr_slice_malloc(length); + for (i = 0; i < length; i++) { + GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) - { - tail = gpr_slice_ref (slice); - head = gpr_slice_split_head (&tail, i); - check_head_tail (slice, head, tail); - gpr_slice_unref (tail); - gpr_slice_unref (head); - } - - gpr_slice_unref (slice); + for (i = 0; i < length; i++) { + tail = gpr_slice_ref(slice); + head = gpr_slice_split_head(&tail, i); + check_head_tail(slice, head, tail); + gpr_slice_unref(tail); + gpr_slice_unref(head); + } + + gpr_slice_unref(slice); } -static void -test_slice_split_tail_works (size_t length) -{ +static void test_slice_split_tail_works(size_t length) { gpr_slice slice; gpr_slice head, tail; size_t i; - LOG_TEST_NAME ("test_slice_split_tail_works"); - gpr_log (GPR_INFO, "length=%d", length); + LOG_TEST_NAME("test_slice_split_tail_works"); + gpr_log(GPR_INFO, "length=%d", length); /* Create a slice in which each byte is equal to the distance from it to the beginning of the slice. */ - slice = gpr_slice_malloc (length); - for (i = 0; i < length; i++) - { - GPR_SLICE_START_PTR (slice)[i] = (gpr_uint8) i; - } + slice = gpr_slice_malloc(length); + for (i = 0; i < length; i++) { + GPR_SLICE_START_PTR(slice)[i] = (gpr_uint8)i; + } /* Ensure that for all subsets length is correct and that we start on the correct byte. Additionally check that no copies were made. */ - for (i = 0; i < length; i++) - { - head = gpr_slice_ref (slice); - tail = gpr_slice_split_tail (&head, i); - check_head_tail (slice, head, tail); - gpr_slice_unref (tail); - gpr_slice_unref (head); - } - - gpr_slice_unref (slice); + for (i = 0; i < length; i++) { + head = gpr_slice_ref(slice); + tail = gpr_slice_split_tail(&head, i); + check_head_tail(slice, head, tail); + gpr_slice_unref(tail); + gpr_slice_unref(head); + } + + gpr_slice_unref(slice); } -static void -test_slice_from_copied_string_works (void) -{ +static void test_slice_from_copied_string_works(void) { static const char *text = "HELLO WORLD!"; gpr_slice slice; - LOG_TEST_NAME ("test_slice_from_copied_string_works"); + LOG_TEST_NAME("test_slice_from_copied_string_works"); - slice = gpr_slice_from_copied_string (text); - GPR_ASSERT (strlen (text) == GPR_SLICE_LENGTH (slice)); - GPR_ASSERT (0 == memcmp (text, GPR_SLICE_START_PTR (slice), GPR_SLICE_LENGTH (slice))); - gpr_slice_unref (slice); + slice = gpr_slice_from_copied_string(text); + GPR_ASSERT(strlen(text) == GPR_SLICE_LENGTH(slice)); + GPR_ASSERT(0 == + memcmp(text, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice))); + gpr_slice_unref(slice); } -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { unsigned length; - grpc_test_init (argc, argv); - test_slice_malloc_returns_something_sensible (); - test_slice_new_returns_something_sensible (); - test_slice_new_with_len_returns_something_sensible (); - for (length = 0; length < 128; length++) - { - test_slice_sub_works (length); - test_slice_split_head_works (length); - test_slice_split_tail_works (length); - } - test_slice_from_copied_string_works (); + grpc_test_init(argc, argv); + test_slice_malloc_returns_something_sensible(); + test_slice_new_returns_something_sensible(); + test_slice_new_with_len_returns_something_sensible(); + for (length = 0; length < 128; length++) { + test_slice_sub_works(length); + test_slice_split_head_works(length); + test_slice_split_tail_works(length); + } + test_slice_from_copied_string_works(); return 0; } diff --git a/test/core/support/stack_lockfree_test.c b/test/core/support/stack_lockfree_test.c index 8ad4ad26df..0f49e6fa52 100644 --- a/test/core/support/stack_lockfree_test.c +++ b/test/core/support/stack_lockfree_test.c @@ -46,51 +46,42 @@ #define MAX_THREADS 32 -static void -test_serial_sized (size_t size) -{ - gpr_stack_lockfree *stack = gpr_stack_lockfree_create (size); +static void test_serial_sized(size_t size) { + gpr_stack_lockfree *stack = gpr_stack_lockfree_create(size); size_t i; size_t j; /* First try popping empty */ - GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); + GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); /* Now add one item and check it */ - gpr_stack_lockfree_push (stack, 3); - GPR_ASSERT (gpr_stack_lockfree_pop (stack) == 3); - GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); + gpr_stack_lockfree_push(stack, 3); + GPR_ASSERT(gpr_stack_lockfree_pop(stack) == 3); + GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); /* Now add repeatedly more items and check them */ - for (i = 1; i < size; i *= 2) - { - for (j = 0; j <= i; j++) - { - GPR_ASSERT (gpr_stack_lockfree_push (stack, (int) j) == (j == 0)); - } - for (j = 0; j <= i; j++) - { - GPR_ASSERT (gpr_stack_lockfree_pop (stack) == (int) (i - j)); - } - GPR_ASSERT (gpr_stack_lockfree_pop (stack) == -1); + for (i = 1; i < size; i *= 2) { + for (j = 0; j <= i; j++) { + GPR_ASSERT(gpr_stack_lockfree_push(stack, (int)j) == (j == 0)); } + for (j = 0; j <= i; j++) { + GPR_ASSERT(gpr_stack_lockfree_pop(stack) == (int)(i - j)); + } + GPR_ASSERT(gpr_stack_lockfree_pop(stack) == -1); + } - gpr_stack_lockfree_destroy (stack); + gpr_stack_lockfree_destroy(stack); } -static void -test_serial () -{ +static void test_serial() { size_t i; - for (i = 128; i < MAX_STACK_SIZE; i *= 2) - { - test_serial_sized (i); - } - test_serial_sized (MAX_STACK_SIZE); + for (i = 128; i < MAX_STACK_SIZE; i *= 2) { + test_serial_sized(i); + } + test_serial_sized(MAX_STACK_SIZE); } -struct test_arg -{ +struct test_arg { gpr_stack_lockfree *stack; int stack_size; int nthreads; @@ -98,83 +89,67 @@ struct test_arg int sum; }; -static void -test_mt_body (void *v) -{ - struct test_arg *arg = (struct test_arg *) v; +static void test_mt_body(void *v) { + struct test_arg *arg = (struct test_arg *)v; int lo, hi; int i; int res; lo = arg->rank * arg->stack_size / arg->nthreads; hi = (arg->rank + 1) * arg->stack_size / arg->nthreads; - for (i = lo; i < hi; i++) - { - gpr_stack_lockfree_push (arg->stack, i); - if ((res = gpr_stack_lockfree_pop (arg->stack)) != -1) - { - arg->sum += res; - } - } - while ((res = gpr_stack_lockfree_pop (arg->stack)) != -1) - { + for (i = lo; i < hi; i++) { + gpr_stack_lockfree_push(arg->stack, i); + if ((res = gpr_stack_lockfree_pop(arg->stack)) != -1) { arg->sum += res; } + } + while ((res = gpr_stack_lockfree_pop(arg->stack)) != -1) { + arg->sum += res; + } } -static void -test_mt_sized (size_t size, int nth) -{ +static void test_mt_sized(size_t size, int nth) { gpr_stack_lockfree *stack; struct test_arg args[MAX_THREADS]; gpr_thd_id thds[MAX_THREADS]; int sum; int i; - gpr_thd_options options = gpr_thd_options_default (); - - stack = gpr_stack_lockfree_create (size); - for (i = 0; i < nth; i++) - { - args[i].stack = stack; - args[i].stack_size = (int) size; - args[i].nthreads = nth; - args[i].rank = i; - args[i].sum = 0; - } - gpr_thd_options_set_joinable (&options); - for (i = 0; i < nth; i++) - { - GPR_ASSERT (gpr_thd_new (&thds[i], test_mt_body, &args[i], &options)); - } + gpr_thd_options options = gpr_thd_options_default(); + + stack = gpr_stack_lockfree_create(size); + for (i = 0; i < nth; i++) { + args[i].stack = stack; + args[i].stack_size = (int)size; + args[i].nthreads = nth; + args[i].rank = i; + args[i].sum = 0; + } + gpr_thd_options_set_joinable(&options); + for (i = 0; i < nth; i++) { + GPR_ASSERT(gpr_thd_new(&thds[i], test_mt_body, &args[i], &options)); + } sum = 0; - for (i = 0; i < nth; i++) - { - gpr_thd_join (thds[i]); - sum = sum + args[i].sum; - } - GPR_ASSERT ((unsigned) sum == ((unsigned) size * (size - 1)) / 2); - gpr_stack_lockfree_destroy (stack); + for (i = 0; i < nth; i++) { + gpr_thd_join(thds[i]); + sum = sum + args[i].sum; + } + GPR_ASSERT((unsigned)sum == ((unsigned)size * (size - 1)) / 2); + gpr_stack_lockfree_destroy(stack); } -static void -test_mt () -{ +static void test_mt() { size_t size; int nth; - for (nth = 1; nth < MAX_THREADS; nth++) - { - for (size = 128; size < MAX_STACK_SIZE; size *= 2) - { - test_mt_sized (size, nth); - } - test_mt_sized (MAX_STACK_SIZE, nth); + for (nth = 1; nth < MAX_THREADS; nth++) { + for (size = 128; size < MAX_STACK_SIZE; size *= 2) { + test_mt_sized(size, nth); } + test_mt_sized(MAX_STACK_SIZE, nth); + } } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_serial (); - test_mt (); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_serial(); + test_mt(); return 0; } diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c index faf4d01a11..f62cbe3435 100644 --- a/test/core/support/string_test.c +++ b/test/core/support/string_test.c @@ -45,273 +45,256 @@ #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x) -static void -test_strdup (void) -{ +static void test_strdup(void) { static const char *src1 = "hello world"; char *dst1; - LOG_TEST_NAME ("test_strdup"); + LOG_TEST_NAME("test_strdup"); - dst1 = gpr_strdup (src1); - GPR_ASSERT (0 == strcmp (src1, dst1)); - gpr_free (dst1); + dst1 = gpr_strdup(src1); + GPR_ASSERT(0 == strcmp(src1, dst1)); + gpr_free(dst1); - GPR_ASSERT (NULL == gpr_strdup (NULL)); + GPR_ASSERT(NULL == gpr_strdup(NULL)); } -static void -expect_dump (const char *buf, size_t len, gpr_uint32 flags, const char *result) -{ - char *got = gpr_dump (buf, len, flags); - GPR_ASSERT (0 == strcmp (got, result)); - gpr_free (got); +static void expect_dump(const char *buf, size_t len, gpr_uint32 flags, + const char *result) { + char *got = gpr_dump(buf, len, flags); + GPR_ASSERT(0 == strcmp(got, result)); + gpr_free(got); } -static void -test_dump (void) -{ - LOG_TEST_NAME ("test_dump"); - expect_dump ("\x01", 1, GPR_DUMP_HEX, "01"); - expect_dump ("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); - expect_dump ("\x01\x02", 2, GPR_DUMP_HEX, "01 02"); - expect_dump ("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX, "01 23 45 67 89 ab cd ef"); - expect_dump ("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'"); +static void test_dump(void) { + LOG_TEST_NAME("test_dump"); + expect_dump("\x01", 1, GPR_DUMP_HEX, "01"); + expect_dump("\x01", 1, GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); + expect_dump("\x01\x02", 2, GPR_DUMP_HEX, "01 02"); + expect_dump("\x01\x23\x45\x67\x89\xab\xcd\xef", 8, GPR_DUMP_HEX, + "01 23 45 67 89 ab cd ef"); + expect_dump("ab", 2, GPR_DUMP_HEX | GPR_DUMP_ASCII, "61 62 'ab'"); } -static void -expect_slice_dump (gpr_slice slice, gpr_uint32 flags, const char *result) -{ - char *got = gpr_dump_slice (slice, flags); - GPR_ASSERT (0 == strcmp (got, result)); - gpr_free (got); - gpr_slice_unref (slice); +static void expect_slice_dump(gpr_slice slice, gpr_uint32 flags, + const char *result) { + char *got = gpr_dump_slice(slice, flags); + GPR_ASSERT(0 == strcmp(got, result)); + gpr_free(got); + gpr_slice_unref(slice); } -static void -test_dump_slice (void) -{ +static void test_dump_slice(void) { static const char *text = "HELLO WORLD!"; - static const char *long_text = "It was a bright cold day in April, and the clocks were striking " "thirteen. Winston Smith, his chin nuzzled into his breast in an effort " "to escape the vile wind, slipped quickly through the glass doors of " "Victory Mansions, though not quickly enough to prevent a swirl of " "gritty dust from entering along with him."; - - LOG_TEST_NAME ("test_dump_slice"); - - expect_slice_dump (gpr_slice_from_copied_string (text), GPR_DUMP_ASCII, text); - expect_slice_dump (gpr_slice_from_copied_string (long_text), GPR_DUMP_ASCII, long_text); - expect_slice_dump (gpr_slice_from_copied_buffer ("\x01", 1), GPR_DUMP_HEX, "01"); - expect_slice_dump (gpr_slice_from_copied_buffer ("\x01", 1), GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); + static const char *long_text = + "It was a bright cold day in April, and the clocks were striking " + "thirteen. Winston Smith, his chin nuzzled into his breast in an effort " + "to escape the vile wind, slipped quickly through the glass doors of " + "Victory Mansions, though not quickly enough to prevent a swirl of " + "gritty dust from entering along with him."; + + LOG_TEST_NAME("test_dump_slice"); + + expect_slice_dump(gpr_slice_from_copied_string(text), GPR_DUMP_ASCII, text); + expect_slice_dump(gpr_slice_from_copied_string(long_text), GPR_DUMP_ASCII, + long_text); + expect_slice_dump(gpr_slice_from_copied_buffer("\x01", 1), GPR_DUMP_HEX, + "01"); + expect_slice_dump(gpr_slice_from_copied_buffer("\x01", 1), + GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'"); } -static void -test_pu32_fail (const char *s) -{ +static void test_pu32_fail(const char *s) { gpr_uint32 out; - GPR_ASSERT (!gpr_parse_bytes_to_uint32 (s, strlen (s), &out)); + GPR_ASSERT(!gpr_parse_bytes_to_uint32(s, strlen(s), &out)); } -static void -test_pu32_succeed (const char *s, gpr_uint32 want) -{ +static void test_pu32_succeed(const char *s, gpr_uint32 want) { gpr_uint32 out; - GPR_ASSERT (gpr_parse_bytes_to_uint32 (s, strlen (s), &out)); - GPR_ASSERT (out == want); + GPR_ASSERT(gpr_parse_bytes_to_uint32(s, strlen(s), &out)); + GPR_ASSERT(out == want); } -static void -test_parse_uint32 (void) -{ - LOG_TEST_NAME ("test_parse_uint32"); - - test_pu32_fail ("-1"); - test_pu32_fail ("a"); - test_pu32_fail (""); - test_pu32_succeed ("0", 0); - test_pu32_succeed ("1", 1); - test_pu32_succeed ("2", 2); - test_pu32_succeed ("3", 3); - test_pu32_succeed ("4", 4); - test_pu32_succeed ("5", 5); - test_pu32_succeed ("6", 6); - test_pu32_succeed ("7", 7); - test_pu32_succeed ("8", 8); - test_pu32_succeed ("9", 9); - test_pu32_succeed ("10", 10); - test_pu32_succeed ("11", 11); - test_pu32_succeed ("12", 12); - test_pu32_succeed ("13", 13); - test_pu32_succeed ("14", 14); - test_pu32_succeed ("15", 15); - test_pu32_succeed ("16", 16); - test_pu32_succeed ("17", 17); - test_pu32_succeed ("18", 18); - test_pu32_succeed ("19", 19); - test_pu32_succeed ("1234567890", 1234567890); - test_pu32_succeed ("4294967295", 4294967295u); - test_pu32_fail ("4294967296"); - test_pu32_fail ("4294967297"); - test_pu32_fail ("4294967298"); - test_pu32_fail ("4294967299"); +static void test_parse_uint32(void) { + LOG_TEST_NAME("test_parse_uint32"); + + test_pu32_fail("-1"); + test_pu32_fail("a"); + test_pu32_fail(""); + test_pu32_succeed("0", 0); + test_pu32_succeed("1", 1); + test_pu32_succeed("2", 2); + test_pu32_succeed("3", 3); + test_pu32_succeed("4", 4); + test_pu32_succeed("5", 5); + test_pu32_succeed("6", 6); + test_pu32_succeed("7", 7); + test_pu32_succeed("8", 8); + test_pu32_succeed("9", 9); + test_pu32_succeed("10", 10); + test_pu32_succeed("11", 11); + test_pu32_succeed("12", 12); + test_pu32_succeed("13", 13); + test_pu32_succeed("14", 14); + test_pu32_succeed("15", 15); + test_pu32_succeed("16", 16); + test_pu32_succeed("17", 17); + test_pu32_succeed("18", 18); + test_pu32_succeed("19", 19); + test_pu32_succeed("1234567890", 1234567890); + test_pu32_succeed("4294967295", 4294967295u); + test_pu32_fail("4294967296"); + test_pu32_fail("4294967297"); + test_pu32_fail("4294967298"); + test_pu32_fail("4294967299"); } -static void -test_asprintf (void) -{ +static void test_asprintf(void) { char *buf; int i, j; - LOG_TEST_NAME ("test_asprintf"); + LOG_TEST_NAME("test_asprintf"); /* Print an empty string. */ - GPR_ASSERT (gpr_asprintf (&buf, "") == 0); - GPR_ASSERT (buf[0] == '\0'); - gpr_free (buf); + GPR_ASSERT(gpr_asprintf(&buf, "") == 0); + GPR_ASSERT(buf[0] == '\0'); + gpr_free(buf); /* Print strings of various lengths. */ - for (i = 1; i < 100; i++) - { - GPR_ASSERT (gpr_asprintf (&buf, "%0*d", i, 1) == i); - - /* The buffer should resemble "000001\0". */ - for (j = 0; j < i - 2; j++) - { - GPR_ASSERT (buf[j] == '0'); - } - GPR_ASSERT (buf[i - 1] == '1'); - GPR_ASSERT (buf[i] == '\0'); - gpr_free (buf); + for (i = 1; i < 100; i++) { + GPR_ASSERT(gpr_asprintf(&buf, "%0*d", i, 1) == i); + + /* The buffer should resemble "000001\0". */ + for (j = 0; j < i - 2; j++) { + GPR_ASSERT(buf[j] == '0'); } + GPR_ASSERT(buf[i - 1] == '1'); + GPR_ASSERT(buf[i] == '\0'); + gpr_free(buf); + } } -static void -test_strjoin (void) -{ - const char *parts[4] = { "one", "two", "three", "four" }; +static void test_strjoin(void) { + const char *parts[4] = {"one", "two", "three", "four"}; size_t joined_len; char *joined; - LOG_TEST_NAME ("test_strjoin"); + LOG_TEST_NAME("test_strjoin"); - joined = gpr_strjoin (parts, 4, &joined_len); - GPR_ASSERT (0 == strcmp ("onetwothreefour", joined)); - gpr_free (joined); + joined = gpr_strjoin(parts, 4, &joined_len); + GPR_ASSERT(0 == strcmp("onetwothreefour", joined)); + gpr_free(joined); - joined = gpr_strjoin (parts, 0, &joined_len); - GPR_ASSERT (0 == strcmp ("", joined)); - gpr_free (joined); + joined = gpr_strjoin(parts, 0, &joined_len); + GPR_ASSERT(0 == strcmp("", joined)); + gpr_free(joined); - joined = gpr_strjoin (parts, 1, &joined_len); - GPR_ASSERT (0 == strcmp ("one", joined)); - gpr_free (joined); + joined = gpr_strjoin(parts, 1, &joined_len); + GPR_ASSERT(0 == strcmp("one", joined)); + gpr_free(joined); } -static void -test_strjoin_sep (void) -{ - const char *parts[4] = { "one", "two", "three", "four" }; +static void test_strjoin_sep(void) { + const char *parts[4] = {"one", "two", "three", "four"}; size_t joined_len; char *joined; - LOG_TEST_NAME ("test_strjoin_sep"); + LOG_TEST_NAME("test_strjoin_sep"); - joined = gpr_strjoin_sep (parts, 4, ", ", &joined_len); - GPR_ASSERT (0 == strcmp ("one, two, three, four", joined)); - gpr_free (joined); + joined = gpr_strjoin_sep(parts, 4, ", ", &joined_len); + GPR_ASSERT(0 == strcmp("one, two, three, four", joined)); + gpr_free(joined); /* empty separator */ - joined = gpr_strjoin_sep (parts, 4, "", &joined_len); - GPR_ASSERT (0 == strcmp ("onetwothreefour", joined)); - gpr_free (joined); + joined = gpr_strjoin_sep(parts, 4, "", &joined_len); + GPR_ASSERT(0 == strcmp("onetwothreefour", joined)); + gpr_free(joined); /* degenerated case specifying zero input parts */ - joined = gpr_strjoin_sep (parts, 0, ", ", &joined_len); - GPR_ASSERT (0 == strcmp ("", joined)); - gpr_free (joined); + joined = gpr_strjoin_sep(parts, 0, ", ", &joined_len); + GPR_ASSERT(0 == strcmp("", joined)); + gpr_free(joined); /* single part should have no separator */ - joined = gpr_strjoin_sep (parts, 1, ", ", &joined_len); - GPR_ASSERT (0 == strcmp ("one", joined)); - gpr_free (joined); + joined = gpr_strjoin_sep(parts, 1, ", ", &joined_len); + GPR_ASSERT(0 == strcmp("one", joined)); + gpr_free(joined); } -static void -test_strsplit (void) -{ +static void test_strsplit(void) { gpr_slice_buffer *parts; gpr_slice str; - LOG_TEST_NAME ("test_strsplit"); + LOG_TEST_NAME("test_strsplit"); - parts = gpr_malloc (sizeof (gpr_slice_buffer)); - gpr_slice_buffer_init (parts); + parts = gpr_malloc(sizeof(gpr_slice_buffer)); + gpr_slice_buffer_init(parts); - str = gpr_slice_from_copied_string ("one, two, three, four"); - gpr_slice_split (str, ", ", parts); - GPR_ASSERT (4 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "one")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "two")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[2], "three")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[3], "four")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); + str = gpr_slice_from_copied_string("one, two, three, four"); + gpr_slice_split(str, ", ", parts); + GPR_ASSERT(4 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "two")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[2], "three")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[3], "four")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); /* separator not present in string */ - str = gpr_slice_from_copied_string ("one two three four"); - gpr_slice_split (str, ", ", parts); - GPR_ASSERT (1 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "one two three four")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); + str = gpr_slice_from_copied_string("one two three four"); + gpr_slice_split(str, ", ", parts); + GPR_ASSERT(1 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one two three four")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); /* separator at the end */ - str = gpr_slice_from_copied_string ("foo,"); - gpr_slice_split (str, ",", parts); - GPR_ASSERT (2 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "foo")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); + str = gpr_slice_from_copied_string("foo,"); + gpr_slice_split(str, ",", parts); + GPR_ASSERT(2 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "foo")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); /* separator at the beginning */ - str = gpr_slice_from_copied_string (",foo"); - gpr_slice_split (str, ",", parts); - GPR_ASSERT (2 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "foo")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); + str = gpr_slice_from_copied_string(",foo"); + gpr_slice_split(str, ",", parts); + GPR_ASSERT(2 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "foo")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); /* standalone separator */ - str = gpr_slice_from_copied_string (","); - gpr_slice_split (str, ",", parts); - GPR_ASSERT (2 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[1], "")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); + str = gpr_slice_from_copied_string(","); + gpr_slice_split(str, ",", parts); + GPR_ASSERT(2 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); /* empty input */ - str = gpr_slice_from_copied_string (""); - gpr_slice_split (str, ", ", parts); - GPR_ASSERT (1 == parts->count); - GPR_ASSERT (0 == gpr_slice_str_cmp (parts->slices[0], "")); - gpr_slice_buffer_reset_and_unref (parts); - gpr_slice_unref (str); - - gpr_slice_buffer_destroy (parts); - gpr_free (parts); + str = gpr_slice_from_copied_string(""); + gpr_slice_split(str, ", ", parts); + GPR_ASSERT(1 == parts->count); + GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "")); + gpr_slice_buffer_reset_and_unref(parts); + gpr_slice_unref(str); + + gpr_slice_buffer_destroy(parts); + gpr_free(parts); } -int -main (int argc, char **argv) -{ - grpc_test_init (argc, argv); - test_strdup (); - test_dump (); - test_dump_slice (); - test_parse_uint32 (); - test_asprintf (); - test_strjoin (); - test_strjoin_sep (); - test_strsplit (); +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + test_strdup(); + test_dump(); + test_dump_slice(); + test_parse_uint32(); + test_asprintf(); + test_strjoin(); + test_strjoin_sep(); + test_strsplit(); return 0; } diff --git a/test/core/support/sync_test.c b/test/core/support/sync_test.c index 72788a75ba..220b16b60f 100644 --- a/test/core/support/sync_test.c +++ b/test/core/support/sync_test.c @@ -49,44 +49,37 @@ #define N 4 -typedef struct queue -{ - gpr_cv non_empty; /* Signalled when length becomes non-zero. */ - gpr_cv non_full; /* Signalled when length becomes non-N. */ - gpr_mu mu; /* Protects all fields below. - (That is, except during initialization or - destruction, the fields below should be accessed - only by a thread that holds mu.) */ - int head; /* Index of head of queue 0..N-1. */ - int length; /* Number of valid elements in queue 0..N. */ - int elem[N]; /* elem[head .. head+length-1] are queue elements. */ +typedef struct queue { + gpr_cv non_empty; /* Signalled when length becomes non-zero. */ + gpr_cv non_full; /* Signalled when length becomes non-N. */ + gpr_mu mu; /* Protects all fields below. + (That is, except during initialization or + destruction, the fields below should be accessed + only by a thread that holds mu.) */ + int head; /* Index of head of queue 0..N-1. */ + int length; /* Number of valid elements in queue 0..N. */ + int elem[N]; /* elem[head .. head+length-1] are queue elements. */ } queue; /* Initialize *q. */ -void -queue_init (queue * q) -{ - gpr_mu_init (&q->mu); - gpr_cv_init (&q->non_empty); - gpr_cv_init (&q->non_full); +void queue_init(queue *q) { + gpr_mu_init(&q->mu); + gpr_cv_init(&q->non_empty); + gpr_cv_init(&q->non_full); q->head = 0; q->length = 0; } /* Free storage associated with *q. */ -void -queue_destroy (queue * q) -{ - gpr_mu_destroy (&q->mu); - gpr_cv_destroy (&q->non_empty); - gpr_cv_destroy (&q->non_full); +void queue_destroy(queue *q) { + gpr_mu_destroy(&q->mu); + gpr_cv_destroy(&q->non_empty); + gpr_cv_destroy(&q->non_full); } /* Wait until there is room in *q, then append x to *q. */ -void -queue_append (queue * q, int x) -{ - gpr_mu_lock (&q->mu); +void queue_append(queue *q, int x) { + gpr_mu_lock(&q->mu); /* To wait for a predicate without a deadline, loop on the negation of the predicate, and use gpr_cv_wait(..., gpr_inf_future(GPR_CLOCK_REALTIME)) inside the loop @@ -94,91 +87,78 @@ queue_append (queue * q, int x) makes the condition true should use gpr_cv_broadcast() on the corresponding condition variable. The predicate must be on state protected by the lock. */ - while (q->length == N) - { - gpr_cv_wait (&q->non_full, &q->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); - } - if (q->length == 0) - { /* Wake threads blocked in queue_remove(). */ - /* It's normal to use gpr_cv_broadcast() or gpr_signal() while - holding the lock. */ - gpr_cv_broadcast (&q->non_empty); - } + while (q->length == N) { + gpr_cv_wait(&q->non_full, &q->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); + } + if (q->length == 0) { /* Wake threads blocked in queue_remove(). */ + /* It's normal to use gpr_cv_broadcast() or gpr_signal() while + holding the lock. */ + gpr_cv_broadcast(&q->non_empty); + } q->elem[(q->head + q->length) % N] = x; q->length++; - gpr_mu_unlock (&q->mu); + gpr_mu_unlock(&q->mu); } /* If it can be done without blocking, append x to *q and return non-zero. Otherwise return 0. */ -int -queue_try_append (queue * q, int x) -{ +int queue_try_append(queue *q, int x) { int result = 0; - if (gpr_mu_trylock (&q->mu)) - { - if (q->length != N) - { - if (q->length == 0) - { /* Wake threads blocked in queue_remove(). */ - gpr_cv_broadcast (&q->non_empty); - } - q->elem[(q->head + q->length) % N] = x; - q->length++; - result = 1; - } - gpr_mu_unlock (&q->mu); + if (gpr_mu_trylock(&q->mu)) { + if (q->length != N) { + if (q->length == 0) { /* Wake threads blocked in queue_remove(). */ + gpr_cv_broadcast(&q->non_empty); + } + q->elem[(q->head + q->length) % N] = x; + q->length++; + result = 1; } + gpr_mu_unlock(&q->mu); + } return result; } /* Wait until the *q is non-empty or deadline abs_deadline passes. If the queue is non-empty, remove its head entry, place it in *head, and return non-zero. Otherwise return 0. */ -int -queue_remove (queue * q, int *head, gpr_timespec abs_deadline) -{ +int queue_remove(queue *q, int *head, gpr_timespec abs_deadline) { int result = 0; - gpr_mu_lock (&q->mu); + gpr_mu_lock(&q->mu); /* To wait for a predicate with a deadline, loop on the negation of the predicate or until gpr_cv_wait() returns true. Code that makes the condition true should use gpr_cv_broadcast() on the corresponding condition variable. The predicate must be on state protected by the lock. */ - while (q->length == 0 && !gpr_cv_wait (&q->non_empty, &q->mu, abs_deadline)) - { + while (q->length == 0 && !gpr_cv_wait(&q->non_empty, &q->mu, abs_deadline)) { + } + if (q->length != 0) { /* Queue is non-empty. */ + result = 1; + if (q->length == N) { /* Wake threads blocked in queue_append(). */ + gpr_cv_broadcast(&q->non_full); } - if (q->length != 0) - { /* Queue is non-empty. */ - result = 1; - if (q->length == N) - { /* Wake threads blocked in queue_append(). */ - gpr_cv_broadcast (&q->non_full); - } - *head = q->elem[q->head]; - q->head = (q->head + 1) % N; - q->length--; - } /* else deadline exceeded */ - gpr_mu_unlock (&q->mu); + *head = q->elem[q->head]; + q->head = (q->head + 1) % N; + q->length--; + } /* else deadline exceeded */ + gpr_mu_unlock(&q->mu); return result; } /* ------------------------------------------------- */ /* Tests for gpr_mu and gpr_cv, and the queue example. */ -struct test -{ - int threads; /* number of threads */ +struct test { + int threads; /* number of threads */ - gpr_int64 iterations; /* number of iterations per thread */ + gpr_int64 iterations; /* number of iterations per thread */ gpr_int64 counter; - int thread_count; /* used to allocate thread ids */ - int done; /* threads not yet completed */ + int thread_count; /* used to allocate thread ids */ + int done; /* threads not yet completed */ - gpr_mu mu; /* protects iterations, counter, thread_count, done */ + gpr_mu mu; /* protects iterations, counter, thread_count, done */ - gpr_cv cv; /* signalling depends on test */ + gpr_cv cv; /* signalling depends on test */ - gpr_cv done_cv; /* signalled when done == 0 */ + gpr_cv done_cv; /* signalled when done == 0 */ queue q; @@ -190,338 +170,287 @@ struct test }; /* Return pointer to a new struct test. */ -static struct test * -test_new (int threads, gpr_int64 iterations) -{ - struct test *m = gpr_malloc (sizeof (*m)); +static struct test *test_new(int threads, gpr_int64 iterations) { + struct test *m = gpr_malloc(sizeof(*m)); m->threads = threads; m->iterations = iterations; m->counter = 0; m->thread_count = 0; m->done = threads; - gpr_mu_init (&m->mu); - gpr_cv_init (&m->cv); - gpr_cv_init (&m->done_cv); - queue_init (&m->q); - gpr_stats_init (&m->stats_counter, 0); - gpr_ref_init (&m->refcount, 0); - gpr_ref_init (&m->thread_refcount, threads); - gpr_event_init (&m->event); + gpr_mu_init(&m->mu); + gpr_cv_init(&m->cv); + gpr_cv_init(&m->done_cv); + queue_init(&m->q); + gpr_stats_init(&m->stats_counter, 0); + gpr_ref_init(&m->refcount, 0); + gpr_ref_init(&m->thread_refcount, threads); + gpr_event_init(&m->event); return m; } /* Return pointer to a new struct test. */ -static void -test_destroy (struct test *m) -{ - gpr_mu_destroy (&m->mu); - gpr_cv_destroy (&m->cv); - gpr_cv_destroy (&m->done_cv); - queue_destroy (&m->q); - gpr_free (m); +static void test_destroy(struct test *m) { + gpr_mu_destroy(&m->mu); + gpr_cv_destroy(&m->cv); + gpr_cv_destroy(&m->done_cv); + queue_destroy(&m->q); + gpr_free(m); } /* Create m->threads threads, each running (*body)(m) */ -static void -test_create_threads (struct test *m, void (*body) (void *arg)) -{ +static void test_create_threads(struct test *m, void (*body)(void *arg)) { gpr_thd_id id; int i; - for (i = 0; i != m->threads; i++) - { - GPR_ASSERT (gpr_thd_new (&id, body, m, NULL)); - } + for (i = 0; i != m->threads; i++) { + GPR_ASSERT(gpr_thd_new(&id, body, m, NULL)); + } } /* Wait until all threads report done. */ -static void -test_wait (struct test *m) -{ - gpr_mu_lock (&m->mu); - while (m->done != 0) - { - gpr_cv_wait (&m->done_cv, &m->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock (&m->mu); +static void test_wait(struct test *m) { + gpr_mu_lock(&m->mu); + while (m->done != 0) { + gpr_cv_wait(&m->done_cv, &m->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock(&m->mu); } /* Get an integer thread id in the raneg 0..threads-1 */ -static int -thread_id (struct test *m) -{ +static int thread_id(struct test *m) { int id; - gpr_mu_lock (&m->mu); + gpr_mu_lock(&m->mu); id = m->thread_count++; - gpr_mu_unlock (&m->mu); + gpr_mu_unlock(&m->mu); return id; } /* Indicate that a thread is done, by decrementing m->done and signalling done_cv if m->done==0. */ -static void -mark_thread_done (struct test *m) -{ - gpr_mu_lock (&m->mu); - GPR_ASSERT (m->done != 0); +static void mark_thread_done(struct test *m) { + gpr_mu_lock(&m->mu); + GPR_ASSERT(m->done != 0); m->done--; - if (m->done == 0) - { - gpr_cv_signal (&m->done_cv); - } - gpr_mu_unlock (&m->mu); + if (m->done == 0) { + gpr_cv_signal(&m->done_cv); + } + gpr_mu_unlock(&m->mu); } /* Test several threads running (*body)(struct test *m) for increasing settings of m->iterations, until about timeout_s to 2*timeout_s seconds have elapsed. If extra!=NULL, run (*extra)(m) in an additional thread. */ -static void -test (const char *name, void (*body) (void *m), void (*extra) (void *m), int timeout_s) -{ +static void test(const char *name, void (*body)(void *m), + void (*extra)(void *m), int timeout_s) { gpr_int64 iterations = 1024; struct test *m; - gpr_timespec start = gpr_now (GPR_CLOCK_REALTIME); + gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME); gpr_timespec time_taken; - gpr_timespec deadline = gpr_time_add (start, gpr_time_from_micros (timeout_s * 1000000, GPR_TIMESPAN)); - fprintf (stderr, "%s:", name); - while (gpr_time_cmp (gpr_now (GPR_CLOCK_REALTIME), deadline) < 0) - { - iterations <<= 1; - fprintf (stderr, " %ld", (long) iterations); - m = test_new (10, iterations); - if (extra != NULL) - { - gpr_thd_id id; - GPR_ASSERT (gpr_thd_new (&id, extra, m, NULL)); - m->done++; /* one more thread to wait for */ - } - test_create_threads (m, body); - test_wait (m); - if (m->counter != m->threads * m->iterations) - { - fprintf (stderr, "counter %ld threads %d iterations %ld\n", (long) m->counter, m->threads, (long) m->iterations); - GPR_ASSERT (0); - } - test_destroy (m); + gpr_timespec deadline = gpr_time_add( + start, gpr_time_from_micros(timeout_s * 1000000, GPR_TIMESPAN)); + fprintf(stderr, "%s:", name); + while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0) { + iterations <<= 1; + fprintf(stderr, " %ld", (long)iterations); + m = test_new(10, iterations); + if (extra != NULL) { + gpr_thd_id id; + GPR_ASSERT(gpr_thd_new(&id, extra, m, NULL)); + m->done++; /* one more thread to wait for */ + } + test_create_threads(m, body); + test_wait(m); + if (m->counter != m->threads * m->iterations) { + fprintf(stderr, "counter %ld threads %d iterations %ld\n", + (long)m->counter, m->threads, (long)m->iterations); + GPR_ASSERT(0); } - time_taken = gpr_time_sub (gpr_now (GPR_CLOCK_REALTIME), start); - fprintf (stderr, " done %ld.%09d s\n", (long) time_taken.tv_sec, (int) time_taken.tv_nsec); + 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, + (int)time_taken.tv_nsec); } /* Increment m->counter on each iteration; then mark thread as done. */ -static void -inc (void *v /*=m*/ ) -{ +static void inc(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) - { - gpr_mu_lock (&m->mu); - m->counter++; - gpr_mu_unlock (&m->mu); - } - mark_thread_done (m); + for (i = 0; i != m->iterations; i++) { + gpr_mu_lock(&m->mu); + m->counter++; + gpr_mu_unlock(&m->mu); + } + mark_thread_done(m); } /* Increment m->counter under lock acquired with trylock, m->iterations times; then mark thread as done. */ -static void -inctry (void *v /*=m*/ ) -{ +static void inctry(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations;) - { - if (gpr_mu_trylock (&m->mu)) - { - m->counter++; - gpr_mu_unlock (&m->mu); - i++; - } + for (i = 0; i != m->iterations;) { + if (gpr_mu_trylock(&m->mu)) { + m->counter++; + gpr_mu_unlock(&m->mu); + i++; } - mark_thread_done (m); + } + mark_thread_done(m); } /* Increment counter only when (m->counter%m->threads)==m->thread_id; then mark thread as done. */ -static void -inc_by_turns (void *v /*=m*/ ) -{ +static void inc_by_turns(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - int id = thread_id (m); - for (i = 0; i != m->iterations; i++) - { - gpr_mu_lock (&m->mu); - while ((m->counter % m->threads) != id) - { - gpr_cv_wait (&m->cv, &m->mu, gpr_inf_future (GPR_CLOCK_REALTIME)); - } - m->counter++; - gpr_cv_broadcast (&m->cv); - gpr_mu_unlock (&m->mu); + int id = thread_id(m); + for (i = 0; i != m->iterations; i++) { + gpr_mu_lock(&m->mu); + while ((m->counter % m->threads) != id) { + gpr_cv_wait(&m->cv, &m->mu, gpr_inf_future(GPR_CLOCK_REALTIME)); } - mark_thread_done (m); + m->counter++; + gpr_cv_broadcast(&m->cv); + gpr_mu_unlock(&m->mu); + } + mark_thread_done(m); } /* Wait a millisecond and increment counter on each iteration; then mark thread as done. */ -static void -inc_with_1ms_delay (void *v /*=m*/ ) -{ +static void inc_with_1ms_delay(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) - { - gpr_timespec deadline; - gpr_mu_lock (&m->mu); - deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000, GPR_TIMESPAN)); - while (!gpr_cv_wait (&m->cv, &m->mu, deadline)) - { - } - m->counter++; - gpr_mu_unlock (&m->mu); + for (i = 0; i != m->iterations; i++) { + gpr_timespec deadline; + gpr_mu_lock(&m->mu); + deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_micros(1000, GPR_TIMESPAN)); + while (!gpr_cv_wait(&m->cv, &m->mu, deadline)) { } - mark_thread_done (m); + m->counter++; + gpr_mu_unlock(&m->mu); + } + mark_thread_done(m); } /* Wait a millisecond and increment counter on each iteration, using an event for timing; then mark thread as done. */ -static void -inc_with_1ms_delay_event (void *v /*=m*/ ) -{ +static void inc_with_1ms_delay_event(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) - { - gpr_timespec deadline; - deadline = gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000, GPR_TIMESPAN)); - GPR_ASSERT (gpr_event_wait (&m->event, deadline) == NULL); - gpr_mu_lock (&m->mu); - m->counter++; - gpr_mu_unlock (&m->mu); - } - mark_thread_done (m); + for (i = 0; i != m->iterations; i++) { + gpr_timespec deadline; + deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_micros(1000, GPR_TIMESPAN)); + GPR_ASSERT(gpr_event_wait(&m->event, deadline) == NULL); + gpr_mu_lock(&m->mu); + m->counter++; + gpr_mu_unlock(&m->mu); + } + mark_thread_done(m); } /* Produce m->iterations elements on queue m->q, then mark thread as done. Even threads use queue_append(), and odd threads use queue_try_append() until it succeeds. */ -static void -many_producers (void *v /*=m*/ ) -{ +static void many_producers(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - int x = thread_id (m); - if ((x & 1) == 0) - { - for (i = 0; i != m->iterations; i++) - { - queue_append (&m->q, 1); - } + int x = thread_id(m); + if ((x & 1) == 0) { + for (i = 0; i != m->iterations; i++) { + queue_append(&m->q, 1); } - else - { - for (i = 0; i != m->iterations; i++) - { - while (!queue_try_append (&m->q, 1)) - { - } - } + } else { + for (i = 0; i != m->iterations; i++) { + while (!queue_try_append(&m->q, 1)) { + } } - mark_thread_done (m); + } + mark_thread_done(m); } /* Consume elements from m->q until m->threads*m->iterations are seen, wait an extra second to confirm that no more elements are arriving, then mark thread as done. */ -static void -consumer (void *v /*=m*/ ) -{ +static void consumer(void *v /*=m*/) { struct test *m = v; gpr_int64 n = m->iterations * m->threads; gpr_int64 i; int value; - for (i = 0; i != n; i++) - { - queue_remove (&m->q, &value, gpr_inf_future (GPR_CLOCK_REALTIME)); - } - gpr_mu_lock (&m->mu); + for (i = 0; i != n; i++) { + queue_remove(&m->q, &value, gpr_inf_future(GPR_CLOCK_REALTIME)); + } + gpr_mu_lock(&m->mu); m->counter = n; - gpr_mu_unlock (&m->mu); - GPR_ASSERT (!queue_remove (&m->q, &value, gpr_time_add (gpr_now (GPR_CLOCK_REALTIME), gpr_time_from_micros (1000000, GPR_TIMESPAN)))); - mark_thread_done (m); + gpr_mu_unlock(&m->mu); + GPR_ASSERT( + !queue_remove(&m->q, &value, + gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_micros(1000000, GPR_TIMESPAN)))); + mark_thread_done(m); } /* Increment m->stats_counter m->iterations times, transfer counter value to m->counter, then mark thread as done. */ -static void -statsinc (void *v /*=m*/ ) -{ +static void statsinc(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) - { - gpr_stats_inc (&m->stats_counter, 1); - } - gpr_mu_lock (&m->mu); - m->counter = gpr_stats_read (&m->stats_counter); - gpr_mu_unlock (&m->mu); - mark_thread_done (m); + for (i = 0; i != m->iterations; i++) { + gpr_stats_inc(&m->stats_counter, 1); + } + gpr_mu_lock(&m->mu); + m->counter = gpr_stats_read(&m->stats_counter); + gpr_mu_unlock(&m->mu); + mark_thread_done(m); } /* Increment m->refcount m->iterations times, decrement m->thread_refcount once, and if it reaches zero, set m->event to (void*)1; then mark thread as done. */ -static void -refinc (void *v /*=m*/ ) -{ +static void refinc(void *v /*=m*/) { struct test *m = v; gpr_int64 i; - for (i = 0; i != m->iterations; i++) - { - gpr_ref (&m->refcount); - } - if (gpr_unref (&m->thread_refcount)) - { - gpr_event_set (&m->event, (void *) 1); - } - mark_thread_done (m); + for (i = 0; i != m->iterations; i++) { + gpr_ref(&m->refcount); + } + if (gpr_unref(&m->thread_refcount)) { + gpr_event_set(&m->event, (void *)1); + } + mark_thread_done(m); } /* Wait until m->event is set to (void *)1, then decrement m->refcount m->stats_counter m->iterations times, and ensure that the last decrement caused the counter to reach zero, then mark thread as done. */ -static void -refcheck (void *v /*=m*/ ) -{ +static void refcheck(void *v /*=m*/) { struct test *m = v; gpr_int64 n = m->iterations * m->threads; gpr_int64 i; - GPR_ASSERT (gpr_event_wait (&m->event, gpr_inf_future (GPR_CLOCK_REALTIME)) == (void *) 1); - GPR_ASSERT (gpr_event_get (&m->event) == (void *) 1); - for (i = 1; i != n; i++) - { - GPR_ASSERT (!gpr_unref (&m->refcount)); - m->counter++; - } - GPR_ASSERT (gpr_unref (&m->refcount)); + GPR_ASSERT(gpr_event_wait(&m->event, gpr_inf_future(GPR_CLOCK_REALTIME)) == + (void *)1); + GPR_ASSERT(gpr_event_get(&m->event) == (void *)1); + for (i = 1; i != n; i++) { + GPR_ASSERT(!gpr_unref(&m->refcount)); + m->counter++; + } + GPR_ASSERT(gpr_unref(&m->refcount)); m->counter++; - mark_thread_done (m); + mark_thread_done(m); } /* ------------------------------------------------- */ -int -main (int argc, char *argv[]) -{ - grpc_test_init (argc, argv); - test ("mutex", &inc, NULL, 1); - test ("mutex try", &inctry, NULL, 1); - test ("cv", &inc_by_turns, NULL, 1); - test ("timedcv", &inc_with_1ms_delay, NULL, 1); - test ("queue", &many_producers, &consumer, 10); - test ("stats_counter", &statsinc, NULL, 1); - test ("refcount", &refinc, &refcheck, 1); - test ("timedevent", &inc_with_1ms_delay_event, NULL, 1); +int main(int argc, char *argv[]) { + grpc_test_init(argc, argv); + test("mutex", &inc, NULL, 1); + test("mutex try", &inctry, NULL, 1); + test("cv", &inc_by_turns, NULL, 1); + test("timedcv", &inc_with_1ms_delay, NULL, 1); + test("queue", &many_producers, &consumer, 10); + test("stats_counter", &statsinc, NULL, 1); + test("refcount", &refinc, &refcheck, 1); + test("timedevent", &inc_with_1ms_delay_event, NULL, 1); return 0; } diff --git a/test/core/support/thd_test.c b/test/core/support/thd_test.c index 09303c1958..faba33c5e8 100644 --- a/test/core/support/thd_test.c +++ b/test/core/support/thd_test.c @@ -41,8 +41,7 @@ #include #include "test/core/util/test_config.h" -struct test -{ +struct test { gpr_mu mu; int n; int is_done; @@ -50,67 +49,53 @@ struct test }; /* A Thread body. Decrement t->n, and if is becomes zero, set t->done. */ -static void -thd_body (void *v) -{ +static void thd_body(void *v) { struct test *t = v; - gpr_mu_lock (&t->mu); + gpr_mu_lock(&t->mu); t->n--; - if (t->n == 0) - { - t->is_done = 1; - gpr_cv_signal (&t->done_cv); - } - gpr_mu_unlock (&t->mu); + if (t->n == 0) { + t->is_done = 1; + gpr_cv_signal(&t->done_cv); + } + gpr_mu_unlock(&t->mu); } -static void -thd_body_joinable (void *v) -{ -} +static void thd_body_joinable(void *v) {} /* Test that we can create a number of threads and wait for them. */ -static void -test (void) -{ +static void test(void) { int i; gpr_thd_id thd; gpr_thd_id thds[1000]; struct test t; int n = 1000; - gpr_thd_options options = gpr_thd_options_default (); - gpr_mu_init (&t.mu); - gpr_cv_init (&t.done_cv); + gpr_thd_options options = gpr_thd_options_default(); + gpr_mu_init(&t.mu); + gpr_cv_init(&t.done_cv); t.n = n; t.is_done = 0; - for (i = 0; i != n; i++) - { - GPR_ASSERT (gpr_thd_new (&thd, &thd_body, &t, NULL)); - } - gpr_mu_lock (&t.mu); - while (!t.is_done) - { - gpr_cv_wait (&t.done_cv, &t.mu, gpr_inf_future (GPR_CLOCK_REALTIME)); - } - gpr_mu_unlock (&t.mu); - GPR_ASSERT (t.n == 0); - gpr_thd_options_set_joinable (&options); - for (i = 0; i < n; i++) - { - GPR_ASSERT (gpr_thd_new (&thds[i], &thd_body_joinable, NULL, &options)); - } - for (i = 0; i < n; i++) - { - gpr_thd_join (thds[i]); - } + for (i = 0; i != n; i++) { + GPR_ASSERT(gpr_thd_new(&thd, &thd_body, &t, NULL)); + } + gpr_mu_lock(&t.mu); + while (!t.is_done) { + gpr_cv_wait(&t.done_cv, &t.mu, gpr_inf_future(GPR_CLOCK_REALTIME)); + } + gpr_mu_unlock(&t.mu); + GPR_ASSERT(t.n == 0); + gpr_thd_options_set_joinable(&options); + for (i = 0; i < n; i++) { + GPR_ASSERT(gpr_thd_new(&thds[i], &thd_body_joinable, NULL, &options)); + } + for (i = 0; i < n; i++) { + gpr_thd_join(thds[i]); + } } /* ------------------------------------------------- */ -int -main (int argc, char *argv[]) -{ - grpc_test_init (argc, argv); - test (); +int main(int argc, char *argv[]) { + grpc_test_init(argc, argv); + test(); return 0; } diff --git a/test/core/support/time_test.c b/test/core/support/time_test.c index df65b45ddd..ce35edd83c 100644 --- a/test/core/support/time_test.c +++ b/test/core/support/time_test.c @@ -43,236 +43,225 @@ #include #include "test/core/util/test_config.h" -static void -to_fp (void *arg, const char *buf, size_t len) -{ - fwrite (buf, 1, len, (FILE *) arg); +static void to_fp(void *arg, const char *buf, size_t len) { + fwrite(buf, 1, len, (FILE *)arg); } /* Convert gpr_uintmax x to ascii base b (2..16), and write with (*writer)(arg, ...), zero padding to "chars" digits). */ -static void -u_to_s (gpr_uintmax x, unsigned base, int chars, void (*writer) (void *arg, const char *buf, size_t len), void *arg) -{ +static void u_to_s(gpr_uintmax x, unsigned base, int chars, + void (*writer)(void *arg, const char *buf, size_t len), + void *arg) { char buf[64]; - char *p = buf + sizeof (buf); - do - { - *--p = "0123456789abcdef"[x % base]; - x /= base; - chars--; - } - while (x != 0 || chars > 0); - (*writer) (arg, p, (size_t) (buf + sizeof (buf) - p)); + char *p = buf + sizeof(buf); + do { + *--p = "0123456789abcdef"[x % base]; + x /= base; + chars--; + } while (x != 0 || chars > 0); + (*writer)(arg, p, (size_t)(buf + sizeof(buf) - p)); } /* Convert gpr_intmax x to ascii base b (2..16), and write with (*writer)(arg, ...), zero padding to "chars" digits). */ -static void -i_to_s (gpr_intmax x, unsigned base, int chars, void (*writer) (void *arg, const char *buf, size_t len), void *arg) -{ - if (x < 0) - { - (*writer) (arg, "-", 1); - u_to_s ((gpr_uintmax) - x, base, chars - 1, writer, arg); - } - else - { - u_to_s ((gpr_uintmax) x, base, chars, writer, arg); - } +static void i_to_s(gpr_intmax x, unsigned base, int chars, + void (*writer)(void *arg, const char *buf, size_t len), + void *arg) { + if (x < 0) { + (*writer)(arg, "-", 1); + u_to_s((gpr_uintmax)-x, base, chars - 1, writer, arg); + } else { + u_to_s((gpr_uintmax)x, base, chars, writer, arg); + } } /* Convert ts to ascii, and write with (*writer)(arg, ...). */ -static void -ts_to_s (gpr_timespec t, void (*writer) (void *arg, const char *buf, size_t len), void *arg) -{ - if (t.tv_sec < 0 && t.tv_nsec != 0) - { - t.tv_sec++; - t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec; - } - i_to_s (t.tv_sec, 10, 0, writer, arg); - (*writer) (arg, ".", 1); - i_to_s (t.tv_nsec, 10, 9, writer, arg); +static void ts_to_s(gpr_timespec t, + void (*writer)(void *arg, const char *buf, size_t len), + void *arg) { + if (t.tv_sec < 0 && t.tv_nsec != 0) { + t.tv_sec++; + t.tv_nsec = GPR_NS_PER_SEC - t.tv_nsec; + } + i_to_s(t.tv_sec, 10, 0, writer, arg); + (*writer)(arg, ".", 1); + i_to_s(t.tv_nsec, 10, 9, writer, arg); } -static void -test_values (void) -{ +static void test_values(void) { int i; - gpr_timespec x = gpr_time_0 (GPR_CLOCK_REALTIME); - GPR_ASSERT (x.tv_sec == 0 && x.tv_nsec == 0); + gpr_timespec x = gpr_time_0(GPR_CLOCK_REALTIME); + GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0); - x = gpr_inf_future (GPR_CLOCK_REALTIME); - 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); - fprintf (stderr, "far future "); - ts_to_s (x, &to_fp, stderr); - fprintf (stderr, "\n"); + x = gpr_inf_future(GPR_CLOCK_REALTIME); + 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); + fprintf(stderr, "far future "); + ts_to_s(x, &to_fp, stderr); + fprintf(stderr, "\n"); - x = gpr_inf_past (GPR_CLOCK_REALTIME); - 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); - fprintf (stderr, "far past "); - ts_to_s (x, &to_fp, stderr); - fprintf (stderr, "\n"); + x = gpr_inf_past(GPR_CLOCK_REALTIME); + 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); + fprintf(stderr, "far past "); + ts_to_s(x, &to_fp, stderr); + fprintf(stderr, "\n"); - for (i = 1; i != 1000 * 1000 * 1000; i *= 10) - { - x = gpr_time_from_micros (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec == i / GPR_US_PER_SEC && x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US); - x = gpr_time_from_nanos (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec == i / GPR_NS_PER_SEC && x.tv_nsec == (i % GPR_NS_PER_SEC)); - x = gpr_time_from_millis (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec == i / GPR_MS_PER_SEC && x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS); - } + for (i = 1; i != 1000 * 1000 * 1000; i *= 10) { + x = gpr_time_from_micros(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC && + x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US); + x = gpr_time_from_nanos(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC && + x.tv_nsec == (i % GPR_NS_PER_SEC)); + x = gpr_time_from_millis(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC && + x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS); + } /* Test possible overflow in conversion of -ve values. */ - x = gpr_time_from_micros (-(LONG_MAX - 999997), GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec < 0); - GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_micros(-(LONG_MAX - 999997), GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec < 0); + GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); - x = gpr_time_from_nanos (-(LONG_MAX - 999999997), GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec < 0); - GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_nanos(-(LONG_MAX - 999999997), GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec < 0); + GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); - x = gpr_time_from_millis (-(LONG_MAX - 997), GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec < 0); - GPR_ASSERT (x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); + x = gpr_time_from_millis(-(LONG_MAX - 997), GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec < 0); + GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC); /* Test general -ve values. */ - for (i = -1; i > -1000 * 1000 * 1000; i *= 7) - { - x = gpr_time_from_micros (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i); - x = gpr_time_from_nanos (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i); - x = gpr_time_from_millis (i, GPR_TIMESPAN); - GPR_ASSERT (x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i); - } + for (i = -1; i > -1000 * 1000 * 1000; i *= 7) { + x = gpr_time_from_micros(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i); + x = gpr_time_from_nanos(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i); + x = gpr_time_from_millis(i, GPR_TIMESPAN); + GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i); + } } -static void -test_add_sub (void) -{ +static void test_add_sub(void) { int i; int j; int k; /* Basic addition and subtraction. */ - for (i = -100; i <= 100; i++) - { - for (j = -100; j <= 100; j++) - { - for (k = 1; k <= 10000000; k *= 10) - { - int sum = i + j; - int diff = i - j; - gpr_timespec it = gpr_time_from_micros (i * k, GPR_TIMESPAN); - gpr_timespec jt = gpr_time_from_micros (j * k, GPR_TIMESPAN); - gpr_timespec sumt = gpr_time_add (it, jt); - gpr_timespec difft = gpr_time_sub (it, jt); - if (gpr_time_cmp (gpr_time_from_micros (sum * k, GPR_TIMESPAN), sumt) != 0) - { - fprintf (stderr, "i %d j %d sum %d sumt ", i, j, sum); - ts_to_s (sumt, &to_fp, stderr); - fprintf (stderr, "\n"); - GPR_ASSERT (0); - } - if (gpr_time_cmp (gpr_time_from_micros (diff * k, GPR_TIMESPAN), difft) != 0) - { - fprintf (stderr, "i %d j %d diff %d diff ", i, j, diff); - ts_to_s (sumt, &to_fp, stderr); - fprintf (stderr, "\n"); - GPR_ASSERT (0); - } - } - } + for (i = -100; i <= 100; i++) { + for (j = -100; j <= 100; j++) { + for (k = 1; k <= 10000000; k *= 10) { + int sum = i + j; + int diff = i - j; + gpr_timespec it = gpr_time_from_micros(i * k, GPR_TIMESPAN); + gpr_timespec jt = gpr_time_from_micros(j * k, GPR_TIMESPAN); + gpr_timespec sumt = gpr_time_add(it, jt); + gpr_timespec difft = gpr_time_sub(it, jt); + if (gpr_time_cmp(gpr_time_from_micros(sum * k, GPR_TIMESPAN), sumt) != + 0) { + fprintf(stderr, "i %d j %d sum %d sumt ", i, j, sum); + ts_to_s(sumt, &to_fp, stderr); + fprintf(stderr, "\n"); + GPR_ASSERT(0); + } + if (gpr_time_cmp(gpr_time_from_micros(diff * k, GPR_TIMESPAN), difft) != + 0) { + fprintf(stderr, "i %d j %d diff %d diff ", i, j, diff); + ts_to_s(sumt, &to_fp, stderr); + fprintf(stderr, "\n"); + GPR_ASSERT(0); + } + } } + } } -static void -test_overflow (void) -{ +static void test_overflow(void) { /* overflow */ - gpr_timespec x = gpr_time_from_micros (1, GPR_TIMESPAN); - do - { - x = gpr_time_add (x, x); - } - while (gpr_time_cmp (x, gpr_inf_future (GPR_TIMESPAN)) < 0); - GPR_ASSERT (gpr_time_cmp (x, gpr_inf_future (GPR_TIMESPAN)) == 0); - x = gpr_time_from_micros (-1, GPR_TIMESPAN); - do - { - x = gpr_time_add (x, x); - } - while (gpr_time_cmp (x, gpr_inf_past (GPR_TIMESPAN)) > 0); - GPR_ASSERT (gpr_time_cmp (x, gpr_inf_past (GPR_TIMESPAN)) == 0); + gpr_timespec x = gpr_time_from_micros(1, GPR_TIMESPAN); + do { + x = gpr_time_add(x, x); + } while (gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) < 0); + GPR_ASSERT(gpr_time_cmp(x, gpr_inf_future(GPR_TIMESPAN)) == 0); + x = gpr_time_from_micros(-1, GPR_TIMESPAN); + do { + x = gpr_time_add(x, x); + } while (gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) > 0); + GPR_ASSERT(gpr_time_cmp(x, gpr_inf_past(GPR_TIMESPAN)) == 0); } -static void -test_sticky_infinities (void) -{ +static void test_sticky_infinities(void) { int i; int j; int k; gpr_timespec infinity[2]; gpr_timespec addend[3]; - infinity[0] = gpr_inf_future (GPR_TIMESPAN); - infinity[1] = gpr_inf_past (GPR_TIMESPAN); - addend[0] = gpr_inf_future (GPR_TIMESPAN); - addend[1] = gpr_inf_past (GPR_TIMESPAN); - addend[2] = gpr_time_0 (GPR_TIMESPAN); + infinity[0] = gpr_inf_future(GPR_TIMESPAN); + infinity[1] = gpr_inf_past(GPR_TIMESPAN); + addend[0] = gpr_inf_future(GPR_TIMESPAN); + addend[1] = gpr_inf_past(GPR_TIMESPAN); + addend[2] = gpr_time_0(GPR_TIMESPAN); /* Infinities are sticky */ - for (i = 0; i != sizeof (infinity) / sizeof (infinity[0]); i++) - { - for (j = 0; j != sizeof (addend) / sizeof (addend[0]); j++) - { - gpr_timespec x = gpr_time_add (infinity[i], addend[j]); - GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); - x = gpr_time_sub (infinity[i], addend[j]); - GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); - } - for (k = -200; k <= 200; k++) - { - gpr_timespec y = gpr_time_from_micros (k * 100000, GPR_TIMESPAN); - gpr_timespec x = gpr_time_add (infinity[i], y); - GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); - x = gpr_time_sub (infinity[i], y); - GPR_ASSERT (gpr_time_cmp (x, infinity[i]) == 0); - } + for (i = 0; i != sizeof(infinity) / sizeof(infinity[0]); i++) { + for (j = 0; j != sizeof(addend) / sizeof(addend[0]); j++) { + gpr_timespec x = gpr_time_add(infinity[i], addend[j]); + GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); + x = gpr_time_sub(infinity[i], addend[j]); + GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); + } + for (k = -200; k <= 200; k++) { + gpr_timespec y = gpr_time_from_micros(k * 100000, GPR_TIMESPAN); + gpr_timespec x = gpr_time_add(infinity[i], y); + GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); + x = gpr_time_sub(infinity[i], y); + GPR_ASSERT(gpr_time_cmp(x, infinity[i]) == 0); } + } } -static void -test_similar (void) -{ - GPR_ASSERT (1 == gpr_time_similar (gpr_inf_future (GPR_TIMESPAN), gpr_inf_future (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); - GPR_ASSERT (1 == gpr_time_similar (gpr_inf_past (GPR_TIMESPAN), gpr_inf_past (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); - GPR_ASSERT (0 == gpr_time_similar (gpr_inf_past (GPR_TIMESPAN), gpr_inf_future (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); - GPR_ASSERT (0 == gpr_time_similar (gpr_inf_future (GPR_TIMESPAN), gpr_inf_past (GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); - GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_0 (GPR_TIMESPAN))); - GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (15, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); - GPR_ASSERT (1 == gpr_time_similar (gpr_time_from_micros (15, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); - GPR_ASSERT (0 == gpr_time_similar (gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (25, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); - GPR_ASSERT (0 == gpr_time_similar (gpr_time_from_micros (25, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN), gpr_time_from_micros (10, GPR_TIMESPAN))); +static void test_similar(void) { + GPR_ASSERT(1 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN), + gpr_inf_future(GPR_TIMESPAN), + gpr_time_0(GPR_TIMESPAN))); + GPR_ASSERT(1 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN), + gpr_inf_past(GPR_TIMESPAN), + gpr_time_0(GPR_TIMESPAN))); + GPR_ASSERT(0 == gpr_time_similar(gpr_inf_past(GPR_TIMESPAN), + gpr_inf_future(GPR_TIMESPAN), + gpr_time_0(GPR_TIMESPAN))); + GPR_ASSERT(0 == gpr_time_similar(gpr_inf_future(GPR_TIMESPAN), + gpr_inf_past(GPR_TIMESPAN), + gpr_time_0(GPR_TIMESPAN))); + GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_0(GPR_TIMESPAN))); + GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_from_micros(15, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN))); + GPR_ASSERT(1 == gpr_time_similar(gpr_time_from_micros(15, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN))); + GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_from_micros(25, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN))); + GPR_ASSERT(0 == gpr_time_similar(gpr_time_from_micros(25, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN), + gpr_time_from_micros(10, GPR_TIMESPAN))); } -int -main (int argc, char *argv[]) -{ - grpc_test_init (argc, argv); +int main(int argc, char *argv[]) { + grpc_test_init(argc, argv); - test_values (); - test_add_sub (); - test_overflow (); - test_sticky_infinities (); - test_similar (); + test_values(); + test_add_sub(); + test_overflow(); + test_sticky_infinities(); + test_similar(); return 0; } diff --git a/test/core/support/tls_test.c b/test/core/support/tls_test.c index 9c5cd54d63..0a3c28417f 100644 --- a/test/core/support/tls_test.c +++ b/test/core/support/tls_test.c @@ -43,48 +43,41 @@ #define NUM_THREADS 100 -GPR_TLS_DECL (test_var); +GPR_TLS_DECL(test_var); -static void -thd_body (void *arg) -{ +static void thd_body(void *arg) { gpr_intptr i; - GPR_ASSERT (gpr_tls_get (&test_var) == 0); + GPR_ASSERT(gpr_tls_get(&test_var) == 0); - for (i = 0; i < 10000000; i++) - { - gpr_tls_set (&test_var, i); - GPR_ASSERT (gpr_tls_get (&test_var) == i); - } - gpr_tls_set (&test_var, 0); + for (i = 0; i < 10000000; i++) { + gpr_tls_set(&test_var, i); + GPR_ASSERT(gpr_tls_get(&test_var) == i); + } + gpr_tls_set(&test_var, 0); } /* ------------------------------------------------- */ -int -main (int argc, char *argv[]) -{ - gpr_thd_options opt = gpr_thd_options_default (); +int main(int argc, char *argv[]) { + gpr_thd_options opt = gpr_thd_options_default(); int i; gpr_thd_id threads[NUM_THREADS]; - grpc_test_init (argc, argv); + grpc_test_init(argc, argv); - gpr_tls_init (&test_var); + gpr_tls_init(&test_var); - gpr_thd_options_set_joinable (&opt); + gpr_thd_options_set_joinable(&opt); - for (i = 0; i < NUM_THREADS; i++) - { - gpr_thd_new (&threads[i], thd_body, NULL, &opt); - } - for (i = 0; i < NUM_THREADS; i++) - { - gpr_thd_join (threads[i]); - } + for (i = 0; i < NUM_THREADS; i++) { + gpr_thd_new(&threads[i], thd_body, NULL, &opt); + } + for (i = 0; i < NUM_THREADS; i++) { + gpr_thd_join(threads[i]); + } - gpr_tls_destroy (&test_var); + gpr_tls_destroy(&test_var); return 0; } diff --git a/test/core/support/useful_test.c b/test/core/support/useful_test.c index 49a1bd353e..cbf4f02e26 100644 --- a/test/core/support/useful_test.c +++ b/test/core/support/useful_test.c @@ -36,40 +36,38 @@ #include #include "test/core/util/test_config.h" -int -main (int argc, char **argv) -{ +int main(int argc, char **argv) { int four[4]; int five[5]; gpr_uint32 bitset = 0; - grpc_test_init (argc, argv); + grpc_test_init(argc, argv); - GPR_ASSERT (GPR_MIN (1, 2) == 1); - GPR_ASSERT (GPR_MAX (1, 2) == 2); - GPR_ASSERT (GPR_MIN (2, 1) == 1); - GPR_ASSERT (GPR_MAX (2, 1) == 2); - GPR_ASSERT (GPR_CLAMP (1, 0, 2) == 1); - GPR_ASSERT (GPR_CLAMP (0, 0, 2) == 0); - GPR_ASSERT (GPR_CLAMP (2, 0, 2) == 2); - GPR_ASSERT (GPR_CLAMP (-1, 0, 2) == 0); - GPR_ASSERT (GPR_CLAMP (3, 0, 2) == 2); - GPR_ASSERT (GPR_ROTL ((gpr_uint32) 0x80000001, 1) == 3); - GPR_ASSERT (GPR_ROTR ((gpr_uint32) 0x80000001, 1) == 0xc0000000); - GPR_ASSERT (GPR_ARRAY_SIZE (four) == 4); - GPR_ASSERT (GPR_ARRAY_SIZE (five) == 5); + GPR_ASSERT(GPR_MIN(1, 2) == 1); + GPR_ASSERT(GPR_MAX(1, 2) == 2); + GPR_ASSERT(GPR_MIN(2, 1) == 1); + GPR_ASSERT(GPR_MAX(2, 1) == 2); + GPR_ASSERT(GPR_CLAMP(1, 0, 2) == 1); + GPR_ASSERT(GPR_CLAMP(0, 0, 2) == 0); + GPR_ASSERT(GPR_CLAMP(2, 0, 2) == 2); + GPR_ASSERT(GPR_CLAMP(-1, 0, 2) == 0); + GPR_ASSERT(GPR_CLAMP(3, 0, 2) == 2); + GPR_ASSERT(GPR_ROTL((gpr_uint32)0x80000001, 1) == 3); + GPR_ASSERT(GPR_ROTR((gpr_uint32)0x80000001, 1) == 0xc0000000); + GPR_ASSERT(GPR_ARRAY_SIZE(four) == 4); + GPR_ASSERT(GPR_ARRAY_SIZE(five) == 5); - GPR_ASSERT (GPR_BITCOUNT ((1u << 31) - 1) == 31); - GPR_ASSERT (GPR_BITCOUNT (1u << 3) == 1); - GPR_ASSERT (GPR_BITCOUNT (0) == 0); + GPR_ASSERT(GPR_BITCOUNT((1u << 31) - 1) == 31); + GPR_ASSERT(GPR_BITCOUNT(1u << 3) == 1); + GPR_ASSERT(GPR_BITCOUNT(0) == 0); - GPR_ASSERT (GPR_BITSET (&bitset, 3) == 8); - GPR_ASSERT (GPR_BITCOUNT (bitset) == 1); - GPR_ASSERT (GPR_BITGET (bitset, 3) == 1); - GPR_ASSERT (GPR_BITSET (&bitset, 1) == 10); - GPR_ASSERT (GPR_BITCOUNT (bitset) == 2); - GPR_ASSERT (GPR_BITCLEAR (&bitset, 3) == 2); - GPR_ASSERT (GPR_BITCOUNT (bitset) == 1); - GPR_ASSERT (GPR_BITGET (bitset, 3) == 0); + GPR_ASSERT(GPR_BITSET(&bitset, 3) == 8); + GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); + GPR_ASSERT(GPR_BITGET(bitset, 3) == 1); + GPR_ASSERT(GPR_BITSET(&bitset, 1) == 10); + GPR_ASSERT(GPR_BITCOUNT(bitset) == 2); + GPR_ASSERT(GPR_BITCLEAR(&bitset, 3) == 2); + GPR_ASSERT(GPR_BITCOUNT(bitset) == 1); + GPR_ASSERT(GPR_BITGET(bitset, 3) == 0); return 0; } -- cgit v1.2.3