refactor: merge protocol cores as SETProtocol
This commit is contained in:
74
c/set-protocol/tests/test_abi.c
Normal file
74
c/set-protocol/tests/test_abi.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pcan_abi.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint32_t raw = pcan_abi_id_pack(1U, 0U, 7U, 14U, 3U, 0x1234U);
|
||||
if (raw != 0x17E31234UL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t data[] = { 0xAAU, 0xBBU };
|
||||
uint8_t encoded[32];
|
||||
size_t encoded_size = pcan_abi_frame_encode(
|
||||
1U, 0x05U, raw, data, 2U, encoded, sizeof encoded);
|
||||
if (encoded_size == 0U) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
size_t parser_size = pcan_abi_parser_size();
|
||||
void *parser = malloc(parser_size);
|
||||
if ((parser == NULL) || !pcan_abi_parser_init(parser, parser_size)) {
|
||||
free(parser);
|
||||
return 3;
|
||||
}
|
||||
|
||||
pcan_abi_frame_t frame;
|
||||
memset(&frame, 0, sizeof frame);
|
||||
int found = 0;
|
||||
for (size_t i = 0U; i < encoded_size; ++i) {
|
||||
found += pcan_abi_parser_push(parser, encoded[i], &frame) > 0;
|
||||
}
|
||||
free(parser);
|
||||
|
||||
if ((found != 1) || (frame.can_id != raw) || (frame.dlc != 2U) ||
|
||||
(frame.data[0] != 0xAAU) || (frame.data[1] != 0xBBU)) {
|
||||
return 4;
|
||||
}
|
||||
|
||||
uint8_t gui_raw[32];
|
||||
size_t gui_size = pcan_abi_gui_frame_encode(
|
||||
1U, 0x1234U, NULL, 0U, gui_raw, sizeof gui_raw);
|
||||
static const uint8_t gui_want[] = {
|
||||
0xA5U, 0x5AU, 0x01U, 0x01U, 0x12U, 0x34U, 0x00U, 0x00U,
|
||||
0xEEU, 0x89U, 0x8CU, 0x9EU
|
||||
};
|
||||
if ((gui_size != sizeof gui_want) ||
|
||||
(memcmp(gui_raw, gui_want, sizeof gui_want) != 0)) {
|
||||
return 5;
|
||||
}
|
||||
size_t gui_parser_size = pcan_abi_gui_parser_size();
|
||||
void *gui_parser = malloc(gui_parser_size);
|
||||
pcan_abi_gui_frame_t gui_frame;
|
||||
memset(&gui_frame, 0, sizeof gui_frame);
|
||||
if ((gui_parser == NULL) ||
|
||||
!pcan_abi_gui_parser_init(gui_parser, gui_parser_size)) {
|
||||
free(gui_parser);
|
||||
return 6;
|
||||
}
|
||||
found = 0;
|
||||
for (size_t i = 0U; i < gui_size; ++i) {
|
||||
found += pcan_abi_gui_parser_push(gui_parser, gui_raw[i],
|
||||
&gui_frame) > 0;
|
||||
}
|
||||
free(gui_parser);
|
||||
if ((found != 1) || (gui_frame.message_type != 1U) ||
|
||||
(gui_frame.sequence != 0x1234U) || (gui_frame.size != 0U)) {
|
||||
return 7;
|
||||
}
|
||||
puts("ABI tests passed");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user