test-bufferiszero.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * QEMU buffer_is_zero test
  3. *
  4. * Copyright (c) 2016 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/cutils.h"
  22. static char buffer[8 * 1024 * 1024];
  23. static void test_1(void)
  24. {
  25. size_t s, a, o;
  26. /* Basic positive test. */
  27. g_assert(buffer_is_zero(buffer, sizeof(buffer)));
  28. /* Basic negative test. */
  29. buffer[sizeof(buffer) - 1] = 1;
  30. g_assert(!buffer_is_zero(buffer, sizeof(buffer)));
  31. buffer[sizeof(buffer) - 1] = 0;
  32. /* Positive tests for size and alignment. */
  33. for (a = 1; a <= 64; a++) {
  34. for (s = 1; s < 1024; s++) {
  35. buffer[a - 1] = 1;
  36. buffer[a + s] = 1;
  37. g_assert(buffer_is_zero(buffer + a, s));
  38. buffer[a - 1] = 0;
  39. buffer[a + s] = 0;
  40. }
  41. }
  42. /* Negative tests for size, alignment, and the offset of the marker. */
  43. for (a = 1; a <= 64; a++) {
  44. for (s = 1; s < 1024; s++) {
  45. for (o = 0; o < s; ++o) {
  46. buffer[a + o] = 1;
  47. g_assert(!buffer_is_zero(buffer + a, s));
  48. buffer[a + o] = 0;
  49. }
  50. }
  51. }
  52. }
  53. static void test_2(void)
  54. {
  55. if (g_test_perf()) {
  56. test_1();
  57. } else {
  58. do {
  59. test_1();
  60. } while (test_buffer_is_zero_next_accel());
  61. }
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. g_test_init(&argc, &argv, NULL);
  66. g_test_add_func("/cutils/bufferiszero", test_2);
  67. return g_test_run();
  68. }