bswap_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2015 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/test.h>
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. GRUB_MOD_LICENSE ("GPLv3+");
  22. static grub_uint64_t vectors[] = {
  23. 0xffffffffffffffffULL, 1, 2, 0, 0x0102030405060708ULL
  24. };
  25. static void
  26. test16 (grub_uint16_t a)
  27. {
  28. grub_uint16_t b, c;
  29. grub_uint8_t *ap, *bp;
  30. int i;
  31. b = grub_swap_bytes16 (a);
  32. c = grub_swap_bytes16 (b);
  33. grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
  34. (long long) a, (long long) b, (long long) c);
  35. ap = (grub_uint8_t *) &a;
  36. bp = (grub_uint8_t *) &b;
  37. for (i = 0; i < 2; i++)
  38. {
  39. grub_test_assert (ap[i] == bp[1 - i],
  40. "bswap bytes wrong: 0x%llx, 0x%llx",
  41. (long long) a, (long long) b);
  42. }
  43. }
  44. static void
  45. test32 (grub_uint32_t a)
  46. {
  47. grub_uint32_t b, c;
  48. grub_uint8_t *ap, *bp;
  49. int i;
  50. b = grub_swap_bytes32 (a);
  51. c = grub_swap_bytes32 (b);
  52. grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
  53. (long long) a, (long long) b, (long long) c);
  54. ap = (grub_uint8_t *) &a;
  55. bp = (grub_uint8_t *) &b;
  56. for (i = 0; i < 4; i++)
  57. {
  58. grub_test_assert (ap[i] == bp[3 - i],
  59. "bswap bytes wrong: 0x%llx, 0x%llx",
  60. (long long) a, (long long) b);
  61. }
  62. }
  63. static void
  64. test64 (grub_uint64_t a)
  65. {
  66. grub_uint64_t b, c;
  67. grub_uint8_t *ap, *bp;
  68. int i;
  69. b = grub_swap_bytes64 (a);
  70. c = grub_swap_bytes64 (b);
  71. grub_test_assert (a == c, "bswap not idempotent: 0x%llx, 0x%llx, 0x%llx",
  72. (long long) a, (long long) b, (long long) c);
  73. ap = (grub_uint8_t *) &a;
  74. bp = (grub_uint8_t *) &b;
  75. for (i = 0; i < 4; i++)
  76. {
  77. grub_test_assert (ap[i] == bp[7 - i],
  78. "bswap bytes wrong: 0x%llx, 0x%llx",
  79. (long long) a, (long long) b);
  80. }
  81. }
  82. static void
  83. test_all(grub_uint64_t a)
  84. {
  85. test64 (a);
  86. test32 (a);
  87. test16 (a);
  88. }
  89. static void
  90. bswap_test (void)
  91. {
  92. grub_uint64_t a = 404, b = 7;
  93. grub_size_t i;
  94. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  95. {
  96. test_all (vectors[i]);
  97. }
  98. for (i = 0; i < 40000; i++)
  99. {
  100. a = 17 * a + 13 * b;
  101. b = 23 * a + 29 * b;
  102. if (b == 0)
  103. b = 1;
  104. if (a == 0)
  105. a = 1;
  106. test_all (a);
  107. test_all (b);
  108. }
  109. }
  110. /* Register example_test method as a functional test. */
  111. GRUB_FUNCTIONAL_TEST (bswap_test, bswap_test);