|
| 1 | +#![allow(clippy::unusual_byte_groupings)] |
1 | 2 | #![deny(rust_2018_idioms)]
|
2 | 3 | #![doc(
|
3 | 4 | html_logo_url = "https://storage.googleapis.com/fdo-gitlab-uploads/project/avatar/3213/zbus-logomark.png"
|
@@ -210,6 +211,7 @@ pub mod export {
|
210 | 211 | }
|
211 | 212 |
|
212 | 213 | #[cfg(test)]
|
| 214 | +#[allow(clippy::blacklisted_name)] |
213 | 215 | mod tests {
|
214 | 216 | use std::{
|
215 | 217 | collections::HashMap,
|
@@ -309,6 +311,62 @@ mod tests {
|
309 | 311 | }};
|
310 | 312 | }
|
311 | 313 |
|
| 314 | + fn f64_type_test( |
| 315 | + format: EncodingFormat, |
| 316 | + value: f64, |
| 317 | + expected_len: usize, |
| 318 | + expected_value_len: usize, |
| 319 | + ) -> Vec<u8> { |
| 320 | + // Lie that we're starting at byte 1 in the overall message to test padding |
| 321 | + let ctxt = Context::<BE>::new(format, 1); |
| 322 | + let (encoded, fds) = to_bytes_fds(ctxt, &value).unwrap(); |
| 323 | + let padding = crate::padding_for_n_bytes(1, 8); |
| 324 | + assert_eq!( |
| 325 | + encoded.len(), |
| 326 | + expected_len + padding, |
| 327 | + "invalid encoding using `to_bytes`" |
| 328 | + ); |
| 329 | + |
| 330 | + let decoded: f64 = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap(); |
| 331 | + assert!( |
| 332 | + (decoded - value).abs() < f64::EPSILON, |
| 333 | + "invalid decoding using `from_slice`" |
| 334 | + ); |
| 335 | + |
| 336 | + // Now encode w/o padding |
| 337 | + let ctxt = Context::<BE>::new(format, 0); |
| 338 | + let (encoded, _) = to_bytes_fds(ctxt, &value).unwrap(); |
| 339 | + assert_eq!( |
| 340 | + encoded.len(), |
| 341 | + expected_len, |
| 342 | + "invalid encoding using `to_bytes`" |
| 343 | + ); |
| 344 | + |
| 345 | + f64_type_test_as_value(format, value, expected_value_len); |
| 346 | + encoded |
| 347 | + } |
| 348 | + |
| 349 | + fn f64_type_test_as_value(format: EncodingFormat, value: f64, expected_value_len: usize) { |
| 350 | + let v: Value<'_> = value.into(); |
| 351 | + assert_eq!(v.value_signature(), f64::SIGNATURE_STR); |
| 352 | + assert_eq!(v, Value::F64(value)); |
| 353 | + f64_value_test(format, v.clone(), expected_value_len); |
| 354 | + let v: f64 = v.try_into().unwrap(); |
| 355 | + assert!((v - value).abs() < f64::EPSILON); |
| 356 | + } |
| 357 | + |
| 358 | + fn f64_value_test(format: EncodingFormat, v: Value<'_>, expected_value_len: usize) { |
| 359 | + let ctxt = Context::<LE>::new(format, 0); |
| 360 | + let (encoded, fds) = to_bytes_fds(ctxt, &v).unwrap(); |
| 361 | + assert_eq!( |
| 362 | + encoded.len(), |
| 363 | + expected_value_len, |
| 364 | + "invalid encoding using `to_bytes`" |
| 365 | + ); |
| 366 | + let decoded: Value<'_> = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap(); |
| 367 | + assert!(decoded == v, "invalid decoding using `from_slice`"); |
| 368 | + } |
| 369 | + |
312 | 370 | #[cfg(feature = "gvariant")]
|
313 | 371 | fn decode_with_gvariant<B, T>(encoded: B) -> T
|
314 | 372 | where
|
@@ -411,15 +469,15 @@ mod tests {
|
411 | 469 |
|
412 | 470 | #[test]
|
413 | 471 | fn f64_value() {
|
414 |
| - let encoded = basic_type_test!(BE, DBus, 99999.99999_f64, 8, f64, 8, F64, 16); |
415 |
| - assert_eq!(LE::read_f64(&encoded), -5759340900185448e-143); |
| 472 | + let encoded = f64_type_test(EncodingFormat::DBus, 99999.99999_f64, 8, 16); |
| 473 | + assert!((LE::read_f64(&encoded) - -5.759340900185448e-128).abs() < f64::EPSILON); |
416 | 474 | #[cfg(feature = "gvariant")]
|
417 | 475 | {
|
418 |
| - assert_eq!( |
419 |
| - decode_with_gvariant::<_, f64>(encoded), |
420 |
| - -5759340900185448e-143 |
| 476 | + assert!( |
| 477 | + (decode_with_gvariant::<_, f64>(encoded) - -5.759340900185448e-128).abs() |
| 478 | + < f64::EPSILON |
421 | 479 | );
|
422 |
| - basic_type_test!(BE, GVariant, 99999.99999_f64, 8, f64, 8, F64, 10); |
| 480 | + f64_type_test(EncodingFormat::GVariant, 99999.99999_f64, 8, 10); |
423 | 481 | }
|
424 | 482 | }
|
425 | 483 |
|
@@ -456,8 +514,6 @@ mod tests {
|
456 | 514 | assert_eq!(variant.get_str().unwrap(), "hello world");
|
457 | 515 | }
|
458 | 516 |
|
459 |
| - let v: String = v.try_into().unwrap(); |
460 |
| - assert_eq!(v, "hello world"); |
461 | 517 | let v: String = v.try_into().unwrap();
|
462 | 518 | assert_eq!(v, "hello world");
|
463 | 519 |
|
@@ -569,8 +625,8 @@ mod tests {
|
569 | 625 | let ctxt = Context::<BE>::new_dbus(0);
|
570 | 626 | let (encoded, fds) = to_bytes_fds(ctxt, &()).unwrap();
|
571 | 627 | assert_eq!(encoded.len(), 0, "invalid encoding using `to_bytes`");
|
572 |
| - let decoded: () = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap(); |
573 |
| - assert_eq!(decoded, (), "invalid decoding using `from_slice`"); |
| 628 | + let _decoded: () = from_slice_fds(&encoded, Some(&fds), ctxt) |
| 629 | + .expect("invalid decoding using `from_slice`"); |
574 | 630 | }
|
575 | 631 |
|
576 | 632 | #[test]
|
|
0 commit comments