[libc] Make more of the libc unit testing llvm independent

(WIP, hopefully I'll add more to this patch before submitting)

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D91665
This commit is contained in:
Michael Jones
2020-11-17 23:47:19 +00:00
parent 3e18fb3390
commit 8a4ee3550b
5 changed files with 30 additions and 20 deletions

View File

@@ -7,17 +7,19 @@
//===----------------------------------------------------------------------===//
#include "FDReader.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cassert>
#include <cstring>
#include <iostream>
#include <unistd.h>
namespace __llvm_libc {
namespace testutils {
FDReader::FDReader() {
if (::pipe(pipefd))
llvm::report_fatal_error("pipe(2) failed");
if (::pipe(pipefd)) {
std::cerr << "pipe(2) failed";
abort();
}
}
FDReader::~FDReader() {
@@ -26,15 +28,25 @@ FDReader::~FDReader() {
}
bool FDReader::matchWritten(const char *str) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> bufOrErr =
llvm::MemoryBuffer::getOpenFile(pipefd[0], "<pipe>",
/* FileSize (irrelevant) */ 0);
if (!bufOrErr) {
assert(0 && "Error reading from pipe");
return false;
::close(pipefd[1]);
constexpr ssize_t ChunkSize = 4096 * 4;
char Buffer[ChunkSize];
std::string PipeStr;
std::string InputStr(str);
for (int BytesRead; (BytesRead = ::read(pipefd[0], Buffer, ChunkSize));) {
if (BytesRead > 0) {
PipeStr.insert(PipeStr.size(), Buffer, BytesRead);
} else {
assert(0 && "Error reading from pipe");
return false;
}
}
const llvm::MemoryBuffer &buf = **bufOrErr;
return !std::strncmp(buf.getBufferStart(), str, buf.getBufferSize());
return PipeStr == InputStr;
}
} // namespace testutils