1 #include <marnav/nmea/nmea.hpp>
2 #include <marnav/nmea/checksum.hpp>
3 #include <marnav/nmea/detail.hpp>
4 #include <marnav/nmea/sentence.hpp>
5 #include <marnav/nmea/vwr.hpp>
6 #include <gtest/gtest.h>
10 using namespace marnav
;
12 class Test_nmea
: public ::testing::Test
16 static constexpr std::size_t num_supported_sentences
= 91u;
18 TEST_F(Test_nmea
, checksum_to_string
)
20 EXPECT_STREQ("00", nmea::checksum_to_string(0x00).c_str());
21 EXPECT_STREQ("01", nmea::checksum_to_string(0x01).c_str());
22 EXPECT_STREQ("02", nmea::checksum_to_string(0x02).c_str());
23 EXPECT_STREQ("04", nmea::checksum_to_string(0x04).c_str());
24 EXPECT_STREQ("08", nmea::checksum_to_string(0x08).c_str());
25 EXPECT_STREQ("10", nmea::checksum_to_string(0x10).c_str());
26 EXPECT_STREQ("20", nmea::checksum_to_string(0x20).c_str());
27 EXPECT_STREQ("40", nmea::checksum_to_string(0x40).c_str());
28 EXPECT_STREQ("80", nmea::checksum_to_string(0x80).c_str());
29 EXPECT_STREQ("5A", nmea::checksum_to_string(0x5a).c_str());
30 EXPECT_STREQ("A5", nmea::checksum_to_string(0xa5).c_str());
31 EXPECT_STREQ("FF", nmea::checksum_to_string(0xff).c_str());
34 TEST_F(Test_nmea
, make_sentence_empty_string
)
36 EXPECT_ANY_THROW(nmea::make_sentence(""));
39 TEST_F(Test_nmea
, make_sentence_no_start_token
)
41 EXPECT_ANY_THROW(nmea::make_sentence("1234567890"));
44 TEST_F(Test_nmea
, make_sentence_to_short
)
46 EXPECT_ANY_THROW(nmea::make_sentence("12"));
49 TEST_F(Test_nmea
, make_sentence_no_end_token
)
51 EXPECT_ANY_THROW(nmea::make_sentence("$GPMTW,,1E"));
54 TEST_F(Test_nmea
, make_sentence_invalid_checksum
)
56 EXPECT_ANY_THROW(nmea::make_sentence("$GPMTW,,*1E"));
59 TEST_F(Test_nmea
, make_sentence_minimal_possible_sentence
)
62 nmea::make_sentence("$IIYYY*59");
63 } catch (std::exception
& e
) {
64 EXPECT_STREQ("unknown regular tag in address: [IIYYY]", e
.what());
68 TEST_F(Test_nmea
, make_sentence_vendor_extension
)
71 nmea::make_sentence("$PXXX*08");
72 } catch (std::exception
& e
) {
73 EXPECT_STREQ("unknown or malformed address field: [PXXX]", e
.what());
77 TEST_F(Test_nmea
, make_sentence_invalid_checksum_what
)
80 nmea::make_sentence("$GPMTW,,*1E");
81 } catch (nmea::checksum_error
& e
) {
82 EXPECT_STREQ("checksum error (actual:59, expected:1E)", e
.what());
86 TEST_F(Test_nmea
, make_sentence_unknown_sentence
)
88 EXPECT_ANY_THROW(nmea::make_sentence("$XX???,1,2,3*23"));
91 TEST_F(Test_nmea
, make_sentence_ignoring_checksum_ignored
)
93 const std::string raw
= "$IIVWR,084.0,R,10.4,N,5.4,M,19.3,K*4A";
95 auto s
= nmea::make_sentence(raw
, nmea::checksum_handling::ignore
);
97 EXPECT_TRUE(s
!= nullptr);
98 EXPECT_TRUE(s
->tag() == "VWR");
101 TEST_F(Test_nmea
, make_sentence_ignoring_checksum_equal
)
103 const std::string raw_1
= "$IIVWR,084.0,R,10.4,N,5.4,M,19.3,K*4A";
104 const std::string raw_2
= "$IIVWR,084.0,R,10.4,N,5.4,M,19.3,K*00";
106 auto s1
= nmea::make_sentence(raw_1
, nmea::checksum_handling::check
);
107 auto s2
= nmea::make_sentence(raw_2
, nmea::checksum_handling::ignore
);
109 ASSERT_TRUE(s1
!= nullptr);
110 ASSERT_TRUE(s2
!= nullptr);
111 EXPECT_TRUE(s1
->tag() == s2
->tag());
114 TEST_F(Test_nmea
, get_supported_sentences_str
)
116 auto v
= nmea::get_supported_sentences_str();
118 EXPECT_EQ(num_supported_sentences
, v
.size());
121 TEST_F(Test_nmea
, get_supported_sentences_id
)
123 auto v
= nmea::get_supported_sentences_id();
125 EXPECT_EQ(num_supported_sentences
, v
.size());
128 TEST_F(Test_nmea
, tag_to_id
)
130 auto id
= nmea::tag_to_id("BOD");
132 EXPECT_EQ(nmea::sentence_id::BOD
, id
);
135 TEST_F(Test_nmea
, tag_to_id_invalid_tag
)
137 EXPECT_ANY_THROW(nmea::tag_to_id("???"));
140 TEST_F(Test_nmea
, to_string_sentence_id
)
142 auto tag
= nmea::to_string(nmea::sentence_id::BOD
);
144 EXPECT_STREQ("BOD", tag
.c_str());
147 TEST_F(Test_nmea
, to_string_sentence_id_invalid_id
)
149 EXPECT_ANY_THROW(nmea::to_string(static_cast<nmea::sentence_id
>(-1)));
152 TEST_F(Test_nmea
, extract_id
)
154 EXPECT_EQ(nmea::sentence_id::BOD
, nmea::extract_id("$GPBOD,,T,,M,,*47"));
155 EXPECT_ANY_THROW(nmea::extract_id(""));
156 EXPECT_ANY_THROW(nmea::extract_id("$"));
157 EXPECT_ANY_THROW(nmea::extract_id("XGPBOD,,T,,M,,*47"));
158 EXPECT_NO_THROW(nmea::extract_id("$GPBOD,,T,,M,,*XX"));
159 EXPECT_NO_THROW(nmea::extract_id("!GPBOD,,T,,M,,*XX"));
160 EXPECT_NO_THROW(nmea::extract_id("$GPBOD,"));
161 EXPECT_ANY_THROW(nmea::extract_id("$GPBOD"));
162 EXPECT_ANY_THROW(nmea::extract_id("$TMVTD"));
163 EXPECT_ANY_THROW(nmea::extract_id("$TMVTD,,T,,M,,*47"));
166 TEST_F(Test_nmea
, extract_id_tag_block
)
168 static const std::string t
= "\\g:1-2-73874,n:157036,s:r003669945,c:1241544035*4A\\!AIVDM,"
169 "1,1,,B,15N4cJ`005Jrek0H@9n`DW5608EP,0*13";
171 EXPECT_NO_THROW(nmea::extract_id(t
));
172 EXPECT_EQ(nmea::sentence_id::VDM
, nmea::extract_id(t
));
175 TEST_F(Test_nmea
, make_sentence_tag_block
)
177 static const std::string t
= "\\g:1-2-73874,n:157036,s:r003669945,c:1241544035*4A\\!AIVDM,"
178 "1,1,,B,15N4cJ`005Jrek0H@9n`DW5608EP,0*13";
180 const auto s
= nmea::make_sentence(t
);
182 EXPECT_TRUE(s
!= nullptr);
183 EXPECT_TRUE(!s
->get_tag_block().empty());
185 "g:1-2-73874,n:157036,s:r003669945,c:1241544035*4A", s
->get_tag_block().c_str());
188 TEST_F(Test_nmea
, parse_address_empty_string
)
190 EXPECT_ANY_THROW(nmea::detail::parse_address(std::string
{}));
193 TEST_F(Test_nmea
, parse_address_regular
)
195 EXPECT_NO_THROW(nmea::detail::parse_address("GPAAM"));
198 TEST_F(Test_nmea
, parse_address_proprietary
)
201 const auto result
= nmea::detail::parse_address("PGRMZ");
202 EXPECT_EQ(nmea::talker::none
, std::get
<0>(result
));
203 EXPECT_STREQ("PGRMZ", std::get
<1>(result
).c_str());
206 const auto result
= nmea::detail::parse_address("PGRMM");
207 EXPECT_EQ(nmea::talker::none
, std::get
<0>(result
));
208 EXPECT_STREQ("PGRMM", std::get
<1>(result
).c_str());
211 EXPECT_ANY_THROW(nmea::detail::parse_address("TMVTD"));
214 EXPECT_ANY_THROW(nmea::detail::parse_address("XXYYY"));
218 TEST_F(Test_nmea
, parse_address_proprietary_unknown
)
221 nmea::detail::parse_address("PFOOBAR");
222 } catch (std::invalid_argument
& e
) {
223 EXPECT_STREQ("unknown or malformed address field: [PFOOBAR]", e
.what());
227 TEST_F(Test_nmea
, parse_address_regular_invalid_length
)
230 nmea::detail::parse_address("GPFOOBAR");
231 } catch (std::invalid_argument
& e
) {
232 EXPECT_STREQ("unknown or malformed address field: [GPFOOBAR]", e
.what());
236 nmea::detail::parse_address("GPRMZFOO");
237 } catch (std::invalid_argument
& e
) {
238 EXPECT_STREQ("unknown or malformed address field: [GPRMZFOO]", e
.what());
242 TEST_F(Test_nmea
, parse_address_regular_unknown
)
245 nmea::detail::parse_address("GPXXX");
246 } catch (std::invalid_argument
& e
) {
247 EXPECT_STREQ("unknown regular tag in address: [GPXXX]", e
.what());