unique_small_name_test.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "gtest/gtest.h"
  15. #include "absl/base/optimization.h"
  16. #include "absl/strings/string_view.h"
  17. // This test by itself does not do anything fancy, but it serves as binary I can
  18. // query in shell test.
  19. namespace {
  20. template <class T>
  21. void DoNotOptimize(const T& var) {
  22. #ifdef __GNUC__
  23. asm volatile("" : "+m"(const_cast<T&>(var)));
  24. #else
  25. std::cout << (void*)&var;
  26. #endif
  27. }
  28. int very_long_int_variable_name ABSL_INTERNAL_UNIQUE_SMALL_NAME() = 0;
  29. char very_long_str_variable_name[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = "abc";
  30. TEST(UniqueSmallName, NonAutomaticVar) {
  31. EXPECT_EQ(very_long_int_variable_name, 0);
  32. EXPECT_EQ(absl::string_view(very_long_str_variable_name), "abc");
  33. }
  34. int VeryLongFreeFunctionName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  35. TEST(UniqueSmallName, FreeFunction) {
  36. DoNotOptimize(&VeryLongFreeFunctionName);
  37. EXPECT_EQ(VeryLongFreeFunctionName(), 456);
  38. }
  39. int VeryLongFreeFunctionName() { return 456; }
  40. struct VeryLongStructName {
  41. explicit VeryLongStructName(int i);
  42. int VeryLongMethodName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  43. static int VeryLongStaticMethodName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
  44. private:
  45. int fld;
  46. };
  47. TEST(UniqueSmallName, Struct) {
  48. VeryLongStructName var(10);
  49. DoNotOptimize(var);
  50. DoNotOptimize(&VeryLongStructName::VeryLongMethodName);
  51. DoNotOptimize(&VeryLongStructName::VeryLongStaticMethodName);
  52. EXPECT_EQ(var.VeryLongMethodName(), 10);
  53. EXPECT_EQ(VeryLongStructName::VeryLongStaticMethodName(), 123);
  54. }
  55. VeryLongStructName::VeryLongStructName(int i) : fld(i) {}
  56. int VeryLongStructName::VeryLongMethodName() { return fld; }
  57. int VeryLongStructName::VeryLongStaticMethodName() { return 123; }
  58. } // namespace