| 1 |
#![cfg(feature = "unstable-test")] |
| 2 |
|
| 3 |
// NOTE these tests should live in `defmt-macros` but the expansion of the macros defined there |
| 4 |
// depend on `defmt` and `defmt` depends on `defmt-macros` -- the circular dependency may get in |
| 5 |
// the way of `cargo test` |
| 6 |
|
| 7 |
// NOTE string interning is mocked when testing so that it does not do real interning. Instead |
| 8 |
// the "interner" always returns a **7-bit** `u8` value that's bumped on every interning operation. |
| 9 |
// |
| 10 |
// In practice, this means that the following operation: |
| 11 |
// ``` |
| 12 |
// fn foo(f: &mut Formatter) { |
| 13 |
// write!(f, "Hello") |
| 14 |
// } |
| 15 |
// ``` |
| 16 |
// writes a *different* index on each call. With real interning this operation always writes the |
| 17 |
// same index. |
| 18 |
// |
| 19 |
// `fetch_string_index` returns the current index of the mocked interner. Use this |
| 20 |
// when writing the expected output of a unit test. |
| 21 |
// |
| 22 |
// ``` |
| 23 |
// let mut f = Internalexport::make_formatter(); |
| 24 |
// let index = defmt::export::fetch_string_index(); |
| 25 |
// foo(&mut f); // NOTE increases the interner index |
| 26 |
// assert_eq!(fetch_bytes(), [index]); |
| 27 |
// |
| 28 |
// let mut f = Internalexport::make_formatter(); |
| 29 |
// foo(&mut f); |
| 30 |
// assert_eq!(fetch_bytes(), [index.wrapping_add(1)]); |
| 31 |
// ^^^^^^^^^^^^^^^ account for the previous `foo` call |
| 32 |
// ``` |
| 33 |
// |
| 34 |
// The mocked string index is thread local so you can run unit tests in parallel. |
| 35 |
// `fetch_string_index` returns the thread-local interner index. |
| 36 |
// |
| 37 |
// Additional notes: |
| 38 |
// |
| 39 |
// - the mocked index is 7 bits so its LEB128 encoding is the input byte |
| 40 |
|
| 41 |
use defmt::{export::fetch_string_index, write, Debug2Format, Display2Format, Format, Formatter}; |
| 42 |
|
| 43 |
// Increase the 7-bit mocked interned index |
| 44 |
fn inc(index: u16, n: u16) -> u16 { |
| 45 |
index.wrapping_add(n) |
| 46 |
} |
| 47 |
|
| 48 |
fn write_format<T: Format + ?Sized>(val: &T) { |
| 49 |
defmt::export::istr(&T::_format_tag()); |
| 50 |
val._format_data(); |
| 51 |
} |
| 52 |
|
| 53 |
macro_rules! check { |
| 54 |
([$($x:expr),* $(,)?]) => { |
| 55 |
{ |
| 56 |
let mut v = Vec::<u8>::new(); |
| 57 |
$( |
| 58 |
v.extend(&($x).to_le_bytes()); |
| 59 |
)* |
| 60 |
assert_eq!(defmt::export::fetch_bytes(), v); |
| 61 |
} |
| 62 |
}; |
| 63 |
} |
| 64 |
|
| 65 |
macro_rules! check_format { |
| 66 |
($format:expr, [$($x:expr),* $(,)?] $(,)?) => { |
| 67 |
{ |
| 68 |
let mut v = Vec::<u8>::new(); |
| 69 |
$( |
| 70 |
v.extend(&($x).to_le_bytes()); |
| 71 |
)* |
| 72 |
write_format($format); |
| 73 |
assert_eq!(defmt::export::fetch_bytes(), v); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn write() { |
| 80 |
let index = fetch_string_index(); |
| 81 |
let g = defmt::export::make_formatter(); |
| 82 |
write!(g, "The answer is {=u8}", 42); |
| 83 |
check!([ |
| 84 |
index, // "The answer is {=u8}", |
| 85 |
42u8, // u8 value |
| 86 |
]); |
| 87 |
|
| 88 |
let g = defmt::export::make_formatter(); |
| 89 |
write!(g, "The answer is {=?}", 42u8); |
| 90 |
check!([ |
| 91 |
inc(index, 1), // "The answer is {=?}" |
| 92 |
inc(index, 2), // "{=u8}" / impl Format for u8 |
| 93 |
42u8, // u8 value |
| 94 |
]); |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn bitfields_mixed() { |
| 99 |
let index = fetch_string_index(); |
| 100 |
let g = defmt::export::make_formatter(); |
| 101 |
|
| 102 |
write!( |
| 103 |
g, |
| 104 |
"bitfields {0=7..12}, {1=0..5}", |
| 105 |
0b1110_0101_1111_0000u16, 0b1111_0000u8 |
| 106 |
); |
| 107 |
check!([ |
| 108 |
index, // bitfields {0=7..12}, {1=0..5}", |
| 109 |
0b1111_0000u8, |
| 110 |
0b1110_0101u8, // u16 |
| 111 |
0b1111_0000u8, // u8 |
| 112 |
]); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn bitfields_across_octets() { |
| 117 |
let index = fetch_string_index(); |
| 118 |
let g = defmt::export::make_formatter(); |
| 119 |
|
| 120 |
write!(g, "bitfields {0=0..7} {0=9..14}", 0b0110_0011_1101_0010u16); |
| 121 |
check!([ |
| 122 |
index, // bitfields {0=0..7} {0=9..14}", |
| 123 |
0b1101_0010u8, |
| 124 |
0b0110_0011u8, // u16 |
| 125 |
]); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn bitfields_truncate_lower() { |
| 130 |
let index = fetch_string_index(); |
| 131 |
let g = defmt::export::make_formatter(); |
| 132 |
|
| 133 |
write!( |
| 134 |
g, |
| 135 |
"bitfields {0=9..14}", |
| 136 |
0b0000_0000_0000_1111_0110_0011_1101_0010u32 |
| 137 |
); |
| 138 |
check!([ |
| 139 |
index, // bitfields {0=9..14}", |
| 140 |
0b0110_0011u8, // the first octet should have been truncated away |
| 141 |
]); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn bitfields_assert_range_exclusive() { |
| 146 |
let index = fetch_string_index(); |
| 147 |
let g = defmt::export::make_formatter(); |
| 148 |
|
| 149 |
write!(g, "bitfields {0=6..8}", 0b1010_0101u8,); |
| 150 |
check!([ |
| 151 |
index, // "bitfields {0=6..8}" |
| 152 |
0b1010_0101u8 |
| 153 |
]); |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn debug_attr_struct() { |
| 158 |
#[derive(Debug)] |
| 159 |
struct DebugOnly { |
| 160 |
_a: i32, |
| 161 |
} |
| 162 |
|
| 163 |
#[derive(Format)] |
| 164 |
struct X { |
| 165 |
y: bool, |
| 166 |
#[defmt(Debug2Format)] |
| 167 |
d: DebugOnly, |
| 168 |
} |
| 169 |
|
| 170 |
let index = fetch_string_index(); |
| 171 |
check_format!( |
| 172 |
&X { |
| 173 |
y: false, |
| 174 |
d: DebugOnly { _a: 3 } |
| 175 |
}, |
| 176 |
[ |
| 177 |
index, // "X {{ y: {=bool}, d: {=?} }}" |
| 178 |
0b0u8, // y |
| 179 |
inc(index, 1), // DebugOnly's format string |
| 180 |
b'D', // Text of the Debug output |
| 181 |
b'e', |
| 182 |
b'b', |
| 183 |
b'u', |
| 184 |
b'g', |
| 185 |
b'O', |
| 186 |
b'n', |
| 187 |
b'l', |
| 188 |
b'y', |
| 189 |
b' ', |
| 190 |
b'{', |
| 191 |
b' ', |
| 192 |
b'_', |
| 193 |
b'a', |
| 194 |
b':', |
| 195 |
b' ', |
| 196 |
b'3', |
| 197 |
b' ', |
| 198 |
b'}', |
| 199 |
0xffu8 |
| 200 |
], |
| 201 |
) |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn display_attr_enum() { |
| 206 |
use std::fmt; |
| 207 |
struct DisplayOnly {} |
| 208 |
|
| 209 |
impl fmt::Display for DisplayOnly { |
| 210 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 211 |
f.write_str("Display") |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[derive(Format)] |
| 216 |
enum X { |
| 217 |
#[allow(dead_code)] |
| 218 |
Bool(bool), |
| 219 |
Display(#[defmt(Display2Format)] DisplayOnly), |
| 220 |
} |
| 221 |
|
| 222 |
let index = fetch_string_index(); |
| 223 |
check_format!( |
| 224 |
&X::Display(DisplayOnly {}), |
| 225 |
[ |
| 226 |
index, // "Bool({=bool})|Display({=?})" |
| 227 |
0b1u8, // Variant: Display |
| 228 |
inc(index, 1), // DisplayOnly's format string |
| 229 |
b'D', // Text of the Display output |
| 230 |
b'i', |
| 231 |
b's', |
| 232 |
b'p', |
| 233 |
b'l', |
| 234 |
b'a', |
| 235 |
b'y', |
| 236 |
0xffu8 |
| 237 |
], |
| 238 |
) |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn boolean_struct() { |
| 243 |
#[derive(Format)] |
| 244 |
struct X { |
| 245 |
y: bool, |
| 246 |
z: bool, |
| 247 |
} |
| 248 |
|
| 249 |
let index = fetch_string_index(); |
| 250 |
check_format!( |
| 251 |
&X { y: false, z: true }, |
| 252 |
[ |
| 253 |
index, // "X {{ y: {=bool}, z: {=bool} }}" |
| 254 |
0b0u8, // y |
| 255 |
0b1u8, // z |
| 256 |
], |
| 257 |
) |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn single_struct() { |
| 262 |
#[derive(Format)] |
| 263 |
struct X { |
| 264 |
y: u8, |
| 265 |
z: u16, |
| 266 |
} |
| 267 |
|
| 268 |
let index = fetch_string_index(); |
| 269 |
check_format!( |
| 270 |
&X { y: 1, z: 2 }, |
| 271 |
[ |
| 272 |
index, // "X {{ y: {=u8}, z: {=u16} }}" |
| 273 |
1u8, // x |
| 274 |
2u8, // y.low |
| 275 |
0u8, // y.high |
| 276 |
], |
| 277 |
) |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn single_struct_manual() { |
| 282 |
// Above `#[derive]`d impl should be equivalent to this: |
| 283 |
struct X { |
| 284 |
y: u8, |
| 285 |
z: u16, |
| 286 |
} |
| 287 |
|
| 288 |
impl Format for X { |
| 289 |
fn format(&self, f: Formatter) { |
| 290 |
defmt::write!(f, "X {{ y: {=u8}, z: {=u16} }}", self.y, self.z) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
let index = fetch_string_index(); |
| 295 |
check_format!( |
| 296 |
&X { y: 1, z: 2 }, |
| 297 |
[ |
| 298 |
index, // "{=__internal_FormatSequence}" |
| 299 |
inc(index, 1), // "X {{ y: {=u8}, z: {=u16} }}" |
| 300 |
1u8, // y |
| 301 |
2u16, // z |
| 302 |
0u16, // terminator |
| 303 |
], |
| 304 |
) |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn single_struct_manual_multiwrite() { |
| 309 |
// Above `#[derive]`d impl should be equivalent to this: |
| 310 |
struct X { |
| 311 |
y: u8, |
| 312 |
z: u16, |
| 313 |
} |
| 314 |
|
| 315 |
impl Format for X { |
| 316 |
fn format(&self, f: Formatter) { |
| 317 |
defmt::write!(f, "y={=u8}", self.y); |
| 318 |
defmt::write!(f, "z={=u16}", self.z); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
let index = fetch_string_index(); |
| 323 |
check_format!( |
| 324 |
&X { y: 1, z: 2 }, |
| 325 |
[ |
| 326 |
index, // "{=__internal_FormatSequence}" |
| 327 |
inc(index, 1), // "y={=u8}" |
| 328 |
1u8, // y |
| 329 |
inc(index, 2), // "z={=u16}" |
| 330 |
2u16, // z |
| 331 |
0u16, // terminator |
| 332 |
], |
| 333 |
) |
| 334 |
} |
| 335 |
|
| 336 |
#[test] |
| 337 |
fn slice_struct_manual_multiwrite() { |
| 338 |
// Above `#[derive]`d impl should be equivalent to this: |
| 339 |
struct X { |
| 340 |
y: u8, |
| 341 |
z: u16, |
| 342 |
} |
| 343 |
|
| 344 |
impl Format for X { |
| 345 |
fn format(&self, f: Formatter) { |
| 346 |
defmt::write!(f, "y={=u8}", self.y); |
| 347 |
defmt::write!(f, "z={=u16}", self.z); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
let index = fetch_string_index(); |
| 352 |
check_format!( |
| 353 |
&[X { y: 1, z: 2 }, X { y: 3, z: 4 }][..], |
| 354 |
[ |
| 355 |
index, // "{=[?]}" |
| 356 |
2u32, // len |
| 357 |
inc(index, 1), // "{=__internal_FormatSequence}" |
| 358 |
// first element |
| 359 |
inc(index, 2), // "y={=u8}" |
| 360 |
1u8, // y |
| 361 |
inc(index, 3), // "z={=u16}" |
| 362 |
2u16, // z |
| 363 |
0u16, // terminator |
| 364 |
// second element |
| 365 |
inc(index, 4), // "y={=u8}" |
| 366 |
3u8, // y |
| 367 |
inc(index, 5), // "z={=u16}" |
| 368 |
4u16, // z |
| 369 |
0u16, // terminator |
| 370 |
], |
| 371 |
) |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn nested_struct() { |
| 376 |
#[derive(Format)] |
| 377 |
struct X { |
| 378 |
y: Y, |
| 379 |
} |
| 380 |
|
| 381 |
#[derive(Format)] |
| 382 |
struct Y { |
| 383 |
z: u8, |
| 384 |
} |
| 385 |
|
| 386 |
let val = 42u8; |
| 387 |
let index = fetch_string_index(); |
| 388 |
check_format!( |
| 389 |
&X { y: Y { z: val } }, |
| 390 |
[ |
| 391 |
index, // "X {{ y: {=?} }}" |
| 392 |
inc(index, 1), // "Y {{ z: {=u8} }}" |
| 393 |
val, |
| 394 |
], |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
#[test] |
| 399 |
fn tuple_struct() { |
| 400 |
#[derive(Format)] |
| 401 |
struct Struct(u8, u16); |
| 402 |
|
| 403 |
let index = fetch_string_index(); |
| 404 |
check_format!( |
| 405 |
&Struct(0x1f, 0xaaaa), |
| 406 |
[ |
| 407 |
index, // "Struct({=u8}, {=u16})" |
| 408 |
0x1fu8, // u8 |
| 409 |
0xaaaau16, // u16 |
| 410 |
], |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn c_like_enum() { |
| 416 |
#[derive(Format)] |
| 417 |
#[allow(dead_code)] |
| 418 |
enum Enum { |
| 419 |
A, |
| 420 |
B, |
| 421 |
C, |
| 422 |
} |
| 423 |
|
| 424 |
let index = fetch_string_index(); |
| 425 |
check_format!( |
| 426 |
&Enum::A, |
| 427 |
[ |
| 428 |
index, // |
| 429 |
0u8, // `Enum::A` |
| 430 |
], |
| 431 |
); |
| 432 |
let index = fetch_string_index(); |
| 433 |
check_format!( |
| 434 |
&Enum::B, |
| 435 |
[ |
| 436 |
index, // |
| 437 |
1u8, // `Enum::B` |
| 438 |
], |
| 439 |
); |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
#[allow(dead_code)] |
| 444 |
fn uninhabited_enum() { |
| 445 |
#[derive(Format)] |
| 446 |
enum Void {} |
| 447 |
} |
| 448 |
|
| 449 |
/// Tests that univariant enums do not encode the discriminant (since the variant in use is always |
| 450 |
/// the same). |
| 451 |
#[test] |
| 452 |
fn univariant_enum() { |
| 453 |
#[derive(Format)] |
| 454 |
enum NoData { |
| 455 |
Variant, |
| 456 |
} |
| 457 |
|
| 458 |
let index = fetch_string_index(); |
| 459 |
check_format!( |
| 460 |
&NoData::Variant, |
| 461 |
[ |
| 462 |
index, // |
| 463 |
], |
| 464 |
); |
| 465 |
|
| 466 |
#[derive(Format)] |
| 467 |
enum Data { |
| 468 |
Variant(u8, u16), |
| 469 |
} |
| 470 |
|
| 471 |
let index = fetch_string_index(); |
| 472 |
check_format!( |
| 473 |
&Data::Variant(0x1f, 0xaaaa), |
| 474 |
[ |
| 475 |
index, // |
| 476 |
0x1fu8, // u8 |
| 477 |
0xaaaau16, // u16 |
| 478 |
], |
| 479 |
); |
| 480 |
} |
| 481 |
|
| 482 |
#[test] |
| 483 |
fn nested_enum() { |
| 484 |
#[derive(Format)] |
| 485 |
#[allow(dead_code)] |
| 486 |
enum CLike { |
| 487 |
A, |
| 488 |
B, |
| 489 |
C, |
| 490 |
} |
| 491 |
|
| 492 |
#[derive(Format)] |
| 493 |
enum Inner { |
| 494 |
A(CLike, u8), |
| 495 |
_B, |
| 496 |
} |
| 497 |
|
| 498 |
#[derive(Format)] |
| 499 |
enum Outer { |
| 500 |
Variant1 { pre: u8, inner: Inner, post: u8 }, |
| 501 |
Variant2, |
| 502 |
Variant3(Inner), |
| 503 |
} |
| 504 |
|
| 505 |
let index = fetch_string_index(); |
| 506 |
check_format!( |
| 507 |
&Outer::Variant1 { |
| 508 |
pre: 0xEE, |
| 509 |
inner: Inner::A(CLike::B, 0x07), |
| 510 |
post: 0xAB, |
| 511 |
}, |
| 512 |
[ |
| 513 |
index, // |
| 514 |
0u8, // `Outer::Variant1` |
| 515 |
0xEEu8, // u8 pre |
| 516 |
inc(index, 1), // `Inner`'s formatting string |
| 517 |
0u8, // `Inner::A` |
| 518 |
inc(index, 2), // `CLike`'s formatting string |
| 519 |
1u8, // `CLike::B` |
| 520 |
0x07u8, // u8 |
| 521 |
0xABu8, // u8 post |
| 522 |
], |
| 523 |
); |
| 524 |
|
| 525 |
let index = fetch_string_index(); |
| 526 |
check_format!( |
| 527 |
&Outer::Variant2, |
| 528 |
[ |
| 529 |
index, // |
| 530 |
1u8, // `Outer::Variant2` |
| 531 |
], |
| 532 |
); |
| 533 |
|
| 534 |
let index = fetch_string_index(); |
| 535 |
check_format!( |
| 536 |
&Outer::Variant3(Inner::A(CLike::B, 0x07)), |
| 537 |
[ |
| 538 |
index, // |
| 539 |
2u8, // `Outer::Variant3` |
| 540 |
inc(index, 1), // `Inner`'s formatting string |
| 541 |
0u8, // `Inner::A` |
| 542 |
inc(index, 2), // `CLike`'s formatting string |
| 543 |
1u8, // `CLike::B` |
| 544 |
0x07u8, // u8 |
| 545 |
], |
| 546 |
); |
| 547 |
} |
| 548 |
|
| 549 |
#[test] |
| 550 |
fn slice() { |
| 551 |
let index = fetch_string_index(); |
| 552 |
let val: &[u8] = &[23u8, 42u8]; |
| 553 |
check_format!( |
| 554 |
val, |
| 555 |
[ |
| 556 |
index, // "{=[?]}" |
| 557 |
val.len() as u32, // length |
| 558 |
inc(index, 1), // "{=u8}" |
| 559 |
23u8, // val[0] |
| 560 |
42u8, // val[1] |
| 561 |
], |
| 562 |
) |
| 563 |
} |
| 564 |
|
| 565 |
#[test] |
| 566 |
fn slice_of_usize() { |
| 567 |
let index = fetch_string_index(); |
| 568 |
let val: &[usize] = &[23usize, 42]; |
| 569 |
check_format!( |
| 570 |
val, |
| 571 |
[ |
| 572 |
index, // "{=[?]}" |
| 573 |
val.len() as u32, // length |
| 574 |
inc(index, 1), // "{=usize}" |
| 575 |
23u32, // val[0] |
| 576 |
42u32, // val[1] |
| 577 |
], |
| 578 |
) |
| 579 |
} |
| 580 |
|
| 581 |
#[test] |
| 582 |
fn slice_of_bools() { |
| 583 |
let index = fetch_string_index(); |
| 584 |
let val: &[bool] = &[true, true, false]; |
| 585 |
check_format!( |
| 586 |
val, |
| 587 |
[ |
| 588 |
index, // "{=[?]}" |
| 589 |
val.len() as u32, // length |
| 590 |
inc(index, 1), // "{=bool}" |
| 591 |
0b1u8, |
| 592 |
0b1u8, |
| 593 |
0b0u8, |
| 594 |
], |
| 595 |
) |
| 596 |
} |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn format_primitives() { |
| 600 |
let index = fetch_string_index(); |
| 601 |
check_format!( |
| 602 |
&42u8, |
| 603 |
[ |
| 604 |
index, // "{=u8}" |
| 605 |
42u8, |
| 606 |
], |
| 607 |
); |
| 608 |
check_format!( |
| 609 |
&42u16, |
| 610 |
[ |
| 611 |
inc(index, 1), // "{=u16}" |
| 612 |
42u16, |
| 613 |
], |
| 614 |
); |
| 615 |
check_format!( |
| 616 |
&513u16, |
| 617 |
[ |
| 618 |
inc(index, 2), // "{=u16}" |
| 619 |
513u16, |
| 620 |
], |
| 621 |
); |
| 622 |
|
| 623 |
check_format!( |
| 624 |
&42u32, |
| 625 |
[ |
| 626 |
inc(index, 3), // "{=u32}" |
| 627 |
42u32, |
| 628 |
], |
| 629 |
); |
| 630 |
check_format!( |
| 631 |
&513u32, |
| 632 |
[ |
| 633 |
inc(index, 4), // "{=u32}" |
| 634 |
513u32, |
| 635 |
], |
| 636 |
); |
| 637 |
|
| 638 |
check_format!( |
| 639 |
&5.13f32, |
| 640 |
[ |
| 641 |
inc(index, 5), // "{=f32}" |
| 642 |
246u8, |
| 643 |
40u8, |
| 644 |
164u8, |
| 645 |
64u8, |
| 646 |
], |
| 647 |
); |
| 648 |
|
| 649 |
check_format!( |
| 650 |
&42i8, |
| 651 |
[ |
| 652 |
inc(index, 6), // "{=i8}" |
| 653 |
42u8, |
| 654 |
], |
| 655 |
); |
| 656 |
check_format!( |
| 657 |
&-42i8, |
| 658 |
[ |
| 659 |
inc(index, 7), // "{=i8}" |
| 660 |
-42i8 as u8, |
| 661 |
], |
| 662 |
); |
| 663 |
|
| 664 |
check_format!( |
| 665 |
&None::<u8>, |
| 666 |
[ |
| 667 |
inc(index, 8), // "<option-format-string>" |
| 668 |
0u8, // None discriminant |
| 669 |
], |
| 670 |
); |
| 671 |
|
| 672 |
check_format!( |
| 673 |
&Some(42u8), |
| 674 |
[ |
| 675 |
inc(index, 9), // "<option-format-string>" |
| 676 |
1u8, // Some discriminant |
| 677 |
inc(index, 10), // "{=u8}" |
| 678 |
42u8, // Some.0 field |
| 679 |
], |
| 680 |
); |
| 681 |
|
| 682 |
check_format!(&-1isize, [inc(index, 11), -1i32]); |
| 683 |
check_format!(&-128isize, [inc(index, 12), -128i32]); |
| 684 |
|
| 685 |
check_format!( |
| 686 |
&true, |
| 687 |
[ |
| 688 |
inc(index, 13), // "{=bool}" |
| 689 |
0b1u8, |
| 690 |
], |
| 691 |
); |
| 692 |
|
| 693 |
check_format!( |
| 694 |
&513u64, |
| 695 |
[ |
| 696 |
inc(index, 14), // "{=u64}" |
| 697 |
513u64, |
| 698 |
], |
| 699 |
); |
| 700 |
|
| 701 |
check_format!( |
| 702 |
&-2i64, |
| 703 |
[ |
| 704 |
inc(index, 15), // "{=i64}" |
| 705 |
-2i64, |
| 706 |
], |
| 707 |
); |
| 708 |
|
| 709 |
check_format!( |
| 710 |
&'a', |
| 711 |
[ |
| 712 |
inc(index, 16), // "{=char}" |
| 713 |
0x61u8, |
| 714 |
0x00u8, |
| 715 |
0x00u8, |
| 716 |
0x00u8, |
| 717 |
], |
| 718 |
); |
| 719 |
} |
| 720 |
|
| 721 |
#[test] |
| 722 |
fn istr() { |
| 723 |
let index = fetch_string_index(); |
| 724 |
let interned = defmt::intern!("interned string contents"); |
| 725 |
check_format!( |
| 726 |
&interned, |
| 727 |
[ |
| 728 |
inc(index, 1), // "{=istr}" |
| 729 |
index, |
| 730 |
], |
| 731 |
); |
| 732 |
} |
| 733 |
|
| 734 |
#[test] |
| 735 |
fn format_arrays() { |
| 736 |
let index = fetch_string_index(); |
| 737 |
let array: [u16; 0] = []; |
| 738 |
check_format!( |
| 739 |
&array, |
| 740 |
[ |
| 741 |
index, // "{=[?;0]}" |
| 742 |
inc(index, 1), // "{=u16}" |
| 743 |
] |
| 744 |
); |
| 745 |
|
| 746 |
let index = fetch_string_index(); |
| 747 |
let array: [u16; 3] = [1, 256, 257]; |
| 748 |
check_format!( |
| 749 |
&array, |
| 750 |
[ |
| 751 |
index, // "{=[?;3]}" |
| 752 |
inc(index, 1), // "{=u16}" |
| 753 |
1u16, // [0] |
| 754 |
256u16, // [1] |
| 755 |
257u16, // [2] |
| 756 |
], |
| 757 |
); |
| 758 |
} |
| 759 |
|
| 760 |
#[test] |
| 761 |
fn format_slice_of_primitives() { |
| 762 |
let index = fetch_string_index(); |
| 763 |
let slice: &[u16] = &[1, 256, 257]; |
| 764 |
check_format!( |
| 765 |
slice, |
| 766 |
[ |
| 767 |
index, // "{=[?]}" |
| 768 |
slice.len() as u32, // |
| 769 |
inc(index, 1), // "{=u16}" |
| 770 |
1u16, // [0] |
| 771 |
256u16, // [1] |
| 772 |
257u16, // [2] |
| 773 |
], |
| 774 |
); |
| 775 |
} |
| 776 |
|
| 777 |
#[test] |
| 778 |
fn format_slice_of_structs() { |
| 779 |
#[derive(Format)] |
| 780 |
struct X { |
| 781 |
y: Y, |
| 782 |
} |
| 783 |
|
| 784 |
#[derive(Format)] |
| 785 |
struct Y { |
| 786 |
z: u8, |
| 787 |
} |
| 788 |
|
| 789 |
let index = fetch_string_index(); |
| 790 |
let slice: &[_] = &[X { y: Y { z: 42 } }, X { y: Y { z: 24 } }]; |
| 791 |
check_format!( |
| 792 |
slice, |
| 793 |
[ |
| 794 |
index, // "{=[?]}" |
| 795 |
slice.len() as u32, // |
| 796 |
// first element |
| 797 |
inc(index, 1), // "X {{ y: {=?} }}" |
| 798 |
inc(index, 2), // "Y {{ z: {=u8} }}" |
| 799 |
42u8, // [0].y.z |
| 800 |
// second element: no outer tag |
| 801 |
inc(index, 3), // "Y {{ z: {=u8} }}" |
| 802 |
24u8, // [1].y.z |
| 803 |
], |
| 804 |
); |
| 805 |
} |
| 806 |
|
| 807 |
#[test] |
| 808 |
fn format_slice_of_slices() { |
| 809 |
let index = fetch_string_index(); |
| 810 |
let slice: &[&[u16]] = &[&[256, 257], &[258, 259, 260]]; |
| 811 |
check_format!( |
| 812 |
slice, |
| 813 |
[ |
| 814 |
index, // "{=[?]}" |
| 815 |
slice.len() as u32, // |
| 816 |
inc(index, 1), // "{=[?]}" |
| 817 |
// first slice |
| 818 |
slice[0].len() as u32, |
| 819 |
inc(index, 2), // "{=u16}" |
| 820 |
256u16, // [0][0] |
| 821 |
257u16, // [0][1] |
| 822 |
// second slice |
| 823 |
slice[1].len() as u32, |
| 824 |
inc(index, 3), // "{=u16}" |
| 825 |
258u16, // [1][0] |
| 826 |
259u16, // [1][1] |
| 827 |
260u16, // [1][2] |
| 828 |
], |
| 829 |
); |
| 830 |
} |
| 831 |
|
| 832 |
#[test] |
| 833 |
fn format_slice_enum_slice() { |
| 834 |
let index = fetch_string_index(); |
| 835 |
let slice: &[Option<&[u8]>] = &[None, Some(&[42, 43])]; |
| 836 |
check_format!( |
| 837 |
slice, |
| 838 |
[ |
| 839 |
index, // "{=[?]}" |
| 840 |
slice.len() as u32, // |
| 841 |
// first optional slice |
| 842 |
inc(index, 1), // "None|Some({=?})" |
| 843 |
0u8, // discriminant |
| 844 |
// second optional slice |
| 845 |
// omitted: "None|Some({=?})" index |
| 846 |
1u8, // discriminant |
| 847 |
inc(index, 2), // "{=[?]}" (the ? behind "Some({=?})") |
| 848 |
2u32, // length of second optional slice |
| 849 |
inc(index, 3), // "{=u8}" (the ? behind "{=[?]}") |
| 850 |
42u8, |
| 851 |
// omitted: "{=u8}" index |
| 852 |
43u8, |
| 853 |
], |
| 854 |
); |
| 855 |
} |
| 856 |
|
| 857 |
#[test] |
| 858 |
fn format_slice_enum_generic_struct() { |
| 859 |
#[derive(Format)] |
| 860 |
struct S<T> { |
| 861 |
x: u8, |
| 862 |
y: T, |
| 863 |
} |
| 864 |
|
| 865 |
let index = fetch_string_index(); |
| 866 |
let slice: &[Option<S<u8>>] = &[None, Some(S { x: 42, y: 43 })]; |
| 867 |
check_format!( |
| 868 |
slice, |
| 869 |
[ |
| 870 |
index, // "{=[?]}" |
| 871 |
slice.len() as u32, // |
| 872 |
// first optional element |
| 873 |
inc(index, 1), // "None|Some({=?})" |
| 874 |
0u8, // discriminant |
| 875 |
// second optional element |
| 876 |
// omitted: "None|Some({=?})" index |
| 877 |
1u8, // discriminant |
| 878 |
inc(index, 2), // "S {{ x: {=u8}, y: {=?} }}" (the ? behind "Some({=?})") |
| 879 |
42u8, // S.x |
| 880 |
inc(index, 3), // "{=u8}" (the ? behind S.y) |
| 881 |
43u8, // S. y |
| 882 |
], |
| 883 |
); |
| 884 |
} |
| 885 |
|
| 886 |
#[test] |
| 887 |
fn derive_with_bounds() { |
| 888 |
#[derive(Format)] |
| 889 |
struct S<T: Copy> { |
| 890 |
val: T, |
| 891 |
} |
| 892 |
|
| 893 |
#[derive(Format)] |
| 894 |
struct S2<'a: 'b, 'b> { |
| 895 |
a: &'a u8, |
| 896 |
b: &'b u8, |
| 897 |
} |
| 898 |
|
| 899 |
let index = fetch_string_index(); |
| 900 |
check_format!( |
| 901 |
&S { val: 0 }, |
| 902 |
[ |
| 903 |
index, // "S {{ val: {=?} }}" |
| 904 |
inc(index, 1), // "{=i32}" |
| 905 |
0u32, |
| 906 |
], |
| 907 |
); |
| 908 |
|
| 909 |
let index = fetch_string_index(); |
| 910 |
check_format!( |
| 911 |
&S2 { a: &1, b: &2 }, |
| 912 |
[ |
| 913 |
index, // "S2 { a: {=u8}, b: {=u8} }}" |
| 914 |
1u8, 2u8, |
| 915 |
], |
| 916 |
); |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn format_bools() { |
| 921 |
#[derive(Format)] |
| 922 |
struct A(bool); |
| 923 |
|
| 924 |
#[derive(Format)] |
| 925 |
struct B(bool); |
| 926 |
|
| 927 |
let index = fetch_string_index(); |
| 928 |
check_format!( |
| 929 |
&(A(true), B(true)), |
| 930 |
[ |
| 931 |
index, // "({=?}, {=?})" |
| 932 |
inc(index, 1), // "A({=bool})" |
| 933 |
0b1u8, // A |
| 934 |
inc(index, 2), // "B({=bool})" |
| 935 |
0b1u8, // B |
| 936 |
], |
| 937 |
); |
| 938 |
} |
| 939 |
|
| 940 |
#[test] |
| 941 |
fn enum_variants() { |
| 942 |
#[allow(dead_code)] |
| 943 |
#[derive(Format)] |
| 944 |
enum EnumSmall { |
| 945 |
A0, |
| 946 |
A1, |
| 947 |
A2, |
| 948 |
} |
| 949 |
|
| 950 |
#[rustfmt::skip] |
| 951 |
#[allow(dead_code)] |
| 952 |
#[derive(Format)] |
| 953 |
enum EnumLarge { |
| 954 |
A000, A001, A002, A003, A004, A005, A006, A007, A008, A009, A010, A011, A012, A013, A014, A015, A016, A017, |
| 955 |
A018, A019, A020, A021, A022, A023, A024, A025, A026, A027, A028, A029, A030, A031, A032, A033, A034, A035, |
| 956 |
A036, A037, A038, A039, A040, A041, A042, A043, A044, A045, A046, A047, A048, A049, A050, A051, A052, A053, |
| 957 |
A054, A055, A056, A057, A058, A059, A060, A061, A062, A063, A064, A065, A066, A067, A068, A069, A070, A071, |
| 958 |
A072, A073, A074, A075, A076, A077, A078, A079, A080, A081, A082, A083, A084, A085, A086, A087, A088, A089, |
| 959 |
A090, A091, A092, A093, A094, A095, A096, A097, A098, A099, A100, A101, A102, A103, A104, A105, A106, A107, |
| 960 |
A108, A109, A110, A111, A112, A113, A114, A115, A116, A117, A118, A119, A120, A121, A122, A123, A124, A125, |
| 961 |
A126, A127, A128, A129, A130, A131, A132, A133, A134, A135, A136, A137, A138, A139, A140, A141, A142, A143, |
| 962 |
A144, A145, A146, A147, A148, A149, A150, A151, A152, A153, A154, A155, A156, A157, A158, A159, A160, A161, |
| 963 |
A162, A163, A164, A165, A166, A167, A168, A169, A170, A171, A172, A173, A174, A175, A176, A177, A178, A179, |
| 964 |
A180, A181, A182, A183, A184, A185, A186, A187, A188, A189, A190, A191, A192, A193, A194, A195, A196, A197, |
| 965 |
A198, A199, A200, A201, A202, A203, A204, A205, A206, A207, A208, A209, A210, A211, A212, A213, A214, A215, |
| 966 |
A216, A217, A218, A219, A220, A221, A222, A223, A224, A225, A226, A227, A228, A229, A230, A231, A232, A233, |
| 967 |
A234, A235, A236, A237, A238, A239, A240, A241, A242, A243, A244, A245, A246, A247, A248, A249, A250, A251, |
| 968 |
A252, A253, A254, A255, A256, A257, A258, A259, A260, A261, A262, A263, A264, A265, A266, A267, A268, A269, |
| 969 |
} |
| 970 |
|
| 971 |
let e = EnumSmall::A2; |
| 972 |
|
| 973 |
let index = fetch_string_index(); |
| 974 |
check_format!(&e, [index, 2u8]); |
| 975 |
|
| 976 |
let e = EnumLarge::A002; |
| 977 |
let index = fetch_string_index(); |
| 978 |
check_format!(&e, [index, 2u16]); |
| 979 |
|
| 980 |
let e = EnumLarge::A269; |
| 981 |
let index = fetch_string_index(); |
| 982 |
check_format!(&e, [index, 269u16]); |
| 983 |
} |
| 984 |
|
| 985 |
#[test] |
| 986 |
fn derive_str() { |
| 987 |
#[derive(Format)] |
| 988 |
struct S { |
| 989 |
x: &'static str, |
| 990 |
} |
| 991 |
|
| 992 |
let s = S { x: "hi" }; |
| 993 |
|
| 994 |
let index = fetch_string_index(); |
| 995 |
check_format!( |
| 996 |
&s, |
| 997 |
[ |
| 998 |
index, // "S {{ s: {:str} }}" (NOTE: `s` field is not {:?}) |
| 999 |
// so no extra format string index here |
| 1000 |
2u32, // s.x.len() |
| 1001 |
104u8, // b'h' |
| 1002 |
105u8, // b'i' |
| 1003 |
], |
| 1004 |
); |
| 1005 |
} |
| 1006 |
|
| 1007 |
#[test] |
| 1008 |
fn core_fmt_adapters() { |
| 1009 |
let index = fetch_string_index(); |
| 1010 |
check_format!(&Debug2Format(&123u8), [index, b'1', b'2', b'3', 0xffu8]); |
| 1011 |
let index = fetch_string_index(); |
| 1012 |
check_format!(&Display2Format(&123u8), [index, b'1', b'2', b'3', 0xffu8]); |
| 1013 |
} |