ifw-daq  1.0.0
IFW Data Acquisition modules
testKeyword.cpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_fits_test
4  * @copyright 2021 ESO - European Southern Observatory
5  *
6  * @brief Unit tests for json handling.
7  */
8 
9 #include <gtest/gtest.h>
10 #include <gmock/gmock.h>
11 #include <nlohmann/json.hpp>
12 #include <ostream>
13 
14 #include <daq/fits/json.hpp>
15 
16 using namespace daq::fits;
17 
18 /**
19  * Note that test cases must use `TypeParam` and cannot declare templated aliases.
20  */
21 template <typename Type>
22 class TestKeyword : public ::testing::Test {
23 };
24 
25 using Types = ::testing::Types<ValueKeyword, EsoKeyword>;
27 
28 TYPED_TEST(TestKeyword, Construction) {
29  using Keyword = TypeParam;
30 
31  Keyword orig("name", 1.0, "comment");
32  EXPECT_EQ(orig.name, "name");
33  EXPECT_EQ(orig.value, typename Keyword::ValueType(1.0));
34  EXPECT_EQ(orig.comment, "comment");
35 
36  Keyword copy_assign;
37  copy_assign = orig;
38  EXPECT_EQ(orig, copy_assign);
39 
40  Keyword move_assign("TOBE", "replaced");
41  move_assign = std::move(copy_assign);
42  EXPECT_EQ(orig, move_assign);
43 
44  Keyword copy_construct(orig);
45  EXPECT_EQ(orig, copy_construct);
46 
47  Keyword move_construct(std::move(copy_construct));
48  EXPECT_EQ(orig, move_construct);
49 }
50 
51 TYPED_TEST(TestKeyword, OptionalComment) {
52  using Keyword = TypeParam;
53 
54  Keyword orig("name", 1.0, std::nullopt);
55  EXPECT_EQ(orig.comment, std::nullopt);
56 }
57 
58 /**
59  * Test that construction of keyword from a C string is stored as std::string and not a bool,
60  * which is the implicitly converted type for the variant construction otherwise.
61  */
62 TYPED_TEST(TestKeyword, ConstructFromString) {
63  using Keyword = TypeParam;
64 
65  Keyword kw("name", "str", "comment");
66  ASSERT_TRUE(std::holds_alternative<std::string>(kw.value));
67  EXPECT_EQ("str", std::get<std::string>(kw.value));
68 }
69 
70 TYPED_TEST(TestKeyword, Equality) {
71  using Keyword = TypeParam;
72 
73  Keyword lhs("name", 1.0, "comment");
74  Keyword rhs("name", 1.0, "comment");
75 
76  EXPECT_TRUE(lhs == rhs);
77  EXPECT_FALSE(lhs != rhs);
78 
79  rhs.comment = std::nullopt;
80  EXPECT_NE(lhs, rhs);
81 }
82 
83 TYPED_TEST(TestKeyword, Ordering) {
84  using Keyword = TypeParam;
85 
86  Keyword lhs("A", 1.0, "comment");
87  Keyword rhs("B", 1.0, "comment");
88 
89  EXPECT_LT(lhs, rhs);
90  EXPECT_FALSE(rhs < lhs);
91  EXPECT_FALSE(lhs < lhs);
92 }
93 
94 TYPED_TEST(TestKeyword, OstreamOperator) {
95  using Keyword = TypeParam;
96 
97  // Setup
98  using namespace ::testing;
99 
100  // Test
101  {
102  Keyword kw("name", std::string("value"), "comment");
103  std::stringstream ss;
104  ss << kw;
105  EXPECT_EQ(ss.str(), "name='name', value=(str)'value', comment='comment'");
106  }
107  {
108  Keyword kw("name", true, std::nullopt);
109  std::stringstream ss;
110  ss << kw;
111  EXPECT_EQ(ss.str(), "name='name', value=(bool)true, comment=n/a");
112  }
113  {
114  Keyword kw("name", 1234ul, std::nullopt);
115  std::stringstream ss;
116  ss << kw;
117  EXPECT_THAT(ss.str(), "name='name', value=(uint64_t)1234, comment=n/a");
118  }
119  {
120  Keyword kw("name", 1234l, std::nullopt);
121  std::stringstream ss;
122  ss << kw;
123  EXPECT_THAT(ss.str(), "name='name', value=(int64_t)1234, comment=n/a");
124  }
125  {
126  Keyword kw("name", 1.234, std::nullopt);
127  std::stringstream ss;
128  ss << kw;
129  EXPECT_THAT(ss.str(), "name='name', value=(double)1.234, comment=n/a");
130  }
131 }
132 
133 TEST(TestKeywordCombinations, Ordering) {
134  EsoKeyword eso_kw("A", "B");
135  ValueKeyword value_kw("A", "B");
136 
137  EXPECT_LT(value_kw, eso_kw);
138  EXPECT_LT(KeywordVariant(value_kw), KeywordVariant(eso_kw));
139 
140  EXPECT_FALSE(eso_kw < value_kw);
141  EXPECT_FALSE(KeywordVariant(eso_kw) < KeywordVariant(value_kw));
142 }
143 
144 TEST(TestKeywordCombinations, NameEquals) {
145  EsoKeyword eso_kw("A", "B");
146  ValueKeyword value_kw("A", "B");
147 
148  EXPECT_FALSE(NameEquals(value_kw, eso_kw));
149  EXPECT_FALSE(NameEquals(eso_kw, value_kw));
150 
151  EXPECT_TRUE(NameEquals(value_kw, ValueKeyword("A", "NOT B")));
152  EXPECT_TRUE(NameEquals(eso_kw, EsoKeyword("A", "NOT B")));
153 }
154 
155 
157  using namespace daq::fits;
158  using namespace ::testing;
159  // Setup
160 
161  // Initial values to be updated
162  KeywordVector to{EsoKeyword("A", "A1-orig"),
163  ValueKeyword("A", "A2-orig"),
164  ValueKeyword("B", static_cast<uint64_t>(1u)),
165  EsoKeyword("B", "B")};
166 
167  // Overwrite the A keywords and insert the C keywords
168  KeywordVector from{EsoKeyword("A", "A1-updated"),
169  ValueKeyword("A", "A2-updated"),
170  ValueKeyword("C", "C"),
171  EsoKeyword("C", true)};
172 
173  KeywordVector result{EsoKeyword("A", "A1-updated"),
174  ValueKeyword("A", "A2-updated"),
175  ValueKeyword("B", static_cast<uint64_t>(1u)),
176  EsoKeyword("B", "B"),
177  ValueKeyword("C", "C"),
178  EsoKeyword("C", true)};
179  // Run
180  UpdateKeywords(to, from);
181  EXPECT_THAT(to, ContainerEq(result));
182 }
TYPED_TEST
TYPED_TEST(TestKeyword, Construction)
Definition: testKeyword.cpp:28
daq::fits::KeywordVariant
std::variant< ValueKeyword, EsoKeyword > KeywordVariant
The different variants of keywords that are supported.
Definition: keyword.hpp:131
Types
::testing::Types< ValueKeyword, EsoKeyword > Types
Definition: testKeyword.cpp:25
daq::fits::NameEquals
bool NameEquals(KeywordVariant const &lhs, KeywordVariant const &rhs) noexcept
Compare keyword names of keyword of the same type.
Definition: keyword.cpp:73
TYPED_TEST_CASE
TYPED_TEST_CASE(TestKeyword, Types)
daq::fits::UpdateKeywords
void UpdateKeywords(KeywordVector &to, KeywordVector const &from)
Updates a with keywords from b.
Definition: keyword.cpp:120
TEST
TEST(TestKeywordCombinations, Ordering)
Definition: testKeyword.cpp:133
daq::fits
Definition: cfitsio.cpp:14
daq::fits::EsoKeyword
BasicKeyword< EsoKeywordTraits > EsoKeyword
ESO hiearchical keyword.
Definition: keyword.hpp:99
daq::fits::BasicKeyword
A type safe version of FormattedKeyword that consist of the three basic components of a FITS keyword ...
Definition: keyword.hpp:51
TestKeyword
Note that test cases must use TypeParam and cannot declare templated aliases.
Definition: testKeyword.cpp:22
json.hpp
Contains data structure for FITS keywords.
daq::fits::ValueKeyword
BasicKeyword< ValueKeywordTraits > ValueKeyword
Standard FITS value keyword.
Definition: keyword.hpp:92
daq::fits::KeywordVector
std::vector< KeywordVariant > KeywordVector
Vector of keywords.
Definition: keyword.hpp:138