refactor: mock filesystem in ocloc ult pt.1

Mocked IO operations in ./ocloc_tests application

Mocked gtest stdout capture in ocloc tests

Related-To: NEO-14084
Signed-off-by: Marcel Skierkowski <marcel.skierkowski@intel.com>
This commit is contained in:
Marcel Skierkowski
2025-04-08 14:26:04 +00:00
committed by Compute-Runtime-Automation
parent 679d3a2be5
commit b75fbe8e2c
24 changed files with 2136 additions and 1197 deletions

View File

@@ -10,6 +10,7 @@
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/test/common/helpers/gtest_helpers.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/test_files.h"
#include "shared/test/common/helpers/variable_backup.h"
@@ -493,7 +494,6 @@ TEST(DecoderTests, GivenProperStructWhenReadingStructFieldsThenFieldsVectorGetsP
TEST(DecoderTests, GivenProperPatchListFileWhenParsingTokensThenFileIsParsedCorrectly) {
MockDecoder decoder;
decoder.pathToPatch = clFiles;
decoder.parseTokens();
EXPECT_EQ(static_cast<uint32_t>(28), (decoder.programHeader.size));
@@ -532,31 +532,12 @@ TEST(DecoderTests, GivenProperPatchListFileWhenParsingTokensThenFileIsParsedCorr
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.kernelHeader.fields[8].size));
EXPECT_EQ("KernelUnpaddedSize", (decoder.kernelHeader.fields[8].name));
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[2]->size));
EXPECT_EQ("PATCH_TOKEN_STATE_SIP", (decoder.patchTokens[2]->name));
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[2]->fields[0].size));
EXPECT_EQ("SystemKernelOffset", (decoder.patchTokens[2]->fields[0].name));
EXPECT_EQ(static_cast<uint8_t>(12), decoder.patchTokens[5]->size);
EXPECT_EQ("PATCH_TOKEN_SAMPLER_STATE_ARRAY", decoder.patchTokens[5]->name);
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[0].size));
EXPECT_EQ("Offset", (decoder.patchTokens[5]->fields[0].name));
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[1].size));
EXPECT_EQ("Count", (decoder.patchTokens[5]->fields[1].name));
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[2].size));
EXPECT_EQ("BorderColorOffset", (decoder.patchTokens[5]->fields[2].name));
EXPECT_EQ(static_cast<uint8_t>(8), decoder.patchTokens[42]->size);
EXPECT_EQ("PATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO", decoder.patchTokens[42]->name);
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[42]->fields[0].size));
EXPECT_EQ("ConstantBufferIndex", (decoder.patchTokens[42]->fields[0].name));
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[42]->fields[1].size));
EXPECT_EQ("InlineDataSize", (decoder.patchTokens[42]->fields[1].name));
EXPECT_EQ(static_cast<uint8_t>(4), decoder.patchTokens[19]->size);
EXPECT_EQ("PATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD", decoder.patchTokens[19]->name);
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[19]->fields[0].size));
EXPECT_EQ("InterfaceDescriptorDataOffset", (decoder.patchTokens[19]->fields[0].name));
}
TEST(DecoderTests, WhenPathToPatchTokensNotProvidedThenUseDefaults) {
@@ -618,7 +599,6 @@ TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBin
std::stringstream ptmFile;
MockDecoder decoder;
decoder.pathToPatch = clFiles;
decoder.pathToDump = "non_existing_folder/";
decoder.parseTokens();
@@ -645,9 +625,10 @@ TEST(DecoderTests, givenBinaryWithKernelBinaryHeaderWhenAtLeastOneOfTheKernelSiz
std::stringstream ptmFile;
MockDecoder decoder{false};
decoder.pathToPatch = clFiles;
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
decoder.pathToDump = "non_existing_folder/";
decoder.parseTokens();
decoder.mockArgHelper->messagePrinter.setSuppressMessages(false);
std::string binaryString = binarySS.str();
std::vector<unsigned char> binary(binaryString.begin(), binaryString.end());
@@ -696,7 +677,6 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa
std::vector<char> binary(binaryString.begin(), binaryString.end());
std::stringstream ptmFile;
MockDecoder decoder;
decoder.pathToPatch = clFiles;
decoder.pathToDump = "non_existing_folder/";
decoder.parseTokens();
@@ -704,7 +684,7 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa
int retVal = decoder.processBinary(ptr, binary.size(), ptmFile);
EXPECT_EQ(0, retVal);
std::string expectedOutput = "ProgramBinaryHeader:\n\t4 Magic 1229870147\n\t4 Version 0\n\t4 Device 0\n\t4 GPUPointerSizeInBytes 0\n\t4 NumberOfKernels 1\n\t4 SteppingId 0\n\t4 PatchListSize 30\nPATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO:\n\t4 Token 42\n\t4 Size 16\n\t4 ConstantBufferIndex 0\n\t4 InlineDataSize 14\n\tHex 0 1 2 3 4 5 6 7 8 9 a b c d\nKernel #0\nKernelBinaryHeader:\n\t4 CheckSum 4294967295\n\t8 ShaderHashCode 18446744073709551615\n\t4 KernelNameSize 14\n\t4 PatchListSize 12\n\t4 KernelHeapSize 0\n\t4 GeneralStateHeapSize 0\n\t4 DynamicStateHeapSize 0\n\t4 SurfaceStateHeapSize 0\n\t4 KernelUnpaddedSize 0\n\tKernelName ExampleKernel\nPATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD:\n\t4 Token 19\n\t4 Size 12\n\t4 InterfaceDescriptorDataOffset 0\n";
std::string expectedOutput = "ProgramBinaryHeader:\n\t4 Magic 1229870147\n\t4 Version 0\n\t4 Device 0\n\t4 GPUPointerSizeInBytes 0\n\t4 NumberOfKernels 1\n\t4 SteppingId 0\n\t4 PatchListSize 30\nPATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO:\n\t4 Token 42\n\t4 Size 16\n\t4 ConstantBufferIndex 0\n\t4 InlineDataSize 14\n\tHex 0 1 2 3 4 5 6 7 8 9 a b c d\nKernel #0\nKernelBinaryHeader:\n\t4 CheckSum 4294967295\n\t8 ShaderHashCode 18446744073709551615\n\t4 KernelNameSize 14\n\t4 PatchListSize 12\n\t4 KernelHeapSize 0\n\t4 GeneralStateHeapSize 0\n\t4 DynamicStateHeapSize 0\n\t4 SurfaceStateHeapSize 0\n\t4 KernelUnpaddedSize 0\n\tKernelName ExampleKernel\nUnidentified PatchToken:\n\t4 Token 19\n\t4 Size 12\n\tHex 0 0 0 0\n";
EXPECT_EQ(expectedOutput, ptmFile.str());
EXPECT_TRUE(decoder.getMockIga()->disasmWasCalled);
EXPECT_FALSE(decoder.getMockIga()->asmWasCalled);
@@ -817,4 +797,4 @@ TEST(DecoderHelperTest, GivenProductFamilyWhenTranslatingToIgaGenBaseThenExpecte
}
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -8,6 +8,7 @@
#include "shared/offline_compiler/source/decoder/binary_decoder.h"
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/array_count.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/helpers/test_files.h"
#include "opencl/test/unit_test/offline_compiler/environment.h"
@@ -50,9 +51,10 @@ TEST(EncoderTests, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingThenError
constexpr auto suppressMessages{false};
MockEncoder encoder{suppressMessages};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto result = encoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(-1, result);
@@ -111,9 +113,10 @@ TEST(EncoderTests, GivenMissingDumpFlagAndArgHelperOutputEnabledWhenParsingValid
encoder.mockArgHelper->hasOutput = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto result = encoder.validateInput(args);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(0, result);
EXPECT_TRUE(output.empty()) << output;
@@ -126,9 +129,10 @@ TEST(EncoderTests, GivenMissingPTMFileWhenEncodingThenErrorIsReturnedAndLogIsPri
constexpr auto suppressMessages{false};
MockEncoder encoder{suppressMessages};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto result = encoder.encode();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(-1, result);
EXPECT_EQ("Error! Couldn't find PTM.txt", output);
@@ -586,4 +590,4 @@ TEST(EncoderTests, WhenProcessingDeviceBinaryAndAsmIsAvailableThenAseembleItWith
EXPECT_EQ(encoder.filesMap["kernel_KernelHeap.asm"], encoder.getMockIga()->receivedAsm);
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -10,7 +10,10 @@
#include "shared/source/device_binary_format/elf/elf.h"
#include "shared/source/device_binary_format/elf/elf_encoder.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_elf.h"
#include "shared/test/common/mocks/mock_io_functions.h"
#include "shared/test/common/mocks/mock_modules_zebin.h"
#include "shared/test/common/test_macros/test.h"
@@ -107,6 +110,7 @@ kernels:
};
TEST(ZebinManipulatorTests, GivenValidZebinWhenItIsDisassembledAndAssembledBackThenItIsSameBinary) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
MockZebin<NEO::Elf::EI_CLASS_32> mockZebin32;
MockZebin<NEO::Elf::EI_CLASS_64> mockZebin64;
for (const auto zebin : {&mockZebin32.storage, &mockZebin64.storage}) {
@@ -210,9 +214,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenInvalidInputWhenValidatingInputT
"asm/disasm",
"-unknown_arg"};
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
const auto output{testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
EXPECT_EQ("Unknown argument -unknown_arg\n", output);
@@ -224,9 +229,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenMissingFileWhenValidatingInputTh
"-dump",
"./dump/"};
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
const auto output{testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
EXPECT_EQ("Error: Missing -file argument\n", output);
@@ -238,9 +244,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenMissingSecondPartOfTheArgumentWh
"-arg"};
for (const auto halfArg : {"-file", "-device", "-dump"}) {
args[2] = halfArg;
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
const auto output{testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
const auto expectedOutput = "Unknown argument " + std::string(halfArg) + "\n";
@@ -253,9 +260,11 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenValidArgsButDumpNotSpecifiedWhen
"asm/disasm",
"-file",
"binary.bin"};
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
const auto output{testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, retVal);
EXPECT_EQ("Warning: Path to dump -dump not specified. Using \"./dump/\" as dump folder.\n", output);
@@ -567,6 +576,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidIntelGTNotesSectionWhenGetIntelGTNotesThen
}
TEST_F(ZebinEncoderTests, GivenInvalidSymtabFileWhenAppendSymtabThenErrorIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
MockElfEncoder elfEncoder;
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
sectionInfo.name = ".symtab";
@@ -578,6 +589,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidSymtabFileWhenAppendSymtabThenErrorIsRetur
}
TEST_F(ZebinEncoderTests, GivenInvalidRelFileWhenAppendRelThenErrorIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
MockElfEncoder elfEncoder;
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
sectionInfo.name = ".rel.text";
@@ -589,6 +602,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidRelFileWhenAppendRelThenErrorIsReturned) {
}
TEST_F(ZebinEncoderTests, GivenInvalidRelaFileWhenAppendRelaThenErrorIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
MockElfEncoder elfEncoder;
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
sectionInfo.name = ".rela.text";
@@ -649,4 +664,4 @@ TEST_F(ZebinEncoderTests, GivenMissingFileWhenCheckIfAllFilesExistThenErrorIsRet
TEST_F(ZebinEncoderTests, WhenPrintHelpIsCalledThenHelpIsPrinted) {
encoder.printHelp();
EXPECT_FALSE(getOutput().empty());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -69,13 +69,17 @@ class MockOclocArgHelper : public OclocArgHelper {
return std::vector<char>(file.begin(), file.end());
}
std::string getLog() {
return messagePrinter.getLog().str();
}
protected:
bool fileExists(const std::string &filename) const override {
if (filesMap.find(filename) != filesMap.end()) {
return true;
}
return OclocArgHelper::fileExists(filename);
return OclocArgHelper::sourceFileExists(filename);
}
std::unique_ptr<char[]> loadDataFromFile(const std::string &filename, size_t &retSize) override {
@@ -83,6 +87,14 @@ class MockOclocArgHelper : public OclocArgHelper {
return OclocArgHelper::loadDataFromFile(filename, retSize);
}
if (Source *s = findSourceFile(filename)) {
auto size = s->length;
std::unique_ptr<char[]> ret(new char[size]());
memcpy_s(ret.get(), size, s->data, s->length);
retSize = s->length;
return ret;
}
if (shouldLoadDataFromFileReturnZeroSize) {
retSize = 0;
return {};
@@ -90,17 +102,15 @@ class MockOclocArgHelper : public OclocArgHelper {
if (!fileExists(filename)) {
return OclocArgHelper::loadDataFromFile(filename, retSize);
} else {
const auto &file = filesMap[filename];
std::unique_ptr<char[]> result{new char[file.size() + 1]};
std::copy(file.begin(), file.end(), result.get());
result[file.size()] = '\0';
retSize = file.size() + 1;
return result;
}
const auto &file = filesMap[filename];
std::unique_ptr<char[]> result{new char[file.size() + 1]};
std::copy(file.begin(), file.end(), result.get());
result[file.size()] = '\0';
retSize = file.size() + 1;
return result;
}
void saveOutput(const std::string &filename, const void *pData, const size_t &dataSize) override {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -32,8 +32,9 @@ class MockMultiCommand : public MultiCommand {
using MultiCommand::splitLineInSeparateArgs;
MockMultiCommand() : MultiCommand{} {
filesMap[clCopybufferFilename] = kernelSources;
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
uniqueHelper->setAllCallBase(true);
uniqueHelper->setAllCallBase(false);
argHelper = uniqueHelper.get();
}
@@ -53,6 +54,8 @@ class MockMultiCommand : public MultiCommand {
std::unique_ptr<MockOclocArgHelper> uniqueHelper{};
int singleBuildCalledCount{0};
bool callBaseSingleBuild{true};
const std::string clCopybufferFilename = "some_kernel.cl";
std::string kernelSources = "example_kernel(){}";
};
} // namespace NEO
} // namespace NEO

View File

@@ -15,7 +15,7 @@ namespace NEO {
MockOfflineCompiler::MockOfflineCompiler() : OfflineCompiler() {
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
uniqueHelper->setAllCallBase(true);
uniqueHelper->setAllCallBase(false);
argHelper = uniqueHelper.get();
auto uniqueFclFacadeMock = std::make_unique<MockOclocFclFacade>(argHelper);
@@ -88,4 +88,4 @@ void MockOfflineCompiler::createDir(const std::string &path) {
}
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -19,7 +19,11 @@
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/source/helpers/string.h"
#include "shared/test/common/helpers/gtest_helpers.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_io_functions.h"
#include "shared/test/common/mocks/mock_os_library.h"
#include "environment.h"
@@ -29,6 +33,7 @@
#include <algorithm>
#include <array>
#include <filesystem>
#include <stdexcept>
#include <string>
@@ -43,8 +48,52 @@ void mockedAbortOclocExecution(int errorCode) {
TEST(OclocApiTests, WhenOclocVersionIsCalledThenCurrentOclocVersionIsReturned) {
EXPECT_EQ(ocloc_version_t::OCLOC_VERSION_CURRENT, oclocVersion());
}
class OclocApiTest : public ::testing::Test {
protected:
void SetUp() override {
std::string spvFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".spv";
std::string binFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".bin";
std::string dbgFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".dbg";
std::vector<unsigned char> mockByteArray = {0x01, 0x02, 0x03, 0x04};
writeDataToFile(spvFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(binFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(dbgFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(clCopybufferFilename.c_str(), kernelSources.data(), mockByteArray.size());
}
const std::string clCopybufferFilename = "some_kernel.cl";
std::string kernelSources = "example_kernel(){}";
};
TEST_F(OclocApiTest, WhenGoodArgsAreGivenThenSuccessIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"copybuffer.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::fseekPtr)> mockFseek(&NEO::IoFunctions::fseekPtr, [](FILE *stream, long int offset, int origin) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::ftellPtr)> mockFtell(&NEO::IoFunctions::ftellPtr, [](FILE *stream) -> long int {
std::string kernelSources = "example_kernel(){}";
std::stringstream fileStream(kernelSources);
fileStream.seekg(0, std::ios::end);
return static_cast<long int>(fileStream.tellg());
});
VariableBackup<decltype(NEO::IoFunctions::freadPtr)> mockFread(&NEO::IoFunctions::freadPtr, [](void *ptr, size_t size, size_t count, FILE *stream) -> size_t {
std::string kernelSources = "example_kernel(){}";
std::stringstream fileStream(kernelSources);
size_t totalBytes = size * count;
fileStream.read(static_cast<char *>(ptr), totalBytes);
return static_cast<size_t>(fileStream.gcount() / size);
});
TEST(OclocApiTests, WhenGoodArgsAreGivenThenSuccessIsReturned) {
std::string clFileName(clFiles + "copybuffer.cl");
const char *argv[] = {
"ocloc",
@@ -54,19 +103,49 @@ TEST(OclocApiTests, WhenGoodArgsAreGivenThenSuccessIsReturned) {
gEnvironment->devicePrefix.c_str()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_SUCCESS);
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4]));
EXPECT_NE(std::string::npos, output.find("Build succeeded.\n"));
}
TEST(OclocApiTests, GivenQuietModeAndValidArgumentsWhenRunningOclocThenSuccessIsReturnedAndBuildSucceededMessageIsNotPrinted) {
TEST_F(OclocApiTest, GivenQuietModeAndValidArgumentsWhenRunningOclocThenSuccessIsReturnedAndBuildSucceededMessageIsNotPrinted) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"copybuffer.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::fseekPtr)> mockFseek(&NEO::IoFunctions::fseekPtr, [](FILE *stream, long int offset, int origin) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::ftellPtr)> mockFtell(&NEO::IoFunctions::ftellPtr, [](FILE *stream) -> long int {
std::string kernelSources = "example_kernel(){}";
std::stringstream fileStream(kernelSources);
fileStream.seekg(0, std::ios::end);
return static_cast<long int>(fileStream.tellg());
});
VariableBackup<decltype(NEO::IoFunctions::freadPtr)> mockFread(&NEO::IoFunctions::freadPtr, [](void *ptr, size_t size, size_t count, FILE *stream) -> size_t {
std::string kernelSources = "example_kernel(){}";
std::stringstream fileStream(kernelSources);
size_t totalBytes = size * count;
fileStream.read(static_cast<char *>(ptr), totalBytes);
return static_cast<size_t>(fileStream.gcount() / size);
});
std::string clFileName(clFiles + "copybuffer.cl");
const char *argv[] = {
"ocloc",
@@ -77,12 +156,13 @@ TEST(OclocApiTests, GivenQuietModeAndValidArgumentsWhenRunningOclocThenSuccessIs
gEnvironment->devicePrefix.c_str()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_SUCCESS, retVal);
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4]));
@@ -187,12 +267,13 @@ TEST(OclocApiTests, GivenNoQueryWhenQueryingThenErrorIsReturned) {
"ocloc",
"query"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line. Expected ocloc query <argument>. See ocloc query --help\nCommand was: ocloc query\n", output.c_str());
@@ -204,12 +285,13 @@ TEST(OclocApiTests, GivenInvalidQueryWhenQueryingThenErrorIsReturned) {
"query",
"unknown_query"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line.\nUnknown argument unknown_query\nCommand was: ocloc query unknown_query\n", output.c_str());
@@ -220,12 +302,13 @@ TEST(OclocApiTests, givenNoAcronymWhenIdsCommandIsInvokeThenErrorIsReported) {
"ocloc",
"ids"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line. Expected ocloc ids <acronym>.\nCommand was: ocloc ids\n", output.c_str());
@@ -237,18 +320,47 @@ TEST(OclocApiTests, givenUnknownAcronymWhenIdsCommandIsInvokeThenErrorIsReported
"ids",
"unk"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_INVALID_COMMAND_LINE);
EXPECT_STREQ("Error: Invalid command line. Unknown acronym unk.\nCommand was: ocloc ids unk\n", output.c_str());
}
TEST(OclocApiTests, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) {
TEST_F(OclocApiTest, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"copybuffer.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::fseekPtr)> mockFseek(&NEO::IoFunctions::fseekPtr, [](FILE *stream, long int offset, int origin) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::ftellPtr)> mockFtell(&NEO::IoFunctions::ftellPtr, [](FILE *stream) -> long int {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
fileStream.seekg(0, std::ios::end);
return static_cast<long int>(fileStream.tellg());
});
VariableBackup<decltype(NEO::IoFunctions::freadPtr)> mockFread(&NEO::IoFunctions::freadPtr, [](void *ptr, size_t size, size_t count, FILE *stream) -> size_t {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
size_t totalBytes = size * count;
fileStream.read(static_cast<char *>(ptr), totalBytes);
return static_cast<size_t>(fileStream.gcount() / size);
});
std::string clFileName(clFiles + "copybuffer.cl");
std::unique_ptr<OclocArgHelper> argHelper = std::make_unique<OclocArgHelper>();
auto allSupportedDeviceConfigs = argHelper->productConfigHelper->getDeviceAotInfo();
@@ -275,18 +387,34 @@ TEST(OclocApiTests, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) {
family.c_str()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_SUCCESS);
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file " + clFileName + " -device " + family));
}
TEST(OclocApiTests, WhenArgsWithMissingFileAreGivenThenErrorMessageIsProduced) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"-q",
@@ -296,18 +424,34 @@ TEST(OclocApiTests, WhenArgsWithMissingFileAreGivenThenErrorMessageIsProduced) {
gEnvironment->devicePrefix.c_str()};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_INVALID_FILE);
EXPECT_NE(std::string::npos, output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device "s + argv[5]));
}
TEST(OclocApiTests, givenInputOptionsAndInternalOptionsWhenCmdlineIsPrintedThenBothAreInQuotes) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"-q",
@@ -318,12 +462,13 @@ TEST(OclocApiTests, givenInputOptionsAndInternalOptionsWhenCmdlineIsPrintedThenB
"-options", "-D DEBUG -cl-kernel-arg-info", "-internal_options", "-internalOption1 -internal-option-2"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_NE(retVal, OCLOC_SUCCESS);
EXPECT_TRUE(output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device " +
gEnvironment->devicePrefix +
@@ -334,6 +479,21 @@ TEST(OclocApiTests, givenInputOptionsAndInternalOptionsWhenCmdlineIsPrintedThenB
}
TEST(OclocApiTests, givenInputOptionsCalledOptionsWhenCmdlineIsPrintedThenQuotesAreCorrect) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"-q",
@@ -344,12 +504,13 @@ TEST(OclocApiTests, givenInputOptionsCalledOptionsWhenCmdlineIsPrintedThenQuotes
"-options", "-options", "-internal_options", "-internalOption"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_NE(retVal, OCLOC_SUCCESS);
EXPECT_TRUE(output.find("Command was: ocloc -q -file test_files/IDoNotExist.cl -device " +
gEnvironment->devicePrefix +
@@ -359,7 +520,22 @@ TEST(OclocApiTests, givenInputOptionsCalledOptionsWhenCmdlineIsPrintedThenQuotes
EXPECT_EQ(quotesCount, 4u);
}
TEST(OclocApiTests, GivenIncludeHeadersWhenCompilingThenPassesToFclHeadersPackedAsElf) {
TEST_F(OclocApiTest, GivenIncludeHeadersWhenCompilingThenPassesToFclHeadersPackedAsElf) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
auto prevFclDebugVars = NEO::getFclDebugVars();
auto debugVars = prevFclDebugVars;
std::string receivedInput;
@@ -460,17 +636,19 @@ TEST(OclocApiTests, GivenHelpParameterWhenDecodingThenHelpMsgIsPrintedAndSuccess
"--help"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_FALSE(output.empty());
EXPECT_EQ(retVal, OCLOC_SUCCESS);
}
TEST(OclocApiTests, GivenNonExistingFileWhenDecodingThenAbortIsCalled) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
VariableBackup oclocAbortBackup{&abortOclocExecution, &mockedAbortOclocExecution};
const char *argv[] = {
@@ -482,12 +660,13 @@ TEST(OclocApiTests, GivenNonExistingFileWhenDecodingThenAbortIsCalled) {
"test_files/created"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_NE(std::string::npos, output.find("mockedAbortOclocExecution() called with error code = 1"));
EXPECT_EQ(-1, retVal);
@@ -500,18 +679,48 @@ TEST(OclocApiTests, GivenMissingFileNameWhenDecodingThenErrorIsReturned) {
"-file"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_NE(std::string::npos, output.find("Unknown argument -file\n"));
EXPECT_EQ(-1, retVal);
}
TEST(OclocApiTests, GivenOnlySpirVWithMultipleDevicesWhenCompilingThenFirstDeviceIsSelected) {
TEST_F(OclocApiTest, GivenOnlySpirVWithMultipleDevicesWhenCompilingThenFirstDeviceIsSelected) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"copybuffer.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::fseekPtr)> mockFseek(&NEO::IoFunctions::fseekPtr, [](FILE *stream, long int offset, int origin) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::ftellPtr)> mockFtell(&NEO::IoFunctions::ftellPtr, [](FILE *stream) -> long int {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
fileStream.seekg(0, std::ios::end);
return static_cast<long int>(fileStream.tellg());
});
VariableBackup<decltype(NEO::IoFunctions::freadPtr)> mockFread(&NEO::IoFunctions::freadPtr, [](void *ptr, size_t size, size_t count, FILE *stream) -> size_t {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
size_t totalBytes = size * count;
fileStream.read(static_cast<char *>(ptr), totalBytes);
return static_cast<size_t>(fileStream.gcount() / size);
});
std::string clFileName(clFiles + "copybuffer.cl");
AOT::FAMILY productFamily = AOT::UNKNOWN_FAMILY;
std::string familyAcronym("");
@@ -555,12 +764,13 @@ TEST(OclocApiTests, GivenOnlySpirVWithMultipleDevicesWhenCompilingThenFirstDevic
}
}
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_SUCCESS);
EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -device " + firstDeviceAcronym + " -spv_only"));
@@ -584,41 +794,88 @@ TEST(OclocApiTests, GivenHelpParameterWhenCompilingThenHelpMsgIsPrintedAndSucces
}
TEST(OclocApiTests, GivenHelpParameterWhenEncodingThenHelpMsgIsPrintedAndSuccessIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"asm",
"--help"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_FALSE(output.empty());
EXPECT_EQ(retVal, OCLOC_SUCCESS);
}
TEST(OclocApiTests, GivenMissingDumpFileNameWhenEncodingThenErrorIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"asm",
"-dump"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_NE(std::string::npos, output.find("Unknown argument -dump\n"));
EXPECT_EQ(-1, retVal);
}
TEST(OclocApiTests, GivenValidArgumentsAndMissingPtmFileWhenEncodingThenErrorIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"asm",
@@ -628,12 +885,13 @@ TEST(OclocApiTests, GivenValidArgumentsAndMissingPtmFileWhenEncodingThenErrorIsR
"test_files/binary_gen.bin"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_NE(std::string::npos, output.find("Error! Couldn't find PTM.txt"));
EXPECT_EQ(-1, retVal);
@@ -646,18 +904,34 @@ TEST(OclocApiTests, GiveMultiCommandHelpArgumentsWhenInvokingOclocThenHelpIsPrin
"--help"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_FALSE(output.empty());
EXPECT_EQ(-1, retVal);
}
TEST(OclocApiTests, GivenNonexistentFileWhenValidateIsInvokedThenErrorIsPrinted) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"some_kernel.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
const char *argv[] = {
"ocloc",
"validate",
@@ -665,13 +939,14 @@ TEST(OclocApiTests, GivenNonexistentFileWhenValidateIsInvokedThenErrorIsPrinted)
"some_special_nonexistent_file.gen"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_EQ(-1, retVal);
const std::string expectedErrorMessage{"Error : Input file missing : some_special_nonexistent_file.gen\nCommand was: ocloc validate -file some_special_nonexistent_file.gen\n"};
@@ -683,13 +958,14 @@ TEST(OclocApiTests, GivenCommandWithoutArgsWhenOclocIsInvokedThenHelpIsPrinted)
"ocloc"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_SUCCESS, retVal);
EXPECT_FALSE(output.empty());
}
@@ -703,13 +979,14 @@ TEST(OclocApiTests, GivenHelpArgumentWhenOclocIsInvokedThenHelpIsPrinted) {
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
const auto output = testing::internal::GetCapturedStdout();
const auto output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_SUCCESS, retVal);
EXPECT_FALSE(output.empty());
}
@@ -722,12 +999,13 @@ TEST(OclocApiTests, GivenHelpParameterWhenLinkingThenHelpMsgIsPrintedAndSuccessI
"--help"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_FALSE(output.empty());
EXPECT_EQ(OCLOC_SUCCESS, retVal);
}
@@ -739,12 +1017,13 @@ TEST(OclocApiTests, GivenInvalidParameterWhenLinkingThenErrorIsReturned) {
"--dummy_param"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
const std::string expectedInitError{"Invalid option (arg 2): --dummy_param\n"};
@@ -758,12 +1037,13 @@ TEST(OclocApiTests, GivenInvalidCommandLineWhenConcatenatingThenErrorIsReturned)
"ocloc",
"concat"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
const std::string emptyCommandLineError = "No files to concatenate were provided.\n";
const std::string expectedErrorMessage = emptyCommandLineError + NEO::OclocConcat::helpMessage.str() + "Command was: ocloc concat\n";
@@ -808,12 +1088,13 @@ TEST(OclocApiTests, GivenValidCommandLineAndFatBinariesWhenConcatenatingThenNewF
"-out",
"catFatBinary.ar"};
unsigned int argc = sizeof(argv) / sizeof(argv[0]);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
2, sourcesData, sourcesLen, sourcesName,
0, nullptr, nullptr, nullptr,
&numOutputs, &outputData, &outputLen, &outputName);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(OCLOC_SUCCESS, retVal);
EXPECT_TRUE(output.empty());
@@ -851,7 +1132,36 @@ TEST(OclocApiTests, GivenValidCommandLineAndFatBinariesWhenConcatenatingThenNewF
oclocFreeOutput(&numOutputs, &outputData, &outputLen, &outputName);
}
TEST(OclocApiTests, GivenVerboseModeWhenCompilingThenPrintCommandLine) {
TEST_F(OclocApiTest, GivenVerboseModeWhenCompilingThenPrintCommandLine) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {
std::filesystem::path filePath = filename;
std::string fileNameWithExtension = filePath.filename().string();
std::vector<std::string> expectedtedFiles = {
"copybuffer.cl"};
auto itr = std::find(expectedtedFiles.begin(), expectedtedFiles.end(), std::string(fileNameWithExtension));
if (itr != expectedtedFiles.end()) {
return reinterpret_cast<FILE *>(0x40);
}
return NULL;
});
VariableBackup<decltype(NEO::IoFunctions::fclosePtr)> mockFclose(&NEO::IoFunctions::fclosePtr, [](FILE *stream) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::fseekPtr)> mockFseek(&NEO::IoFunctions::fseekPtr, [](FILE *stream, long int offset, int origin) -> int { return 0; });
VariableBackup<decltype(NEO::IoFunctions::ftellPtr)> mockFtell(&NEO::IoFunctions::ftellPtr, [](FILE *stream) -> long int {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
fileStream.seekg(0, std::ios::end);
return static_cast<long int>(fileStream.tellg());
});
VariableBackup<decltype(NEO::IoFunctions::freadPtr)> mockFread(&NEO::IoFunctions::freadPtr, [](void *ptr, size_t size, size_t count, FILE *stream) -> size_t {
std::string kernelSource = "example_kernel(){}";
std::stringstream fileStream(kernelSource);
size_t totalBytes = size * count;
fileStream.read(static_cast<char *>(ptr), totalBytes);
return static_cast<size_t>(fileStream.gcount() / size);
});
std::string clFileName(clFiles + "copybuffer.cl");
const char *argv[] = {
"ocloc",
@@ -862,12 +1172,13 @@ TEST(OclocApiTests, GivenVerboseModeWhenCompilingThenPrintCommandLine) {
"-v"};
unsigned int argc = sizeof(argv) / sizeof(const char *);
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
int retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
EXPECT_EQ(retVal, OCLOC_SUCCESS);
EXPECT_NE(std::string::npos, output.find("Command was: ocloc -file "s + clFileName.c_str() + " -device "s + argv[4] + " -v")) << output;
@@ -919,14 +1230,15 @@ struct OclocFallbackTests : ::testing::Test {
&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
return retVal;
} else {
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
testing::internal::CaptureStderr();
auto retVal = oclocInvoke(argc, argv,
0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr);
capturedStdout = testing::internal::GetCapturedStdout();
capturedStdout = capture.getCapturedStdout();
capturedStderr = testing::internal::GetCapturedStderr();
return retVal;
}
@@ -950,6 +1262,7 @@ struct OclocFallbackTests : ::testing::Test {
};
TEST_F(OclocFallbackTests, GivenNoFormerOclocNameWhenInvalidDeviceErrorIsReturnedThenDontFallback) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
Ocloc::oclocFormerLibName = "";
@@ -966,6 +1279,7 @@ TEST_F(OclocFallbackTests, GivenNoFormerOclocNameWhenInvalidDeviceErrorIsReturne
}
TEST_F(OclocFallbackTests, GivenInvalidFormerOclocNameWhenInvalidDeviceErrorIsReturnedThenFallbackButWithoutLoadingLib) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
Ocloc::oclocFormerLibName = "invalidName";
@@ -1012,6 +1326,7 @@ int mockOclocInvoke(unsigned int numArgs, const char *argv[],
}
TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsSuccessThenSuccessIsPropagatedAndCommandLineIsNotPrinted) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
Ocloc::oclocFormerLibName = "oclocFormer";
VariableBackup<decltype(NEO::OsLibrary::loadFunc)> funcBackup{&NEO::OsLibrary::loadFunc, MockOsLibraryCustom::load};
@@ -1032,6 +1347,7 @@ TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsSucces
}
TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsErrorThenErrorIsPropagated) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
Ocloc::oclocFormerLibName = "oclocFormer";
VariableBackup<decltype(NEO::OsLibrary::loadFunc)> funcBackup{&NEO::OsLibrary::loadFunc, MockOsLibraryCustom::load};
@@ -1063,6 +1379,8 @@ TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsErrorT
}
TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsOutputsThenOutputIsPropagated) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
for (auto &expectedRetVal : {ocloc_error_t::OCLOC_SUCCESS,
ocloc_error_t::OCLOC_OUT_OF_HOST_MEMORY,
ocloc_error_t::OCLOC_BUILD_PROGRAM_FAILURE,
@@ -1098,4 +1416,4 @@ TEST_F(OclocFallbackTests, GivenValidFormerOclocNameWhenFormerOclocReturnsOutput
oclocFreeOutput(&numOutputs, &dataOutputs, &lenOutputs, &nameOutputs);
}
passOutputs = false;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -10,14 +10,35 @@
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/offline_compiler/source/ocloc_fatbinary.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "environment.h"
#include "gtest/gtest.h"
#include "mock/mock_argument_helper.h"
#include <memory>
extern Environment *gEnvironment;
namespace NEO {
class OclocEnabledAcronyms : public ::testing::Test {
class OclocTest : public ::testing::Test {
void SetUp() override {
std::string spvFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".spv";
std::string binFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".bin";
std::string dbgFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".dbg";
std::vector<unsigned char> mockByteArray = {0x01, 0x02, 0x03, 0x04};
writeDataToFile(spvFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(binFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(dbgFile.c_str(), mockByteArray.data(), mockByteArray.size());
}
protected:
const std::string clCopybufferFilename = "some_kernel.cl";
std::string copyKernelSources = "example_kernel(){}";
};
class OclocEnabledAcronyms : public OclocTest {
public:
std::vector<DeviceAotInfo> enabledProducts{};
std::vector<ConstStringRef> enabledProductsAcronyms{};
@@ -28,7 +49,8 @@ class OclocEnabledAcronyms : public ::testing::Test {
class OclocFatBinaryProductAcronymsTests : public OclocEnabledAcronyms {
public:
OclocFatBinaryProductAcronymsTests() {
oclocArgHelperWithoutInput = std::make_unique<OclocArgHelper>();
mockArgHelperFilesMap[clCopybufferFilename] = copyKernelSources;
oclocArgHelperWithoutInput = std::make_unique<MockOclocArgHelper>(mockArgHelperFilesMap);
oclocArgHelperWithoutInput->getPrinterRef().setSuppressMessages(true);
enabledProducts = oclocArgHelperWithoutInput->productConfigHelper->getDeviceAotInfo();
@@ -37,7 +59,9 @@ class OclocFatBinaryProductAcronymsTests : public OclocEnabledAcronyms {
enabledReleasesAcronyms = oclocArgHelperWithoutInput->productConfigHelper->getReleasesAcronyms();
}
std::unique_ptr<OclocArgHelper> oclocArgHelperWithoutInput;
protected:
std::unique_ptr<MockOclocArgHelper> oclocArgHelperWithoutInput;
MockOclocArgHelper::FilesMap mockArgHelperFilesMap{};
};
class OclocFatBinaryTest : public OclocEnabledAcronyms {
@@ -75,4 +99,4 @@ struct OclocFatbinaryPerProductTests : public ::testing::TestWithParam<std::tupl
std::unique_ptr<OclocArgHelper> argHelper;
};
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -11,6 +11,7 @@
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/os_interface/os_inc_base.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "mock/mock_ocloc_fcl_facade.h"
@@ -22,9 +23,10 @@ TEST_F(OclocFclFacadeTest, GivenMissingFclLibraryWhenPreparingFclThenFailureIsRe
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldFailLoadingOfFclLib = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -39,9 +41,10 @@ TEST_F(OclocFclFacadeTest, GivenFailingLoadingOfFclSymbolsWhenPreparingFclThenFa
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldFailLoadingOfFclCreateMainFunction = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -54,9 +57,10 @@ TEST_F(OclocFclFacadeTest, GivenFailingCreationOfFclMainWhenPreparingFclThenFail
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldFailCreationOfFclMain = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -73,9 +77,10 @@ TEST_F(OclocFclFacadeTest, GivenIncompatibleFclInterfacesWhenPreparingFclThenFai
mockFclFacade.isFclInterfaceCompatibleReturnValue = false;
mockFclFacade.getIncompatibleInterfaceReturnValue = "SomeImportantInterface";
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -88,9 +93,10 @@ TEST_F(OclocFclFacadeTest, GivenFailingCreationOfFclDeviceContextWhenPreparingFc
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldFailCreationOfFclDeviceContext = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -103,9 +109,10 @@ TEST_F(OclocFclFacadeTest, GivenNoneErrorsSetAndNotPopulateFclInterfaceWhenPrepa
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldPopulateFclInterfaceReturnValue = false;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, fclPreparationResult);
EXPECT_TRUE(output.empty()) << output;
@@ -118,9 +125,10 @@ TEST_F(OclocFclFacadeTest, GivenPopulateFclInterfaceAndInvalidFclDeviceContextWh
mockFclFacade.shouldPopulateFclInterfaceReturnValue = true;
mockFclFacade.shouldReturnInvalidFclPlatformHandle = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, fclPreparationResult);
EXPECT_FALSE(mockFclFacade.isInitialized());
@@ -135,9 +143,10 @@ TEST_F(OclocFclFacadeTest, GivenPopulateFclInterfaceWhenPreparingFclThenSuccessI
MockOclocFclFacade mockFclFacade{&mockArgHelper};
mockFclFacade.shouldPopulateFclInterfaceReturnValue = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, fclPreparationResult);
EXPECT_TRUE(output.empty()) << output;
@@ -149,9 +158,10 @@ TEST_F(OclocFclFacadeTest, GivenPopulateFclInterfaceWhenPreparingFclThenSuccessI
TEST_F(OclocFclFacadeTest, GivenNoneErrorsSetWhenPreparingFclThenSuccessIsReported) {
MockOclocFclFacade mockFclFacade{&mockArgHelper};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, fclPreparationResult);
EXPECT_TRUE(output.empty()) << output;
@@ -164,9 +174,10 @@ TEST_F(OclocFclFacadeTest, GivenNoneErrorsSetWhenPreparingFclThenSuccessIsReport
TEST_F(OclocFclFacadeTest, GivenInitializedFclWhenGettingIncompatibleInterfaceThenEmptyStringIsReturned) {
MockOclocFclFacade mockFclFacade{&mockArgHelper};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto fclPreparationResult{mockFclFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_SUCCESS, fclPreparationResult);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -11,6 +11,7 @@
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/os_interface/os_inc_base.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/mocks/mock_compilers.h"
#include "mock/mock_ocloc_igc_facade.h"
@@ -23,11 +24,12 @@ TEST_F(OclocIgcFacadeTest, GivenMissingIgcLibraryWhenPreparingIgcThenFailureIsRe
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.shouldFailLoadingOfIgcLib = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
std::string libName = "invalidigc.so";
auto igcNameGuard = NEO::pushIgcDllName(libName.c_str());
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -42,9 +44,10 @@ TEST_F(OclocIgcFacadeTest, GivenFailingLoadingOfIgcSymbolsWhenPreparingIgcThenFa
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.shouldFailLoadingOfIgcCreateMainFunction = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -57,9 +60,10 @@ TEST_F(OclocIgcFacadeTest, GivenFailingCreationOfIgcMainWhenPreparingIgcThenFail
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.shouldFailCreationOfIgcMain = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -76,9 +80,10 @@ TEST_F(OclocIgcFacadeTest, GivenIncompatibleIgcInterfacesWhenPreparingIgcThenFai
mockIgcFacade.isIgcInterfaceCompatibleReturnValue = false;
mockIgcFacade.getIncompatibleInterfaceReturnValue = "SomeImportantInterface";
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -91,9 +96,10 @@ TEST_F(OclocIgcFacadeTest, GivenMissingPatchtokenInterfaceWhenPreparingIgcThenFa
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.isPatchtokenInterfaceSupportedReturnValue = false;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -106,9 +112,10 @@ TEST_F(OclocIgcFacadeTest, GivenFailingCreationOfIgcDeviceContextWhenPreparingIg
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.shouldFailCreationOfIgcDeviceContext = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -127,9 +134,10 @@ TEST_F(OclocIgcFacadeTest, GivenInvalidIgcDeviceContextWhenPreparingIgcThenFailu
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.*invalidReturnFlag = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, igcPreparationResult);
EXPECT_FALSE(mockIgcFacade.isInitialized());
@@ -142,9 +150,10 @@ TEST_F(OclocIgcFacadeTest, GivenInvalidIgcDeviceContextWhenPreparingIgcThenFailu
TEST_F(OclocIgcFacadeTest, GivenNoneErrorsSetWhenPreparingIgcThenSuccessIsReported) {
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, igcPreparationResult);
EXPECT_TRUE(output.empty()) << output;
@@ -154,9 +163,10 @@ TEST_F(OclocIgcFacadeTest, GivenNoneErrorsSetWhenPreparingIgcThenSuccessIsReport
TEST_F(OclocIgcFacadeTest, GivenInitializedIgcWhenGettingIncompatibleInterfaceThenEmptyStringIsReturned) {
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_SUCCESS, igcPreparationResult);
@@ -170,9 +180,10 @@ TEST_F(OclocIgcFacadeTest, GivenFailingCreationOfIgcDeviceContext3WhenGettingRev
MockOclocIgcFacade mockIgcFacade{&mockArgHelper};
mockIgcFacade.shouldFailCreationOfIgcDeviceContext3 = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto igcPreparationResult{mockIgcFacade.initialize(hwInfo)};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_SUCCESS, igcPreparationResult);

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -10,27 +10,45 @@
#include "shared/offline_compiler/source/multi_command.h"
#include "shared/offline_compiler/source/ocloc_api.h"
#include "shared/offline_compiler/source/offline_compiler.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/mocks/mock_io_functions.h"
#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h"
#include "environment.h"
#include "gtest/gtest.h"
#include "mock/mock_argument_helper.h"
#include <cstdint>
#include <memory>
extern Environment *gEnvironment;
namespace NEO {
class OfflineCompilerTests : public ::testing::Test {
public:
OfflineCompiler *pOfflineCompiler = nullptr;
int retVal = OCLOC_SUCCESS;
std::map<std::string, std::string> filesMap;
std::unique_ptr<MockOclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<MockOclocArgHelper>(filesMap);
protected:
void SetUp() override {
oclocArgHelperWithoutInput->setAllCallBase(true);
std::string spvFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".spv";
std::string binFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".bin";
std::string dbgFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".dbg";
std::vector<unsigned char> mockByteArray = {0x01, 0x02, 0x03, 0x04};
writeDataToFile(spvFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(binFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(dbgFile.c_str(), mockByteArray.data(), mockByteArray.size());
filesMap[clCopybufferFilename] = OfflineCompilerTests::kernelSources;
oclocArgHelperWithoutInput->setAllCallBase(false);
}
MockOclocArgHelper::FilesMap filesMap{};
std::string kernelSources = "example_kernel(){}";
const std::string clCopybufferFilename = "some_kernel.cl";
};
class MultiCommandTests : public ::testing::Test {
@@ -42,12 +60,23 @@ class MultiCommandTests : public ::testing::Test {
std::string nameOfFileWithArgs;
std::string outFileList;
int retVal = OCLOC_SUCCESS;
std::map<std::string, std::string> filesMap;
std::unique_ptr<MockOclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<MockOclocArgHelper>(filesMap);
protected:
void SetUp() override {
oclocArgHelperWithoutInput->setAllCallBase(true);
std::string spvFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".spv";
std::string binFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".bin";
std::string dbgFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".dbg";
std::vector<unsigned char> mockByteArray = {0x01, 0x02, 0x03, 0x04};
writeDataToFile(spvFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(binFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(dbgFile.c_str(), mockByteArray.data(), mockByteArray.size());
filesMap[clCopybufferFilename] = kernelSources;
oclocArgHelperWithoutInput->setAllCallBase(false);
}
MockOclocArgHelper::FilesMap filesMap{};
const std::string clCopybufferFilename = "some_kernel.cl";
std::string kernelSources = "example_kernel(){}";
};
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -12,6 +12,8 @@
#include "shared/source/device_binary_format/elf/ocl_elf.h"
#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/os_inc_base.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/mocks/mock_compilers.h"
#include "environment.h"
@@ -32,6 +34,13 @@ namespace NEO {
using OperationMode = MockOfflineLinker::OperationMode;
void OfflineLinkerTest::SetUp() {
std::string spvFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".spv";
std::string binFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".bin";
std::string dbgFile = std::string("copybuffer") + "_" + gEnvironment->devicePrefix + ".dbg";
std::vector<unsigned char> mockByteArray = {0x01, 0x02, 0x03, 0x04};
writeDataToFile(spvFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(binFile.c_str(), mockByteArray.data(), mockByteArray.size());
writeDataToFile(dbgFile.c_str(), mockByteArray.data(), mockByteArray.size());
MockCompilerDebugVars igcDebugVars{gEnvironment->igcDebugVars};
igcDebugVars.binaryToReturn = binaryToReturn;
igcDebugVars.binaryToReturnSize = sizeof(binaryToReturn);
@@ -198,10 +207,11 @@ TEST_F(OfflineLinkerTest, GivenUnknownArgumentWhenParsingThenErrorIsReported) {
"link",
"-some_new_unknown_command"};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
const auto result = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, result);
@@ -222,9 +232,10 @@ TEST_F(OfflineLinkerTest, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingTh
mockOclocIgcFacade = std::make_unique<MockOclocIgcFacade>(&mockArgHelper);
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto result = mockOfflineLinker.parseCommand(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, result);
@@ -237,9 +248,10 @@ TEST_F(OfflineLinkerTest, GivenCommandWithoutInputFilesWhenVerificationIsPerform
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
mockOfflineLinker.inputFilenames = {};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto verificationResult = mockOfflineLinker.verifyLinkerCommand();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, verificationResult);
@@ -256,9 +268,10 @@ TEST_F(OfflineLinkerTest, GivenCommandWithEmptyFilenameWhenVerificationIsPerform
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto verificationResult = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, verificationResult);
@@ -279,9 +292,10 @@ TEST_F(OfflineLinkerTest, GivenCommandWithNonexistentInputFileWhenVerificationIs
const auto parsingResult = mockOfflineLinker.parseCommand(argv.size(), argv);
ASSERT_EQ(OCLOC_SUCCESS, parsingResult);
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto verificationResult = mockOfflineLinker.verifyLinkerCommand();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_FILE, verificationResult);
@@ -304,9 +318,10 @@ TEST_F(OfflineLinkerTest, GivenCommandWithInvalidOutputFormatWhenVerificationIsP
const auto parsingResult = mockOfflineLinker.parseCommand(argv.size(), argv);
ASSERT_EQ(OCLOC_SUCCESS, parsingResult);
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto verificationResult = mockOfflineLinker.verifyLinkerCommand();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, verificationResult);
@@ -330,9 +345,10 @@ TEST_F(OfflineLinkerTest, GivenValidCommandWhenVerificationIsPerformedThenSucces
const auto parsingResult = mockOfflineLinker.parseCommand(argv.size(), argv);
ASSERT_EQ(OCLOC_SUCCESS, parsingResult);
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto verificationResult = mockOfflineLinker.verifyLinkerCommand();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, verificationResult);
EXPECT_TRUE(output.empty());
@@ -351,9 +367,10 @@ TEST_F(OfflineLinkerTest, GivenEmptyFileWhenLoadingInputFilesThenErrorIsReturned
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto initializationResult = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_INVALID_FILE, initializationResult);
@@ -370,9 +387,10 @@ TEST_F(OfflineLinkerTest, GivenValidFileWithUnknownFormatWhenLoadingInputFilesTh
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
mockOfflineLinker.inputFilenames.push_back(filename);
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto readingResult = mockOfflineLinker.loadInputFilesContent();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_INVALID_PROGRAM, readingResult);
@@ -391,9 +409,10 @@ TEST_F(OfflineLinkerTest, GivenValidFilesWithValidFormatsWhenLoadingInputFilesTh
mockOfflineLinker.inputFilenames.push_back(firstFilename);
mockOfflineLinker.inputFilenames.push_back(secondFilename);
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto readingResult = mockOfflineLinker.loadInputFilesContent();
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_SUCCESS, readingResult);
EXPECT_TRUE(output.empty());
@@ -430,9 +449,10 @@ TEST_F(OfflineLinkerTest, GivenValidFilesWhenInitializationIsSuccessfulThenLinkM
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto initializationResult = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_SUCCESS, initializationResult);
EXPECT_TRUE(output.empty());
@@ -536,9 +556,10 @@ TEST_F(OfflineLinkerTest, GivenValidInputFileContentsAndFailingIGCWhenLlvmBcOutp
mockOfflineLinker.outputFormat = IGC::CodeType::llvmBc;
mockOfflineLinker.operationMode = OperationMode::linkFiles;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto linkingResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_BUILD_PROGRAM_FAILURE, linkingResult);
EXPECT_EQ(0u, mockArgHelper.interceptedFiles.count("linker_output"));
@@ -567,9 +588,10 @@ TEST_F(OfflineLinkerTest, GivenValidInputFileContentsAndIGCSignalingSuccessButRe
mockOfflineLinker.outputFormat = IGC::CodeType::llvmBc;
mockOfflineLinker.operationMode = OperationMode::linkFiles;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto linkingResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_BUILD_PROGRAM_FAILURE, linkingResult);
EXPECT_EQ(0u, mockArgHelper.interceptedFiles.count("linker_output"));
@@ -598,9 +620,10 @@ TEST_F(OfflineLinkerTest, GivenValidInputFileContentsAndInvalidTranslationOutput
mockOfflineLinker.outputFormat = IGC::CodeType::llvmBc;
mockOfflineLinker.operationMode = OperationMode::linkFiles;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto linkingResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_OUT_OF_HOST_MEMORY, linkingResult);
EXPECT_EQ(0u, mockArgHelper.interceptedFiles.count("linker_output"));
@@ -612,9 +635,10 @@ TEST_F(OfflineLinkerTest, GivenValidInputFileContentsAndInvalidTranslationOutput
TEST_F(OfflineLinkerTest, GivenUninitializedLinkerWhenExecuteIsInvokedThenErrorIsIssued) {
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto executionResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_INVALID_COMMAND_LINE, executionResult);
ASSERT_FALSE(output.empty());
@@ -624,9 +648,10 @@ TEST_F(OfflineLinkerTest, GivenHelpRequestWhenExecuteIsInvokedThenHelpIsPrinted)
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
mockOfflineLinker.operationMode = OperationMode::showHelp;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto executionResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_SUCCESS, executionResult);
ASSERT_FALSE(output.empty());
@@ -636,9 +661,10 @@ TEST_F(OfflineLinkerTest, GivenInvalidOperationModeWhenExecuteIsInvokedThenError
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
mockOfflineLinker.operationMode = static_cast<OperationMode>(7); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange), NEO-12901
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto executionResult{mockOfflineLinker.execute()};
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
ASSERT_EQ(OCLOC_INVALID_COMMAND_LINE, executionResult);
ASSERT_FALSE(output.empty());
@@ -671,9 +697,10 @@ TEST_F(OfflineLinkerTest, GivenEmptyHwInfoTableWhenInitializationIsPerformedThen
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
mockOfflineLinker.shouldReturnEmptyHardwareInfoTable = true;
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto initializationResult = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_INVALID_DEVICE, initializationResult);
@@ -699,9 +726,10 @@ TEST_F(OfflineLinkerTest, GivenMissingIgcLibraryWhenInitializationIsPerformedThe
mockOclocIgcFacade->shouldFailLoadingOfIgcLib = true;
MockOfflineLinker mockOfflineLinker{&mockArgHelper, std::move(mockOclocIgcFacade)};
::testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
const auto initializationResult = mockOfflineLinker.initialize(argv.size(), argv);
const auto output{::testing::internal::GetCapturedStdout()};
const auto output{capture.getCapturedStdout()};
EXPECT_EQ(OCLOC_OUT_OF_HOST_MEMORY, initializationResult);
@@ -735,4 +763,4 @@ TEST_F(OfflineLinkerTest, GivenOfflineLinkerWhenStoringInvalidBuildLogThenItIsIg
EXPECT_TRUE(buildLog2.empty());
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -7,6 +7,8 @@
#pragma once
#include "shared/test/common/helpers/stdout_capture.h"
#include <string>
namespace NEO {
@@ -19,21 +21,22 @@ namespace NEO {
class StdoutCapturer {
public:
StdoutCapturer() {
::testing::internal::CaptureStdout();
capture.captureStdout();
}
~StdoutCapturer() {
if (!outputAcquired) {
::testing::internal::GetCapturedStdout();
capture.getCapturedStdout();
}
}
std::string acquireOutput() {
outputAcquired = true;
return ::testing::internal::GetCapturedStdout();
return capture.getCapturedStdout();
}
private:
StdoutCapture capture;
bool outputAcquired{false};
};

View File

@@ -8,6 +8,7 @@
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/source/xe3_core/hw_info_xe3_core.h"
#include "shared/test/common/helpers/stdout_capture.h"
#include "shared/test/common/test_macros/header/per_product_test_definitions.h"
#include "shared/test/common/test_macros/test.h"
@@ -27,9 +28,10 @@ PTLTEST_F(PtlOfflineCompilerTests, givenPtlHDeviceIdValueWhenInitHwInfoThenCorre
std::stringstream deviceIDStr, expectedOutput;
deviceIDStr << "0x" << std::hex << deviceID;
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
mockOfflineCompiler.initHardwareInfo(deviceIDStr.str());
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
expectedOutput << "Auto-detected target based on " << deviceIDStr.str() << " device id: ptl-h-a0\n";
EXPECT_STREQ(output.c_str(), expectedOutput.str().c_str());
@@ -47,13 +49,14 @@ PTLTEST_F(PtlOfflineCompilerTests, givenPtlUDeviceIdValueWhenInitHwInfoThenCorre
deviceIDStr << "0x" << std::hex << deviceID;
testing::internal::CaptureStdout();
StdoutCapture capture;
capture.captureStdout();
mockOfflineCompiler.initHardwareInfo(deviceIDStr.str());
std::string output = testing::internal::GetCapturedStdout();
std::string output = capture.getCapturedStdout();
expectedOutput << "Auto-detected target based on " << deviceIDStr.str() << " device id: ptl-u-a0\n";
EXPECT_STREQ(output.c_str(), expectedOutput.str().c_str());
EXPECT_EQ(mockOfflineCompiler.hwInfo.platform.usDeviceID, deviceID);
EXPECT_EQ(mockOfflineCompiler.deviceConfig, ptlConfig.value);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -9,6 +9,7 @@
#include "shared/offline_compiler/source/decoder/iga_wrapper.h"
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/product_config_helper.h"
#include "shared/source/os_interface/os_inc_base.h"
@@ -34,14 +35,11 @@ void addSlash(std::string &path) {
}
std::vector<char> readBinaryFile(const std::string &fileName) {
std::ifstream file(fileName, std::ios_base::binary);
if (file.good()) {
size_t length;
file.seekg(0, file.end);
length = static_cast<size_t>(file.tellg());
file.seekg(0, file.beg);
std::vector<char> binary(length);
file.read(binary.data(), length);
size_t length;
std::unique_ptr<char[]> data = loadDataFromFile(fileName.c_str(), length);
if (data) {
std::vector<char> binary(data.get(), data.get() + length);
return binary;
} else {
printf("Error! Couldn't open %s\n", fileName.c_str());
@@ -68,8 +66,13 @@ void istreamToVectorOfStrings(std::istream &input, std::vector<std::string> &lin
}
void readFileToVectorOfStrings(std::vector<std::string> &lines, const std::string &fileName, bool replaceTabs) {
std::ifstream file(fileName);
istreamToVectorOfStrings(file, lines, replaceTabs);
size_t fileSize = 0;
std::unique_ptr<char[]> fileData = loadDataFromFile(fileName.c_str(), fileSize);
if (fileData && fileSize > 0) {
std::istringstream inputStream(std::string(fileData.get(), fileSize));
istreamToVectorOfStrings(inputStream, lines, replaceTabs);
}
}
size_t findPos(const std::vector<std::string> &lines, const std::string &whatToFind) {
@@ -107,4 +110,4 @@ void setProductFamilyForIga(const std::string &device, IgaWrapper *iga, OclocArg
}
}
iga->setProductFamily(productFamily);
}
}

View File

@@ -31,6 +31,7 @@
#include "shared/source/helpers/string.h"
#include "shared/source/helpers/validators.h"
#include "shared/source/release_helper/release_helper.h"
#include "shared/source/utilities/io_functions.h"
#include "offline_compiler_ext.h"
#include "platforms.h"
@@ -43,11 +44,9 @@
#ifdef _WIN32
#include <direct.h>
#define MakeDirectory _mkdir
#define GetCurrentWorkingDirectory _getcwd
#else
#include <sys/stat.h>
#define MakeDirectory(dir) mkdir(dir, 0777)
#define GetCurrentWorkingDirectory getcwd
#endif
@@ -1646,7 +1645,7 @@ void OfflineCompiler::writeOutAllFiles() {
}
void OfflineCompiler::createDir(const std::string &path) {
MakeDirectory(path.c_str());
IoFunctions::mkdirPtr(path.c_str());
}
bool OfflineCompiler::readOptionsFromFile(std::string &options, const std::string &file, OclocArgHelper *helper) {
@@ -1706,4 +1705,4 @@ std::string generateFilePath(const std::string &directory, const std::string &fi
return ret;
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2023 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -19,5 +19,7 @@ rewindFuncPtr rewindPtr = &rewind;
freadFuncPtr freadPtr = &fread;
fwriteFuncPtr fwritePtr = &fwrite;
fflushFuncPtr fflushPtr = &fflush;
mkdirFuncPtr mkdirPtr = &makedir;
} // namespace IoFunctions
} // namespace NEO
} // namespace NEO

View File

@@ -14,18 +14,25 @@
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif
namespace NEO {
namespace IoFunctions {
using fopenFuncPtr = FILE *(*)(const char *, const char *);
using vfprintfFuncPtr = int (*)(FILE *, char const *const formatStr, va_list arg);
using fcloseFuncPtr = int (*)(FILE *);
using getenvFuncPtr = decltype(&getenv);
using fseekFuncPtr = decltype(&fseek);
using ftellFuncPtr = decltype(&ftell);
using fseekFuncPtr = int (*)(FILE *, long int, int);
using ftellFuncPtr = long int (*)(FILE *);
using rewindFuncPtr = decltype(&rewind);
using freadFuncPtr = decltype(&fread);
using freadFuncPtr = size_t (*)(void *, size_t, size_t, FILE *);
using fwriteFuncPtr = decltype(&fwrite);
using fflushFuncPtr = decltype(&fflush);
using mkdirFuncPtr = int (*)(const char *);
extern fopenFuncPtr fopenPtr;
extern vfprintfFuncPtr vfprintfPtr;
@@ -37,6 +44,7 @@ extern rewindFuncPtr rewindPtr;
extern freadFuncPtr freadPtr;
extern fwriteFuncPtr fwritePtr;
extern fflushFuncPtr fflushPtr;
extern mkdirFuncPtr mkdirPtr;
inline int fprintf(FILE *fileDesc, char const *const formatStr, ...) {
va_list args;
@@ -64,5 +72,15 @@ inline char *getEnvironmentVariable(const char *name) {
return nullptr;
}
#ifdef _WIN32
inline int makedir(const char *dirName) {
return _mkdir(dirName);
}
#else
inline int makedir(const char *dirName) {
return mkdir(dirName, 0777);
}
#endif
} // namespace IoFunctions
} // namespace NEO
} // namespace NEO

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include <fcntl.h>
#include <iostream>
#include <string>
#ifdef _WIN32
#include <io.h>
#include <stdio.h>
#else
#include <cstdio>
#include <cstring>
#include <unistd.h>
#endif
class StdoutCapture {
public:
void captureStdout() {
#ifdef _WIN32
_pipe(pipefd, 4096, O_TEXT);
fflush(stdout);
saveStdout = _dup(_fileno(stdout));
_dup2(pipefd[1], _fileno(stdout));
_close(pipefd[1]);
#else
fflush(stdout);
pipe(pipefd);
saveStdout = dup(fileno(stdout));
dup2(pipefd[1], fileno(stdout));
close(pipefd[1]);
#endif
}
std::string getCapturedStdout() {
#ifdef _WIN32
fflush(stdout);
_dup2(saveStdout, _fileno(stdout));
_close(saveStdout);
char buffer[4096];
int count = _read(pipefd[0], buffer, sizeof(buffer) - 1);
if (count > 0) {
buffer[count] = '\0';
return std::string(buffer);
}
return "";
#else
fflush(stdout);
dup2(saveStdout, fileno(stdout));
close(saveStdout);
constexpr size_t bufferSize = 4096;
char buffer[bufferSize];
ssize_t count = read(pipefd[0], buffer, bufferSize - 1);
if (count > 0) {
buffer[count] = '\0';
return std::string(buffer);
}
return "";
#endif
}
private:
int pipefd[2];
int saveStdout;
};

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -19,6 +19,7 @@ rewindFuncPtr rewindPtr = &mockRewind;
freadFuncPtr freadPtr = &mockFread;
fwriteFuncPtr fwritePtr = &mockFwrite;
fflushFuncPtr fflushPtr = &mockFflush;
mkdirFuncPtr mkdirPtr = &mockMkdir;
uint32_t mockFopenCalled = 0;
FILE *mockFopenReturned = reinterpret_cast<FILE *>(0x40);
@@ -43,4 +44,4 @@ const char *openCLDriverName = "igdrcl.dll";
std::unordered_map<std::string, std::string> *mockableEnvValues = nullptr;
} // namespace IoFunctions
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -8,9 +8,9 @@
#include "mock_compilers.h"
#include "shared/source/compiler_interface/compiler_options.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/os_interface/os_inc_base.h"
#include "shared/test/common/helpers/mock_file_io.h"
#include "shared/test/common/helpers/test_files.h"
#include "shared/test/common/mocks/mock_compiler_interface.h"
#include "shared/test/common/mocks/mock_compilers.h"
@@ -20,6 +20,7 @@
#include "ocl_igc_interface/fcl_ocl_device_ctx.h"
#include "ocl_igc_interface/igc_ocl_device_ctx.h"
#include <filesystem>
#include <fstream>
#include <map>
namespace NEO {
@@ -385,19 +386,25 @@ namespace NEO {
template <typename StrT>
std::unique_ptr<unsigned char[]> loadBinaryFile(StrT &&fileName, size_t &fileSize) {
std::ifstream f{fileName, std::ios::binary | std::ios::in | std::ios::ate};
auto end = f.tellg();
f.seekg(0, std::ios::beg);
auto beg = f.tellg();
auto s = static_cast<size_t>(end - beg);
if (s == 0) {
fileSize = 0;
return nullptr;
std::unique_ptr<char[]> data = loadDataFromFile(fileName.c_str(), fileSize);
return std::unique_ptr<unsigned char[]>(reinterpret_cast<unsigned char *>(data.release()));
};
template <typename StrT>
std::unique_ptr<unsigned char[]> loadVirtualBinaryFile(StrT &&fileName, size_t &fileSize) {
std::filesystem::path filePath = std::forward<StrT>(fileName);
std::string fileNameWithExtension = filePath.filename().string();
if (!virtualFileExists(fileNameWithExtension)) {
return loadBinaryFile(fileName, fileSize);
}
std::unique_ptr<unsigned char[]> data{new unsigned char[s]};
f.read(reinterpret_cast<char *>(data.get()), s);
fileSize = s;
return data;
std::unique_ptr<char[]> charData = loadDataFromVirtualFile(fileNameWithExtension.c_str(), fileSize);
std::unique_ptr<unsigned char[]> ucharData(new unsigned char[fileSize]);
std::memcpy(ucharData.get(), charData.get(), fileSize);
return ucharData;
};
void translate(bool usingIgc, CIF::Builtins::BufferSimple *src, CIF::Builtins::BufferSimple *options,
@@ -474,7 +481,7 @@ void translate(bool usingIgc, CIF::Builtins::BufferSimple *src, CIF::Builtins::B
out->setOutput(debugVars.binaryToReturn, debugVars.binaryToReturnSize);
} else {
size_t fileSize = 0;
auto fileData = loadBinaryFile(inputFile, fileSize);
auto fileData = loadVirtualBinaryFile(inputFile, fileSize);
out->setOutput(fileData.get(), fileSize);
if (fileSize == 0) {
@@ -486,7 +493,7 @@ void translate(bool usingIgc, CIF::Builtins::BufferSimple *src, CIF::Builtins::B
out->setDebugData(debugVars.debugDataToReturn, debugVars.debugDataToReturnSize);
} else {
size_t fileSize = 0;
auto fileData = loadBinaryFile(debugFile, fileSize);
auto fileData = loadVirtualBinaryFile(debugFile, fileSize);
out->setDebugData(fileData.get(), fileSize);
}
} else {
@@ -735,4 +742,4 @@ std::vector<char> MockCompilerInterface::getDummyGenBinary() {
void MockCompilerInterface::releaseDummyGenBinary() {
}
} // namespace NEO
} // namespace NEO

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2024 Intel Corporation
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -108,5 +108,9 @@ inline int mockFflush(FILE *stream) {
return 0;
}
inline int mockMkdir(const char *filename) {
return 0;
}
} // namespace IoFunctions
} // namespace NEO
} // namespace NEO