summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/StreamingMemoryObjectTest.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-03-27 23:00:59 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2016-03-27 23:00:59 +0000
commit456c9968e51cc015f803cca1063703fbbc6fe958 (patch)
treee3784174127e00deb1f71b70b3503e70047f2ff6 /llvm/unittests/Support/StreamingMemoryObjectTest.cpp
parent6648a0817ef664d75374b303c1f4abb350576f31 (diff)
downloadbcm5719-llvm-456c9968e51cc015f803cca1063703fbbc6fe958.tar.gz
bcm5719-llvm-456c9968e51cc015f803cca1063703fbbc6fe958.zip
Support: Implement StreamingMemoryObject::getPointer
The implementation is fairly obvious. This is preparation for using some blobs in bitcode. For clarity (and perhaps future-proofing?), I moved the call to JumpToBit in BitstreamCursor::readRecord ahead of calling MemoryObject::getPointer, since JumpToBit can theoretically (a) read bytes, which (b) invalidates the blob pointer. This isn't strictly necessary the two memory objects we have: - The return of RawMemoryObject::getPointer is valid until the memory object is destroyed. - StreamingMemoryObject::getPointer is valid until the next chunk is read from the stream. Since the JumpToBit call is only going ahead to a word boundary, we'll never load another chunk. However, reordering makes it clear by inspection that the blob returned by BitstreamCursor::readRecord will be valid. I added some tests for StreamingMemoryObject::getPointer and BitstreamCursor::readRecord. llvm-svn: 264549
Diffstat (limited to 'llvm/unittests/Support/StreamingMemoryObjectTest.cpp')
-rw-r--r--llvm/unittests/Support/StreamingMemoryObjectTest.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/llvm/unittests/Support/StreamingMemoryObjectTest.cpp b/llvm/unittests/Support/StreamingMemoryObjectTest.cpp
index 261f2144392..836dfa9084f 100644
--- a/llvm/unittests/Support/StreamingMemoryObjectTest.cpp
+++ b/llvm/unittests/Support/StreamingMemoryObjectTest.cpp
@@ -24,6 +24,21 @@ class NullDataStreamer : public DataStreamer {
}
};
+class BufferStreamer : public DataStreamer {
+ StringRef Buffer;
+
+public:
+ BufferStreamer(StringRef Buffer) : Buffer(Buffer) {}
+ size_t GetBytes(unsigned char *OutBuffer, size_t Length) override {
+ if (Length >= Buffer.size())
+ Length = Buffer.size();
+
+ std::copy(Buffer.begin(), Buffer.begin() + Length, OutBuffer);
+ Buffer = Buffer.drop_front(Length);
+ return Length;
+ }
+};
+
TEST(StreamingMemoryObjectTest, isValidAddress) {
auto DS = make_unique<NullDataStreamer>();
StreamingMemoryObject O(std::move(DS));
@@ -39,4 +54,15 @@ TEST(StreamingMemoryObjectTest, setKnownObjectSize) {
EXPECT_EQ(8u, O.readBytes(Buf, 16, 16));
}
+TEST(StreamingMemoryObjectTest, getPointer) {
+ uint8_t InputBuffer[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
+ StreamingMemoryObject O(make_unique<BufferStreamer>(StringRef(
+ reinterpret_cast<const char *>(InputBuffer), sizeof(InputBuffer))));
+
+ EXPECT_TRUE(std::equal(InputBuffer + 1, InputBuffer + 2, O.getPointer(1, 2)));
+ EXPECT_TRUE(std::equal(InputBuffer + 3, InputBuffer + 7, O.getPointer(3, 4)));
+ EXPECT_TRUE(std::equal(InputBuffer + 4, InputBuffer + 8, O.getPointer(4, 5)));
+ EXPECT_TRUE(std::equal(InputBuffer, InputBuffer + 8, O.getPointer(0, 20)));
+}
+
} // end namespace
OpenPOWER on IntegriCloud