2021-01-08 18:24:51 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-11-30 04:33:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
#include "util/binary.h"
|
2019-11-30 04:33:00 +00:00
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_write16be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint16_t val = 0xABCD;
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
sc_write16be(buf, val);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(buf[0] == 0xAB);
|
|
|
|
assert(buf[1] == 0xCD);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_write32be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint32_t val = 0xABCD1234;
|
|
|
|
uint8_t buf[4];
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
sc_write32be(buf, val);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(buf[0] == 0xAB);
|
|
|
|
assert(buf[1] == 0xCD);
|
|
|
|
assert(buf[2] == 0x12);
|
|
|
|
assert(buf[3] == 0x34);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_write64be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint64_t val = 0xABCD1234567890EF;
|
|
|
|
uint8_t buf[8];
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
sc_write64be(buf, val);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(buf[0] == 0xAB);
|
|
|
|
assert(buf[1] == 0xCD);
|
|
|
|
assert(buf[2] == 0x12);
|
|
|
|
assert(buf[3] == 0x34);
|
|
|
|
assert(buf[4] == 0x56);
|
|
|
|
assert(buf[5] == 0x78);
|
|
|
|
assert(buf[6] == 0x90);
|
|
|
|
assert(buf[7] == 0xEF);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_read16be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint8_t buf[2] = {0xAB, 0xCD};
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
uint16_t val = sc_read16be(buf);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(val == 0xABCD);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_read32be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint8_t buf[4] = {0xAB, 0xCD, 0x12, 0x34};
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
uint32_t val = sc_read32be(buf);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(val == 0xABCD1234);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
static void test_read64be(void) {
|
2019-11-30 04:33:00 +00:00
|
|
|
uint8_t buf[8] = {0xAB, 0xCD, 0x12, 0x34,
|
|
|
|
0x56, 0x78, 0x90, 0xEF};
|
|
|
|
|
2022-02-12 08:12:46 +00:00
|
|
|
uint64_t val = sc_read64be(buf);
|
2019-11-30 04:33:00 +00:00
|
|
|
|
|
|
|
assert(val == 0xABCD1234567890EF);
|
|
|
|
}
|
|
|
|
|
2022-08-03 13:23:39 +00:00
|
|
|
static void test_float_to_u16fp(void) {
|
|
|
|
assert(sc_float_to_u16fp(0.0f) == 0);
|
|
|
|
assert(sc_float_to_u16fp(0.03125f) == 0x800);
|
|
|
|
assert(sc_float_to_u16fp(0.0625f) == 0x1000);
|
|
|
|
assert(sc_float_to_u16fp(0.125f) == 0x2000);
|
|
|
|
assert(sc_float_to_u16fp(0.25f) == 0x4000);
|
|
|
|
assert(sc_float_to_u16fp(0.5f) == 0x8000);
|
|
|
|
assert(sc_float_to_u16fp(0.75f) == 0xc000);
|
|
|
|
assert(sc_float_to_u16fp(1.0f) == 0xffff);
|
|
|
|
}
|
|
|
|
|
2022-08-03 11:04:15 +00:00
|
|
|
static void test_float_to_i16fp(void) {
|
|
|
|
assert(sc_float_to_i16fp(0.0f) == 0);
|
|
|
|
assert(sc_float_to_i16fp(0.03125f) == 0x400);
|
|
|
|
assert(sc_float_to_i16fp(0.0625f) == 0x800);
|
|
|
|
assert(sc_float_to_i16fp(0.125f) == 0x1000);
|
|
|
|
assert(sc_float_to_i16fp(0.25f) == 0x2000);
|
|
|
|
assert(sc_float_to_i16fp(0.5f) == 0x4000);
|
|
|
|
assert(sc_float_to_i16fp(0.75f) == 0x6000);
|
|
|
|
assert(sc_float_to_i16fp(1.0f) == 0x7fff);
|
|
|
|
|
|
|
|
assert(sc_float_to_i16fp(-0.03125f) == -0x400);
|
|
|
|
assert(sc_float_to_i16fp(-0.0625f) == -0x800);
|
|
|
|
assert(sc_float_to_i16fp(-0.125f) == -0x1000);
|
|
|
|
assert(sc_float_to_i16fp(-0.25f) == -0x2000);
|
|
|
|
assert(sc_float_to_i16fp(-0.5f) == -0x4000);
|
|
|
|
assert(sc_float_to_i16fp(-0.75f) == -0x6000);
|
|
|
|
assert(sc_float_to_i16fp(-1.0f) == -0x8000);
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:17:04 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2022-08-03 13:13:16 +00:00
|
|
|
test_write16be();
|
|
|
|
test_write32be();
|
|
|
|
test_write64be();
|
|
|
|
test_read16be();
|
|
|
|
test_read32be();
|
|
|
|
test_read64be();
|
2022-08-03 13:23:39 +00:00
|
|
|
|
|
|
|
test_float_to_u16fp();
|
2022-08-03 11:04:15 +00:00
|
|
|
test_float_to_i16fp();
|
2019-11-30 04:33:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|