1 //===------------------ RustDemangleTest.cpp ------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Demangle/Demangle.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 
13 #include <cstdlib>
14 
TEST(RustDemangle,Success)15 TEST(RustDemangle, Success) {
16   char *Demangled =
17       llvm::rustDemangle("_RNvC1a4main", nullptr, nullptr, nullptr);
18   EXPECT_STREQ(Demangled, "a::main");
19   std::free(Demangled);
20 
21   // With status.
22   int Status = 0;
23   Demangled = llvm::rustDemangle("_RNvC1a4main", nullptr, nullptr, &Status);
24   EXPECT_EQ(Status, llvm::demangle_success);
25   EXPECT_STREQ(Demangled, "a::main");
26   std::free(Demangled);
27 
28   // With status and length.
29   size_t N = 0;
30   Demangled = llvm::rustDemangle("_RNvC1a4main", nullptr, &N, &Status);
31   EXPECT_EQ(Status, llvm::demangle_success);
32   EXPECT_EQ(N, 8u);
33   EXPECT_STREQ(Demangled, "a::main");
34   std::free(Demangled);
35 }
36 
TEST(RustDemangle,Invalid)37 TEST(RustDemangle, Invalid) {
38   int Status = 0;
39   char *Demangled = nullptr;
40 
41   // Invalid prefix.
42   Demangled = llvm::rustDemangle("_ABCDEF", nullptr, nullptr, &Status);
43   EXPECT_EQ(Status, llvm::demangle_invalid_mangled_name);
44   EXPECT_EQ(Demangled, nullptr);
45 
46   // Correct prefix but still invalid.
47   Demangled = llvm::rustDemangle("_RRR", nullptr, nullptr, &Status);
48   EXPECT_EQ(Status, llvm::demangle_invalid_mangled_name);
49   EXPECT_EQ(Demangled, nullptr);
50 }
51 
TEST(RustDemangle,OutputBufferWithoutLength)52 TEST(RustDemangle, OutputBufferWithoutLength) {
53   char *Buffer = static_cast<char *>(std::malloc(1024));
54   ASSERT_NE(Buffer, nullptr);
55 
56   int Status = 0;
57   char *Demangled =
58       llvm::rustDemangle("_RNvC1a4main", Buffer, nullptr, &Status);
59 
60   EXPECT_EQ(Status, llvm::demangle_invalid_args);
61   EXPECT_EQ(Demangled, nullptr);
62   std::free(Buffer);
63 }
64 
TEST(RustDemangle,OutputBuffer)65 TEST(RustDemangle, OutputBuffer) {
66   size_t N = 1024;
67   char *Buffer = static_cast<char *>(std::malloc(N));
68   ASSERT_NE(Buffer, nullptr);
69 
70   int Status = 0;
71   char *Demangled = llvm::rustDemangle("_RNvC1a4main", Buffer, &N, &Status);
72 
73   EXPECT_EQ(Status, llvm::demangle_success);
74   EXPECT_EQ(Demangled, Buffer);
75   EXPECT_STREQ(Demangled, "a::main");
76   std::free(Demangled);
77 }
78 
TEST(RustDemangle,SmallOutputBuffer)79 TEST(RustDemangle, SmallOutputBuffer) {
80   size_t N = 1;
81   char *Buffer = static_cast<char *>(std::malloc(N));
82   ASSERT_NE(Buffer, nullptr);
83 
84   int Status = 0;
85   char *Demangled = llvm::rustDemangle("_RNvC1a4main", Buffer, &N, &Status);
86 
87   EXPECT_EQ(Status, llvm::demangle_success);
88   EXPECT_STREQ(Demangled, "a::main");
89   std::free(Demangled);
90 }
91