Skip to content

Commit

Permalink
tests: Add regression test for Debug impl of raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Enselic committed Jan 27, 2025
1 parent 8206d70 commit 5f086cc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ version = "0.0.0"
dependencies = [
"rand",
"rand_xorshift",
"regex",
]

[[package]]
Expand Down Expand Up @@ -275,6 +276,31 @@ dependencies = [
"rand_core",
]

[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"regex-automata",
"regex-syntax",
]

[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"regex-syntax",
]

[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"

[[package]]
name = "rustc-demangle"
version = "0.1.24"
Expand Down
1 change: 1 addition & 0 deletions library/coretests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ test = true
[dev-dependencies]
rand = { version = "0.8.5", default-features = false }
rand_xorshift = { version = "0.3.0", default-features = false }
regex = { version = "1.11.1", default-features = false }
31 changes: 31 additions & 0 deletions library/coretests/tests/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,37 @@ fn test_pointer_formats_data_pointer() {
assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
}

#[test]
fn test_fmt_debug_of_raw_pointers() {
use core::fmt::Debug;

fn check_fmt<T: Debug>(t: T, expected: &str) {
use std::sync::LazyLock;

use regex::Regex;

static ADDR_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());

let formatted = format!("{:?}", t);
let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");

assert_eq!(normalized, expected);
}

let plain = &mut 100;
check_fmt(plain as *mut i32, "$HEX");
check_fmt(plain as *const i32, "$HEX");

let slice = &mut [200, 300, 400][..];
check_fmt(slice as *mut [i32], "$HEX");
check_fmt(slice as *const [i32], "$HEX");

let vtable = &mut 500 as &mut dyn Debug;
check_fmt(vtable as *mut dyn Debug, "$HEX");
check_fmt(vtable as *const dyn Debug, "$HEX");
}

#[test]
fn test_estimated_capacity() {
assert_eq!(format_args!("").estimated_capacity(), 0);
Expand Down

0 comments on commit 5f086cc

Please sign in to comment.