test_binary.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "common.h"
  2. #include <assert.h>
  3. #include "util/binary.h"
  4. static void test_write16be(void) {
  5. uint16_t val = 0xABCD;
  6. uint8_t buf[2];
  7. sc_write16be(buf, val);
  8. assert(buf[0] == 0xAB);
  9. assert(buf[1] == 0xCD);
  10. }
  11. static void test_write32be(void) {
  12. uint32_t val = 0xABCD1234;
  13. uint8_t buf[4];
  14. sc_write32be(buf, val);
  15. assert(buf[0] == 0xAB);
  16. assert(buf[1] == 0xCD);
  17. assert(buf[2] == 0x12);
  18. assert(buf[3] == 0x34);
  19. }
  20. static void test_write64be(void) {
  21. uint64_t val = 0xABCD1234567890EF;
  22. uint8_t buf[8];
  23. sc_write64be(buf, val);
  24. assert(buf[0] == 0xAB);
  25. assert(buf[1] == 0xCD);
  26. assert(buf[2] == 0x12);
  27. assert(buf[3] == 0x34);
  28. assert(buf[4] == 0x56);
  29. assert(buf[5] == 0x78);
  30. assert(buf[6] == 0x90);
  31. assert(buf[7] == 0xEF);
  32. }
  33. static void test_read16be(void) {
  34. uint8_t buf[2] = {0xAB, 0xCD};
  35. uint16_t val = sc_read16be(buf);
  36. assert(val == 0xABCD);
  37. }
  38. static void test_read32be(void) {
  39. uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
  40. uint32_t val = sc_read32be(buf);
  41. assert(val == 0xABCD1234);
  42. }
  43. static void test_read64be(void) {
  44. uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
  45. 0x56, 0x78, 0x90, 0xEF};
  46. uint64_t val = sc_read64be(buf);
  47. assert(val == 0xABCD1234567890EF);
  48. }
  49. static void test_float_to_u16fp(void) {
  50. assert(sc_float_to_u16fp(0.0f) == 0);
  51. assert(sc_float_to_u16fp(0.03125f) == 0x800);
  52. assert(sc_float_to_u16fp(0.0625f) == 0x1000);
  53. assert(sc_float_to_u16fp(0.125f) == 0x2000);
  54. assert(sc_float_to_u16fp(0.25f) == 0x4000);
  55. assert(sc_float_to_u16fp(0.5f) == 0x8000);
  56. assert(sc_float_to_u16fp(0.75f) == 0xc000);
  57. assert(sc_float_to_u16fp(1.0f) == 0xffff);
  58. }
  59. static void test_float_to_i16fp(void) {
  60. assert(sc_float_to_i16fp(0.0f) == 0);
  61. assert(sc_float_to_i16fp(0.03125f) == 0x400);
  62. assert(sc_float_to_i16fp(0.0625f) == 0x800);
  63. assert(sc_float_to_i16fp(0.125f) == 0x1000);
  64. assert(sc_float_to_i16fp(0.25f) == 0x2000);
  65. assert(sc_float_to_i16fp(0.5f) == 0x4000);
  66. assert(sc_float_to_i16fp(0.75f) == 0x6000);
  67. assert(sc_float_to_i16fp(1.0f) == 0x7fff);
  68. assert(sc_float_to_i16fp(-0.03125f) == -0x400);
  69. assert(sc_float_to_i16fp(-0.0625f) == -0x800);
  70. assert(sc_float_to_i16fp(-0.125f) == -0x1000);
  71. assert(sc_float_to_i16fp(-0.25f) == -0x2000);
  72. assert(sc_float_to_i16fp(-0.5f) == -0x4000);
  73. assert(sc_float_to_i16fp(-0.75f) == -0x6000);
  74. assert(sc_float_to_i16fp(-1.0f) == -0x8000);
  75. }
  76. int main(int argc, char *argv[]) {
  77. (void) argc;
  78. (void) argv;
  79. test_write16be();
  80. test_write32be();
  81. test_write64be();
  82. test_read16be();
  83. test_read32be();
  84. test_read64be();
  85. test_float_to_u16fp();
  86. test_float_to_i16fp();
  87. return 0;
  88. }