Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed tests/c/decoder_framed
Binary file not shown.
45 changes: 0 additions & 45 deletions tests/c/decoder_framed.c

This file was deleted.

Binary file removed tests/c/encoder_framed
Binary file not shown.
43 changes: 0 additions & 43 deletions tests/c/encoder_framed.c

This file was deleted.

22 changes: 20 additions & 2 deletions tests/c/test_arrays.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ int test_array_operations() {
msg.bounded_sensors.data[0].status = 2;
strncpy(msg.bounded_sensors.data[0].name, "Pressure", 16);

// Encode message into BasicPacket format
uint8_t encode_buffer[1024];
msg_encode_buffer buffer = {0};
buffer.data = encode_buffer;
Expand All @@ -84,6 +85,7 @@ int test_array_operations() {
return 0;
}

// Validate and decode the BasicPacket
msg_info_t decode_result = format->validate_packet(encode_buffer, buffer.size);
if (!decode_result.valid) {
print_failure_details("Validation failed", encode_buffer, buffer.size);
Expand All @@ -93,8 +95,24 @@ int test_array_operations() {
ComprehensiveArraysComprehensiveArrayMessage decoded_msg =
comprehensive_arrays_comprehensive_array_message_get(decode_result);

if (decoded_msg.fixed_ints[0] != 1 || decoded_msg.bounded_uints.count != 3) {
print_failure_details("Value mismatch", encode_buffer, buffer.size);
// Compare original and decoded messages
if (decoded_msg.fixed_ints[0] != msg.fixed_ints[0]) {
print_failure_details("Value mismatch: fixed_ints[0]", encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.bounded_uints.count != msg.bounded_uints.count) {
print_failure_details("Value mismatch: bounded_uints.count", encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.bounded_uints.data[0] != msg.bounded_uints.data[0]) {
print_failure_details("Value mismatch: bounded_uints.data[0]", encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.fixed_sensors[0].id != msg.fixed_sensors[0].id) {
print_failure_details("Value mismatch: fixed_sensors[0].id", encode_buffer, buffer.size);
return 0;
}

Expand Down
23 changes: 20 additions & 3 deletions tests/c/test_basic_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int test_basic_types() {
msg.description.length = strlen("Test description for basic types");
strncpy(msg.description.data, "Test description for basic types", msg.description.length);

// Encode message into BasicPacket format
uint8_t encode_buffer[1024];
msg_encode_buffer buffer = {0};
buffer.data = encode_buffer;
Expand All @@ -71,6 +72,7 @@ int test_basic_types() {
return 0;
}

// Validate and decode the BasicPacket
msg_info_t decode_result = format->validate_packet(encode_buffer, buffer.size);
if (!decode_result.valid) {
print_failure_details("Validation failed", &msg, NULL, encode_buffer, buffer.size);
Expand All @@ -79,9 +81,24 @@ int test_basic_types() {

BasicTypesBasicTypesMessage decoded_msg = basic_types_basic_types_message_get(decode_result);

if (decoded_msg.small_int != -42 || decoded_msg.medium_int != -1000 ||
decoded_msg.flag != true) {
print_failure_details("Value mismatch", &msg, &decoded_msg, encode_buffer, buffer.size);
// Compare original and decoded messages
if (decoded_msg.small_int != msg.small_int) {
print_failure_details("Value mismatch: small_int", &msg, &decoded_msg, encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.medium_int != msg.medium_int) {
print_failure_details("Value mismatch: medium_int", &msg, &decoded_msg, encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.flag != msg.flag) {
print_failure_details("Value mismatch: flag", &msg, &decoded_msg, encode_buffer, buffer.size);
return 0;
}

if (decoded_msg.single_precision != msg.single_precision) {
print_failure_details("Value mismatch: single_precision", &msg, &decoded_msg, encode_buffer, buffer.size);
return 0;
}

Expand Down
125 changes: 125 additions & 0 deletions tests/c/test_cross_platform_deserialization.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "serialization_test.sf.h"
#include "struct_frame_default_frame.h"

void print_failure_details(const char* label, const void* raw_data, size_t raw_data_size) {
printf("\n");
printf("============================================================\n");
printf("FAILURE DETAILS: %s\n", label);
printf("============================================================\n");

if (raw_data && raw_data_size > 0) {
printf("\nRaw Data (%zu bytes):\n Hex: ", raw_data_size);
for (size_t i = 0; i < raw_data_size && i < 64; i++) {
printf("%02x", ((const uint8_t*)raw_data)[i]);
}
if (raw_data_size > 64) printf("...");
printf("\n");
}

printf("============================================================\n\n");
}

int validate_message(SerializationTestSerializationTestMessage* msg) {
// Expected values from expected_values.json
uint32_t expected_magic = 3735928559; // 0xDEADBEEF
const char* expected_string = "Cross-platform test!";
float expected_float = 3.14159f;
bool expected_bool = true;
int expected_array[3] = {100, 200, 300};

if (msg->magic_number != expected_magic) {
printf(" Value mismatch: magic_number (expected %u, got %u)\n",
expected_magic, msg->magic_number);
return 0;
}

if (strncmp(msg->test_string.data, expected_string, msg->test_string.length) != 0) {
printf(" Value mismatch: test_string\n");
return 0;
}

if (fabs(msg->test_float - expected_float) > 0.0001f) {
printf(" Value mismatch: test_float (expected %f, got %f)\n",
expected_float, msg->test_float);
return 0;
}

if (msg->test_bool != expected_bool) {
printf(" Value mismatch: test_bool\n");
return 0;
}

if (msg->test_array.count != 3) {
printf(" Value mismatch: test_array.count (expected 3, got %u)\n",
msg->test_array.count);
return 0;
}

for (int i = 0; i < 3; i++) {
if (msg->test_array.data[i] != expected_array[i]) {
printf(" Value mismatch: test_array[%d] (expected %d, got %d)\n",
i, expected_array[i], msg->test_array.data[i]);
return 0;
}
}

return 1;
}

int read_and_validate_test_data(const char* filename, const char* language) {
FILE* file = fopen(filename, "rb");
if (!file) {
printf(" Skipping %s - file not found: %s\n", language, filename);
return 1; // Skip if file not available
}

uint8_t buffer[512];
size_t size = fread(buffer, 1, sizeof(buffer), file);
fclose(file);

if (size == 0) {
print_failure_details("Empty file", buffer, size);
return 0;
}

packet_format_t* format = &default_frame_format;
msg_info_t decode_result = format->validate_packet(buffer, size);

if (!decode_result.valid) {
char label[256];
snprintf(label, sizeof(label), "Failed to decode %s data", language);
print_failure_details(label, buffer, size);
return 0;
}

SerializationTestSerializationTestMessage decoded_msg =
serialization_test_serialization_test_message_get(decode_result);

if (!validate_message(&decoded_msg)) {
printf(" Validation failed for %s data\n", language);
return 0;
}

printf(" ✓ %s data validated successfully\n", language);
return 1;
}

int main() {
printf("\n[TEST START] C Cross-Platform Deserialization\n");

int success = 1;
success = success && read_and_validate_test_data("python_test_data.bin", "Python");
success = success && read_and_validate_test_data("c_test_data.bin", "C");
success = success && read_and_validate_test_data("cpp_test_data.bin", "C++");
success = success && read_and_validate_test_data("typescript_test_data.bin", "TypeScript");

const char* status = success ? "PASS" : "FAIL";
printf("[TEST END] C Cross-Platform Deserialization: %s\n\n", status);

return success ? 0 : 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ void print_failure_details(const char* label, const void* raw_data, size_t raw_d
int create_test_data() {
SerializationTestSerializationTestMessage msg = {0};

msg.magic_number = 0xDEADBEEF;
msg.test_string.length = strlen("Hello from C!");
strncpy(msg.test_string.data, "Hello from C!", msg.test_string.length);
// Use values from expected_values.json
msg.magic_number = 3735928559; // 0xDEADBEEF
msg.test_string.length = strlen("Cross-platform test!");
strncpy(msg.test_string.data, "Cross-platform test!", msg.test_string.length);
msg.test_float = 3.14159f;
msg.test_bool = true;
msg.test_array.count = 3;
Expand Down Expand Up @@ -57,6 +58,7 @@ int create_test_data() {
fwrite(encode_buffer, 1, buffer.size, file);
fclose(file);

// Self-validate
msg_info_t decode_result = format->validate_packet(encode_buffer, buffer.size);
if (!decode_result.valid) {
print_failure_details("Self-validation failed", encode_buffer, buffer.size);
Expand All @@ -66,53 +68,21 @@ int create_test_data() {
SerializationTestSerializationTestMessage decoded_msg =
serialization_test_serialization_test_message_get(decode_result);

if (decoded_msg.magic_number != 0xDEADBEEF || decoded_msg.test_array.count != 3) {
if (decoded_msg.magic_number != 3735928559 || decoded_msg.test_array.count != 3) {
print_failure_details("Self-verification failed", encode_buffer, buffer.size);
return 0;
}

return 1;
}

int read_test_data(const char* filename, const char* language) {
FILE* file = fopen(filename, "rb");
if (!file) {
return 1; // Skip if file not available
}

uint8_t buffer[512];
size_t size = fread(buffer, 1, sizeof(buffer), file);
fclose(file);

if (size == 0) {
print_failure_details("Empty file", buffer, size);
return 0;
}

packet_format_t* format = &default_frame_format;
msg_info_t decode_result = format->validate_packet(buffer, size);

if (!decode_result.valid) {
char label[256];
snprintf(label, sizeof(label), "Failed to decode %s data", language);
print_failure_details(label, buffer, size);
return 0;
}

return 1;
}

int main() {
printf("\n[TEST START] C Cross-Language Serialization\n");
printf("\n[TEST START] C Cross-Platform Serialization\n");

int success = create_test_data();
if (success) {
success = success && read_test_data("python_test_data.bin", "Python");
success = success && read_test_data("typescript_test_data.bin", "TypeScript");
}

const char* status = success ? "PASS" : "FAIL";
printf("[TEST END] C Cross-Language Serialization: %s\n\n", status);
printf("[TEST END] C Cross-Platform Serialization: %s\n\n", status);

return success ? 0 : 1;
}
Loading