BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
containerutils.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/containerutils.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/sessionitem.h"
18 #include <complex>
19 
20 using namespace ModelView;
21 
22 //! Tests of container utils.
23 
24 class ContainerUtilsTest : public ::testing::Test {
25 public:
27 };
28 
30 
32 {
33  EXPECT_FALSE(Utils::is_unique_ptr<int>::value);
34  EXPECT_TRUE(Utils::is_unique_ptr<std::unique_ptr<int>>::value);
35 }
36 
38 {
39  // searching in vector of integers
40  std::vector<int> vv{1, 7, 5};
41  EXPECT_EQ(Utils::IndexOfItem(vv, 1), 0);
42  EXPECT_EQ(Utils::IndexOfItem(vv, 10), -1);
43  EXPECT_EQ(Utils::IndexOfItem(vv, 5), 2);
44  EXPECT_EQ(Utils::IndexOfItem(vv.begin(), vv.end(), 7), 1);
45 
46  // searching in vector of SessionItem's
47  std::vector<SessionItem*> items{new SessionItem, new SessionItem, new SessionItem};
48  SessionItem other;
49  EXPECT_EQ(Utils::IndexOfItem(items, items[0]), 0);
50  EXPECT_EQ(Utils::IndexOfItem(items, items[1]), 1);
51  EXPECT_EQ(Utils::IndexOfItem(items, items[2]), 2);
52  EXPECT_EQ(Utils::IndexOfItem(items, &other), -1);
53  for (auto x : items)
54  delete x;
55 
56  // searching in vector of unique_ptr
57  std::vector<std::unique_ptr<SessionItem>> unique_items;
58  unique_items.emplace_back(std::make_unique<SessionItem>());
59  unique_items.emplace_back(std::make_unique<SessionItem>());
60  EXPECT_EQ(Utils::IndexOfItem(unique_items, unique_items[0].get()), 0);
61  EXPECT_EQ(Utils::IndexOfItem(unique_items, unique_items[1].get()), 1);
62  EXPECT_EQ(Utils::IndexOfItem(unique_items, &other), -1);
63 }
64 
66 {
67  std::vector<std::complex<double>> data = {{1.0, 10.0}, {2.0, 20.0}};
68  EXPECT_EQ(Utils::Real(data), (std::vector<double>{1.0, 2.0}));
69  EXPECT_EQ(Utils::Imag(data), (std::vector<double>{10.0, 20.0}));
70 }
71 
73 {
74  std::vector<int> data = {1, 42, 1, 6, 43, 6};
75  EXPECT_EQ(Utils::UniqueWithOrder(data), (std::vector<int>{1, 42, 6, 43}));
76 }
77 
79 {
80  std::vector<int> data = {1, 42, 1, 6, 43, 6};
81  EXPECT_TRUE(Utils::Contains(data, 42));
82  EXPECT_FALSE(Utils::Contains(data, 99));
83 }
Tests of container utils.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Defines class CLASS?
TEST_F(ContainerUtilsTest, isUniquePtr)
Defines class CLASS?
bool Contains(const A &container, const B &element)
Returns true if container contains a given element.
std::vector< double > Real(const C &container)
Returns vector with real part of complex numbers.
C UniqueWithOrder(const C &container)
Returns copy of container with all duplicated elements filtered our. The order is preserved.
int IndexOfItem(It begin, It end, const T &item)
Returns index corresponding to the first occurance of the item in the container.
std::vector< double > Imag(const C &container)
Returns vector with imag part of complex numbers.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?