Files
binproto/tests/cpp_generator.rs
2025-11-05 15:00:05 +08:00

249 lines
6.4 KiB
Rust

use std::fs;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use binproto::{TargetLanguage, compile_schema, generator};
#[test]
fn test_cpp_simple_struct() {
let schema_path = "tests/schemas/simple_struct.binproto";
let src = fs::read_to_string(schema_path).expect("read schema");
let schema = compile_schema(&src).expect("compile schema");
let cpp_out = generator::generate(&schema, TargetLanguage::Cpp).expect("generate cpp");
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let tmp = std::env::temp_dir().join(format!("binproto_cpp_test_simple_{}", now));
fs::create_dir_all(&tmp).expect("create tmp dir");
let header_path = tmp.join("simple_struct.hpp");
fs::write(&header_path, cpp_out).expect("write header");
let test_cpp = r#"
#include <iostream>
#include <cassert>
#include "simple_struct.hpp"
int main() {
a obj;
obj.a = 1;
obj.b = 2;
obj.c = 1;
obj.d = 12345678;
obj.e = -87654321;
auto data = obj.to_bytes();
// Verify size and content
assert(data.size() == 10);
assert(data[0] == 0x01);
assert(data[1] == 0x05);
// Test decoding
auto obj2 = a::from_bytes(data);
assert(obj2.a == 1);
assert(obj2.b == 2);
assert(obj2.c == 1);
assert(obj2.d == 12345678);
assert(obj2.e == -87654321);
std::cout << "cpp_simple_struct test passed" << std::endl;
return 0;
}
"#;
let test_path = tmp.join("test.cpp");
fs::write(&test_path, test_cpp).expect("write test cpp");
// Compile
let output = Command::new("g++")
.arg("-std=c++17")
.arg("-O2")
.arg("-I")
.arg(&tmp)
.arg(&test_path)
.arg("-o")
.arg(tmp.join("test"))
.output()
.expect("compile cpp");
if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
panic!("cpp compilation failed");
}
// Run
let output = Command::new(tmp.join("test")).output().expect("run test");
assert!(
output.status.success(),
"test failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_cpp_nested_structs() {
let schema_path = "tests/schemas/nested_structs_and_constant_array.binproto";
let src = fs::read_to_string(schema_path).expect("read schema");
let schema = compile_schema(&src).expect("compile schema");
let cpp_out = generator::generate(&schema, TargetLanguage::Cpp).expect("generate cpp");
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let tmp = std::env::temp_dir().join(format!("binproto_cpp_test_nested_{}", now));
fs::create_dir_all(&tmp).expect("create tmp dir");
let header_path = tmp.join("nested.hpp");
fs::write(&header_path, cpp_out).expect("write header");
let test_cpp = r#"
#include <iostream>
#include <cassert>
#include "nested.hpp"
int main() {
outer obj;
obj.hdr.x = 1;
obj.hdr.y = 2;
obj.flags = 3;
obj.data[0] = 1;
obj.data[1] = 2;
obj.data[2] = 3;
auto data = obj.to_bytes();
// Verify expected encoding
assert(data.size() == 8);
assert(data[0] == 0x01);
assert(data[1] == 0x26);
// Test decoding
auto obj2 = outer::from_bytes(data);
assert(obj2.hdr.x == 1);
assert(obj2.hdr.y == 2);
assert(obj2.flags == 3);
assert(obj2.data[0] == 1);
assert(obj2.data[1] == 2);
assert(obj2.data[2] == 3);
std::cout << "cpp_nested_structs test passed" << std::endl;
return 0;
}
"#;
let test_path = tmp.join("test.cpp");
fs::write(&test_path, test_cpp).expect("write test cpp");
let output = Command::new("g++")
.arg("-std=c++17")
.arg("-O2")
.arg("-I")
.arg(&tmp)
.arg(&test_path)
.arg("-o")
.arg(tmp.join("test"))
.output()
.expect("compile cpp");
if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
panic!("cpp compilation failed");
}
let output = Command::new(tmp.join("test")).output().expect("run test");
assert!(
output.status.success(),
"test failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn test_cpp_dynamic_arrays() {
let schema_path = "tests/schemas/nested_array_of_structs_and_dynamic_array.binproto";
let src = fs::read_to_string(schema_path).expect("read schema");
let schema = compile_schema(&src).expect("compile schema");
let cpp_out = generator::generate(&schema, TargetLanguage::Cpp).expect("generate cpp");
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let tmp = std::env::temp_dir().join(format!("binproto_cpp_test_dynamic_{}", now));
fs::create_dir_all(&tmp).expect("create tmp dir");
let header_path = tmp.join("dynamic.hpp");
fs::write(&header_path, cpp_out).expect("write header");
let test_cpp = r#"
#include <iostream>
#include <cassert>
#include "dynamic.hpp"
int main() {
collection obj;
obj.count = 3;
obj.items.resize(3);
obj.items[0].a = 1;
obj.items[1].a = 2;
obj.items[2].a = 3;
auto data = obj.to_bytes();
// Verify expected encoding
assert(data.size() == 4);
assert(data[0] == 0x03);
assert(data[1] == 0x01);
assert(data[2] == 0x02);
assert(data[3] == 0x03);
// Test decoding
auto obj2 = collection::from_bytes(data);
assert(obj2.count == 3);
assert(obj2.items.size() == 3);
assert(obj2.items[0].a == 1);
assert(obj2.items[1].a == 2);
assert(obj2.items[2].a == 3);
std::cout << "cpp_dynamic_arrays test passed" << std::endl;
return 0;
}
"#;
let test_path = tmp.join("test.cpp");
fs::write(&test_path, test_cpp).expect("write test cpp");
let output = Command::new("g++")
.arg("-std=c++17")
.arg("-O2")
.arg("-I")
.arg(&tmp)
.arg(&test_path)
.arg("-o")
.arg(tmp.join("test"))
.output()
.expect("compile cpp");
if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
panic!("cpp compilation failed");
}
let output = Command::new(tmp.join("test")).output().expect("run test");
assert!(
output.status.success(),
"test failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}