Add MSVC support for FOLLY_FINAL and FOLLY_OVERRIDE
[folly.git] / folly / test / MemoryTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/Memory.h>
18 #include <folly/Arena.h>
19 #include <folly/String.h>
20
21 #include <glog/logging.h>
22 #include <gtest/gtest.h>
23
24 #include <type_traits>
25
26 using namespace folly;
27
28 TEST(shared_ptr, example) {
29   auto uptr = make_unique<std::string>("hello");
30   auto sptr = to_shared_ptr(std::move(uptr));
31   EXPECT_EQ(nullptr, uptr);
32   EXPECT_EQ("hello", *sptr);
33 }
34
35 template <std::size_t> struct T {};
36 template <std::size_t> struct S {};
37 template <std::size_t> struct P {};
38
39 TEST(as_stl_allocator, sanity_check) {
40   typedef StlAllocator<SysArena, int> stl_arena_alloc;
41
42   EXPECT_TRUE((std::is_same<
43     as_stl_allocator<int, SysArena>::type,
44     stl_arena_alloc
45   >::value));
46
47   EXPECT_TRUE((std::is_same<
48     as_stl_allocator<int, stl_arena_alloc>::type,
49     stl_arena_alloc
50   >::value));
51 }
52
53 TEST(StlAllocator, void_allocator) {
54   typedef StlAllocator<SysArena, void> void_allocator;
55   SysArena arena;
56   void_allocator valloc(&arena);
57
58   typedef void_allocator::rebind<int>::other int_allocator;
59   int_allocator ialloc(valloc);
60
61   auto i = std::allocate_shared<int>(ialloc, 10);
62   ASSERT_NE(nullptr, i.get());
63   EXPECT_EQ(10, *i);
64   i.reset();
65   ASSERT_EQ(nullptr, i.get());
66 }
67
68 TEST(rebind_allocator, sanity_check) {
69   std::allocator<long> alloc;
70
71   auto i = std::allocate_shared<int>(
72     rebind_allocator<int, decltype(alloc)>(alloc), 10
73   );
74   ASSERT_NE(nullptr, i.get());
75   EXPECT_EQ(10, *i);
76   i.reset();
77   ASSERT_EQ(nullptr, i.get());
78
79   auto d = std::allocate_shared<double>(
80     rebind_allocator<double>(alloc), 5.6
81   );
82   ASSERT_NE(nullptr, d.get());
83   EXPECT_EQ(5.6, *d);
84   d.reset();
85   ASSERT_EQ(nullptr, d.get());
86
87   auto s = std::allocate_shared<std::string>(
88     rebind_allocator<std::string>(alloc), "HELLO, WORLD"
89   );
90   ASSERT_NE(nullptr, s.get());
91   EXPECT_EQ("HELLO, WORLD", *s);
92   s.reset();
93   ASSERT_EQ(nullptr, s.get());
94 }
95
96 int main(int argc, char **argv) {
97   FLAGS_logtostderr = true;
98   google::InitGoogleLogging(argv[0]);
99   testing::InitGoogleTest(&argc, argv);
100
101   return RUN_ALL_TESTS();
102 }