Add a uniq test module for yunq in rust.

This commit is contained in:
Drew Galbraith 2024-08-29 21:27:27 -07:00
parent c1db6cb11f
commit 4ee8dc924c
13 changed files with 132 additions and 10 deletions

View File

@ -1,13 +1,9 @@
package ex;
message Basic {
u64 field;
}
message Types {
u64 unsigned_int;
i64 signed_int;
string str;
string strn;
}
message Cap {

View File

@ -4,3 +4,6 @@ build-std = ["core", "compiler_builtins", "alloc"]
[build]
target = "x86_64-acadia-os.json"
[alias]
test_pc = "test --target=x86_64-unknown-linux-gnu -Z build-std=std --lib"

10
rust/Cargo.lock generated
View File

@ -210,6 +210,16 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "yunq-test"
version = "0.1.0"
dependencies = [
"mammoth",
"yunq",
"yunq-derive",
"yunqc",
]
[[package]]
name = "yunqc"
version = "0.1.0"

View File

@ -1,7 +1,7 @@
[workspace]
members = [ "lib/denali",
"lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "sys/teton", "sys/yellowstone", "usr/testbed",
"lib/mammoth", "lib/victoriafalls", "lib/voyageurs", "lib/yellowstone", "lib/yunq", "lib/yunq-derive", "lib/yunq-test", "sys/teton", "sys/yellowstone", "usr/testbed",
]
resolver = "2"

View File

@ -9,4 +9,6 @@ name = "mammoth"
[dependencies]
linked_list_allocator = "0.10.5"
[build-dependencies]
[features]
hosted = []
default = ["hosted"]

View File

@ -2,13 +2,17 @@ use crate::cap::Capability;
use crate::syscall;
use crate::zion::ZError;
use alloc::slice;
#[cfg(feature = "hosted")]
use linked_list_allocator::LockedHeap;
#[cfg(feature = "hosted")]
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
pub static mut CAN_ALLOC: bool = false;
#[cfg(feature = "hosted")]
pub fn init_heap() {
// 1 MiB
let size = 0x10_0000;

View File

@ -5,6 +5,8 @@ use crate::zion;
use crate::zion::z_cap_t;
use crate::zion::ZError;
use core::ffi::c_void;
#[cfg(feature = "hosted")]
use core::panic::PanicInfo;
#[must_use]
@ -18,6 +20,7 @@ fn syscall<T>(id: u64, req: &T) -> Result<(), ZError> {
Ok(())
}
#[cfg(feature = "hosted")]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
unsafe {

View File

@ -29,7 +29,13 @@ fn serialize_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenS
} else if path.is_ident("u64") {
quote! {
{
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name as u64)?;
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?;
}
}
} else if path.is_ident("i64") {
quote! {
{
buf.write_at(yunq::message::field_offset(offset, #ind), self.#name)?;
}
}
} else {
@ -67,6 +73,10 @@ fn parse_field(name: &Ident, ind: usize, path: &Path) -> proc_macro2::TokenStrea
quote! {
let #name = buf.at::<u64>(yunq::message::field_offset(offset, #ind))?;
}
} else if path.is_ident("i64") {
quote! {
let #name = buf.at::<i64>(yunq::message::field_offset(offset, #ind))?;
}
} else {
quote! {
let #name = {

View File

@ -0,0 +1,14 @@
[package]
name = "yunq-test"
version = "0.1.0"
edition = "2021"
[dependencies]
mammoth = { path = "../mammoth", default-features = false}
yunq = {path = "../yunq", default-features = false}
yunq-derive = {path = "../yunq-derive"}
[build-dependencies]
yunqc = {path = "../../../yunq/rust"}
[dev-dependencies]

View File

@ -0,0 +1,14 @@
use std::fs;
fn main() {
let input_file = "../../../lib/yunq/test/example/example.yunq";
println!("cargo::rerun-if-changed={input_file}");
let input = fs::read_to_string(input_file).expect("Failed to read input file");
let code = yunqc::codegen(&input).expect("Failed to generate yunq code.");
let out = std::env::var("OUT_DIR").unwrap() + "/yunq.rs";
fs::write(out, code).expect("Failed to write generated code.");
}

View File

@ -0,0 +1,62 @@
#![no_std]
include!(concat!(env!("OUT_DIR"), "/yunq.rs"));
#[cfg(test)]
mod tests {
use super::*;
extern crate std;
use std::vec;
#[test]
fn basic_serialization() -> Result<(), ZError> {
let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
basic.serialize(&mut buf, 0, &mut caps)?;
let parsed = Basic::parse(&buf, 0, &caps)?;
assert!(parsed == basic);
Ok(())
}
#[test]
fn basic_serialization_as_request() -> Result<(), ZError> {
let basic = Basic { unsigned_int: 82, signed_int: -1234, strn: "abc".to_string() };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
let req_id = 12;
basic.serialize_as_request(req_id, &mut buf, &mut caps)?;
assert!(buf.at::<u64>(8)? == req_id);
let parsed = Basic::parse_from_request(&buf, &caps)?;
assert!(parsed == basic);
Ok(())
}
#[test]
fn capability_serialization() -> Result<(), ZError> {
let cap_id = 100;
let cap = Cap { cap: cap_id };
let mut buf = ByteBuffer::<1024>::new();
let mut caps = Vec::new();
cap.serialize(&mut buf, 0, &mut caps)?;
assert!(caps.len() == 1);
assert!(caps[0] == cap_id);
let parsed = Cap::parse(&buf, 0, &caps)?;
assert!(parsed == cap);
Ok(())
}
}

View File

@ -4,5 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
mammoth = {path = "../mammoth"}
mammoth = {path = "../mammoth", default-features = false}
yunq-derive = {path = "../yunq-derive"}
[features]
default = ["hosted"]
hosted = ["mammoth/hosted"]

View File

@ -17,7 +17,7 @@ fn generate_message(message: &Message) -> TokenStream {
.iter()
.map(|f| Ident::new(f.field_type.rust_type(), Span::call_site()));
quote! {
#[derive(YunqMessage)]
#[derive(YunqMessage, PartialEq, Eq)]
pub struct #name {
#(pub #field_names: #field_types),*
}