2 * Copyright 2015 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 <gflags/gflags.h>
20 #include <gtest/gtest.h>
22 using folly::demangle;
24 namespace folly_test {
25 struct ThisIsAVeryLongStructureName {
27 } // namespace folly_test
29 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
30 TEST(Demangle, demangle) {
31 char expected[] = "folly_test::ThisIsAVeryLongStructureName";
34 demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str());
37 char buf[sizeof(expected)];
38 EXPECT_EQ(sizeof(expected) - 1,
39 demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
41 EXPECT_STREQ(expected, buf);
43 EXPECT_EQ(sizeof(expected) - 1,
44 demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
46 EXPECT_STREQ("folly_test", buf);
51 TEST(Demangle, strlcpy) {
54 EXPECT_EQ(3, folly::strlcpy(buf, "abc", 6));
55 EXPECT_EQ('\0', buf[3]);
56 EXPECT_EQ("abc", std::string(buf));
58 EXPECT_EQ(7, folly::strlcpy(buf, "abcdefg", 3));
59 EXPECT_EQ('\0', buf[2]);
60 EXPECT_EQ("ab", std::string(buf));
62 const char* big_string = "abcdefghijklmnop";
64 EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, sizeof(buf)));
65 EXPECT_EQ('\0', buf[5]);
66 EXPECT_EQ("abcde", std::string(buf));
69 EXPECT_EQ(strlen(big_string), folly::strlcpy(buf, big_string, 0));
70 EXPECT_EQ('z', buf[0]); // unchanged, size = 0
73 int main(int argc, char *argv[]) {
74 testing::InitGoogleTest(&argc, argv);
75 gflags::ParseCommandLineFlags(&argc, &argv, true);
76 return RUN_ALL_TESTS();