BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
itempool.test.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/tests/testmodel/itempool.test.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "google_test.h"
16 #include "mvvm/model/itempool.h"
17 #include "mvvm/model/sessionitem.h"
18 #include <memory>
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
23 //! Tests of ItemPool and its abilities to register/deregister SessionItem.
24 
25 class ItemPoolTest : public ::testing::Test {
26 public:
28 };
29 
30 ItemPoolTest::~ItemPoolTest() = default;
31 
32 TEST_F(ItemPoolTest, initialState)
33 {
34  std::unique_ptr<ItemPool> pool(new ItemPool);
35  EXPECT_EQ(pool->size(), 0u);
36 }
37 
38 //! Explicit item registrations.
39 
40 TEST_F(ItemPoolTest, registerItem)
41 {
42  std::unique_ptr<ItemPool> pool(new ItemPool);
43  std::unique_ptr<SessionItem> item(new SessionItem);
44 
45  // registering item
46  auto key = pool->register_item(item.get());
47  EXPECT_EQ(pool->size(), 1u);
48  EXPECT_FALSE(key.empty());
49 
50  // checking registered key and item
51  EXPECT_EQ(key, pool->key_for_item(item.get()));
52  EXPECT_EQ(item.get(), pool->item_for_key(key));
53 
54  // checking unexisting registration
55  std::unique_ptr<SessionItem> item2(new SessionItem);
56  EXPECT_EQ(identifier_type(), pool->key_for_item(item2.get()));
57  EXPECT_EQ(nullptr, pool->item_for_key("ABC"));
58 
59  // registering second item
60  auto key2 = pool->register_item(item2.get());
61  EXPECT_EQ(pool->size(), 2u);
62  EXPECT_EQ(key2, pool->key_for_item(item2.get()));
63  EXPECT_FALSE(key == key2);
64 
65  // attempt to register item twice
66  EXPECT_THROW(pool->register_item(item2.get()), std::runtime_error);
67 }
68 
69 //! Explicit item de-registrations.
70 
71 TEST_F(ItemPoolTest, deregisterItem)
72 {
73  std::unique_ptr<ItemPool> pool(new ItemPool);
74  std::unique_ptr<SessionItem> item1(new SessionItem);
75  std::unique_ptr<SessionItem> item2(new SessionItem);
76 
77  auto key1 = pool->register_item(item1.get());
78  auto key2 = pool->register_item(item2.get());
79 
80  EXPECT_EQ(pool->size(), 2u);
81  EXPECT_EQ(item1.get(), pool->item_for_key(key1));
82  EXPECT_EQ(item2.get(), pool->item_for_key(key2));
83 
84  // deregistering item
85  pool->unregister_item(item1.get());
86  EXPECT_EQ(pool->size(), 1u);
87  EXPECT_EQ(nullptr, pool->item_for_key(key1));
88  EXPECT_EQ(item2.get(), pool->item_for_key(key2));
89 
90  // attempt to deregister twice
91  EXPECT_THROW(pool->unregister_item(item1.get()), std::runtime_error);
92 
93  // deregistering last remaining item
94  pool->unregister_item(item2.get());
95  EXPECT_EQ(pool->size(), 0u);
96 }
97 
98 //! Providing custom key.
99 
100 TEST_F(ItemPoolTest, customKey)
101 {
102  std::shared_ptr<ItemPool> pool(new ItemPool);
103  EXPECT_EQ(pool.use_count(), 1l);
104 
105  // explicit item registration
106  const identifier_type id("abc-cde-fgh");
107  auto item = new SessionItem;
108  pool->register_item(item, id);
109 
110  // attempt to reuse key again
111  std::unique_ptr<SessionItem> item2(new SessionItem);
112  EXPECT_THROW(pool->register_item(item2.get(), id), std::runtime_error);
113 
114  delete item;
115 }
Tests of ItemPool and its abilities to register/deregister SessionItem.
Provides registration of SessionItem pointers and their unique identifiers in global memory pool.
Definition: itempool.h:29
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Defines class CLASS?
Defines class CLASS?
TEST_F(ItemPoolTest, initialState)
materialitems.h Collection of materials to populate MaterialModel.
std::string identifier_type
Definition: types.h:22
Defines class CLASS?