varint_test.cc 1.9 KB

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