Debian Codegraph @

third-party/rust-demangle/test-harness/tests/top_level.rs

raw
1 //! Tests copied from `https://github.com/rust-lang/rustc-demangle`'s
2 //! `src/lib.rs` at `fd906f850f90f6d4845c7b8219d218293e0ab3ed`.
3 //!
4 //! These are the only changes made to the tests:
5 //! * `super::` paths -> `rust_demangle_c_test_harness::`
6 //! * `#[ignore = "stack overflow"]` was added to tests that overflow the stack
7 //! * `#[should_panic]` was added to tests that don't pass yet
8
9 use rust_demangle_c_test_harness::{assert_contains, assert_ends_with};
10
11 macro_rules! t {
12 ($a:expr, $b:expr) => {
13 assert!(ok($a, $b))
14 };
15 }
16
17 macro_rules! t_err {
18 ($a:expr) => {
19 assert!(ok_err($a))
20 };
21 }
22
23 macro_rules! t_nohash {
24 ($a:expr, $b:expr) => {{
25 assert_eq!(
26 format!("{:#}", rust_demangle_c_test_harness::demangle($a)),
27 $b
28 );
29 }};
30 }
31
32 fn ok(sym: &str, expected: &str) -> bool {
33 match rust_demangle_c_test_harness::try_demangle(sym) {
34 Ok(s) => {
35 if s.to_string() == expected {
36 true
37 } else {
38 println!("\n{}\n!=\n{}\n", s, expected);
39 false
40 }
41 }
42 Err(_) => {
43 println!("error demangling");
44 false
45 }
46 }
47 }
48
49 fn ok_err(sym: &str) -> bool {
50 match rust_demangle_c_test_harness::try_demangle(sym) {
51 Ok(_) => {
52 println!("succeeded in demangling");
53 false
54 }
55 Err(_) => rust_demangle_c_test_harness::demangle(sym).to_string() == sym,
56 }
57 }
58
59 #[test]
60 fn demangle() {
61 t_err!("test");
62 t!("_ZN4testE", "test");
63 t_err!("_ZN4test");
64 t!("_ZN4test1a2bcE", "test::a::bc");
65 }
66
67 #[test]
68 fn demangle_dollars() {
69 t!("_ZN4$RP$E", ")");
70 t!("_ZN8$RF$testE", "&test");
71 t!("_ZN8$BP$test4foobE", "*test::foob");
72 t!("_ZN9$u20$test4foobE", " test::foob");
73 t!("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>");
74 }
75
76 #[test]
77 fn demangle_many_dollars() {
78 t!("_ZN13test$u20$test4foobE", "test test::foob");
79 t!("_ZN12test$BP$test4foobE", "test*test::foob");
80 }
81
82 #[test]
83 fn demangle_osx() {
84 t!(
85 "__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E",
86 "alloc::allocator::Layout::for_value::h02a996811f781011"
87 );
88 t!("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E", "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659");
89 t!("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E", "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170");
90 }
91
92 #[test]
93 fn demangle_windows() {
94 t!("ZN4testE", "test");
95 t!("ZN13test$u20$test4foobE", "test test::foob");
96 t!("ZN12test$RF$test4foobE", "test&test::foob");
97 }
98
99 #[test]
100 fn demangle_elements_beginning_with_underscore() {
101 t!("_ZN13_$LT$test$GT$E", "<test>");
102 t!("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}");
103 t!("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR");
104 }
105
106 #[test]
107 fn demangle_trait_impls() {
108 t!(
109 "_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
110 "<Test + 'static as foo::Bar<Test>>::bar"
111 );
112 }
113
114 #[test]
115 fn demangle_without_hash() {
116 let s = "_ZN3foo17h05af221e174051e9E";
117 t!(s, "foo::h05af221e174051e9");
118 t_nohash!(s, "foo");
119 }
120
121 #[test]
122 fn demangle_without_hash_edgecases() {
123 // One element, no hash.
124 t_nohash!("_ZN3fooE", "foo");
125 // Two elements, no hash.
126 t_nohash!("_ZN3foo3barE", "foo::bar");
127 // Longer-than-normal hash.
128 t_nohash!("_ZN3foo20h05af221e174051e9abcE", "foo");
129 // Shorter-than-normal hash.
130 t_nohash!("_ZN3foo5h05afE", "foo");
131 // Valid hash, but not at the end.
132 t_nohash!("_ZN17h05af221e174051e93fooE", "h05af221e174051e9::foo");
133 // Not a valid hash, missing the 'h'.
134 t_nohash!("_ZN3foo16ffaf221e174051e9E", "foo::ffaf221e174051e9");
135 // Not a valid hash, has a non-hex-digit.
136 t_nohash!("_ZN3foo17hg5af221e174051e9E", "foo::hg5af221e174051e9");
137 }
138
139 #[test]
140 fn demangle_thinlto() {
141 // One element, no hash.
142 t!("_ZN3fooE.llvm.9D1C9369", "foo");
143 t!("_ZN3fooE.llvm.9D1C9369@@16", "foo");
144 t_nohash!(
145 "_ZN9backtrace3foo17hbb467fcdaea5d79bE.llvm.A5310EB9",
146 "backtrace::foo"
147 );
148 }
149
150 #[test]
151 fn demangle_llvm_ir_branch_labels() {
152 t!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut::haf9727c2edfbc47b.exit.i.i");
153 t_nohash!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut.exit.i.i");
154 }
155
156 #[test]
157 fn demangle_ignores_suffix_that_doesnt_look_like_a_symbol() {
158 t_err!("_ZN3fooE.llvm moocow");
159 }
160
161 #[test]
162 fn dont_panic() {
163 rust_demangle_c_test_harness::demangle("_ZN2222222222222222222222EE").to_string();
164 rust_demangle_c_test_harness::demangle("_ZN5*70527e27.ll34csaғE").to_string();
165 rust_demangle_c_test_harness::demangle("_ZN5*70527a54.ll34_$b.1E").to_string();
166 rust_demangle_c_test_harness::demangle(
167 "\
168 _ZN5~saäb4e\n\
169 2734cOsbE\n\
170 5usage20h)3\0\0\0\0\0\0\07e2734cOsbE\
171 ",
172 )
173 .to_string();
174 }
175
176 #[test]
177 fn invalid_no_chop() {
178 t_err!("_ZNfooE");
179 }
180
181 #[test]
182 fn handle_assoc_types() {
183 t!("_ZN151_$LT$alloc..boxed..Box$LT$alloc..boxed..FnBox$LT$A$C$$u20$Output$u3d$R$GT$$u20$$u2b$$u20$$u27$a$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$9call_once17h69e8f44b3723e1caE", "<alloc::boxed::Box<alloc::boxed::FnBox<A, Output=R> + 'a> as core::ops::function::FnOnce<A>>::call_once::h69e8f44b3723e1ca");
184 }
185
186 #[test]
187 fn handle_bang() {
188 t!(
189 "_ZN88_$LT$core..result..Result$LT$$u21$$C$$u20$E$GT$$u20$as$u20$std..process..Termination$GT$6report17hfc41d0da4a40b3e8E",
190 "<core::result::Result<!, E> as std::process::Termination>::report::hfc41d0da4a40b3e8"
191 );
192 }
193
194 // FIXME(eddyb) port recursion limits to C.
195 #[ignore = "stack overflow"]
196 #[test]
197 fn limit_recursion() {
198 assert_contains!(
199 rust_demangle_c_test_harness::demangle("_RNvB_1a").to_string(),
200 "{recursion limit reached}"
201 );
202 assert_contains!(
203 rust_demangle_c_test_harness::demangle("_RMC0RB2_").to_string(),
204 "{recursion limit reached}"
205 );
206 }
207
208 // FIXME(eddyb) port the relevant functionality to C.
209 #[ignore = "would slowly use up all RAM before being OOM-killed"]
210 #[test]
211 fn limit_output_oom_hazard() {
212 assert_ends_with!(
213 rust_demangle_c_test_harness::demangle("RYFG_FGyyEvRYFF_EvRYFFEvERLB_B_B_ERLRjB_B_B_")
214 .to_string(),
215 "{size limit reached}"
216 );
217 }
218
219 // FIXME(eddyb) port the relevant functionality to C.
220 #[should_panic]
221 #[test]
222 fn limit_output() {
223 // NOTE(eddyb) somewhat reduced version of the above, effectively
224 // `<for<...> fn()>` with a larger number of lifetimes in `...`.
225 assert_ends_with!(
226 rust_demangle_c_test_harness::demangle("_RMC0FGZZZ_Eu").to_string(),
227 "{size limit reached}"
228 );
229 }