1 | /* |
2 | * Copyright 2015-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include <folly/Demangle.h> |
18 | |
19 | #include <folly/detail/Demangle.h> |
20 | #include <folly/portability/GTest.h> |
21 | |
22 | using folly::demangle; |
23 | |
24 | namespace folly_test { |
25 | struct ThisIsAVeryLongStructureName {}; |
26 | } // namespace folly_test |
27 | |
28 | #if FOLLY_DETAIL_HAVE_DEMANGLE_H |
29 | TEST(Demangle, demangle) { |
30 | char expected[] = "folly_test::ThisIsAVeryLongStructureName" ; |
31 | EXPECT_STREQ( |
32 | expected, |
33 | demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str()); |
34 | |
35 | { |
36 | char buf[sizeof(expected)]; |
37 | EXPECT_EQ( |
38 | sizeof(expected) - 1, |
39 | demangle( |
40 | typeid(folly_test::ThisIsAVeryLongStructureName), |
41 | buf, |
42 | sizeof(buf))); |
43 | EXPECT_STREQ(expected, buf); |
44 | |
45 | EXPECT_EQ( |
46 | sizeof(expected) - 1, |
47 | demangle(typeid(folly_test::ThisIsAVeryLongStructureName), buf, 11)); |
48 | EXPECT_STREQ("folly_test" , buf); |
49 | } |
50 | } |
51 | |
52 | #if defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) |
53 | namespace { |
54 | |
55 | template <int I, class T1, class T2> |
56 | struct Node {}; |
57 | |
58 | template <int N, int I = 1> |
59 | struct LongSymbol { |
60 | using arg1 = typename LongSymbol<N / 2, 2 * I>::type; |
61 | using arg2 = typename LongSymbol<N / 2, 2 * I + 1>::type; |
62 | using type = Node<I, arg1, arg2>; |
63 | }; |
64 | |
65 | template <int I> |
66 | struct LongSymbol<0, I> { |
67 | using type = void; |
68 | }; |
69 | |
70 | } // namespace |
71 | |
72 | TEST(Demangle, LongSymbolFallback) { |
73 | // The symbol must be at least FOLLY_DEMANGLE_MAX_SYMBOL_SIZE long. |
74 | using Symbol = LongSymbol<FOLLY_DEMANGLE_MAX_SYMBOL_SIZE>::type; |
75 | auto name = typeid(Symbol).name(); |
76 | |
77 | EXPECT_STREQ(name, demangle(name).c_str()); |
78 | |
79 | char buf[16]; |
80 | char expected[16]; |
81 | folly::demangle(name, buf, 16); |
82 | folly::strlcpy(expected, name, 16); |
83 | EXPECT_STREQ(expected, buf); |
84 | } |
85 | #endif // defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) |
86 | |
87 | #endif // FOLLY_DETAIL_HAVE_DEMANGLE_H |
88 | |
89 | TEST(Demangle, strlcpy) { |
90 | char buf[6]; |
91 | |
92 | EXPECT_EQ(3, folly::strlcpy(buf, "abc" , 6)); |
93 | EXPECT_EQ('\0', buf[3]); |
94 | EXPECT_EQ("abc" , std::string(buf)); |
95 | |
96 | EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg" , 3)); |
97 | EXPECT_EQ('\0', buf[2]); |
98 | EXPECT_EQ("ab" , std::string(buf)); |
99 | |
100 | const char* big_string = "abcdefghijklmnop" ; |
101 | |
102 | EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, sizeof(buf))); |
103 | EXPECT_EQ('\0', buf[5]); |
104 | EXPECT_EQ("abcde" , std::string(buf)); |
105 | |
106 | buf[0] = 'z'; |
107 | EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0)); |
108 | EXPECT_EQ('z', buf[0]); // unchanged, size = 0 |
109 | } |
110 | |