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
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,9 @@ tasks.register('generateRustTestCodecs', JavaExec) {
'sbe-tool/src/test/resources/issue1028.xml',
'sbe-tool/src/test/resources/issue1057.xml',
'sbe-tool/src/test/resources/issue1066.xml',
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
'sbe-tool/src/test/resources/basic-variable-length-schema.xml',
'sbe-tool/src/test/resources/example-bigendian-test-schema.xml',
'sbe-tool/src/test/resources/fixed-sized-primitive-array-types.xml',
'sbe-tool/src/test/resources/nested-composite-name.xml',
]
}
Expand Down
9 changes: 5 additions & 4 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "sbe_rust_example"
version = "0.1.0"
authors = []
edition = "2018"
edition = "2021"
publish = false

[dependencies]
Expand All @@ -19,12 +19,13 @@ issue_987 = { path = "../generated/rust/issue987" }
issue_1028 = { path = "../generated/rust/issue1028" }
issue_1057 = { path = "../generated/rust/issue1057" }
issue_1066 = { path = "../generated/rust/issue1066" }
baseline_bigendian = { path = "../generated/rust/baseline-bigendian" }
nested_composite_name = { path = "../generated/rust/nested-composite-name" }
baseline_bigendian = { path = "../generated/rust/baseline_bigendian" }
nested_composite_name = { path = "../generated/rust/nested_composite_name" }
sbe_tests = { path = "../generated/rust/sbe_tests" }
fixed_sized_primitive_array = { path = "../generated/rust/fixed_sized_primitive_array" }

[dev-dependencies]
criterion = "0.5"
criterion = "0.7"

[[bench]]
name = "car_benchmark"
Expand Down
64 changes: 51 additions & 13 deletions rust/tests/baseline_enum_from_str.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,60 @@
use examples_baseline::boost_type::BoostType;
use BoostType::{NullVal, KERS, NITROUS, SUPERCHARGER, TURBO};
use examples_baseline::{
boost_type::BoostType,
};

#[test]
fn test_boost_type_from_str() -> Result<(), ()> {
assert_eq!("TURBO".parse::<BoostType>()?, TURBO, "Parse \"TURBO\" as BoostType");
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType");
assert_eq!("NITROUS".parse::<BoostType>()?, NITROUS, "Parse \"NITROUS\" as BoostType");
assert_eq!("KERS".parse::<BoostType>()?, KERS, "Parse \"KERS\" as BoostType");
assert_eq!(
"TURBO".parse::<BoostType>()?,
TURBO,
"Parse \"TURBO\" as BoostType"
);
assert_eq!(
"SUPERCHARGER".parse::<BoostType>()?,
SUPERCHARGER,
"Parse \"SUPERCHARGER\" as BoostType"
);
assert_eq!(
"NITROUS".parse::<BoostType>()?,
NITROUS,
"Parse \"NITROUS\" as BoostType"
);
assert_eq!(
"KERS".parse::<BoostType>()?,
KERS,
"Parse \"KERS\" as BoostType"
);

assert_eq!("Turbo".parse::<BoostType>()?, NullVal, "Parse \"Turbo\" as BoostType");
assert_eq!("Supercharger".parse::<BoostType>()?, NullVal, "Parse \"Supercharger\" as BoostType");
assert_eq!("Nitrous".parse::<BoostType>()?, NullVal, "Parse \"Nitrous\" as BoostType");
assert_eq!("Kers".parse::<BoostType>()?, NullVal, "Parse \"Kers\" as BoostType");
assert_eq!(
"Turbo".parse::<BoostType>()?,
NullVal,
"Parse \"Turbo\" as BoostType"
);
assert_eq!(
"Supercharger".parse::<BoostType>()?,
NullVal,
"Parse \"Supercharger\" as BoostType"
);
assert_eq!(
"Nitrous".parse::<BoostType>()?,
NullVal,
"Parse \"Nitrous\" as BoostType"
);
assert_eq!(
"Kers".parse::<BoostType>()?,
NullVal,
"Parse \"Kers\" as BoostType"
);

assert_eq!("AA".parse::<BoostType>()?, NullVal, "Parse \"AA\" as BoostType");
assert_eq!("".parse::<BoostType>().unwrap(), NullVal, "Parse \"\" as BoostType");
assert_eq!(
"AA".parse::<BoostType>()?,
NullVal,
"Parse \"AA\" as BoostType"
);
assert_eq!(
"".parse::<BoostType>().unwrap(),
NullVal,
"Parse \"\" as BoostType"
);

Ok(())
}
34 changes: 26 additions & 8 deletions rust/tests/baseline_enum_into.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
use examples_baseline::{
boost_type::BoostType,
};
use examples_baseline::boost_type::BoostType;

