Endian_test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Endian.h"
  16. #include <stdio.h>
  17. #include "util/Assert.h"
  18. static void printInfo()
  19. {
  20. printf("According to testing this machine is %s\n",
  21. Endian_isBigEndian() ? "big endian" : "little endian");
  22. }
  23. int main()
  24. {
  25. printInfo();
  26. volatile uint64_t b = 0x0123456789abcdef;
  27. volatile uint64_t sb = 0xefcdab8967452301;
  28. volatile uint32_t a = 0x01234567;
  29. volatile uint32_t sa = 0x67452301;
  30. volatile uint16_t c = 0xcabe;
  31. volatile uint16_t sc = 0xbeca;
  32. if (!Endian_isBigEndian()) {
  33. Assert_true(c == Endian_bigEndianToHost16(sc));
  34. Assert_true(c == Endian_hostToBigEndian16(sc));
  35. Assert_true(c == Endian_hostToLittleEndian16(c));
  36. Assert_true(c == Endian_littleEndianToHost16(c));
  37. Assert_true(a == Endian_bigEndianToHost32(sa));
  38. Assert_true(a == Endian_hostToBigEndian32(sa));
  39. Assert_true(a == Endian_hostToLittleEndian32(a));
  40. Assert_true(a == Endian_littleEndianToHost32(a));
  41. Assert_true(b == Endian_bigEndianToHost64(sb));
  42. Assert_true(b == Endian_hostToBigEndian64(sb));
  43. Assert_true(b == Endian_hostToLittleEndian64(b));
  44. Assert_true(b == Endian_littleEndianToHost64(b));
  45. } else {
  46. Assert_true(c == Endian_bigEndianToHost16(c));
  47. Assert_true(c == Endian_hostToBigEndian16(c));
  48. Assert_true(c == Endian_hostToLittleEndian16(sc));
  49. Assert_true(c == Endian_littleEndianToHost16(sc));
  50. Assert_true(a == Endian_bigEndianToHost32(a));
  51. Assert_true(a == Endian_hostToBigEndian32(a));
  52. Assert_true(a == Endian_hostToLittleEndian32(sa));
  53. Assert_true(a == Endian_littleEndianToHost32(sa));
  54. Assert_true(b == Endian_bigEndianToHost64(b));
  55. Assert_true(b == Endian_hostToBigEndian64(b));
  56. Assert_true(b == Endian_hostToLittleEndian64(sb));
  57. Assert_true(b == Endian_littleEndianToHost64(sb));
  58. }
  59. Assert_true(b == Endian_byteSwap64(sb));
  60. Assert_true(a == Endian_byteSwap32(sa));
  61. Assert_true(c == Endian_byteSwap16(sc));
  62. return 0;
  63. }