2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Demangle.h>
19 #include <folly/portability/GTest.h>
21 using folly::demangle;
23 namespace folly_test {
24 struct ThisIsAVeryLongStructureName {
26 } // namespace folly_test
28 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
29 TEST(Demangle, demangle) {
30 char expected[] = "folly_test::ThisIsAVeryLongStructureName";
33 demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str());
36 char buf[sizeof(expected)];
37 EXPECT_EQ(sizeof(expected) - 1,
38 demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
40 EXPECT_STREQ(expected, buf);
42 EXPECT_EQ(sizeof(expected) - 1,
43 demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
45 EXPECT_STREQ("folly_test", buf);
49 #if defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
52 template <int I, class T1, class T2>
55 template <int N, int I = 1>
57 using arg1 = typename LongSymbol<N / 2, 2 * I>::type;
58 using arg2 = typename LongSymbol<N / 2, 2 * I + 1>::type;
59 using type = Node<I, arg1, arg2>;
63 struct LongSymbol<0, I> {
69 TEST(Demangle, LongSymbolFallback) {
70 // The symbol must be at least FOLLY_DEMANGLE_MAX_SYMBOL_SIZE long.
71 using Symbol = LongSymbol<FOLLY_DEMANGLE_MAX_SYMBOL_SIZE>::type;
72 auto name = typeid(Symbol).name();
74 EXPECT_STREQ(name, demangle(name).c_str());
78 folly::demangle(name, buf, 16);
79 folly::strlcpy(expected, name, 16);
80 EXPECT_STREQ(expected, buf);
82 #endif // defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
84 #endif // FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
86 TEST(Demangle, strlcpy) {
89 EXPECT_EQ(3, folly::strlcpy(buf, "abc", 6));
90 EXPECT_EQ('\0', buf[3]);
91 EXPECT_EQ("abc", std::string(buf));
93 EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg", 3));
94 EXPECT_EQ('\0', buf[2]);
95 EXPECT_EQ("ab", std::string(buf));
97 const char* big_string = "abcdefghijklmnop";
99 EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, sizeof(buf)));
100 EXPECT_EQ('\0', buf[5]);
101 EXPECT_EQ("abcde", std::string(buf));
104 EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0));
105 EXPECT_EQ('z', buf[0]); // unchanged, size = 0