Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(310)

Side by Side Diff: third_party/grpc/test/core/compression/compression_test.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <grpc/compression.h>
38 #include <grpc/grpc.h>
39 #include <grpc/support/log.h>
40 #include <grpc/support/useful.h>
41
42 #include "test/core/util/test_config.h"
43
44 static void test_compression_algorithm_parse(void) {
45 size_t i;
46 const char *valid_names[] = {"identity", "gzip", "deflate"};
47 const grpc_compression_algorithm valid_algorithms[] = {
48 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE};
49 const char *invalid_names[] = {"gzip2", "foo", "", "2gzip"};
50
51 gpr_log(GPR_DEBUG, "test_compression_algorithm_parse");
52
53 for (i = 0; i < GPR_ARRAY_SIZE(valid_names); i++) {
54 const char *valid_name = valid_names[i];
55 grpc_compression_algorithm algorithm;
56 const int success = grpc_compression_algorithm_parse(
57 valid_name, strlen(valid_name), &algorithm);
58 GPR_ASSERT(success != 0);
59 GPR_ASSERT(algorithm == valid_algorithms[i]);
60 }
61
62 for (i = 0; i < GPR_ARRAY_SIZE(invalid_names); i++) {
63 const char *invalid_name = invalid_names[i];
64 grpc_compression_algorithm algorithm;
65 int success;
66 success = grpc_compression_algorithm_parse(
67 invalid_name, strlen(invalid_name), &algorithm);
68 GPR_ASSERT(success == 0);
69 /* the value of "algorithm" is undefined upon failure */
70 }
71 }
72
73 static void test_compression_algorithm_name(void) {
74 int success;
75 char *name;
76 size_t i;
77 const char *valid_names[] = {"identity", "gzip", "deflate"};
78 const grpc_compression_algorithm valid_algorithms[] = {
79 GRPC_COMPRESS_NONE, GRPC_COMPRESS_GZIP, GRPC_COMPRESS_DEFLATE};
80
81 gpr_log(GPR_DEBUG, "test_compression_algorithm_name");
82
83 for (i = 0; i < GPR_ARRAY_SIZE(valid_algorithms); i++) {
84 success = grpc_compression_algorithm_name(valid_algorithms[i], &name);
85 GPR_ASSERT(success != 0);
86 GPR_ASSERT(strcmp(name, valid_names[i]) == 0);
87 }
88
89 success =
90 grpc_compression_algorithm_name(GRPC_COMPRESS_ALGORITHMS_COUNT, &name);
91 GPR_ASSERT(success == 0);
92 /* the value of "name" is undefined upon failure */
93 }
94
95 static void test_compression_algorithm_for_level(void) {
96 size_t i;
97 grpc_compression_level levels[] = {
98 GRPC_COMPRESS_LEVEL_NONE, GRPC_COMPRESS_LEVEL_LOW,
99 GRPC_COMPRESS_LEVEL_MED, GRPC_COMPRESS_LEVEL_HIGH};
100 grpc_compression_algorithm algorithms[] = {
101 GRPC_COMPRESS_NONE, GRPC_COMPRESS_DEFLATE, GRPC_COMPRESS_DEFLATE,
102 GRPC_COMPRESS_DEFLATE};
103 gpr_log(GPR_DEBUG, "test_compression_algorithm_for_level");
104
105 for (i = 0; i < GPR_ARRAY_SIZE(levels); i++) {
106 GPR_ASSERT(algorithms[i] ==
107 grpc_compression_algorithm_for_level(levels[i]));
108 }
109 }
110
111 static void test_compression_enable_disable_algorithm(void) {
112 grpc_compression_options options;
113 grpc_compression_algorithm algorithm;
114
115 gpr_log(GPR_DEBUG, "test_compression_enable_disable_algorithm");
116
117 grpc_compression_options_init(&options);
118 for (algorithm = GRPC_COMPRESS_NONE;
119 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) {
120 /* all algorithms are enabled by default */
121 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
122 algorithm) != 0);
123 }
124 /* disable one by one */
125 for (algorithm = GRPC_COMPRESS_NONE;
126 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) {
127 grpc_compression_options_disable_algorithm(&options, algorithm);
128 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
129 algorithm) == 0);
130 }
131 /* re-enable one by one */
132 for (algorithm = GRPC_COMPRESS_NONE;
133 algorithm < GRPC_COMPRESS_ALGORITHMS_COUNT; algorithm++) {
134 grpc_compression_options_enable_algorithm(&options, algorithm);
135 GPR_ASSERT(grpc_compression_options_is_algorithm_enabled(&options,
136 algorithm) != 0);
137 }
138 }
139
140 int main(int argc, char **argv) {
141 grpc_init();
142 test_compression_algorithm_parse();
143 test_compression_algorithm_name();
144 test_compression_algorithm_for_level();
145 test_compression_enable_disable_algorithm();
146 grpc_shutdown();
147
148 return 0;
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698