tst-support_blob_repeat.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Tests for <support/blob_repeat.h>
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <support/blob_repeat.h>
  17. #include <support/check.h>
  18. static int
  19. do_test (void)
  20. {
  21. struct support_blob_repeat repeat
  22. = support_blob_repeat_allocate ("5", 1, 5);
  23. TEST_COMPARE_BLOB (repeat.start, repeat.size, "55555", 5);
  24. support_blob_repeat_free (&repeat);
  25. repeat = support_blob_repeat_allocate ("ABC", 3, 3);
  26. TEST_COMPARE_BLOB (repeat.start, repeat.size, "ABCABCABC", 9);
  27. support_blob_repeat_free (&repeat);
  28. repeat = support_blob_repeat_allocate ("abc", 4, 3);
  29. TEST_COMPARE_BLOB (repeat.start, repeat.size, "abc\0abc\0abc", 12);
  30. support_blob_repeat_free (&repeat);
  31. size_t gigabyte = 1U << 30;
  32. repeat = support_blob_repeat_allocate ("X", 1, gigabyte + 1);
  33. if (repeat.start == NULL)
  34. puts ("warning: not enough memory for 1 GiB mapping");
  35. else
  36. {
  37. TEST_COMPARE (repeat.size, gigabyte + 1);
  38. {
  39. unsigned char *p = repeat.start;
  40. for (size_t i = 0; i < gigabyte + 1; ++i)
  41. if (p[i] != 'X')
  42. FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i);
  43. /* Check that there is no sharing across the mapping. */
  44. p[0] = 'Y';
  45. p[1U << 24] = 'Z';
  46. for (size_t i = 0; i < gigabyte + 1; ++i)
  47. if (i == 0)
  48. TEST_COMPARE (p[i], 'Y');
  49. else if (i == 1U << 24)
  50. TEST_COMPARE (p[i], 'Z');
  51. else if (p[i] != 'X')
  52. FAIL_EXIT1 ("invalid byte 0x%02x at %zu", p[i], i);
  53. }
  54. }
  55. support_blob_repeat_free (&repeat);
  56. repeat = support_blob_repeat_allocate ("012345678", 9, 10 * 1000 * 1000);
  57. if (repeat.start == NULL)
  58. puts ("warning: not enough memory for large mapping");
  59. else
  60. {
  61. unsigned char *p = repeat.start;
  62. for (int i = 0; i < 10 * 1000 * 1000; ++i)
  63. for (int j = 0; j <= 8; ++j)
  64. if (p[i * 9 + j] != '0' + j)
  65. {
  66. printf ("error: element %d index %d\n", i, j);
  67. TEST_COMPARE (p[i * 9 + j], '0' + j);
  68. }
  69. }
  70. support_blob_repeat_free (&repeat);
  71. return 0;
  72. }
  73. #include <support/test-driver.c>