abseil-cpp/absl/base/internal/unique_small_name_test.cc
Go to the documentation of this file.
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 
15 #include "gtest/gtest.h"
16 #include "absl/base/optimization.h"
17 #include "absl/strings/string_view.h"
18 
19 // This test by itself does not do anything fancy, but it serves as binary I can
20 // query in shell test.
21 
22 namespace {
23 
24 template <class T>
25 void DoNotOptimize(const T& var) {
26 #ifdef __GNUC__
27  asm volatile("" : "+m"(const_cast<T&>(var)));
28 #else
29  std::cout << (void*)&var;
30 #endif
31 }
32 
33 int very_long_int_variable_name ABSL_INTERNAL_UNIQUE_SMALL_NAME() = 0;
34 char very_long_str_variable_name[] ABSL_INTERNAL_UNIQUE_SMALL_NAME() = "abc";
35 
36 TEST(UniqueSmallName, NonAutomaticVar) {
37  EXPECT_EQ(very_long_int_variable_name, 0);
38  EXPECT_EQ(absl::string_view(very_long_str_variable_name), "abc");
39 }
40 
41 int VeryLongFreeFunctionName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
42 
43 TEST(UniqueSmallName, FreeFunction) {
44  DoNotOptimize(&VeryLongFreeFunctionName);
45 
46  EXPECT_EQ(VeryLongFreeFunctionName(), 456);
47 }
48 
49 int VeryLongFreeFunctionName() { return 456; }
50 
51 struct VeryLongStructName {
52  explicit VeryLongStructName(int i);
53 
54  int VeryLongMethodName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
55 
56  static int VeryLongStaticMethodName() ABSL_INTERNAL_UNIQUE_SMALL_NAME();
57 
58  private:
59  int fld;
60 };
61 
62 TEST(UniqueSmallName, Struct) {
63  VeryLongStructName var(10);
64 
65  DoNotOptimize(var);
66  DoNotOptimize(&VeryLongStructName::VeryLongMethodName);
67  DoNotOptimize(&VeryLongStructName::VeryLongStaticMethodName);
68 
69  EXPECT_EQ(var.VeryLongMethodName(), 10);
70  EXPECT_EQ(VeryLongStructName::VeryLongStaticMethodName(), 123);
71 }
72 
73 VeryLongStructName::VeryLongStructName(int i) : fld(i) {}
74 int VeryLongStructName::VeryLongMethodName() { return fld; }
75 int VeryLongStructName::VeryLongStaticMethodName() { return 123; }
76 
77 } // namespace
ABSL_INTERNAL_UNIQUE_SMALL_NAME
#define ABSL_INTERNAL_UNIQUE_SMALL_NAME()
Definition: abseil-cpp/absl/base/optimization.h:249
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
absl::ABSL_NAMESPACE_BEGIN::FreeFunction
void FreeFunction()
Definition: function_type_benchmark.cc:30
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
T
#define T(upbtypeconst, upbtype, ctype, default_value)
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
Struct
struct Struct Struct
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:670
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:44