]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_allocate_unique.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2020 Red Hat
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "common/allocate_unique.h"
18 #include <gtest/gtest.h>
22 // allocation events recorded by logging_allocator
25 bool allocated
; // true for allocate(), false for deallocate()
27 using event_log
= std::vector
<event
>;
30 struct logging_allocator
{
35 explicit logging_allocator(event_log
*log
) : log(log
) {}
36 logging_allocator(const logging_allocator
& other
) : log(other
.log
) {}
39 logging_allocator(const logging_allocator
<U
>& other
) : log(other
.log
) {}
41 T
* allocate(size_t n
, const void* hint
=0)
43 auto p
= std::allocator
<T
>{}.allocate(n
, hint
);
44 log
->emplace_back(event
{n
* sizeof(T
), true});
47 void deallocate(T
* p
, size_t n
)
49 std::allocator
<T
>{}.deallocate(p
, n
);
51 log
->emplace_back(event
{n
* sizeof(T
), false});
56 } // anonymous namespace
60 TEST(AllocateUnique
, Allocate
)
63 auto alloc
= logging_allocator
<char>{&log
};
65 auto p
= allocate_unique
<char>(alloc
, 'a');
66 static_assert(std::is_same_v
<decltype(p
),
67 std::unique_ptr
<char, deallocator
<logging_allocator
<char>>>>);
69 ASSERT_EQ(1, log
.size());
70 EXPECT_EQ(1, log
.front().size
);
71 EXPECT_EQ(true, log
.front().allocated
);
73 ASSERT_EQ(2, log
.size());
74 EXPECT_EQ(1, log
.back().size
);
75 EXPECT_EQ(false, log
.back().allocated
);
78 TEST(AllocateUnique
, RebindAllocate
)
81 auto alloc
= logging_allocator
<char>{&log
};
83 auto p
= allocate_unique
<std::string
>(alloc
, "a");
84 static_assert(std::is_same_v
<decltype(p
),
85 std::unique_ptr
<std::string
,
86 deallocator
<logging_allocator
<std::string
>>>>);
88 ASSERT_EQ(1, log
.size());
89 EXPECT_EQ(sizeof(std::string
), log
.front().size
);
90 EXPECT_EQ(true, log
.front().allocated
);
92 ASSERT_EQ(2, log
.size());
93 EXPECT_EQ(sizeof(std::string
), log
.back().size
);
94 EXPECT_EQ(false, log
.back().allocated
);