10#include "gtest/gtest.h"
22TEST(TypedPortsTest, UnwrapTypeAlias) {
27 TypeAliasType channelAlias(
"channelAlias",
"ChannelAlias", &channel);
35TEST(TypedPortsTest, GetUnwrappedAs) {
37 StructType payload(
"payload", {{
"data", &bits}});
39 TypeAliasType nestedAlias(
"nestedAlias",
"NestedAlias", &alias);
41 TypeAliasType channelAlias(
"channelAlias",
"ChannelAlias", &channel);
42 ChannelType outerChannel(
"outerChannel", &channelAlias);
60TEST(TypedPortsTest, VoidTypeCompatibility) {
73TEST(TypedPortsTest, BoolTypeCompatibility) {
86TEST(TypedPortsTest, SignedIntTypeCompatibility) {
136TEST(TypedPortsTest, UnsignedIntTypeCompatibility) {
202 static constexpr std::string_view _ESI_ID =
"MyModule.TestStruct";
207struct DeserializerWithESIID {
208 static constexpr std::string_view _ESI_ID =
"MyModule.DeserializedStruct";
210 class TypeDeserializer
211 :
public QueuedDecodeTypeDeserializer<DeserializerWithESIID> {
213 using Base = QueuedDecodeTypeDeserializer<DeserializerWithESIID>;
217 explicit TypeDeserializer(OutputCallback output)
218 : Base(std::move(output)) {}
221 DecodedOutputs decode(std::unique_ptr<SegmentedMessageData> &msg)
override {
228TEST(TypedPortsTest, ESIIDTypeCompatibility) {
230 StructType matchType(
"MyModule.TestStruct", {});
234 StructType mismatchType(
"OtherModule.OtherStruct", {});
239 UIntType uintWithMatchingID(
"MyModule.TestStruct", 32);
243TEST(TypedPortsTest, NullPortTypeThrows) {
253struct UnknownCppType {
257TEST(TypedPortsTest, FallbackThrows) {
271 MockWritePort(
const Type *type) : WriteChannelPort(type) {}
273 void connect(
const ConnectOptions &opts = {})
override {
277 void disconnect()
override { connected =
false; }
278 bool isConnected()
const override {
return connected; }
280 MessageData lastWritten;
282 bool acceptTryWrites =
true;
285 void writeImpl(
const MessageData &data)
override { lastWritten =
data; }
286 bool tryWriteImpl(
const MessageData &data)
override {
287 if (!acceptTryWrites)
294 bool connected =
false;
297TEST(TypedPortsTest, TypedWritePortConnectThrowsOnMismatch) {
299 MockWritePort mock(&uint32);
304TEST(TypedPortsTest, TypedWritePortConnectSucceeds) {
306 MockWritePort mock(&sint31);
308 EXPECT_NO_THROW(typed.connect());
309 EXPECT_TRUE(typed.isConnected());
312TEST(TypedPortsTest, TypedWritePortRoundTrip) {
314 MockWritePort mock(&sint15);
322 ASSERT_EQ(mock.lastWritten.getSize(), 2u);
325TEST(TypedPortsTest, SignExtensionNonByteAligned) {
331 EXPECT_EQ(wi.
bytes, 1u);
352 EXPECT_EQ(wi.
bytes, 3u);
353 uint8_t wire[3] = {0xFF, 0xFF, 0x3F};
362 uint8_t wire[3] = {0xFF, 0xFF, 0x1F};
365 EXPECT_EQ(val, 0x1FFFFF);
369TEST(TypedPortsTest, TypedWritePortVoid) {
371 MockWritePort mock(&voidType);
373 EXPECT_NO_THROW(typed.connect());
376 ASSERT_EQ(mock.lastWritten.getSize(), 1u);
377 EXPECT_EQ(mock.lastWritten.getData()[0], 0);
387 MockReadPort(
const Type *type) : ReadChannelPort(type) {}
389 void connect(std::function<
bool(MessageData)>,
390 const ConnectOptions & = {})
override {
391 mode = Mode::Callback;
393 void connect(
const ConnectOptions & = {})
override { mode = Mode::Polling; }
395 void read(MessageData &outData)
override { outData = nextResponse; }
396 std::future<MessageData> readAsync()
override {
397 std::promise<MessageData> p;
398 p.set_value(nextResponse);
399 return p.get_future();
402 MessageData nextResponse;
405TEST(TypedPortsTest, TypedReadPortCustomDeserializerVerifiesESIID) {
406 StructType matchType(
"MyModule.DeserializedStruct", {});
407 MockReadPort matching(&matchType);
409 EXPECT_NO_THROW(ok.connect());
411 StructType mismatchType(
"OtherModule.OtherStruct", {});
412 MockReadPort mismatch(&mismatchType);
419 CallbackDrivenMockReadPort(
const Type *type) : ReadChannelPort(type) {}
421 bool deliver(std::unique_ptr<SegmentedMessageData> msg) {
423 pending = std::move(msg);
424 return retryPending();
427 bool retryPending() {
429 throw std::runtime_error(
430 "CallbackDrivenMockReadPort::retryPending with no message");
431 if (!invokeCallback(pending))
437 bool hasPending()
const {
return static_cast<bool>(pending); }
438 size_t numActiveCallbacks()
const {
return activeCallbacks; }
440 size_t deliveryCount = 0;
443 std::unique_ptr<SegmentedMessageData> pending;
446class ThrowOnCopyReadCallback {
448 explicit ThrowOnCopyReadCallback(std::shared_ptr<bool> shouldThrow)
449 : shouldThrow(std::move(shouldThrow)) {}
451 ThrowOnCopyReadCallback(
const ThrowOnCopyReadCallback &other)
452 : shouldThrow(other.shouldThrow) {
454 throw std::runtime_error(
"ThrowOnCopyReadCallback copy failure");
457 ThrowOnCopyReadCallback(ThrowOnCopyReadCallback &&) =
default;
458 ThrowOnCopyReadCallback &operator=(
const ThrowOnCopyReadCallback &) =
default;
459 ThrowOnCopyReadCallback &operator=(ThrowOnCopyReadCallback &&) =
default;
461 bool operator()(std::unique_ptr<SegmentedMessageData> &)
const {
466 std::shared_ptr<bool> shouldThrow;
469static MessageData packUint32Words(std::initializer_list<uint32_t> values) {
470 std::vector<uint8_t> bytes(values.size() *
sizeof(uint32_t));
472 for (uint32_t value : values) {
473 std::memcpy(bytes.data() + offset, &value,
sizeof(value));
474 offset +=
sizeof(value);
479struct BufferedSequence {
480 std::vector<uint32_t> values;
482 class TypeDeserializer
483 :
public QueuedDecodeTypeDeserializer<BufferedSequence> {
485 using Base = QueuedDecodeTypeDeserializer<BufferedSequence>;
489 explicit TypeDeserializer(OutputCallback output)
490 : Base(std::move(output)) {}
493 DecodedOutputs decode(std::unique_ptr<SegmentedMessageData> &msg)
override {
495 const MessageData &flat =
496 detail::getMessageDataRef<BufferedSequence>(*msg, scratch);
497 if (flat.
getSize() %
sizeof(uint32_t) != 0)
498 throw std::runtime_error(
499 "BufferedSequence::TypeDeserializer: truncated word payload");
501 DecodedOutputs decoded;
502 for (
size_t offset = 0; offset < flat.
getSize();
503 offset +=
sizeof(uint32_t)) {
505 std::memcpy(&value, flat.
getBytes() + offset,
sizeof(value));
506 auto sequence = std::make_unique<BufferedSequence>();
507 sequence->values.push_back(value);
508 decoded.push_back(std::move(sequence));
516TEST(TypedPortsTest, TypedReadPortPODBackpressuresAfterOneBufferedOutput) {
518 CallbackDrivenMockReadPort mock(&uint32);
522 typed.setMaxDataQueueMsgs(1);
525 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))));
530 mock.deliver(std::make_unique<MessageData>(packUint32Words({22}))));
531 EXPECT_FALSE(mock.hasPending());
533 mock.deliver(std::make_unique<MessageData>(packUint32Words({33}))));
534 EXPECT_TRUE(mock.hasPending());
536 std::unique_ptr<uint32_t> first = typed.read();
538 EXPECT_EQ(*first, 11u);
539 EXPECT_TRUE(mock.retryPending());
540 EXPECT_FALSE(mock.hasPending());
541 std::unique_ptr<uint32_t> second = typed.read();
543 EXPECT_EQ(*second, 22u);
544 std::unique_ptr<uint32_t> third = typed.read();
546 EXPECT_EQ(*third, 33u);
549TEST(TypedPortsTest, TypedReadPortPODRetriesSameOwnedObjectOnLaterPush) {
551 CallbackDrivenMockReadPort mock(&uint32);
554 const uint32_t *firstObject =
nullptr;
555 size_t callbackAttempts = 0;
557 typed.connect([&](std::unique_ptr<uint32_t> &value) {
560 if (callbackAttempts == 1) {
561 EXPECT_EQ(*value, 11u);
562 firstObject = value.get();
565 if (callbackAttempts == 2) {
566 EXPECT_EQ(*value, 11u);
567 EXPECT_EQ(value.get(), firstObject);
570 EXPECT_EQ(*value, 22u);
575 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))));
576 EXPECT_FALSE(mock.hasPending());
580 mock.deliver(std::make_unique<MessageData>(packUint32Words({22}))));
581 EXPECT_FALSE(mock.hasPending());
582 EXPECT_EQ(callbackAttempts, 3u);
585TEST(TypedPortsTest, TypedReadPortCustomDeserializerPokesBlockedOutput) {
587 CallbackDrivenMockReadPort mock(&uint32);
591 typed.setMaxDataQueueMsgs(1);
594 mock.deliver(std::make_unique<MessageData>(packUint32Words({10, 20}))));
595 EXPECT_EQ(mock.deliveryCount, 1u);
597 std::unique_ptr<BufferedSequence> first = typed.read();
599 ASSERT_EQ(first->values.size(), 1u);
600 EXPECT_EQ(first->values[0], 10u);
602 std::unique_ptr<BufferedSequence> second = typed.read();
604 ASSERT_EQ(second->values.size(), 1u);
605 EXPECT_EQ(second->values[0], 20u);
606 EXPECT_EQ(mock.deliveryCount, 1u);
610 TypedReadPortCustomDeserializerConsumesMultipleFramesPerRawMessage) {
612 CallbackDrivenMockReadPort mock(&uint32);
617 std::future<std::unique_ptr<BufferedSequence>> first = typed.readAsync();
618 std::future<std::unique_ptr<BufferedSequence>> second = typed.readAsync();
619 std::future<std::unique_ptr<BufferedSequence>> third = typed.readAsync();
622 mock.deliver(std::make_unique<MessageData>(packUint32Words({1, 2, 3}))));
624 std::unique_ptr<BufferedSequence> firstValue = first.get();
625 ASSERT_TRUE(firstValue);
626 EXPECT_EQ(firstValue->values[0], 1u);
628 std::unique_ptr<BufferedSequence> secondValue = second.get();
629 ASSERT_TRUE(secondValue);
630 EXPECT_EQ(secondValue->values[0], 2u);
632 std::unique_ptr<BufferedSequence> thirdValue = third.get();
633 ASSERT_TRUE(thirdValue);
634 EXPECT_EQ(thirdValue->values[0], 3u);
638 TypedReadPortCustomDeserializerQueuesMultiplePendingOutputs) {
640 CallbackDrivenMockReadPort mock(&uint32);
644 typed.setMaxDataQueueMsgs(1);
647 mock.deliver(std::make_unique<MessageData>(packUint32Words({7, 8, 9}))));
648 EXPECT_EQ(mock.deliveryCount, 1u);
650 std::unique_ptr<BufferedSequence> first = typed.read();
652 EXPECT_EQ(first->values[0], 7u);
654 std::unique_ptr<BufferedSequence> second = typed.read();
656 EXPECT_EQ(second->values[0], 8u);
658 std::unique_ptr<BufferedSequence> third = typed.read();
660 EXPECT_EQ(third->values[0], 9u);
661 EXPECT_EQ(mock.deliveryCount, 1u);
664struct FragmentedCoord {
668static_assert(
sizeof(FragmentedCoord) == 8,
"Size mismatch");
670static std::array<uint8_t,
sizeof(FragmentedCoord)> packCoordBytes(uint32_t y,
672 FragmentedCoord coord{y, x};
673 std::array<uint8_t,
sizeof(FragmentedCoord)> bytes{};
674 std::memcpy(bytes.data(), &coord,
sizeof(coord));
678struct FragmentedCoordBatch {
679 std::vector<FragmentedCoord> coords;
681 class TypeDeserializer
682 :
public QueuedDecodeTypeDeserializer<FragmentedCoordBatch> {
684 using Base = QueuedDecodeTypeDeserializer<FragmentedCoordBatch>;
688 explicit TypeDeserializer(OutputCallback output)
689 : Base(std::move(output)) {}
692 DecodedOutputs decode(std::unique_ptr<SegmentedMessageData> &msg)
override {
694 const MessageData &flat =
695 detail::getMessageDataRef<FragmentedCoordBatch>(*msg, scratch);
697 DecodedOutputs decoded;
698 const uint8_t *bytes = flat.
getBytes();
700 while (offset < flat.
getSize()) {
701 size_t needed =
sizeof(FragmentedCoord) - partialFrameBytes.size();
702 size_t chunkSize = std::min(needed, flat.
getSize() - offset);
703 partialFrameBytes.insert(partialFrameBytes.end(), bytes + offset,
704 bytes + offset + chunkSize);
707 if (partialFrameBytes.size() !=
sizeof(FragmentedCoord))
710 FragmentedCoord coord;
711 std::memcpy(&coord, partialFrameBytes.data(),
sizeof(coord));
712 partialFrameBytes.clear();
714 auto batch = std::make_unique<FragmentedCoordBatch>();
715 batch->coords.push_back(coord);
716 decoded.push_back(std::move(batch));
723 std::vector<uint8_t> partialFrameBytes;
728 TypedReadPortCustomDeserializerConsumesSplitFramesAcrossRawMessages) {
730 CallbackDrivenMockReadPort mock(&uint32);
735 std::future<std::unique_ptr<FragmentedCoordBatch>> first = typed.readAsync();
736 std::future<std::unique_ptr<FragmentedCoordBatch>> second = typed.readAsync();
738 std::array<uint8_t,
sizeof(FragmentedCoord)> coordA = packCoordBytes(10, 20);
739 std::array<uint8_t,
sizeof(FragmentedCoord)> coordB = packCoordBytes(30, 40);
741 std::vector<uint8_t> firstChunk(coordA.begin(), coordA.begin() + 6);
742 EXPECT_TRUE(mock.deliver(
743 std::make_unique<MessageData>(
MessageData(std::move(firstChunk)))));
745 std::vector<uint8_t> secondChunk;
746 secondChunk.insert(secondChunk.end(), coordA.begin() + 6, coordA.end());
747 secondChunk.insert(secondChunk.end(), coordB.begin(), coordB.end());
748 EXPECT_TRUE(mock.deliver(
749 std::make_unique<MessageData>(
MessageData(std::move(secondChunk)))));
751 std::unique_ptr<FragmentedCoordBatch> firstBatch = first.get();
752 ASSERT_TRUE(firstBatch);
753 ASSERT_EQ(firstBatch->coords.size(), 1u);
754 EXPECT_EQ(firstBatch->coords[0].y, 10u);
755 EXPECT_EQ(firstBatch->coords[0].x, 20u);
757 std::unique_ptr<FragmentedCoordBatch> secondBatch = second.get();
758 ASSERT_TRUE(secondBatch);
759 ASSERT_EQ(secondBatch->coords.size(), 1u);
760 EXPECT_EQ(secondBatch->coords[0].y, 30u);
761 EXPECT_EQ(secondBatch->coords[0].x, 40u);
767TEST(TypedPortsTest, TypedFunctionNullThrowsAtConnect) {
773TEST(TypedPortsTest, TypedFunctionCallBeforeConnectThrows) {
776 EXPECT_THROW(typed.call(0u).get(), std::runtime_error);
779TEST(TypedPortsTest, TypedFunctionDoubleConnectThrows) {
783 ChannelType argChanType(
"channel<si24>", &argInner);
785 ChannelType resultChanType(
"channel<ui15>", &resultInner);
791 BundleType bundleType(
"func_bundle", channels);
793 MockWritePort mockWrite(&argInner);
794 CallbackDrivenMockReadPort mockRead(&resultInner);
797 mockWrite, mockRead);
801 EXPECT_THROW(typed.connect(), std::runtime_error);
805TEST(TypedPortsTest, TypedFunctionConnectVerifiesTypes) {
808 ChannelType argChanType(
"channel<si24>", &argInner);
810 ChannelType resultChanType(
"channel<ui15>", &resultInner);
816 BundleType bundleType(
"func_bundle", channels);
818 MockWritePort mockWrite(&argInner);
819 MockReadPort mockRead(&resultInner);
822 mockWrite, mockRead);
827 EXPECT_NO_THROW(typed.connect());
831TEST(TypedPortsTest, TypedFunctionConnectRejectsArgMismatch) {
833 ChannelType argChanType(
"channel<ui24>", &argInner);
835 ChannelType resultChanType(
"channel<ui15>", &resultInner);
841 BundleType bundleType(
"func_bundle", channels);
843 MockWritePort mockWrite(&argInner);
844 MockReadPort mockRead(&resultInner);
847 mockWrite, mockRead);
855TEST(TypedPortsTest, TypedFunctionCallRoundTrip) {
857 ChannelType argChanType(
"channel<si24>", &argInner);
859 ChannelType resultChanType(
"channel<ui15>", &resultInner);
865 BundleType bundleType(
"func_bundle", channels);
867 MockWritePort mockWrite(&argInner);
868 CallbackDrivenMockReadPort mockRead(&resultInner);
871 mockWrite, mockRead);
877 std::future<uint16_t> f = typed.call(arg);
880 uint16_t expected = 42;
881 EXPECT_TRUE(mockRead.deliver(std::make_unique<MessageData>(
MessageData(
882 reinterpret_cast<const uint8_t *
>(&expected),
sizeof(expected)))));
883 uint16_t result = f.get();
884 EXPECT_EQ(result, 42);
887 ASSERT_EQ(mockWrite.lastWritten.getSize(), 3u);
905 std::vector<uint32_t> items;
907 size_t numSegments()
const override {
return 2; }
908 Segment segment(
size_t idx)
const override {
910 return {
reinterpret_cast<const uint8_t *
>(&header),
sizeof(Header)};
911 return {
reinterpret_cast<const uint8_t *
>(items.data()),
912 items.size() *
sizeof(uint32_t)};
916TEST(TypedPortsTest, ReadChannelPortSegmentedCallbackRetriesSameMessageObject) {
918 CallbackDrivenMockReadPort mock(&uint32);
920 uint32_t expected = 0x12345678;
921 const uint8_t *firstBytes =
nullptr;
922 size_t callbackCalls = 0;
924 mock.connect([&](std::unique_ptr<SegmentedMessageData> &msg) ->
bool {
926 EXPECT_EQ(msg->numSegments(), 1u);
928 auto *flat =
dynamic_cast<MessageData *
>(msg.get());
929 EXPECT_NE(flat,
nullptr);
932 EXPECT_EQ(*flat->
as<uint32_t>(), expected);
934 if (callbackCalls == 1)
937 EXPECT_EQ(flat->
getBytes(), firstBytes);
939 return callbackCalls == 2;
944 EXPECT_TRUE(mock.hasPending());
945 EXPECT_TRUE(mock.retryPending());
946 EXPECT_FALSE(mock.hasPending());
947 EXPECT_EQ(callbackCalls, 2u);
950TEST(TypedPortsTest, ReadChannelPortFlatCallbackFlattensSegmentedMessageRetry) {
952 CallbackDrivenMockReadPort mock(&uint32);
955 input.header.a = 0x12345678;
956 input.header.b = 0xABCD;
957 input.items = {1, 2, 3};
960 size_t callbackCalls = 0;
964 return callbackCalls == 2;
967 EXPECT_FALSE(mock.deliver(std::make_unique<TestSegmented>(input)));
968 EXPECT_TRUE(mock.hasPending());
969 EXPECT_TRUE(mock.retryPending());
970 EXPECT_FALSE(mock.hasPending());
971 EXPECT_EQ(callbackCalls, 2u);
974TEST(TypedPortsTest, ReadChannelPortPollingRetriesFlattenedSegmentedMessage) {
976 CallbackDrivenMockReadPort mock(&uint32);
978 mock.setMaxDataQueueMsgs(1);
980 TestSegmented firstInput;
981 firstInput.header.a = 0xAAAA5555;
982 firstInput.header.b = 0x1357;
983 firstInput.items = {10};
984 MessageData firstExpected = firstInput.toMessageData();
986 TestSegmented secondInput;
987 secondInput.header.a = 0xDEADBEEF;
988 secondInput.header.b = 0x2468;
989 secondInput.items = {20, 30};
990 MessageData secondExpected = secondInput.toMessageData();
992 EXPECT_TRUE(mock.deliver(std::make_unique<TestSegmented>(firstInput)));
993 EXPECT_FALSE(mock.deliver(std::make_unique<TestSegmented>(secondInput)));
994 EXPECT_TRUE(mock.hasPending());
1000 EXPECT_TRUE(mock.retryPending());
1001 EXPECT_FALSE(mock.hasPending());
1004 mock.read(secondOut);
1008TEST(TypedPortsTest, ReadChannelPortPollingReadAsyncThrowsWhenDisconnected) {
1010 CallbackDrivenMockReadPort mock(&uint32);
1012 EXPECT_THROW(mock.readAsync(), std::runtime_error);
1015 std::future<MessageData> pending = mock.readAsync();
1018 EXPECT_EQ(pending.wait_for(std::chrono::milliseconds(0)),
1019 std::future_status::ready);
1020 EXPECT_THROW(pending.get(), std::future_error);
1021 EXPECT_THROW(mock.readAsync(), std::runtime_error);
1025 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))));
1028 EXPECT_EQ(*out.as<uint32_t>(), 11u);
1031TEST(TypedPortsTest, ReadChannelPortPollingConnectRejectsReconnect) {
1033 CallbackDrivenMockReadPort mock(&uint32);
1036 EXPECT_THROW(mock.connect(), std::runtime_error);
1039TEST(TypedPortsTest, ReadChannelPortDisconnectRevokesCallback) {
1041 CallbackDrivenMockReadPort mock(&uint32);
1047 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))));
1048 EXPECT_TRUE(mock.hasPending());
1051 EXPECT_TRUE(mock.retryPending());
1052 EXPECT_FALSE(mock.hasPending());
1056 EXPECT_EQ(*out.
as<uint32_t>(), 11u);
1059TEST(TypedPortsTest, TypedReadPortDestructorDisconnectsRawPort) {
1061 CallbackDrivenMockReadPort mock(&uint32);
1066 EXPECT_TRUE(mock.isConnected());
1069 EXPECT_FALSE(mock.isConnected());
1071 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))));
1072 EXPECT_TRUE(mock.hasPending());
1075 reconnected.connect();
1076 EXPECT_TRUE(mock.retryPending());
1077 EXPECT_FALSE(mock.hasPending());
1079 std::unique_ptr<uint32_t> out = reconnected.read();
1081 EXPECT_EQ(*out, 11u);
1085 ReadChannelPortInvokeCallbackMaintainsCountOnCallbackCopyFailure) {
1087 CallbackDrivenMockReadPort mock(&uint32);
1088 auto shouldThrow = std::make_shared<bool>(
false);
1090 mock.connect(ThrowOnCopyReadCallback(shouldThrow));
1091 *shouldThrow =
true;
1094 mock.deliver(std::make_unique<MessageData>(packUint32Words({11}))),
1095 std::runtime_error);
1096 EXPECT_EQ(mock.numActiveCallbacks(), 0u);
1099TEST(TypedPortsTest, TypedWritePortSegmentedMessageData) {
1102 MockWritePort mock(&uint32);
1108 msg.header.a = 0x12345678;
1109 msg.header.b = 0xABCD;
1110 msg.items = {1, 2, 3};
1116 EXPECT_EQ(mock.lastWritten.getSize(), 18u);
1119 const uint8_t *bytes = mock.lastWritten.getBytes();
1122 std::memcpy(&gotA, bytes, 4);
1123 std::memcpy(&gotB, bytes + 4, 2);
1124 EXPECT_EQ(gotA, 0x12345678u);
1125 EXPECT_EQ(gotB, 0xABCDu);
1128 uint32_t gotItems[3];
1129 std::memcpy(gotItems, bytes + 6, 12);
1130 EXPECT_EQ(gotItems[0], 1u);
1131 EXPECT_EQ(gotItems[1], 2u);
1132 EXPECT_EQ(gotItems[2], 3u);
1135TEST(TypedPortsTest, WriteChannelPortTryWriteSegmented) {
1137 MockWritePort mock(&uint32);
1140 auto input = std::make_unique<TestSegmented>();
1141 input->header.a = 0x12345678;
1142 input->header.b = 0xABCD;
1143 input->items = {1, 2, 3};
1146 std::unique_ptr<SegmentedMessageData> msg = std::move(input);
1149 mock.acceptTryWrites =
false;
1150 EXPECT_FALSE(mock.tryWrite(msg));
1151 ASSERT_NE(msg,
nullptr);
1152 EXPECT_EQ(msg.get(), raw);
1153 EXPECT_EQ(mock.lastWritten.getSize(), 0u);
1156 mock.acceptTryWrites =
true;
1157 EXPECT_TRUE(mock.tryWrite(msg));
1158 EXPECT_EQ(msg,
nullptr);
1159 EXPECT_EQ(mock.lastWritten.getData(), expected.
getData());
1161 std::unique_ptr<SegmentedMessageData> null;
1162 EXPECT_THROW(mock.tryWrite(null), std::runtime_error);
1165TEST(TypedPortsTest, TypedWritePortTryWriteSegmentedUniquePtr) {
1167 MockWritePort mock(&uint32);
1172 auto msg = std::make_unique<TestSegmented>();
1175 msg->items = {100, 200};
1177 TestSegmented *raw = msg.get();
1179 mock.acceptTryWrites =
false;
1180 EXPECT_FALSE(typed.tryWrite(msg));
1181 ASSERT_NE(msg,
nullptr);
1182 EXPECT_EQ(msg.get(), raw);
1183 EXPECT_EQ(msg->items.size(), 2u);
1185 mock.acceptTryWrites =
true;
1186 EXPECT_TRUE(typed.tryWrite(msg));
1187 EXPECT_EQ(msg,
nullptr);
1188 EXPECT_EQ(mock.lastWritten.getData(), expected.
getData());
1190 std::unique_ptr<TestSegmented> null;
1191 EXPECT_THROW(typed.tryWrite(null), std::runtime_error);
1194TEST(TypedPortsTest, TypedWritePortTryWriteNonSegmentedUniquePtr) {
1196 MockWritePort mock(&uint32);
1201 auto value = std::make_unique<uint32_t>(0xDEADBEEF);
1203 mock.acceptTryWrites =
false;
1204 EXPECT_FALSE(typed.tryWrite(value));
1205 ASSERT_NE(value,
nullptr);
1206 EXPECT_EQ(*value, 0xDEADBEEFu);
1208 mock.acceptTryWrites =
true;
1209 EXPECT_TRUE(typed.tryWrite(value));
1210 EXPECT_EQ(value,
nullptr);
1211 ASSERT_EQ(mock.lastWritten.getSize(), 4u);
1212 EXPECT_EQ(*mock.lastWritten.as<uint32_t>(), 0xDEADBEEFu);
1215TEST(TypedPortsTest, TypedWritePortSegmentedNoTypeCheck) {
1219 MockWritePort mock(&sint8);
1222 EXPECT_NO_THROW(typed.connect());
1231 EXPECT_EQ(mock.lastWritten.getSize(), 10u);
1234TEST(TypedPortsTest, TypedFunctionSegmentedArg) {
1238 ChannelType argChanType(
"channel<ui32>", &argInner);
1240 ChannelType resultChanType(
"channel<ui16>", &resultInner);
1246 BundleType bundleType(
"func_bundle", channels);
1248 MockWritePort mockWrite(&argInner);
1249 CallbackDrivenMockReadPort mockRead(&resultInner);
1252 mockWrite, mockRead);
1258 arg.header.a = 0xDEAD;
1259 arg.header.b = 0xBE;
1260 arg.items = {10, 20};
1262 std::future<uint16_t> f = typed.call(arg);
1264 uint16_t expected = 99;
1265 EXPECT_TRUE(mockRead.deliver(std::make_unique<MessageData>(
MessageData(
1266 reinterpret_cast<const uint8_t *
>(&expected),
sizeof(expected)))));
1267 uint16_t result = f.get();
1268 EXPECT_EQ(result, 99u);
1271 EXPECT_EQ(mockWrite.lastWritten.getSize(), 14u);
1273 const uint8_t *bytes = mockWrite.lastWritten.getBytes();
1275 std::memcpy(&gotA, bytes, 4);
1276 EXPECT_EQ(gotA, 0xDEADu);
1278 uint32_t gotItem0, gotItem1;
1279 std::memcpy(&gotItem0, bytes + 6, 4);
1280 std::memcpy(&gotItem1, bytes + 10, 4);
1281 EXPECT_EQ(gotItem0, 10u);
1282 EXPECT_EQ(gotItem1, 20u);
1288struct UnrecognizedCppType {
1293TEST(TypedPortsTest, VerifyTypeCompatibilityThrowsForUnsupportedType) {
1319 class TypeDeserializer {
1321 using OutputCallback = detail::TypedReadOwnedCallback<OneWord>;
1323 explicit TypeDeserializer(OutputCallback output)
1324 : output(std::move(output)) {}
1326 bool push(std::unique_ptr<SegmentedMessageData> &msg) {
1329 if (!output(pending))
1334 MessageData scratch;
1335 const MessageData &flat =
1336 detail::getMessageDataRef<OneWord>(*msg, scratch);
1337 if (flat.
getSize() !=
sizeof(uint32_t))
1338 throw std::runtime_error(
"OneWord: bad size");
1339 pending = std::make_unique<OneWord>();
1340 std::memcpy(&pending->value, flat.
getBytes(),
sizeof(uint32_t));
1344 if (output(pending))
1350 if (pending && output(pending)) {
1358 OutputCallback output;
1359 std::unique_ptr<OneWord> pending;
1363TEST(TypedPortsTest, TypedFunctionCustomDeserializerSingleFrame) {
1365 ChannelType argChanType(
"channel<ui32>", &argInner);
1367 ChannelType resultChanType(
"channel<ui32>", &resultInner);
1373 BundleType bundleType(
"func_bundle", channels);
1375 MockWritePort mockWrite(&argInner);
1376 CallbackDrivenMockReadPort mockRead(&resultInner);
1379 mockWrite, mockRead);
1384 std::future<OneWord> f = typed.call(0u);
1387 uint32_t expected = 0xCAFE;
1388 EXPECT_TRUE(mockRead.deliver(std::make_unique<MessageData>(
MessageData(
1389 reinterpret_cast<const uint8_t *
>(&expected),
sizeof(expected)))));
1391 OneWord result = f.get();
1392 EXPECT_EQ(result.value, expected);
1397TEST(TypedPortsTest, TypedFunctionCustomDeserializerReadsMultipleFrames) {
1403 ChannelType argChanType(
"channel<ui32>", &argInner);
1405 ChannelType resultChanType(
"channel<ui32>", &resultInner);
1411 BundleType bundleType(
"func_bundle", channels);
1413 MockWritePort mockWrite(&argInner);
1414 CallbackDrivenMockReadPort mockRead(&resultInner);
1417 mockWrite, mockRead);
1422 std::future<FragmentedCoordBatch> f = typed.call(0u);
1425 std::array<uint8_t,
sizeof(FragmentedCoord)> coordBytes =
1426 packCoordBytes(55, 77);
1427 std::vector<uint8_t> firstChunk(coordBytes.begin(), coordBytes.begin() + 6);
1428 std::vector<uint8_t> secondChunk(coordBytes.begin() + 6, coordBytes.end());
1430 mockRead.deliver(std::make_unique<MessageData>(std::move(firstChunk))));
1432 mockRead.deliver(std::make_unique<MessageData>(std::move(secondChunk))));
1434 FragmentedCoordBatch batch = f.get();
1435 ASSERT_EQ(batch.coords.size(), 1u);
1436 EXPECT_EQ(batch.coords[0].y, 55u);
1437 EXPECT_EQ(batch.coords[0].x, 77u);
1442TEST(TypedPortsTest, TypedFunctionCustomDeserializerSkipsResultTypeCheck) {
1447 ChannelType argChanType(
"channel<ui32>", &argInner);
1450 ChannelType resultChanType(
"channel<Anything>", &resultInner);
1456 BundleType bundleType(
"func_bundle", channels);
1458 MockWritePort mockWrite(&argInner);
1459 CallbackDrivenMockReadPort mockRead(&resultInner);
1462 mockWrite, mockRead);
1465 EXPECT_NO_THROW(typed.connect());
1470TEST(TypedPortsTest, TypedFunctionPipelinedCallsGetOutOfOrder) {
1477 ChannelType argChanType(
"channel<ui32>", &argInner);
1479 ChannelType resultChanType(
"channel<ui32>", &resultInner);
1485 BundleType bundleType(
"func_bundle", channels);
1487 MockWritePort mockWrite(&argInner);
1488 CallbackDrivenMockReadPort mockRead(&resultInner);
1491 mockWrite, mockRead);
1498 std::future<FragmentedCoordBatch> f1 = typed.call(0u);
1499 std::future<FragmentedCoordBatch> f2 = typed.call(0u);
1501 auto deliverCoord = [&](uint32_t y, uint32_t x) {
1502 auto bytes = packCoordBytes(y, x);
1503 std::vector<uint8_t> first(bytes.begin(), bytes.begin() + 6);
1504 std::vector<uint8_t> second(bytes.begin() + 6, bytes.end());
1506 mockRead.deliver(std::make_unique<MessageData>(std::move(first))));
1508 mockRead.deliver(std::make_unique<MessageData>(std::move(second))));
1510 deliverCoord(11, 22);
1511 deliverCoord(33, 44);
1514 FragmentedCoordBatch r2 = f2.get();
1515 ASSERT_EQ(r2.coords.size(), 1u);
1516 EXPECT_EQ(r2.coords[0].y, 33u);
1517 EXPECT_EQ(r2.coords[0].x, 44u);
1519 FragmentedCoordBatch r1 = f1.get();
1520 ASSERT_EQ(r1.coords.size(), 1u);
1521 EXPECT_EQ(r1.coords[0].y, 11u);
1522 EXPECT_EQ(r1.coords[0].x, 22u);
Arrays have a compile time specified (static) size and an element type.
Bits are just an array of bits.
Bundles represent a collection of channels.
std::vector< std::tuple< std::string, Direction, const Type * > > ChannelVector
Channels are the basic communication primitives.
A concrete flat message backed by a single vector of bytes.
const std::vector< uint8_t > & getData() const
Get the data as a vector of bytes.
const uint8_t * getBytes() const
const T * as() const
Cast to a type.
size_t getSize() const
Get the size of the data in bytes.
static MessageData from(T &t)
Cast from a type to its raw bytes.
detail::TypedReadOwnedCallback< DeserializerWithESIID > OutputCallback
std::vector< std::unique_ptr< DeserializerWithESIID > > DecodedOutputs
A ChannelPort which reads data from the accelerator.
Abstract multi-segment message.
Structs are an ordered collection of fields, each with a name and a type.
Type aliases provide a named type which forwards to an inner type.
Root class of the ESI type system.
Strongly typed wrapper around a raw read channel.
The "void" type is a special type which can be used to represent no type.
A ChannelPort which sends data to the accelerator.
static Function * get(AppID id, BundleType *type, WriteChannelPort &arg, ReadChannelPort &result)
connect(destination, source)
void verifyTypeCompatibility(const Type *portType)
WireInfo getWireInfo(const Type *portType)
const T * getUnwrappedAs(const Type *type)
Unwrap aliases and channels, then cast the payload to T.
T fromMessageData(const MessageData &msg, WireInfo wi)
Unpack a MessageData into a C++ integral value with the given wire info.
const Type * unwrapTypeAlias(const Type *type)
Unwrap TypeAliasType (possibly recursively) to get the underlying type.
Compute the wire byte count for a port type.