BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data1ditem.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/data1ditem.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 Data1DItemTest : public ::testing::Test {
28 public:
30 };
31 
33 
34 //! Initial state.
35 
36 TEST_F(Data1DItemTest, initialState)
37 {
38  Data1DItem item;
39 
40  EXPECT_EQ(item.getItem(Data1DItem::T_AXIS), nullptr);
41  EXPECT_EQ(item.binCenters(), std::vector<double>());
42  EXPECT_EQ(item.binValues(), std::vector<double>());
43  EXPECT_EQ(item.binErrors(), std::vector<double>());
44  EXPECT_FALSE(item.hasData());
45 }
46 
47 //! Checking the method ::setFixedBinAxis.
48 
49 TEST_F(Data1DItemTest, setFixedBinAxis)
50 {
51  Data1DItem item;
52 
53  item.setAxis<FixedBinAxisItem>(5, 0.0, 5.0);
54 
55  // check type of the axis
56  EXPECT_TRUE(item.item<FixedBinAxisItem>(Data1DItem::T_AXIS) != nullptr);
57 
58  // check bin centers and values
59  std::vector<double> expected_centers = {0.5, 1.5, 2.5, 3.5, 4.5};
60  EXPECT_EQ(item.binCenters(), expected_centers);
61  std::vector<double> expected_values = std::vector<double>(expected_centers.size(), 0.0);
62  EXPECT_EQ(item.binValues(), expected_values);
63 
64  // setting another axis
65  // for the moment we have disabled possibility to re-create axes to faciltate undo/redo
66  // item.setAxis(FixedBinAxisItem::create(1, 1.0, 2.0));
67  // expected_centers = {1.5};
68  // EXPECT_EQ(item.binCenters(), expected_centers);
69  // expected_values = {0.0};
70  // EXPECT_EQ(item.binValues(), expected_values);
71 }
72 
73 //! Sets fixed bin axis via templated method.
74 
75 TEST_F(Data1DItemTest, setTemplatedFixedBinAxis)
76 {
77  Data1DItem item;
78 
79  auto axis = item.setAxis<FixedBinAxisItem>(5, 0.0, 5.0);
80 
81  // check type of the axis
82  EXPECT_EQ(item.item<FixedBinAxisItem>(Data1DItem::T_AXIS), axis);
83 
84  // check bin centers and values
85  std::vector<double> expected_centers = {0.5, 1.5, 2.5, 3.5, 4.5};
86  EXPECT_EQ(item.binCenters(), expected_centers);
87  std::vector<double> expected_values = std::vector<double>(expected_centers.size(), 0.0);
88  EXPECT_EQ(item.binValues(), expected_values);
89 }
90 
91 //! Sets fixed bin axis via templated method.
92 
93 TEST_F(Data1DItemTest, setTemplatedFixedBinAxisInModelContext)
94 {
95  SessionModel model;
96  auto dataItem = model.insertItem<Data1DItem>();
97 
98  auto axis = dataItem->setAxis<FixedBinAxisItem>(5, 0.0, 5.0);
99 
100  // check type of the axis
101  EXPECT_EQ(dataItem->item<FixedBinAxisItem>(Data1DItem::T_AXIS), axis);
102 
103  // check bin centers and values
104  std::vector<double> expected_centers = {0.5, 1.5, 2.5, 3.5, 4.5};
105  EXPECT_EQ(dataItem->binCenters(), expected_centers);
106  std::vector<double> expected_values = std::vector<double>(expected_centers.size(), 0.0);
107  EXPECT_EQ(dataItem->binValues(), expected_values);
108 }
109 
110 //! Sets fixed bin axis via model context.
111 // FIXME Not clear if this method should be used
112 
113 TEST_F(Data1DItemTest, setFixedBinAxisInModel)
114 {
115  SessionModel model;
116 
117  auto dataItem = model.insertItem<Data1DItem>();
118  model.insertItem<FixedBinAxisItem>(dataItem)->setParameters(5, 0.0, 5.0);
119 
120  // check type of the axis
121  EXPECT_TRUE(dataItem->item<FixedBinAxisItem>(Data1DItem::T_AXIS) != nullptr);
122 
123  // check bin centers and values
124  std::vector<double> expected_centers = {0.5, 1.5, 2.5, 3.5, 4.5};
125  EXPECT_EQ(dataItem->binCenters(), expected_centers);
126  std::vector<double> expected_values = std::vector<double>(expected_centers.size(), 0.0);
127  EXPECT_TRUE(dataItem->binValues().empty());
128 }
129 
130 //! Checking the method ::setValues.
131 
133 {
134  Data1DItem item;
135 
136  // check that it is not possible to set content to uninitialized axis
137  std::vector<double> expected_content = {1.0, 2.0, 3.0};
138  EXPECT_THROW(item.setValues(expected_content), std::runtime_error);
139 
140  item.setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
141  item.setValues(expected_content);
142  EXPECT_EQ(item.binValues(), expected_content);
143 }
144 
145 //! Checking the method ::setErrors.
146 
148 {
149  Data1DItem item;
150 
151  // check that it is not possible to errors to uninitialized axis
152  std::vector<double> expected_errors = {10.0, 20.0, 30.0};
153 
154  EXPECT_THROW(item.setErrors(expected_errors), std::runtime_error);
155 
156  item.setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
157  item.setErrors(expected_errors);
158 
159  EXPECT_EQ(item.binErrors(), expected_errors);
160 }
161 
162 //! Checking the signals when axes changed.
163 
164 TEST_F(Data1DItemTest, checkSignalsOnAxisChange)
165 {
166  SessionModel model;
167  auto item = model.insertItem<Data1DItem>();
168 
169  MockWidgetForItem widget(item);
170 
171  const std::string expected_value_tag{Data1DItem::P_VALUES};
172  const TagRow expected_axis_tagrow{Data1DItem::T_AXIS, 0};
173 
174  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
175  EXPECT_CALL(widget, onPropertyChange(item, expected_value_tag)).Times(1);
176  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(2);
177  EXPECT_CALL(widget, onItemInserted(item, expected_axis_tagrow)).Times(1);
178  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
179 
180  // trigger change
181  item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
182 }
183 
184 //! Checking the signals when bin values changed.
185 
186 TEST_F(Data1DItemTest, checkSignalsOnBinValuesChange)
187 {
188  SessionModel model;
189  auto item = model.insertItem<Data1DItem>();
190  item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
191 
192  MockWidgetForItem widget(item);
193 
194  EXPECT_CALL(widget, onDataChange(_, _)).Times(0);
195  EXPECT_CALL(widget, onPropertyChange(item, Data1DItem::P_VALUES)).Times(1);
196  EXPECT_CALL(widget, onChildPropertyChange(_, _)).Times(0);
197  EXPECT_CALL(widget, onItemInserted(_, _)).Times(0);
198  EXPECT_CALL(widget, onAboutToRemoveItem(_, _)).Times(0);
199 
200  // trigger change
201  item->setValues(std::vector<double>{1.0, 2.0, 3.0});
202 }
Defines class CLASS?
Testing Data1DItem.
Mock widget to test ItemMapper functionality.
Definition: mockwidgets.h:36
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
static const std::string T_AXIS
Definition: data1ditem.h:34
std::vector< double > binValues() const
Returns values stored in bins.
Definition: data1ditem.cpp:76
void setValues(const std::vector< double > &data)
Sets internal data buffer to given data.
Definition: data1ditem.cpp:66
std::vector< double > binCenters() const
Sets axis. Bin content will be set to zero.
Definition: data1ditem.cpp:57
T * setAxis(Args &&... args)
Inserts axis of given type.
Definition: data1ditem.h:56
static const std::string P_VALUES
Definition: data1ditem.h:32
void setErrors(const std::vector< double > &errors)
Sets errors on values in bins.
Definition: data1ditem.cpp:83
std::vector< double > binErrors() const
Returns value errors stored in bins.
Definition: data1ditem.cpp:93
Item to represent fixed bin axis.
Definition: axisitems.h:75
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
bool hasData(int role=ItemDataRole::DATA) const
Returns true if item has data on board with given role.
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
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Defines class CLASS?
TEST_F(Data1DItemTest, initialState)
Initial state.
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?