Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // $ gcc --pedantic c-byteswap-test.c -o c-byteswap-test && ./c-byteswap-test
- #include <byteswap.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #include <inttypes.h>
- #include <assert.h>
- int main (void) {
- fprintf (stdout, "---- int16_t ----\n");
- {
- int16_t a = 0;
- int16_t b = 0;
- int16_t a_r = 0;
- int16_t b_r = 0;
- a = 0xbeef;
- b = 0xefbe;
- a_r = bswap_16 (a);
- b_r = bswap_16 (b);
- //fprintf (stdout, "print format: " PRIx16 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx16 "(%"PRIu16") \t b: \t 0x%" PRIx16 "(%"PRIu16")\n"
- "b_r: \t 0x%" PRIx16"(%"PRIu16") \t a_r: \t 0x%" PRIx16 "(%"PRIu16")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- fprintf (stdout, "---- uint16_t ----\n");
- {
- uint16_t a = 0;
- uint16_t b = 0;
- uint16_t a_r = 0;
- uint16_t b_r = 0;
- a = 0xbeef;
- b = 0xefbe;
- a_r = bswap_16 (a);
- b_r = bswap_16 (b);
- //fprintf (stdout, "print format: " PRIx16 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx16 "(%"PRIi16") \t b: \t 0x%" PRIx16 "(%"PRIi16")\n"
- "b_r: \t 0x%" PRIx16"(%"PRIi16") \t a_r: \t 0x%" PRIx16 "(%"PRIi16")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- fprintf (stdout, "---- int32_t ----\n");
- {
- int32_t a = 0;
- int32_t b = 0;
- int32_t a_r = 0;
- int32_t b_r = 0;
- a = 0xdeadbeef;
- b = 0xefbeadde;
- a_r = bswap_32 (a);
- b_r = bswap_32 (b);
- // fprintf (stdout, "print format: " PRIx32 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx32 "(%"PRIi32") \t b: \t 0x%" PRIx32 "(%"PRIi32")\n"
- "b_r: \t 0x%" PRIx32"(%"PRIi32") \t a_r: \t 0x%" PRIx32 "(%"PRIi32")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- fprintf (stdout, "---- uint32_t ----\n");
- {
- uint32_t a = 0;
- uint32_t b = 0;
- uint32_t a_r = 0;
- uint32_t b_r = 0;
- a = 0xdeadbeef;
- b = 0xefbeadde;
- a_r = bswap_32 (a);
- b_r = bswap_32 (b);
- // fprintf (stdout, "print format: " PRIx32 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx32 "(%"PRIi32") \t b: \t 0x%" PRIx32 "(%"PRIi32")\n"
- "b_r: \t 0x%" PRIx32"(%"PRIi32") \t a_r: \t 0x%" PRIx32 "(%"PRIi32")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- fprintf (stdout, "---- int64_t ----\n");
- {
- int64_t a = 0;
- int64_t b = 0;
- int64_t a_r = 0;
- int64_t b_r = 0;
- a = 0xdeadbeefdeadbeef;
- b = 0xefbeaddeefbeadde;
- a_r = bswap_64 (a);
- b_r = bswap_64 (b);
- // fprintf (stdout, "print format: " PRIx64 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx64 "(%"PRIi64") \t b: \t 0x%" PRIx64 "(%"PRIi64")\n"
- "b_r: \t 0x%" PRIx64"(%"PRIi64") \t a_r: \t 0x%" PRIx64 "(%"PRIi64")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- fprintf (stdout, "---- uint64_t ----\n");
- {
- uint64_t a = 0;
- uint64_t b = 0;
- uint64_t a_r = 0;
- uint64_t b_r = 0;
- a = 0xdeadbeefdeadbeef;
- b = 0xefbeaddeefbeadde;
- a_r = bswap_64 (a);
- b_r = bswap_64 (b);
- // fprintf (stdout, "print format: " PRIx64 "\n");
- fprintf (stdout,
- "a: \t 0x%" PRIx64 "(%"PRIu64") \t b: \t 0x%" PRIx64 "(%"PRIu64")\n"
- "b_r: \t 0x%" PRIx64"(%"PRIu64") \t a_r: \t 0x%" PRIx64 "(%"PRIu64")\n",
- a, a, b, b,
- b_r, b_r, a_r, a_r);
- assert (b == a_r);
- assert (a == b_r);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement