BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data2ditem.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/data2ditem.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 "mockwidgets.h"
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 using ::testing::_;
24 
25 //! Testing Data1DItem.
26 
27 class Data2DItemTest : public ::testing::Test {
28 public:
30 };
31 
33 
34 //! Initial state.
35 
36 TEST_F(Data2DItemTest, initialState)
37 {
38  Data2DItem item;
39 
40  EXPECT_EQ(item.xAxis(), nullptr);
41  EXPECT_EQ(item.yAxis(), nullptr);
42  EXPECT_EQ(item.content(), std::vector<double>());
43  EXPECT_TRUE(item.hasData());
44  EXPECT_TRUE(item.data<std::vector<double>>().empty());
45 }
46 
47 //! Checking the method ::setAxis.
48 
50 {
51  Data2DItem item;
52 
53  const int nx = 5, ny = 3;
54  item.setAxes(FixedBinAxisItem::create(nx, 0.0, 5.0), FixedBinAxisItem::create(ny, 0.0, 3.0));
55 
56  // checking type of the axis
57  EXPECT_TRUE(item.item<FixedBinAxisItem>(Data2DItem::T_XAXIS) != nullptr);
58  EXPECT_TRUE(item.item<FixedBinAxisItem>(Data2DItem::T_YAXIS) != nullptr);
59 
60  // checking bin values
61  auto values = item.content();
62  EXPECT_EQ(values.size(), nx * ny);
63  EXPECT_EQ(std::accumulate(values.begin(), values.end(), 0), 0.0);
64 }
65 
66 //! Checking the method ::setContent.
67 
68 TEST_F(Data2DItemTest, setContent)
69 {
70  Data2DItem item;
71 
72  // check that it is not possible to set content to uninitialized axis
73  std::vector<double> expected_content = {1.0, 2.0};
74  EXPECT_THROW(item.setContent(expected_content), std::runtime_error);
75 
76  const int nx = 1, ny = 2;
77  item.setAxes(FixedBinAxisItem::create(nx, 0.0, 5.0), FixedBinAxisItem::create(ny, 0.0, 3.0));
78 
79  item.setContent(expected_content);
80  EXPECT_EQ(item.content(), expected_content);
81 }
82 
83 //! Checking the signals when axes changed.
84 
85 TEST_F(Data2DItemTest, checkSignalsOnAxisChange)
86 {
87  SessionModel model;
88  auto item = model.insertItem<Data2DItem>();
89 
90  FixedBinAxisItem::create(3, 0.0, 3.0);
91 
92  MockWidgetForItem widget(item);
93 
94  EXPECT_CALL(widget, onDataChange(item, ItemDataRole::DATA)).Times(1); // values should change
95  EXPECT_CALL(widget, onPropertyChange(_, _)).Times(0);
96  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(0);
97  EXPECT_CALL(widget, onItemInserted(item, _)).Times(2);
98  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
99 
100  // trigger change
101  item->setAxes(FixedBinAxisItem::create(1, 0.0, 5.0), FixedBinAxisItem::create(3, 0.0, 3.0));
102 }
103 
104 //! Checking the signals when content changed.
105 
106 TEST_F(Data2DItemTest, checkSignalsOnContentChange)
107 {
108  SessionModel model;
109  auto item = model.insertItem<Data2DItem>();
110  item->setAxes(FixedBinAxisItem::create(1, 0.0, 5.0), FixedBinAxisItem::create(3, 0.0, 3.0));
111 
112  MockWidgetForItem widget(item);
113 
114  EXPECT_CALL(widget, onDataChange(item, ItemDataRole::DATA)).Times(1); // values should change
115  EXPECT_CALL(widget, onPropertyChange(_, _)).Times(0);
116  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(0);
117  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
118  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
119 
120  // trigger change
121  item->setContent(std::vector<double>{1.0, 2.0, 3.0});
122 }
Defines class CLASS?
Testing Data1DItem.
Mock widget to test ItemMapper functionality.
Definition: mockwidgets.h:36
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
static const std::string T_XAXIS
Definition: data2ditem.h:31
BinnedAxisItem * xAxis() const
Returns x-axis (nullptr if it doesn't exist).
Definition: data2ditem.cpp:51
BinnedAxisItem * yAxis() const
Returns y-axis (nullptr if it doesn't exist).
Definition: data2ditem.cpp:58
static const std::string T_YAXIS
Definition: data2ditem.h:32
void setAxes(std::unique_ptr< BinnedAxisItem > x_axis, std::unique_ptr< BinnedAxisItem > y_axis)
Sets axes and put data points to zero.
Definition: data2ditem.cpp:41
std::vector< double > content() const
Returns 2d vector representing 2d data.
Definition: data2ditem.cpp:73
void setContent(const std::vector< double > &data)
Definition: data2ditem.cpp:63
Item to represent fixed bin axis.
Definition: axisitems.h:75
static std::unique_ptr< FixedBinAxisItem > create(int nbins, double xmin, double xmax)
Definition: axisitems.cpp:81
bool hasData(int role=ItemDataRole::DATA) const
Returns true if item has data on board with given role.
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Defines class CLASS?
TEST_F(Data2DItemTest, initialState)
Initial state.
Defines class CLASS?
Defines class CLASS?
const int DATA
main data role
Definition: mvvm_types.h:30
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?