test-ringbuf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdint.h>
  17. #include <glib.h>
  18. #include "src/shared/ringbuf.h"
  19. #include "src/shared/tester.h"
  20. static unsigned int nlpo2(unsigned int x)
  21. {
  22. x--;
  23. x |= (x >> 1);
  24. x |= (x >> 2);
  25. x |= (x >> 4);
  26. x |= (x >> 8);
  27. x |= (x >> 16);
  28. return x + 1;
  29. }
  30. static unsigned int fls(unsigned int x)
  31. {
  32. return x ? sizeof(x) * 8 - __builtin_clz(x) : 0;
  33. }
  34. static unsigned int align_power2(unsigned int u)
  35. {
  36. return 1 << fls(u - 1);
  37. }
  38. static void test_power2(const void *data)
  39. {
  40. size_t i;
  41. for (i = 1; i < 1000000; i++) {
  42. size_t size1, size2, size3 = 1;
  43. size1 = nlpo2(i);
  44. size2 = align_power2(i);
  45. /* Find the next power of two */
  46. while (size3 < i && size3 < SIZE_MAX)
  47. size3 <<= 1;
  48. tester_debug("%zu -> size1=%zu size2=%zu size3=%zu\n",
  49. i, size1, size2, size3);
  50. g_assert(size1 == size2);
  51. g_assert(size2 == size3);
  52. g_assert(size3 == size1);
  53. }
  54. tester_test_passed();
  55. }
  56. static void test_alloc(const void *data)
  57. {
  58. int i;
  59. for (i = 2; i < 10000; i++) {
  60. struct ringbuf *rb;
  61. tester_debug("Iteration %i\n", i);
  62. rb = ringbuf_new(i);
  63. g_assert(rb != NULL);
  64. g_assert(ringbuf_capacity(rb) == ringbuf_avail(rb));
  65. ringbuf_free(rb);
  66. }
  67. tester_test_passed();
  68. }
  69. static void test_printf(const void *data)
  70. {
  71. static size_t rb_size = 500;
  72. static size_t rb_capa = 512;
  73. struct ringbuf *rb;
  74. int i;
  75. rb = ringbuf_new(rb_size);
  76. g_assert(rb != NULL);
  77. g_assert(ringbuf_capacity(rb) == rb_capa);
  78. for (i = 0; i < 10000; i++) {
  79. size_t len, count = i % rb_capa;
  80. char *str, *ptr;
  81. if (!count)
  82. continue;
  83. tester_debug("Iteration %i\n", i);
  84. len = asprintf(&str, "%*c", (int) count, 'x');
  85. g_assert(len == count);
  86. len = ringbuf_printf(rb, "%s", str);
  87. g_assert(len == count);
  88. g_assert(ringbuf_len(rb) == count);
  89. g_assert(ringbuf_avail(rb) == rb_capa - len);
  90. ptr = ringbuf_peek(rb, 0, &len);
  91. g_assert(ptr != NULL);
  92. g_assert(len == count);
  93. g_assert(strncmp(str, ptr, len) == 0);
  94. len = ringbuf_drain(rb, count);
  95. g_assert(len == count);
  96. g_assert(ringbuf_len(rb) == 0);
  97. g_assert(ringbuf_avail(rb) == rb_capa);
  98. free(str);
  99. }
  100. ringbuf_free(rb);
  101. tester_test_passed();
  102. }
  103. int main(int argc, char *argv[])
  104. {
  105. tester_init(&argc, &argv);
  106. tester_add("/ringbuf/power2", NULL, NULL, test_power2, NULL);
  107. tester_add("/ringbuf/alloc", NULL, NULL, test_alloc, NULL);
  108. tester_add("/ringbuf/printf", NULL, NULL, test_printf, NULL);
  109. return tester_run();
  110. }