slice_buffer_test.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <grpc/support/log.h>
  34. #include <grpc/support/slice_buffer.h>
  35. #include "test/core/util/test_config.h"
  36. int
  37. main (int argc, char **argv)
  38. {
  39. gpr_slice_buffer buf;
  40. gpr_slice aaa = gpr_slice_from_copied_string ("aaa");
  41. gpr_slice bb = gpr_slice_from_copied_string ("bb");
  42. size_t i;
  43. grpc_test_init (argc, argv);
  44. gpr_slice_buffer_init (&buf);
  45. for (i = 0; i < 10; i++)
  46. {
  47. gpr_slice_ref (aaa);
  48. gpr_slice_ref (bb);
  49. gpr_slice_buffer_add (&buf, aaa);
  50. gpr_slice_buffer_add (&buf, bb);
  51. }
  52. GPR_ASSERT (buf.count > 0);
  53. GPR_ASSERT (buf.length == 50);
  54. gpr_slice_buffer_reset_and_unref (&buf);
  55. GPR_ASSERT (buf.count == 0);
  56. GPR_ASSERT (buf.length == 0);
  57. for (i = 0; i < 10; i++)
  58. {
  59. gpr_slice_ref (aaa);
  60. gpr_slice_ref (bb);
  61. gpr_slice_buffer_add (&buf, aaa);
  62. gpr_slice_buffer_add (&buf, bb);
  63. }
  64. GPR_ASSERT (buf.count > 0);
  65. GPR_ASSERT (buf.length == 50);
  66. for (i = 0; i < 10; i++)
  67. {
  68. gpr_slice_buffer_pop (&buf);
  69. gpr_slice_unref (aaa);
  70. gpr_slice_unref (bb);
  71. }
  72. GPR_ASSERT (buf.count == 0);
  73. GPR_ASSERT (buf.length == 0);
  74. gpr_slice_buffer_destroy (&buf);
  75. return 0;
  76. }