MCPcopy Create free account
hub / github.com/apple/coremltools / IsCorrectSubByteData

Function IsCorrectSubByteData

mlmodel/tests/MILBlob/StorageWriterTests.cpp:79–115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77
78template <typename T>
79[[nodiscard]] bool IsCorrectSubByteData(const std::string& filePath, uint64_t offset, Util::Span<const T> expectedSpan)
80{
81 blob_metadata metadata;
82 TestUtil::ReadData<blob_metadata>(filePath, metadata, offset);
83 // sizing this int8 vector with the int4 span size is an overestimation - should be
84 // fine for this test since we need the buffer only anyway
85 std::vector<uint8_t> v(expectedSpan.Size());
86 Util::Span<T> outputSpan((void*)v.data(), expectedSpan.Size());
87 TestUtil::ReadBlobFile(filePath, metadata.offset, outputSpan);
88
89 auto ourBytesPtr = static_cast<const T*>(outputSpan.Data());
90 auto otherBytesPtr = static_cast<const T*>(expectedSpan.Data());
91
92 // scan bytes up to but not including padding
93 std::size_t numBits = outputSpan.Size() * T::SizeInBits;
94 std::size_t remainderBits = numBits % 8;
95
96 std::size_t numFullBytes = numBits / 8;
97 std::size_t numRemainingElements = remainderBits / T::SizeInBits;
98
99 for (size_t i = 0; i < numFullBytes; i++) {
100 if (ourBytesPtr[i] != otherBytesPtr[i]) {
101 return false;
102 }
103 }
104
105 // scan remainder, ignore garbage bits
106 for (size_t i = 0; i < numRemainingElements; i++) {
107 auto mask = T::BitMask << (i * T::SizeInBits);
108 auto ourVal = ourBytesPtr[numFullBytes].data & mask;
109 auto otherVal = otherBytesPtr[numFullBytes].data & mask;
110 if (ourVal != otherVal) {
111 return false;
112 }
113 }
114 return true;
115}
116
117template <typename T>
118[[nodiscard]] bool IsCorrectData(const std::string& filePath, uint64_t offset, Util::Span<const T> expectedSpan)

Callers 1

IsCorrectDataFunction · 0.85

Calls 4

ReadBlobFileFunction · 0.85
SizeMethod · 0.45
dataMethod · 0.45
DataMethod · 0.45

Tested by

no test coverage detected