fidl_fuchsia_pkg_ext/
measure.rsuse fidl_fuchsia_pkg as fidl;
pub trait Measurable {
fn measure(&self) -> usize;
}
impl Measurable for fidl::BlobId {
fn measure(&self) -> usize {
measure_fuchsia_pkg::Measurable::measure(self).num_bytes
}
}
impl Measurable for fidl::BlobInfo {
fn measure(&self) -> usize {
measure_fuchsia_pkg::Measurable::measure(self).num_bytes
}
}
impl Measurable for fidl::PackageIndexEntry {
fn measure(&self) -> usize {
measure_fuchsia_pkg::Measurable::measure(self).num_bytes
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
fn truncate_str(s: &str, max_len: usize) -> &str {
if s.len() <= max_len {
return s;
}
let mut index = max_len;
while index > 0 && !s.is_char_boundary(index) {
index -= 1;
}
&s[..index]
}
prop_compose! {
fn arb_package_index_entry()(
url in ".{0,2048}",
blob_id: crate::BlobId,
) -> fidl::PackageIndexEntry {
let url = truncate_str(&url, 2048).to_owned();
fidl::PackageIndexEntry {
package_url: fidl::PackageUrl { url, },
meta_far_blob_id: blob_id.into(),
}
}
}
proptest! {
#![proptest_config(ProptestConfig{
failure_persistence: None,
.. ProptestConfig::default()
})]
#[test]
fn blob_id_size_is_as_bytes_size(item: crate::BlobId) {
let item: fidl::BlobId = item.into();
let expected = std::mem::size_of_val(&item);
let actual = item.measure();
prop_assert_eq!(expected, actual);
}
#[test]
fn blob_info_size_is_as_bytes_size(item: crate::BlobInfo) {
let item: fidl::BlobInfo = item.into();
let expected = std::mem::size_of_val(&item);
let actual = item.measure();
prop_assert_eq!(expected, actual);
}
#[test]
fn package_index_entry_size_is_fidl_encoded_size(
item in arb_package_index_entry()
) {
let actual = item.measure();
let (bytes, _) =
::fidl::standalone_encode_value(&item).unwrap();
let expected = bytes.len();
prop_assert_eq!(expected, actual);
}
}
}