Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / test / DemangleTest.cpp
1 /*
2  * Copyright 2017 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/portability/GTest.h>
20
21 using folly::demangle;
22
23 namespace folly_test {
24 struct ThisIsAVeryLongStructureName {
25 };
26 } // namespace folly_test
27
28 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
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(sizeof(expected) - 1,
38               demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
39                        buf, sizeof(buf)));
40     EXPECT_STREQ(expected, buf);
41
42     EXPECT_EQ(sizeof(expected) - 1,
43               demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
44                        buf, 11));
45     EXPECT_STREQ("folly_test", buf);
46   }
47 }
48
49 #if defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
50 namespace {
51
52 template <int I, class T1, class T2>
53 struct Node {};
54
55 template <int N, int I = 1>
56 struct LongSymbol {
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>;
60 };
61
62 template <int I>
63 struct LongSymbol<0, I> {
64   using type = void;
65 };
66
67 } // namespace
68
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();
73
74   EXPECT_STREQ(name, demangle(name).c_str());
75
76   char buf[16];
77   char expected[16];
78   folly::demangle(name, buf, 16);
79   folly::strlcpy(expected, name, 16);
80   EXPECT_STREQ(expected, buf);
81 }
82 #endif // defined(FOLLY_DEMANGLE_MAX_SYMBOL_SIZE)
83
84 #endif // FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
85
86 TEST(Demangle, strlcpy) {
87   char buf[6];
88
89   EXPECT_EQ(3, folly::strlcpy(buf, "abc", 6));
90   EXPECT_EQ('\0', buf[3]);
91   EXPECT_EQ("abc", std::string(buf));
92
93   EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg", 3));
94   EXPECT_EQ('\0', buf[2]);
95   EXPECT_EQ("ab", std::string(buf));
96
97   const char* big_string = "abcdefghijklmnop";
98
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));
102
103   buf[0] = 'z';
104   EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0));
105   EXPECT_EQ('z', buf[0]);  // unchanged, size = 0
106 }