class_name_unittest.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include "butil/class_name.h"
  19. #include "butil/logging.h"
  20. namespace butil {
  21. namespace foobar {
  22. struct MyClass {};
  23. }
  24. }
  25. namespace {
  26. class ClassNameTest : public ::testing::Test {
  27. protected:
  28. virtual void SetUp() {
  29. srand(time(0));
  30. };
  31. };
  32. TEST_F(ClassNameTest, demangle) {
  33. ASSERT_EQ("add_something", butil::demangle("add_something"));
  34. ASSERT_EQ("guard variable for butil::my_ip()::ip",
  35. butil::demangle("_ZGVZN5butil5my_ipEvE2ip"));
  36. ASSERT_EQ("dp::FiberPBCommand<proto::PbRouteTable, proto::PbRouteAck>::marshal(dp::ParamWriter*)::__FUNCTION__",
  37. butil::demangle("_ZZN2dp14FiberPBCommandIN5proto12PbRouteTableENS1_10PbRouteAckEE7marshalEPNS_11ParamWriterEE12__FUNCTION__"));
  38. ASSERT_EQ("7&8", butil::demangle("7&8"));
  39. }
  40. TEST_F(ClassNameTest, class_name_sanity) {
  41. ASSERT_EQ("char", butil::class_name_str('\0'));
  42. ASSERT_STREQ("short", butil::class_name<short>());
  43. ASSERT_EQ("long", butil::class_name_str(1L));
  44. ASSERT_EQ("unsigned long", butil::class_name_str(1UL));
  45. ASSERT_EQ("float", butil::class_name_str(1.1f));
  46. ASSERT_EQ("double", butil::class_name_str(1.1));
  47. ASSERT_STREQ("char*", butil::class_name<char*>());
  48. ASSERT_STREQ("char const*", butil::class_name<const char*>());
  49. ASSERT_STREQ("butil::foobar::MyClass", butil::class_name<butil::foobar::MyClass>());
  50. int array[32];
  51. ASSERT_EQ("int [32]", butil::class_name_str(array));
  52. LOG(INFO) << butil::class_name_str(this);
  53. LOG(INFO) << butil::class_name_str(*this);
  54. }
  55. }