slice_string_helpers_test.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/lib/slice/slice_string_helpers.h"
  19. #include <limits.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include "src/core/lib/gpr/string.h"
  27. #include "src/core/lib/slice/slice_internal.h"
  28. #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
  29. static void expect_slice_dump(grpc_slice slice, uint32_t flags,
  30. const char* result) {
  31. char* got = grpc_dump_slice(slice, flags);
  32. GPR_ASSERT(0 == strcmp(got, result));
  33. gpr_free(got);
  34. grpc_slice_unref_internal(slice);
  35. }
  36. static void test_dump_slice(void) {
  37. static const char* text = "HELLO WORLD!";
  38. static const char* long_text =
  39. "It was a bright cold day in April, and the clocks were striking "
  40. "thirteen. Winston Smith, his chin nuzzled into his breast in an effort "
  41. "to escape the vile wind, slipped quickly through the glass doors of "
  42. "Victory Mansions, though not quickly enough to prevent a swirl of "
  43. "gritty dust from entering along with him.";
  44. LOG_TEST_NAME("test_dump_slice");
  45. expect_slice_dump(grpc_slice_from_copied_string(text), GPR_DUMP_ASCII, text);
  46. expect_slice_dump(grpc_slice_from_copied_string(long_text), GPR_DUMP_ASCII,
  47. long_text);
  48. expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1), GPR_DUMP_HEX,
  49. "01");
  50. expect_slice_dump(grpc_slice_from_copied_buffer("\x01", 1),
  51. GPR_DUMP_HEX | GPR_DUMP_ASCII, "01 '.'");
  52. }
  53. int main(int, char**) {
  54. test_dump_slice();
  55. return 0;
  56. }