|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <data_provider/players/EmgPlayer.h> |
| 18 | + |
| 19 | +#include <gtest/gtest.h> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | +#include <stdexcept> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +using namespace projectaria::tools::data_provider; |
| 27 | + |
| 28 | +namespace { |
| 29 | +// Pack sample-major ADC counts into a big-endian (most-significant byte first) blob, the inverse of |
| 30 | +// the layout decodeEmgSamples is expected to unpack. |
| 31 | +std::string packBigEndian(const std::vector<uint16_t>& counts) { |
| 32 | + std::string blob; |
| 33 | + blob.reserve(counts.size() * sizeof(uint16_t)); |
| 34 | + for (const uint16_t value : counts) { |
| 35 | + blob.push_back(static_cast<char>((value >> 8) & 0xFF)); |
| 36 | + blob.push_back(static_cast<char>(value & 0xFF)); |
| 37 | + } |
| 38 | + return blob; |
| 39 | +} |
| 40 | + |
| 41 | +EmgImuSample makeSample(const std::vector<uint16_t>& counts, uint32_t encoding = 0) { |
| 42 | + EmgImuSample sample; |
| 43 | + sample.packedChannelData = packBigEndian(counts); |
| 44 | + sample.encoding = encoding; |
| 45 | + return sample; |
| 46 | +} |
| 47 | +} // namespace |
| 48 | + |
| 49 | +TEST(DecodeEmgSamples, decodesBigEndianSampleMajorBatch) { |
| 50 | + EmgData data; |
| 51 | + data.channelCount = 2; |
| 52 | + data.samplesPerBatch = 2; |
| 53 | + data.bitsPerAdcReading = 16; |
| 54 | + // Two samples, each carrying samplesPerBatch * channelCount counts in sample-major order. |
| 55 | + data.emg.push_back(makeSample({0x0102, 0x0304, 0x0506, 0x0708})); |
| 56 | + data.emg.push_back(makeSample({0x1112, 0x1314, 0x1516, 0x1718})); |
| 57 | + |
| 58 | + const DecodedEmgSamples decoded = decodeEmgSamples(data); |
| 59 | + |
| 60 | + const std::vector<uint16_t> expectedValues{ |
| 61 | + 0x0102, 0x0304, 0x0506, 0x0708, 0x1112, 0x1314, 0x1516, 0x1718}; |
| 62 | + EXPECT_EQ(decoded.values, expectedValues); |
| 63 | + EXPECT_EQ(decoded.numRows, 4u); // samplesPerBatch (2) * number of samples (2) |
| 64 | + EXPECT_EQ(decoded.numChannels, 2u); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(DecodeEmgSamples, skipsMalformedBlobAndKeepsWellFormedSamples) { |
| 68 | + EmgData data; |
| 69 | + data.channelCount = 2; |
| 70 | + data.samplesPerBatch = 2; |
| 71 | + data.bitsPerAdcReading = 16; |
| 72 | + data.emg.push_back(makeSample({0x0102, 0x0304, 0x0506, 0x0708})); |
| 73 | + EmgImuSample malformed; |
| 74 | + malformed.packedChannelData = |
| 75 | + std::string(3, '\0'); // not samplesPerBatch * channelCount * 2 bytes |
| 76 | + data.emg.push_back(malformed); |
| 77 | + |
| 78 | + const DecodedEmgSamples decoded = decodeEmgSamples(data); |
| 79 | + |
| 80 | + const std::vector<uint16_t> expectedValues{0x0102, 0x0304, 0x0506, 0x0708}; |
| 81 | + EXPECT_EQ(decoded.values, expectedValues); |
| 82 | + EXPECT_EQ(decoded.numRows, 2u); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(DecodeEmgSamples, returnsEmptyWhenChannelCountIsZero) { |
| 86 | + EmgData data; |
| 87 | + data.channelCount = 0; |
| 88 | + data.samplesPerBatch = 2; |
| 89 | + data.bitsPerAdcReading = 16; |
| 90 | + |
| 91 | + const DecodedEmgSamples decoded = decodeEmgSamples(data); |
| 92 | + |
| 93 | + EXPECT_TRUE(decoded.values.empty()); |
| 94 | + EXPECT_EQ(decoded.numRows, 0u); |
| 95 | + EXPECT_EQ(decoded.numChannels, 0u); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(DecodeEmgSamples, throwsOnUnsupportedBitDepth) { |
| 99 | + EmgData data; |
| 100 | + data.channelCount = 2; |
| 101 | + data.samplesPerBatch = 2; |
| 102 | + data.bitsPerAdcReading = 12; |
| 103 | + |
| 104 | + EXPECT_THROW(decodeEmgSamples(data), std::invalid_argument); |
| 105 | +} |
| 106 | + |
| 107 | +TEST(DecodeEmgSamples, throwsOnNonZeroEncoding) { |
| 108 | + EmgData data; |
| 109 | + data.channelCount = 2; |
| 110 | + data.samplesPerBatch = 2; |
| 111 | + data.bitsPerAdcReading = 16; |
| 112 | + data.emg.push_back(makeSample({0x0102, 0x0304, 0x0506, 0x0708}, /*encoding=*/1)); |
| 113 | + |
| 114 | + EXPECT_THROW(decodeEmgSamples(data), std::invalid_argument); |
| 115 | +} |
0 commit comments