#[test]
fn test_boost_type_from_str() -> Result<(), ()> {
assert_eq!(<BoostType as Into<u8>>::into(BoostType::TURBO), 84_u8, "BoostType::TURBO into value");
assert_eq!(<BoostType as Into<u8>>::into(BoostType::SUPERCHARGER), 83_u8, "BoostType::SUPERCHARGER into value");
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NITROUS), 78_u8, "BoostType::NITROUS into value");
assert_eq!(<BoostType as Into<u8>>::into(BoostType::KERS), 75_u8, "BoostType::KERS into value");
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NullVal), 0_u8, "BoostType::NullVal into value");
assert_eq!(
<BoostType as Into<u8>>::into(BoostType::TURBO),
84_u8,
"BoostType::TURBO into value"
);
assert_eq!(
<BoostType as Into<u8>>::into(BoostType::SUPERCHARGER),
83_u8,
"BoostType::SUPERCHARGER into value"
);
assert_eq!(
<BoostType as Into<u8>>::into(BoostType::NITROUS),
78_u8,
"BoostType::NITROUS into value"
);
assert_eq!(
<BoostType as Into<u8>>::into(BoostType::KERS),
75_u8,
"BoostType::KERS into value"
);
assert_eq!(
<BoostType as Into<u8>>::into(BoostType::NullVal),
0_u8,
"BoostType::NullVal into value"
);

Ok(())
}
28 changes: 21 additions & 7 deletions rust/tests/baseline_enum_to_str.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use examples_baseline::{
boost_type::BoostType,
};
use examples_baseline::boost_type::BoostType;

#[test]
fn test_boost_type_from_str() -> Result<(), ()> {
assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\"");
assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\"");
assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\"");
assert_eq!(
format!("{}", BoostType::TURBO),
"TURBO",
"Display \"TURBO\""
);
assert_eq!(
format!("{}", BoostType::SUPERCHARGER),
"SUPERCHARGER",
"Display \"SUPERCHARGER\""
);
assert_eq!(
format!("{}", BoostType::NITROUS),
"NITROUS",
"Display \"NITROUS\""
);
assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\"");
assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\"");
assert_eq!(
format!("{}", BoostType::NullVal),
"NullVal",
"Display \"NullVal\""
);

Ok(())
}
2 changes: 1 addition & 1 deletion rust/tests/baseline_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,4 @@ fn test_issue_1018() {
assert_eq!(1, examples_baseline::SBE_SCHEMA_ID);
assert_eq!(0, examples_baseline::SBE_SCHEMA_VERSION);
assert_eq!("5.2", examples_baseline::SBE_SEMANTIC_VERSION);
}
}
43 changes: 43 additions & 0 deletions rust/tests/basic_variable_length_schema_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use sbe_tests::{
message_header_codec::MessageHeaderDecoder,
test_message_1_codec::{
TestMessage1Decoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_TEMPLATE_ID,
},
};
use sbe_tests::{
WriteBuf,
{message_header_codec, test_message_1_codec::TestMessage1Encoder, Encoder, ReadBuf},
};

#[test]
fn should_limit_var_data_length() {
// encode...
let mut buffer = vec![0u8; 1024];
let mut encoder = TestMessage1Encoder::default();
encoder = encoder.wrap(
WriteBuf::new(buffer.as_mut_slice()),
message_header_codec::ENCODED_LENGTH,
);
encoder = encoder.header(0).parent().unwrap();

let password: String = (0..1024).map(|_| 'x' as char).collect();
encoder.encrypted_new_password(password.as_bytes());
assert_eq!(263, encoder.get_limit());

// decode...
let buf = ReadBuf::new(buffer.as_slice());
let header = MessageHeaderDecoder::default().wrap(buf, 0);
assert_eq!(SBE_BLOCK_LENGTH, header.block_length());
assert_eq!(SBE_SCHEMA_VERSION, header.version());
assert_eq!(SBE_TEMPLATE_ID, header.template_id());
assert_eq!(SBE_SCHEMA_ID, header.schema_id());

let mut decoder = TestMessage1Decoder::default().header(header, 0);
let coord = decoder.encrypted_new_password_decoder();
let password = String::from_utf8_lossy(decoder.encrypted_new_password_slice(coord));
assert_eq!(254, password.len());
password
.as_bytes()
.iter()
.for_each(|x| assert_eq!(b'x', *x));
}
6 changes: 2 additions & 4 deletions rust/tests/big_endian_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use baseline_bigendian::car_codec::{CarDecoder, CarEncoder, SBE_TEMPLATE_ID};
use baseline_bigendian::{
boolean_type::BooleanType,
boost_type::BoostType,
car_codec::{
encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder},
*,
},
car_codec::encoder::{AccelerationEncoder, FuelFiguresEncoder, PerformanceFiguresEncoder},
message_header_codec,
message_header_codec::MessageHeaderDecoder,
model::Model,
Expand Down
12 changes: 8 additions & 4 deletions rust/tests/fixed_sized_primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,28 +337,32 @@ fn test_encode_then_decode_non_u8_signed_primitive_slice() {
DemoDecoder::fixed_16_i8,
DemoEncoder::fixed_16_i8_end,
DemoDecoder::fixed_16_i8_end,
i8, i8::from_le_bytes([uninit])
i8,
i8::from_le_bytes([uninit])
);
run_encode_then_decode_for_array_of_signed_len_16!(
DemoEncoder::fixed_16_i16_from_iter,
DemoDecoder::fixed_16_i16,
DemoEncoder::fixed_16_i16_end,
DemoDecoder::fixed_16_i16_end,
i16, i16::from_le_bytes([uninit, uninit])
i16,
i16::from_le_bytes([uninit, uninit])
);
run_encode_then_decode_for_array_of_signed_len_16!(
DemoEncoder::fixed_16_i32_from_iter,
DemoDecoder::fixed_16_i32,
DemoEncoder::fixed_16_i32_end,
DemoDecoder::fixed_16_i32_end,
i32, i32::from_le_bytes([uninit, uninit, uninit, uninit])
i32,
i32::from_le_bytes([uninit, uninit, uninit, uninit])
);
run_encode_then_decode_for_array_of_signed_len_16!(
DemoEncoder::fixed_16_i64_from_iter,
DemoDecoder::fixed_16_i64,
DemoEncoder::fixed_16_i64_end,
DemoDecoder::fixed_16_i64_end,
i64, i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit])
i64,
i64::from_le_bytes([uninit, uninit, uninit, uninit, uninit, uninit, uninit, uninit])
);
}

