1 //===- StorageUniquerTest.cpp - StorageUniquer Tests ----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Support/StorageUniquer.h"
10 #include "gmock/gmock.h"
15 /// Simple storage class used for testing.
16 template <typename ConcreteT
, typename
... Args
>
17 struct SimpleStorage
: public StorageUniquer::BaseStorage
{
18 using Base
= SimpleStorage
<ConcreteT
, Args
...>;
19 using KeyTy
= std::tuple
<Args
...>;
21 SimpleStorage(KeyTy key
) : key(key
) {}
23 /// Get an instance of this storage instance.
24 template <typename
... ParamsT
>
25 static ConcreteT
*get(StorageUniquer
&uniquer
, ParamsT
&&...params
) {
26 return uniquer
.get
<ConcreteT
>(
27 /*initFn=*/{}, std::make_tuple(std::forward
<ParamsT
>(params
)...));
30 /// Construct an instance with the given storage allocator.
31 static ConcreteT
*construct(StorageUniquer::StorageAllocator
&alloc
,
33 return new (alloc
.allocate
<ConcreteT
>())
34 ConcreteT(std::forward
<KeyTy
>(key
));
36 bool operator==(const KeyTy
&key
) const { return this->key
== key
; }
42 TEST(StorageUniquerTest
, NonTrivialDestructor
) {
43 struct NonTrivialStorage
: public SimpleStorage
<NonTrivialStorage
, bool *> {
45 ~NonTrivialStorage() {
46 bool *wasDestructed
= std::get
<0>(key
);
47 *wasDestructed
= true;
51 // Verify that the storage instance destructor was properly called.
52 bool wasDestructed
= false;
54 StorageUniquer uniquer
;
55 uniquer
.registerParametricStorageType
<NonTrivialStorage
>();
56 NonTrivialStorage::get(uniquer
, &wasDestructed
);
59 EXPECT_TRUE(wasDestructed
);