2016-03-28 06:55:19 +08:00
|
|
|
//===- unittests/Support/StreamingMemoryObjectTest.cpp --------------------===//
|
2014-11-21 13:15:41 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-06-17 07:29:49 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-03-28 06:55:19 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-11-21 13:15:41 +08:00
|
|
|
#include "llvm/Support/StreamingMemoryObject.h"
|
|
|
|
#include "gtest/gtest.h"
|
2016-04-06 04:19:49 +08:00
|
|
|
#include <cstring>
|
2014-11-21 13:15:41 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2016-03-28 06:55:19 +08:00
|
|
|
|
2014-11-21 13:15:41 +08:00
|
|
|
class NullDataStreamer : public DataStreamer {
|
2016-03-28 06:55:19 +08:00
|
|
|
size_t GetBytes(unsigned char *Buffer, size_t Length) override {
|
|
|
|
memset(Buffer, 0, Length);
|
|
|
|
return Length;
|
2014-11-21 13:15:41 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-28 07:00:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-28 06:55:19 +08:00
|
|
|
TEST(StreamingMemoryObjectTest, isValidAddress) {
|
2015-06-17 07:29:49 +08:00
|
|
|
auto DS = make_unique<NullDataStreamer>();
|
|
|
|
StreamingMemoryObject O(std::move(DS));
|
2014-11-21 13:15:41 +08:00
|
|
|
EXPECT_TRUE(O.isValidAddress(32 * 1024));
|
|
|
|
}
|
2015-05-22 03:40:19 +08:00
|
|
|
|
2016-03-28 06:55:19 +08:00
|
|
|
TEST(StreamingMemoryObjectTest, setKnownObjectSize) {
|
2015-06-17 07:29:49 +08:00
|
|
|
auto DS = make_unique<NullDataStreamer>();
|
|
|
|
StreamingMemoryObject O(std::move(DS));
|
2015-05-22 03:40:19 +08:00
|
|
|
uint8_t Buf[32];
|
2016-03-28 06:55:19 +08:00
|
|
|
EXPECT_EQ(16u, O.readBytes(Buf, 16, 0));
|
2015-05-22 03:40:19 +08:00
|
|
|
O.setKnownObjectSize(24);
|
2016-03-28 06:55:19 +08:00
|
|
|
EXPECT_EQ(8u, O.readBytes(Buf, 16, 16));
|
2015-05-22 03:40:19 +08:00
|
|
|
}
|
2016-03-28 06:55:19 +08:00
|
|
|
|
2016-03-28 07:00:59 +08:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
} // end anonymous namespace
|