Expand Down
2 changes: 1 addition & 1 deletion rust/tests/issue_1066_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use issue_1066::{
issue_1066_codec,
issue_1066_codec::{Issue1066Decoder, Issue1066Encoder},
*,
message_header_codec, ReadBuf, WriteBuf,
};

#[test]
Expand Down
7 changes: 6 additions & 1 deletion rust/tests/issue_987_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use issue_987::{
issue_987_codec::{Issue987Decoder, Issue987Encoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION, SBE_TEMPLATE_ID}, message_header_codec::MessageHeaderDecoder, *
issue_987_codec::{
Issue987Decoder, Issue987Encoder, SBE_BLOCK_LENGTH, SBE_SCHEMA_ID, SBE_SCHEMA_VERSION,
SBE_TEMPLATE_ID,
},
message_header_codec::MessageHeaderDecoder,
*,
};

fn create_encoder(buffer: &mut Vec<u8>, off: usize) -> Issue987Encoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void generate() throws IOException
// create Cargo.toml
try (Writer writer = outputManager.createCargoToml())
{
final String packageName = toLowerSnakeCase(ir.packageName()).replaceAll("[.-]", "_");
final String packageName = toLowerSnakeCase(ir.packageName().replaceAll("[\\s.-]", "_"));
final String namespace;
if (ir.namespaceName() == null || ir.namespaceName().equalsIgnoreCase(packageName))
{
Expand Down Expand Up @@ -356,14 +356,15 @@ static void generateEncoderVarData(
indent(sb, level, "pub fn %s(&mut self, value: %s) {\n", propertyName, varDataType);

indent(sb, level + 1, "let limit = self.get_limit();\n");
indent(sb, level + 1, "let data_length = value.len();\n");
indent(sb, level + 1, "let data_length = value.len().min((%s::MAX - 1) as usize);\n",
rustTypeName(lengthType));
indent(sb, level + 1, "self.set_limit(limit + %d + data_length);\n", lengthType.size());

indent(sb, level + 1,
"self.get_buf_mut().put_%s_at(limit, data_length as %1$s);\n",
rustTypeName(lengthType));

indent(sb, level + 1, "self.get_buf_mut().put_slice_at(limit + %d, value%s);\n",
indent(sb, level + 1, "self.get_buf_mut().put_slice_at(limit + %d, &value[0..data_length]%s);\n",
lengthType.size(),
toBytesFn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public RustOutputManager(final String baseDirName, final String packageName)
Verify.notNull(packageName, "packageName");

String dirName = baseDirName.endsWith("" + separatorChar) ? baseDirName : baseDirName + separatorChar;
dirName += packageName.replaceAll("\\.", "_").toLowerCase() + separatorChar;
dirName += packageName.replaceAll("[\\s.-]", "_").toLowerCase() + separatorChar;
final String libDirName = dirName;
rootDir = new File(libDirName);

Expand Down
45 changes: 24 additions & 21 deletions sbe-tool/src/test/resources/basic-variable-length-schema.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<messageSchema package="SBE tests"
id="4"
semanticVersion="5.2"
description="Unit Test"
byteOrder="littleEndian">
<types>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
<composite name="varDataEncoding" semanticType="Length">
<type name="length" primitiveType="uint8" semanticType="Length"/>
<type name="varData" primitiveType="char" semanticType="data"/>
</composite>
</types>
<message name="TestMessage1" id="1" description="TestMessage">
<data type="varDataEncoding" name="encryptedNewPassword" id="1404"/>
</message>
</messageSchema>
<sbe:messageSchema
xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="SBE tests"
id="4"
version="1"
semanticVersion="5.2"
description="Unit Test"
byteOrder="littleEndian">
<types>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
<composite name="varDataEncoding" semanticType="Length">
<type name="length" primitiveType="uint8" semanticType="Length"/>
<type name="varData" primitiveType="char" semanticType="data"/>
</composite>
</types>
<sbe:message name="TestMessage1" id="1" description="TestMessage">
<data type="varDataEncoding" name="encryptedNewPassword" id="1404"/>
</sbe:message>
</sbe:messageSchema>
Loading
Loading