Debian Codegraph @

rust_expected_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package demangle
6
7 import (
8 "bufio"
9 "fmt"
10 "os"
11 "strings"
12 "testing"
13 )
14
15 const rustFilename = "testdata/rust-demangle-expected"
16
17 // TestRustExpected is like TestExpected, but for Rust demangling.
18 // We ignore all test inputs that do not start with _R.
19 func TestRustExpected(t *testing.T) {
20 t.Parallel()
21 f, err := os.Open(rustFilename)
22 if err != nil {
23 t.Fatal(err)
24 }
25 scanner := bufio.NewScanner(f)
26 lineno := 1
27 for {
28 format, got := getOptLine(t, scanner, &lineno)
29 if !got {
30 break
31 }
32 report := lineno
33 input := getLine(t, scanner, &lineno)
34 expect := getLine(t, scanner, &lineno)
35
36 if skipExpectedRustTest(t, format, input, report) {
37 continue
38 }
39
40 oneRustTest(t, report, input, expect)
41 }
42 if err := scanner.Err(); err != nil {
43 t.Error(err)
44 }
45 }
46
47 // oneRustTest tests one entry from rust-demangle-expected.
48 func oneRustTest(t *testing.T, report int, input, expect string) {
49 if *verbose {
50 fmt.Println(input)
51 }
52
53 s, err := ToString(input)
54 if err != nil {
55 if err != ErrNotMangledName {
56 if input == expect {
57 return
58 }
59 t.Errorf("%s:%d: %v", rustFilename, report, err)
60 return
61 }
62 s = input
63 }
64
65 if s != expect {
66 t.Errorf("%s:%d: got %q, want %q", rustFilename, report, s, expect)
67 }
68
69 if s == expect && s != input && len(expect) > 64 {
70 ss, err := ToString(input, MaxLength(6))
71 if err != nil {
72 t.Errorf("%s:%d: error with MaxLength: %v", rustFilename, report, err)
73 } else if ss != expect[:64] {
74 t.Errorf("%s:%d: MaxLength mismatch: %q != %q", rustFilename, report, ss, expect[:64])
75 }
76 }
77 }
78
79 // skipExpectedRustTest reports whether to skip a test in
80 // rust-demangle-expected.
81 func skipExpectedRustTest(t testing.TB, format, input string, report int) bool {
82 if len(format) > 0 && format[0] == '-' {
83 for _, arg := range strings.Fields(format) {
84 switch arg {
85 case "--format=gnu-v3":
86 return true
87 case "--format=auto":
88 case "--format=rust":
89 default:
90 t.Errorf("%s:%d: unrecognized argument %s", rustFilename, report, arg)
91 return true
92 }
93 }
94 }
95 return false
96 }
97
98 const rustCheckFilename = "testdata/rust.test"
99
100 func TestRustCheck(t *testing.T) {
101 t.Parallel()
102 f, err := os.Open(rustCheckFilename)
103 if err != nil {
104 t.Fatal(err)
105 }
106 scanner := bufio.NewScanner(f)
107 lineno := 1
108 for scanner.Scan() {
109 report := lineno
110 line := strings.TrimSpace(scanner.Text())
111 lineno++
112 if !strings.HasPrefix(line, "CHECK: ") {
113 continue
114 }
115 want := strings.TrimPrefix(line, "CHECK: ")
116 if !scanner.Scan() {
117 t.Fatalf("%s:%d: unexpected EOF", rustCheckFilename, report)
118 }
119 lineno++
120 input := strings.TrimSpace(scanner.Text())
121
122 got, err := ToString(input, LLVMStyle)
123 if err != nil {
124 if want != input {
125 t.Errorf("%s:%d: %v", rustCheckFilename, report, err)
126 }
127 } else if got != want {
128 t.Errorf("%s:%d: got %q, want %q", rustCheckFilename, report, got, want)
129 }
130 }
131 if err := scanner.Err(); err != nil {
132 t.Error(err)
133 }
134 }
135
136 const rustMangledTemplates = "_RNCINvMNtNtNtNtCs5myfTy8mnaF_6timely8dataflow9operators7generic10builder_rcINtB5_15OperatorBuilderINtNtNtBb_6scopes5child5ChildIB1z_INtNtBd_6worker6WorkerNtNtNtCsbo5udLplCaV_20timely_communication9allocator7generic7GenericENtNtCslnPiKci8RgF_7mz_repr9timestamp9TimestampEB3z_EE16build_rescheduleNCINvB4_5buildNCINvXNtB7_8operatorINtNtBb_6stream10StreamCoreB1y_INtNtCsfohDMHpnFpV_5alloc3vec3VecTTNtNtB3D_3row3RowB6k_EB3z_xEEEINtB52_8OperatorB1y_B5L_E14unary_frontierIB5M_INtNtB5Q_2rc2RcINtNtNtNtCsaEm0OTy3LfN_21differential_dataflow5trace15implementations3ord11OrdValBatchB6k_B6k_B3z_xjINtNtCsicJTUUNBAMQ_16timely_container11columnation11TimelyStackB6k_EB9o_EEENCINvXs1_NtNtNtB7V_9operators7arrange11arrangementINtNtB7V_10collection10CollectionB1y_B6j_xEINtBaK_7ArrangeB1y_B6k_B6k_xE12arrange_coreINtNtNtBb_8channels4pact12ExchangeCoreB5L_B6i_NCINvBaG_13arrange_namedINtNtB7R_12spine_fueled5SpineB7x_EE0EBdV_E0NCNCBaD_00BcN_E0NCNCB4Y_00E0NCNCB4K_00E0Cse28fqe15ASj_8clusterd"
137
138 func TestRustNoTemplaraParams(t *testing.T) {
139 got, err := ToString(rustMangledTemplates, NoTemplateParams)
140 if err != nil {
141 t.Fatalf("ToString(%q) failed: %v", rustMangledTemplates, err)
142 }
143 want := "<timely::dataflow::operators::generic::builder_rc::OperatorBuilder<>>::build_reschedule::<>::{closure#0}"
144 if got != want {
145 t.Errorf("ToString(%q) = %q, want %q", rustMangledTemplates, got, want)
146 }
147 }