#![cfg(feature = "float")]
use const_cbor::{Decoder, Encoder, Error};
#[test]
fn encoder_f16_forced_and_out_of_space() {
let mut buf = [0u8; 8];
let mut e = Encoder::new(&mut buf);
e.f16(0.1).unwrap();
let n = e.written();
assert_eq!(n, 3);
assert_eq!(&buf[..n], &[0xf9, 0x7e, 0x00]);
let mut buf2 = [0u8; 8];
let mut e2 = Encoder::new(&mut buf2);
e2.f16(1.5).unwrap();
assert_eq!(&buf2[..3], &[0xf9, 0x3e, 0x00]);
let mut tiny = [0u8; 2];
let mut e2 = Encoder::new(&mut tiny);
assert!(matches!(e2.f32(1.0), Err(Error::OUT_OF_SPACE)));
assert_eq!(e2.written(), 0);
}
#[test]
fn decoder_f32_accepts_all_widths() {
let mut dec = Decoder::new(&[0xf9, 0x3c, 0x00]);
assert_eq!(dec.f32(), Ok(1.0));
let mut dec = Decoder::new(&[0xfa, 0x47, 0xc3, 0x50, 0x00]);
assert_eq!(dec.f32(), Ok(100000.0));
let mut dec = Decoder::new(&[0xfb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18]);
assert_eq!(dec.f32(), Err(Error::TYPE_MISMATCH));
let mut dec = Decoder::new(&[0xf5]);
assert_eq!(dec.f32(), Err(Error::TYPE_MISMATCH));
let mut dec = Decoder::new(&[0x01]);
assert_eq!(dec.f32(), Err(Error::TYPE_MISMATCH));
}
#[test]
fn decoder_f64_accepts_all_widths() {
let mut dec = Decoder::new(&[0xf9, 0xbc, 0x00]);
assert_eq!(dec.f64(), Ok(-1.0));
let mut dec = Decoder::new(&[0xfa, 0x47, 0xc3, 0x50, 0x00]);
assert_eq!(dec.f64(), Ok(100000.0));
let input = [0xfb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18];
let mut dec = Decoder::new(&input);
assert_eq!(dec.f64(), Ok(core::f64::consts::PI));
let mut dec = Decoder::new(&[0xf6]); assert_eq!(dec.f64(), Err(Error::TYPE_MISMATCH));
}
#[test]
fn float_narrowing_boundary_values() {
let cases: [(f64, &[u8]); 5] = [
(1.5, &[0xf9, 0x3e, 0x00]), (65504.0, &[0xf9, 0x7b, 0xff]), (100000.0, &[0xfa, 0x47, 0xc3, 0x50, 0x00]), (0.1, &[0xfb, 0x3f, 0xb9, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a]), (-0.0, &[0xf9, 0x80, 0x00]), ];
for (v, want) in cases {
let mut buf = [0u8; 16];
let mut e = Encoder::new(&mut buf);
e.f64(v).unwrap();
let n = e.written();
assert_eq!(&buf[..n], want, "encoding {v}");
let mut d = Decoder::new(want);
assert_eq!(d.f64(), Ok(v));
}
}
#[test]
fn diagnostic_renders_f64_and_specials() {
use const_cbor::diagnostic::render;
let mut out = [0u8; 64];
let n = render(&[0xfb, 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], &mut out).unwrap();
assert_eq!(n, "3.141592653589793");
let n = render(&[0xfa, 0xff, 0x80, 0x00, 0x00], &mut out).unwrap();
assert_eq!(n, "-Infinity");
let n = render(&[0xf9, 0x7e, 0x00], &mut out).unwrap();
assert_eq!(n, "NaN");
}