Files
MAG160C/csdk/tests/test_tcm.c
T

70 lines
2.4 KiB
C

/* TCM framing tests (0x7e FTDICommand format). */
#include "mag160c/mag160c.h"
#include <assert.h>
#include <stdio.h>
static void test_rotate_frame(void) {
/* vendor byte-exact: 7e 00 07 85 02 77 00 01 00 05 7f */
const uint8_t expected[] = {0x7e, 0x00, 0x07, 0x85, 0x02, 0x77,
0x00, 0x01, 0x00, 0x05, 0x7f};
uint8_t out[64];
size_t size = 0;
assert(mag160c_tcm_rotate(5, out, sizeof(out), &size) == MAG160C_OK);
assert(size == sizeof(expected));
for (size_t i = 0; i < size; ++i) {
assert(out[i] == expected[i]);
}
}
static void test_light_frame(void) {
/* blink green: 7e 00 09 87 02 32 00 01 01 00 ff 00 35 */
const uint8_t expected[] = {0x7e, 0x00, 0x09, 0x87, 0x02, 0x32, 0x00,
0x01, 0x01, 0x00, 0xff, 0x00, 0x35};
uint8_t out[64];
size_t size = 0;
assert(mag160c_tcm_light(MAG160C_TCM_LIGHT_GREEN, MAG160C_TCM_LIGHT_BLINK,
out, sizeof(out), &size) == MAG160C_OK);
assert(size == sizeof(expected));
for (size_t i = 0; i < size; ++i) {
assert(out[i] == expected[i]);
}
}
static void test_decode_roundtrip(void) {
const uint8_t payload[] = {0x10, 0x20, 0x30};
uint8_t enc[64];
size_t enc_size = 0;
assert(mag160c_tcm_encode(0xa1, 0xb2, 0xc3d4, payload, sizeof(payload),
enc, sizeof(enc), &enc_size) == MAG160C_OK);
uint8_t main = 0, sub = 0, pl[16];
uint16_t frame_id = 0;
size_t pl_size = 0;
assert(mag160c_tcm_decode(enc, enc_size, &main, &sub, &frame_id, pl,
sizeof(pl), &pl_size) == MAG160C_OK);
assert(main == 0xa1 && sub == 0xb2 && frame_id == 0xc3d4);
assert(pl_size == 3 && pl[0] == 0x10 && pl[1] == 0x20 && pl[2] == 0x30);
enc[3] ^= 0xff; /* corrupt header checksum */
assert(mag160c_tcm_decode(enc, enc_size, &main, &sub, &frame_id, pl,
sizeof(pl), &pl_size) == MAG160C_ERR_CHECKSUM);
}
static void test_rotate_clamp(void) {
uint8_t out[64];
size_t size = 0;
assert(mag160c_tcm_rotate(-200, out, sizeof(out), &size) == MAG160C_OK);
/* direction=1, magnitude=128 */
assert(out[8] == 1 && out[9] == 128);
}
int main(void) {
test_rotate_frame();
test_light_frame();
test_decode_roundtrip();
test_rotate_clamp();
printf("test_tcm: all passed\n");
return 0;
}