| 1 | // Test parsing message 16 - assigned mode command. |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | #include "ais.h" |
| 7 | |
| 8 | namespace libais { |
| 9 | namespace { |
| 10 | |
| 11 | std::unique_ptr<Ais16> Init(const string &nmea_string) { |
| 12 | const string body(GetBody(nmea_string)); |
| 13 | const int pad = GetPad(nmea_string); |
| 14 | |
| 15 | // TODO(schwehr): Switch to c++14 make_unique. |
| 16 | std::unique_ptr<Ais16> msg(new Ais16(body.c_str(), pad)); |
| 17 | if (!msg || msg->had_error()) { |
| 18 | return nullptr; |
| 19 | } |
| 20 | return msg; |
| 21 | } |
| 22 | |
| 23 | void Validate( |
| 24 | const Ais16 *msg, |
| 25 | const int repeat_indicator, |
| 26 | const int mmsi, |
| 27 | const int spare, |
| 28 | const int dest_mmsi_a, |
| 29 | const int offset_a, |
| 30 | const int inc_a, |
| 31 | const int dest_mmsi_b, |
| 32 | const int offset_b, |
| 33 | const int inc_b, |
| 34 | const int spare2) { |
| 35 | ASSERT_NE(nullptr, msg); |
| 36 | EXPECT_FALSE(msg->had_error()); |
| 37 | |
| 38 | ASSERT_EQ(16, msg->message_id); |
| 39 | EXPECT_EQ(repeat_indicator, msg->repeat_indicator); |
| 40 | EXPECT_EQ(mmsi, msg->mmsi); |
| 41 | EXPECT_EQ(spare, msg->spare); |
| 42 | EXPECT_EQ(dest_mmsi_a, msg->dest_mmsi_a); |
| 43 | EXPECT_EQ(offset_a, msg->offset_a); |
| 44 | EXPECT_EQ(inc_a, msg->inc_a); |
| 45 | EXPECT_EQ(dest_mmsi_b, msg->dest_mmsi_b); |
| 46 | EXPECT_EQ(offset_b, msg->offset_b); |
| 47 | EXPECT_EQ(inc_b, msg->inc_b); |
| 48 | EXPECT_EQ(spare2, msg->spare2); |
| 49 | } |
| 50 | |
| 51 | TEST(Ais16Test, DecodeAnything) { |
| 52 | // !SAVDO,1,1,,B,@03OwnQ9RgLP3h0000000000,0*32,b003669978,1426173689 |
| 53 | std::unique_ptr<Ais16> msg = Init( |
| 54 | "!SAVDO,1,1,,B,@03OwnQ9RgLP3h0000000000,0*32"); |
| 55 | |
| 56 | Validate( |
| 57 | msg.get(), 0, 3669978, 0, |
| 58 | 308461000, 60, 0, // Destination A. |
| 59 | 0, 0, 00, // Destination B. |
| 60 | -1); |
| 61 | } |
| 62 | |
| 63 | TEST(Ais16Test, BitsLeftOver) { |
| 64 | std::unique_ptr<Ais16> msg(new Ais16("@fffffhfffffffff", 4)); |
| 65 | EXPECT_TRUE(msg->had_error()); |
| 66 | EXPECT_EQ(AIS_ERR_BAD_BIT_COUNT, msg->get_error()); |
| 67 | } |
| 68 | |
| 69 | TEST(Ais16Test, InvalidButCommon168Bits) { |
| 70 | // !AIVDM,1,1,,B,@bQBNdhP010Fh<LMb;:GLOvJP4@d,0*7F |
| 71 | std::unique_ptr<Ais16> msg(new Ais16("@bQBNdhP010Fh<LMb;:GLOvJP4@d", 0)); |
| 72 | Validate(msg.get(), 2, 705994419, 0, 134218757, 2819, 113, 916638301, 3199, |
| 73 | 922, -1); |
| 74 | } |
| 75 | |
| 76 | } // namespace |
| 77 | } // namespace libais |