Skip to content

Commit 632465f

Browse files
committed
tests: Add regression test for Debug impl of raw pointers
1 parent c874b46 commit 632465f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

Cargo.lock

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coretests/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ test = true
2525
[dev-dependencies]
2626
rand = { version = "0.9.0", default-features = false }
2727
rand_xorshift = { version = "0.4.0", default-features = false }
28+
regex = { version = "1.11.1", default-features = false }

coretests/tests/fmt/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,37 @@ fn test_pointer_formats_data_pointer() {
1919
assert_eq!(format!("{b:p}"), format!("{:p}", b as *const _));
2020
}
2121

22+
#[test]
23+
fn test_fmt_debug_of_raw_pointers() {
24+
use core::fmt::Debug;
25+
26+
fn check_fmt<T: Debug>(t: T, expected: &str) {
27+
use std::sync::LazyLock;
28+
29+
use regex::Regex;
30+
31+
static ADDR_REGEX: LazyLock<Regex> =
32+
LazyLock::new(|| Regex::new(r"0x[0-9a-fA-F]+").unwrap());
33+
34+
let formatted = format!("{:?}", t);
35+
let normalized = ADDR_REGEX.replace_all(&formatted, "$$HEX");
36+
37+
assert_eq!(normalized, expected);
38+
}
39+
40+
let plain = &mut 100;
41+
check_fmt(plain as *mut i32, "$HEX");
42+
check_fmt(plain as *const i32, "$HEX");
43+
44+
let slice = &mut [200, 300, 400][..];
45+
check_fmt(slice as *mut [i32], "$HEX");
46+
check_fmt(slice as *const [i32], "$HEX");
47+
48+
let vtable = &mut 500 as &mut dyn Debug;
49+
check_fmt(vtable as *mut dyn Debug, "$HEX");
50+
check_fmt(vtable as *const dyn Debug, "$HEX");
51+
}
52+
2253
#[test]
2354
fn test_estimated_capacity() {
2455
assert_eq!(format_args!("").estimated_capacity(), 0);

0 commit comments

Comments
 (0)