1 //===- unittests/Support/StreamingMemoryObjectTest.cpp --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/ADT/STLExtras.h" 11 #include "llvm/ADT/StringRef.h" 12 #include "llvm/Support/StreamingMemoryObject.h" 13 #include "gtest/gtest.h" 14 #include <string.h> 15 16 using namespace llvm; 17 18 namespace { 19 20 class NullDataStreamer : public DataStreamer { 21 size_t GetBytes(unsigned char *Buffer, size_t Length) override { 22 memset(Buffer, 0, Length); 23 return Length; 24 } 25 }; 26 27 class BufferStreamer : public DataStreamer { 28 StringRef Buffer; 29 30 public: 31 BufferStreamer(StringRef Buffer) : Buffer(Buffer) {} 32 size_t GetBytes(unsigned char *OutBuffer, size_t Length) override { 33 if (Length >= Buffer.size()) 34 Length = Buffer.size(); 35 36 std::copy(Buffer.begin(), Buffer.begin() + Length, OutBuffer); 37 Buffer = Buffer.drop_front(Length); 38 return Length; 39 } 40 }; 41 42 TEST(StreamingMemoryObjectTest, isValidAddress) { 43 auto DS = make_unique<NullDataStreamer>(); 44 StreamingMemoryObject O(std::move(DS)); 45 EXPECT_TRUE(O.isValidAddress(32 * 1024)); 46 } 47 48 TEST(StreamingMemoryObjectTest, setKnownObjectSize) { 49 auto DS = make_unique<NullDataStreamer>(); 50 StreamingMemoryObject O(std::move(DS)); 51 uint8_t Buf[32]; 52 EXPECT_EQ(16u, O.readBytes(Buf, 16, 0)); 53 O.setKnownObjectSize(24); 54 EXPECT_EQ(8u, O.readBytes(Buf, 16, 16)); 55 } 56 57 TEST(StreamingMemoryObjectTest, getPointer) { 58 uint8_t InputBuffer[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; 59 StreamingMemoryObject O(make_unique<BufferStreamer>(StringRef( 60 reinterpret_cast<const char *>(InputBuffer), sizeof(InputBuffer)))); 61 62 EXPECT_TRUE(std::equal(InputBuffer + 1, InputBuffer + 2, O.getPointer(1, 2))); 63 EXPECT_TRUE(std::equal(InputBuffer + 3, InputBuffer + 7, O.getPointer(3, 4))); 64 EXPECT_TRUE(std::equal(InputBuffer + 4, InputBuffer + 8, O.getPointer(4, 5))); 65 EXPECT_TRUE(std::equal(InputBuffer, InputBuffer + 8, O.getPointer(0, 20))); 66 } 67 68 } // end namespace 69