1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 "gtest/gtest.h"
18 
19 #include "berberis/base/forever_pool.h"
20 
21 namespace berberis {
22 
23 namespace {
24 
TEST(ForeverPool,Smoke)25 TEST(ForeverPool, Smoke) {
26   // ForeverPool maintains free lists by type.
27   // Define unique type to test memory reusage.
28   // ATTENTION: assume multiple instances of this test don't run in parallel!
29   struct Foo {
30     char c;
31   };
32 
33   Foo* p1 = ForeverPool<Foo>::Alloc();
34   ASSERT_TRUE(p1);
35 
36   Foo* p2 = ForeverPool<Foo>::Alloc();
37   ASSERT_TRUE(p2);
38   EXPECT_NE(p1, p2);
39 
40   ForeverPool<Foo>::Free(p1);
41 
42   // Expect memory is reused.
43   Foo* p3 = ForeverPool<Foo>::Alloc();
44   EXPECT_EQ(p1, p3);
45 
46   ForeverPool<Foo>::Free(p2);
47   ForeverPool<Foo>::Free(p3);
48 }
49 
50 }  // namespace
51 
52 }  // namespace berberis
53