Debian Codegraph @

test/core/transport/chttp2/varint_test.cc

raw
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
19 #include "src/core/ext/transport/chttp2/transport/varint.h"
20
21 #include "gtest/gtest.h"
22
23 #include <grpc/slice.h>
24 #include <grpc/support/log.h>
25
26 #include "test/core/util/test_config.h"
27
28 template <uint8_t kPrefixBits>
29 static void test_varint(uint32_t value, uint8_t prefix_or,
30 const char* expect_bytes, size_t expect_length) {
31 grpc_core::VarintWriter<kPrefixBits> w(value);
32 grpc_slice expect =
33 grpc_slice_from_copied_buffer(expect_bytes, expect_length);
34 grpc_slice slice;
35 gpr_log(GPR_DEBUG, "Test: 0x%08x", value);
36 ASSERT_EQ(w.length(), expect_length);
37 slice = grpc_slice_malloc(w.length());
38 w.Write(prefix_or, GRPC_SLICE_START_PTR(slice));
39 ASSERT_TRUE(grpc_slice_eq(expect, slice));
40 grpc_slice_unref(expect);
41 grpc_slice_unref(slice);
42 }
43
44 #define TEST_VARINT(value, prefix_bits, prefix_or, expect) \
45 test_varint<prefix_bits>(value, prefix_or, expect, sizeof(expect) - 1)
46
47 TEST(VarintTest, MainTest) {
48 TEST_VARINT(0, 1, 0, "\x00");
49 TEST_VARINT(128, 1, 0, "\x7f\x01");
50 TEST_VARINT(16384, 1, 0, "\x7f\x81\x7f");
51 TEST_VARINT(2097152, 1, 0, "\x7f\x81\xff\x7f");
52 TEST_VARINT(268435456, 1, 0, "\x7f\x81\xff\xff\x7f");
53 TEST_VARINT(0xffffffff, 1, 0, "\x7f\x80\xff\xff\xff\x0f");
54 }
55
56 int main(int argc, char** argv) {
57 grpc::testing::TestEnvironment env(&argc, argv);
58 ::testing::InitGoogleTest(&argc, argv);
59 grpc::testing::TestGrpcScope grpc_scope;
60 return RUN_ALL_TESTS();
61 }