container_memory_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/container/internal/container_memory.h"
00016 
00017 #include <cstdint>
00018 #include <tuple>
00019 #include <utility>
00020 
00021 #include "gmock/gmock.h"
00022 #include "gtest/gtest.h"
00023 #include "absl/strings/string_view.h"
00024 
00025 namespace absl {
00026 namespace container_internal {
00027 namespace {
00028 
00029 using ::testing::Pair;
00030 
00031 TEST(Memory, AlignmentLargerThanBase) {
00032   std::allocator<int8_t> alloc;
00033   void* mem = Allocate<2>(&alloc, 3);
00034   EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
00035   memcpy(mem, "abc", 3);
00036   Deallocate<2>(&alloc, mem, 3);
00037 }
00038 
00039 TEST(Memory, AlignmentSmallerThanBase) {
00040   std::allocator<int64_t> alloc;
00041   void* mem = Allocate<2>(&alloc, 3);
00042   EXPECT_EQ(0, reinterpret_cast<uintptr_t>(mem) % 2);
00043   memcpy(mem, "abc", 3);
00044   Deallocate<2>(&alloc, mem, 3);
00045 }
00046 
00047 class Fixture : public ::testing::Test {
00048   using Alloc = std::allocator<std::string>;
00049 
00050  public:
00051   Fixture() { ptr_ = std::allocator_traits<Alloc>::allocate(*alloc(), 1); }
00052   ~Fixture() override {
00053     std::allocator_traits<Alloc>::destroy(*alloc(), ptr_);
00054     std::allocator_traits<Alloc>::deallocate(*alloc(), ptr_, 1);
00055   }
00056   std::string* ptr() { return ptr_; }
00057   Alloc* alloc() { return &alloc_; }
00058 
00059  private:
00060   Alloc alloc_;
00061   std::string* ptr_;
00062 };
00063 
00064 TEST_F(Fixture, ConstructNoArgs) {
00065   ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple());
00066   EXPECT_EQ(*ptr(), "");
00067 }
00068 
00069 TEST_F(Fixture, ConstructOneArg) {
00070   ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple("abcde"));
00071   EXPECT_EQ(*ptr(), "abcde");
00072 }
00073 
00074 TEST_F(Fixture, ConstructTwoArg) {
00075   ConstructFromTuple(alloc(), ptr(), std::forward_as_tuple(5, 'a'));
00076   EXPECT_EQ(*ptr(), "aaaaa");
00077 }
00078 
00079 TEST(PairArgs, NoArgs) {
00080   EXPECT_THAT(PairArgs(),
00081               Pair(std::forward_as_tuple(), std::forward_as_tuple()));
00082 }
00083 
00084 TEST(PairArgs, TwoArgs) {
00085   EXPECT_EQ(
00086       std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
00087       PairArgs(1, 'A'));
00088 }
00089 
00090 TEST(PairArgs, Pair) {
00091   EXPECT_EQ(
00092       std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
00093       PairArgs(std::make_pair(1, 'A')));
00094 }
00095 
00096 TEST(PairArgs, Piecewise) {
00097   EXPECT_EQ(
00098       std::make_pair(std::forward_as_tuple(1), std::forward_as_tuple('A')),
00099       PairArgs(std::piecewise_construct, std::forward_as_tuple(1),
00100                std::forward_as_tuple('A')));
00101 }
00102 
00103 TEST(WithConstructed, Simple) {
00104   EXPECT_EQ(1, WithConstructed<absl::string_view>(
00105                    std::make_tuple(std::string("a")),
00106                    [](absl::string_view str) { return str.size(); }));
00107 }
00108 
00109 template <class F, class Arg>
00110 decltype(DecomposeValue(std::declval<F>(), std::declval<Arg>()))
00111 DecomposeValueImpl(int, F&& f, Arg&& arg) {
00112   return DecomposeValue(std::forward<F>(f), std::forward<Arg>(arg));
00113 }
00114 
00115 template <class F, class Arg>
00116 const char* DecomposeValueImpl(char, F&& f, Arg&& arg) {
00117   return "not decomposable";
00118 }
00119 
00120 template <class F, class Arg>
00121 decltype(DecomposeValueImpl(0, std::declval<F>(), std::declval<Arg>()))
00122 TryDecomposeValue(F&& f, Arg&& arg) {
00123   return DecomposeValueImpl(0, std::forward<F>(f), std::forward<Arg>(arg));
00124 }
00125 
00126 TEST(DecomposeValue, Decomposable) {
00127   auto f = [](const int& x, int&& y) {
00128     EXPECT_EQ(&x, &y);
00129     EXPECT_EQ(42, x);
00130     return 'A';
00131   };
00132   EXPECT_EQ('A', TryDecomposeValue(f, 42));
00133 }
00134 
00135 TEST(DecomposeValue, NotDecomposable) {
00136   auto f = [](void*) {
00137     ADD_FAILURE() << "Must not be called";
00138     return 'A';
00139   };
00140   EXPECT_STREQ("not decomposable", TryDecomposeValue(f, 42));
00141 }
00142 
00143 template <class F, class... Args>
00144 decltype(DecomposePair(std::declval<F>(), std::declval<Args>()...))
00145 DecomposePairImpl(int, F&& f, Args&&... args) {
00146   return DecomposePair(std::forward<F>(f), std::forward<Args>(args)...);
00147 }
00148 
00149 template <class F, class... Args>
00150 const char* DecomposePairImpl(char, F&& f, Args&&... args) {
00151   return "not decomposable";
00152 }
00153 
00154 template <class F, class... Args>
00155 decltype(DecomposePairImpl(0, std::declval<F>(), std::declval<Args>()...))
00156 TryDecomposePair(F&& f, Args&&... args) {
00157   return DecomposePairImpl(0, std::forward<F>(f), std::forward<Args>(args)...);
00158 }
00159 
00160 TEST(DecomposePair, Decomposable) {
00161   auto f = [](const int& x, std::piecewise_construct_t, std::tuple<int&&> k,
00162               std::tuple<double>&& v) {
00163     EXPECT_EQ(&x, &std::get<0>(k));
00164     EXPECT_EQ(42, x);
00165     EXPECT_EQ(0.5, std::get<0>(v));
00166     return 'A';
00167   };
00168   EXPECT_EQ('A', TryDecomposePair(f, 42, 0.5));
00169   EXPECT_EQ('A', TryDecomposePair(f, std::make_pair(42, 0.5)));
00170   EXPECT_EQ('A', TryDecomposePair(f, std::piecewise_construct,
00171                                   std::make_tuple(42), std::make_tuple(0.5)));
00172 }
00173 
00174 TEST(DecomposePair, NotDecomposable) {
00175   auto f = [](...) {
00176     ADD_FAILURE() << "Must not be called";
00177     return 'A';
00178   };
00179   EXPECT_STREQ("not decomposable",
00180                TryDecomposePair(f));
00181   EXPECT_STREQ("not decomposable",
00182                TryDecomposePair(f, std::piecewise_construct, std::make_tuple(),
00183                                 std::make_tuple(0.5)));
00184 }
00185 
00186 }  // namespace
00187 }  // namespace container_internal
00188 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14