| 1 |
#include <pbcopper/data/FrameEncoders.h> |
| 2 |
#include <pbcopper/data/Frames.h> |
| 3 |
|
| 4 |
#include <cstdint> |
| 5 |
|
| 6 |
#include <numeric> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include <gtest/gtest.h> |
| 10 |
|
| 11 |
using Frames = PacBio::Data::Frames; |
| 12 |
|
| 13 |
// clang-format off |
| 14 |
namespace FramesTests { |
| 15 |
|
| 16 |
const std::vector<uint16_t> RawFrames{ |
| 17 |
0, 8, 140, 0, 0, 7, 4, 0, 85, 2, 1, 3, 2, 10, 1, 20, 47, 10, 9, 60, 20, |
| 18 |
3, 12, 5, 13, 165, 6, 14, 22, 12, 2, 4, 9, 218, 27, 3, 15, 2, 17, 2, 45, 24, |
| 19 |
89, 10, 7, 1, 11, 15, 0, 7, 0, 28, 17, 12, 6, 10, 37, 0, 12, 52, 0, 7, 1, |
| 20 |
14, 3, 26, 12, 0, 20, 17, 2, 13, 2, 9, 13, 7, 15, 29, 3, 6, 2, 1, 28, 10, |
| 21 |
3, 14, 7, 1, 22, 1, 6, 6, 0, 19, 31, 6, 2, 14, 0, 0, 1000, 947, 948}; |
| 22 |
|
| 23 |
const std::vector<uint8_t> EncodedFrames{ |
| 24 |
0, 8, 102, 0, 0, 7, 4, 0, 75, 2, 1, 3, 2, 10, 1, 20, 47, 10, 9, 60, 20, |
| 25 |
3, 12, 5, 13, 115, 6, 14, 22, 12, 2, 4, 9, 135, 27, 3, 15, 2, 17, 2, 45, 24, |
| 26 |
77, 10, 7, 1, 11, 15, 0, 7, 0, 28, 17, 12, 6, 10, 37, 0, 12, 52, 0, 7, 1, |
| 27 |
14, 3, 26, 12, 0, 20, 17, 2, 13, 2, 9, 13, 7, 15, 29, 3, 6, 2, 1, 28, 10, |
| 28 |
3, 14, 7, 1, 22, 1, 6, 6, 0, 19, 31, 6, 2, 14, 0, 0, 255, 254, 255}; |
| 29 |
|
| 30 |
} // namespace FramesTests |
| 31 |
// clang-format on |
| 32 |
|
| 33 |
TEST(Data_Frames, default_is_empty) |
| 34 |
{ |
| 35 |
const Frames f; |
| 36 |
ASSERT_TRUE(f.Data().empty()); |
| 37 |
} |
| 38 |
|
| 39 |
TEST(Data_Frames, can_construct_from_raw_frames) |
| 40 |
{ |
| 41 |
const Frames f{FramesTests::RawFrames}; |
| 42 |
const auto d = f.Data(); |
| 43 |
ASSERT_EQ(FramesTests::RawFrames, d); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(Data_Frames, can_encode_frames_from_raw_data) |
| 47 |
{ |
| 48 |
const Frames f{FramesTests::RawFrames}; |
| 49 |
const auto e = f.Encode(); |
| 50 |
ASSERT_EQ(FramesTests::EncodedFrames, e); |
| 51 |
} |
| 52 |
|
| 53 |
TEST(Data_FrameEncoder, can_default_initialize) |
| 54 |
{ |
| 55 |
// make sure icpc (legacy) compilation works with this construct... sigh... |
| 56 |
struct TestInitialize |
| 57 |
{ |
| 58 |
PacBio::Data::FrameEncoder v1 = PacBio::Data::V1FrameEncoder{}; |
| 59 |
PacBio::Data::FrameEncoder v2 = PacBio::Data::V1FrameEncoder{}; |
| 60 |
}; |
| 61 |
EXPECT_TRUE(true); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(Data_FrameEncoder, decode_throws_if_out_of_range) |
| 65 |
{ |
| 66 |
PacBio::Data::V2FrameEncoder v2{3, 3}; |
| 67 |
EXPECT_THROW({ auto x = v2.Decode({64}); }, std::runtime_error); |
| 68 |
EXPECT_NO_THROW({ auto x = v2.Decode({0}); }); |
| 69 |
EXPECT_NO_THROW({ auto x = v2.Decode({1}); }); |
| 70 |
EXPECT_NO_THROW({ auto x = v2.Decode({63}); }); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(Data_FrameEncoder, v2_encoder_produces_expected_values) |
| 74 |
{ |
| 75 |
std::vector<uint16_t> input(240); |
| 76 |
std::iota(input.begin(), input.end(), 0); |
| 77 |
|
| 78 |
// clang-format off |
| 79 |
const std::vector<uint8_t> expectedEncoded{ |
| 80 |
// 0 - 15 |
| 81 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 82 |
// 16 - 31 |
| 83 |
16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, |
| 84 |
// 32 - 47 |
| 85 |
24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, |
| 86 |
// 48 - 63 |
| 87 |
32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35, |
| 88 |
// 64 - 79 |
| 89 |
36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39, |
| 90 |
// 80 - 95 |
| 91 |
40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43, |
| 92 |
// 96 - 111 |
| 93 |
44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47, |
| 94 |
// 112 - 127 |
| 95 |
48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, |
| 96 |
// 128 - 143 |
| 97 |
50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51, 51, 51, |
| 98 |
// 144 - 159 |
| 99 |
52, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 53, |
| 100 |
// 160 - 175 |
| 101 |
54, 54, 54, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 55, 55, 55, |
| 102 |
// 176 - 191 |
| 103 |
56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, |
| 104 |
// 192 - 207 |
| 105 |
58, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59, 59, 59, |
| 106 |
// 208 - 223 |
| 107 |
60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, 61, 61, |
| 108 |
// 224 - 239 |
| 109 |
62, 62, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 63, 63, 63, |
| 110 |
}; |
| 111 |
// clang-format on |
| 112 |
|
| 113 |
PacBio::Data::V2FrameEncoder v2{2, 4}; |
| 114 |
const auto encoded = v2.Encode(input); |
| 115 |
EXPECT_EQ(encoded.size(), expectedEncoded.size()); |
| 116 |
EXPECT_EQ(encoded, expectedEncoded); |
| 117 |
} |
| 118 |
|
| 119 |
TEST(Data_FrameEncoder, v2_encoder_clamps_to_max) |
| 120 |
{ |
| 121 |
const std::vector<uint16_t> outOfRange{240, 256, 400}; |
| 122 |
const std::vector<uint8_t> clamped{63, 63, 63}; |
| 123 |
|
| 124 |
PacBio::Data::V2FrameEncoder v2{2, 4}; |
| 125 |
const auto encoded = v2.Encode(outOfRange); |
| 126 |
EXPECT_EQ(clamped, encoded); |
| 127 |
} |
| 128 |
|
| 129 |
TEST(Data_FrameEncoder, v2_decoder_produces_expected_values) |
| 130 |
{ |
| 131 |
std::vector<uint8_t> input(64); |
| 132 |
std::iota(input.begin(), input.end(), 0); |
| 133 |
|
| 134 |
// clang-format off |
| 135 |
const std::vector<uint16_t> expectedDecoded{ |
| 136 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 137 |
16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, |
| 138 |
48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 108, |
| 139 |
112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, |
| 140 |
}; |
| 141 |
// clang-format on |
| 142 |
|
| 143 |
PacBio::Data::V2FrameEncoder v2{2, 4}; |
| 144 |
const auto decoded = v2.Decode(input); |
| 145 |
EXPECT_EQ(decoded.Data(), expectedDecoded); |
| 146 |
|
| 147 |
// out-of-range |
| 148 |
std::vector<uint8_t> outOfRange{64, 65, 66}; |
| 149 |
EXPECT_THROW(v2.Decode(outOfRange), std::runtime_error); |
| 150 |
} |