varint_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <grpc/grpc.h>
22 #include <grpc/slice.h>
23 #include <grpc/support/log.h>
24 
26 
27 template <uint8_t kPrefixBits>
28 static void test_varint(uint32_t value, uint8_t prefix_or,
29  const char* expect_bytes, size_t expect_length) {
31  grpc_slice expect =
32  grpc_slice_from_copied_buffer(expect_bytes, expect_length);
34  gpr_log(GPR_DEBUG, "Test: 0x%08x", value);
35  GPR_ASSERT(w.length() == expect_length);
37  w.Write(prefix_or, GRPC_SLICE_START_PTR(slice));
38  GPR_ASSERT(grpc_slice_eq(expect, slice));
39  grpc_slice_unref(expect);
41 }
42 
43 #define TEST_VARINT(value, prefix_bits, prefix_or, expect) \
44  test_varint<prefix_bits>(value, prefix_or, expect, sizeof(expect) - 1)
45 
46 int main(int argc, char** argv) {
48  grpc_init();
49  TEST_VARINT(0, 1, 0, "\x00");
50  TEST_VARINT(128, 1, 0, "\x7f\x01");
51  TEST_VARINT(16384, 1, 0, "\x7f\x81\x7f");
52  TEST_VARINT(2097152, 1, 0, "\x7f\x81\xff\x7f");
53  TEST_VARINT(268435456, 1, 0, "\x7f\x81\xff\xff\x7f");
54  TEST_VARINT(0xffffffff, 1, 0, "\x7f\x80\xff\xff\xff\x0f");
55  grpc_shutdown();
56  return 0;
57 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
log.h
generate.env
env
Definition: generate.py:37
slice.h
varint.h
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_slice_malloc
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
Definition: slice/slice.cc:227
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
TEST_VARINT
#define TEST_VARINT(value, prefix_bits, prefix_or, expect)
Definition: varint_test.cc:43
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
main
int main(int argc, char **argv)
Definition: varint_test.cc:46
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::VarintWriter::Write
void Write(uint8_t prefix, uint8_t *target) const
Definition: varint.h:56
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
grpc_core::VarintWriter::length
uint32_t length() const
Definition: varint.h:54
grpc_core::VarintWriter
Definition: varint.h:44
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
test_varint
static void test_varint(uint32_t value, uint8_t prefix_or, const char *expect_bytes, size_t expect_length)
Definition: varint_test.cc:28
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_slice_eq
GPRAPI int grpc_slice_eq(grpc_slice a, grpc_slice b)
Definition: slice/slice.cc:387
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:52