Skip to content

Commit b75fbe8

Browse files
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 <[email protected]>
1 parent 679d3a2 commit b75fbe8

24 files changed

+2070
-1131
lines changed

opencl/test/unit_test/offline_compiler/decoder/decoder_tests.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "shared/source/helpers/hw_info.h"
1111
#include "shared/source/helpers/product_config_helper.h"
1212
#include "shared/test/common/helpers/gtest_helpers.h"
13+
#include "shared/test/common/helpers/mock_file_io.h"
1314
#include "shared/test/common/helpers/test_files.h"
1415
#include "shared/test/common/helpers/variable_backup.h"
1516

@@ -493,7 +494,6 @@ TEST(DecoderTests, GivenProperStructWhenReadingStructFieldsThenFieldsVectorGetsP
493494

494495
TEST(DecoderTests, GivenProperPatchListFileWhenParsingTokensThenFileIsParsedCorrectly) {
495496
MockDecoder decoder;
496-
decoder.pathToPatch = clFiles;
497497
decoder.parseTokens();
498498

499499
EXPECT_EQ(static_cast<uint32_t>(28), (decoder.programHeader.size));
@@ -532,31 +532,12 @@ TEST(DecoderTests, GivenProperPatchListFileWhenParsingTokensThenFileIsParsedCorr
532532
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.kernelHeader.fields[8].size));
533533
EXPECT_EQ("KernelUnpaddedSize", (decoder.kernelHeader.fields[8].name));
534534

535-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[2]->size));
536-
EXPECT_EQ("PATCH_TOKEN_STATE_SIP", (decoder.patchTokens[2]->name));
537-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[2]->fields[0].size));
538-
EXPECT_EQ("SystemKernelOffset", (decoder.patchTokens[2]->fields[0].name));
539-
540-
EXPECT_EQ(static_cast<uint8_t>(12), decoder.patchTokens[5]->size);
541-
EXPECT_EQ("PATCH_TOKEN_SAMPLER_STATE_ARRAY", decoder.patchTokens[5]->name);
542-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[0].size));
543-
EXPECT_EQ("Offset", (decoder.patchTokens[5]->fields[0].name));
544-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[1].size));
545-
EXPECT_EQ("Count", (decoder.patchTokens[5]->fields[1].name));
546-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[5]->fields[2].size));
547-
EXPECT_EQ("BorderColorOffset", (decoder.patchTokens[5]->fields[2].name));
548-
549535
EXPECT_EQ(static_cast<uint8_t>(8), decoder.patchTokens[42]->size);
550536
EXPECT_EQ("PATCH_TOKEN_ALLOCATE_CONSTANT_MEMORY_SURFACE_PROGRAM_BINARY_INFO", decoder.patchTokens[42]->name);
551537
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[42]->fields[0].size));
552538
EXPECT_EQ("ConstantBufferIndex", (decoder.patchTokens[42]->fields[0].name));
553539
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[42]->fields[1].size));
554540
EXPECT_EQ("InlineDataSize", (decoder.patchTokens[42]->fields[1].name));
555-
556-
EXPECT_EQ(static_cast<uint8_t>(4), decoder.patchTokens[19]->size);
557-
EXPECT_EQ("PATCH_TOKEN_MEDIA_INTERFACE_DESCRIPTOR_LOAD", decoder.patchTokens[19]->name);
558-
EXPECT_EQ(static_cast<uint8_t>(4), (decoder.patchTokens[19]->fields[0].size));
559-
EXPECT_EQ("InterfaceDescriptorDataOffset", (decoder.patchTokens[19]->fields[0].name));
560541
}
561542

562543
TEST(DecoderTests, WhenPathToPatchTokensNotProvidedThenUseDefaults) {
@@ -618,7 +599,6 @@ TEST(DecoderTests, GivenValidBinaryWithoutPatchTokensWhenProcessingBinaryThenBin
618599

619600
std::stringstream ptmFile;
620601
MockDecoder decoder;
621-
decoder.pathToPatch = clFiles;
622602
decoder.pathToDump = "non_existing_folder/";
623603
decoder.parseTokens();
624604

@@ -645,9 +625,10 @@ TEST(DecoderTests, givenBinaryWithKernelBinaryHeaderWhenAtLeastOneOfTheKernelSiz
645625

646626
std::stringstream ptmFile;
647627
MockDecoder decoder{false};
648-
decoder.pathToPatch = clFiles;
628+
decoder.mockArgHelper->messagePrinter.setSuppressMessages(true);
649629
decoder.pathToDump = "non_existing_folder/";
650630
decoder.parseTokens();
631+
decoder.mockArgHelper->messagePrinter.setSuppressMessages(false);
651632

652633
std::string binaryString = binarySS.str();
653634
std::vector<unsigned char> binary(binaryString.begin(), binaryString.end());
@@ -696,15 +677,14 @@ TEST(DecoderTests, GivenValidBinaryWhenProcessingBinaryThenProgramAndKernelAndPa
696677
std::vector<char> binary(binaryString.begin(), binaryString.end());
697678
std::stringstream ptmFile;
698679
MockDecoder decoder;
699-
decoder.pathToPatch = clFiles;
700680
decoder.pathToDump = "non_existing_folder/";
701681
decoder.parseTokens();
702682

703683
const void *ptr = reinterpret_cast<void *>(binary.data());
704684
int retVal = decoder.processBinary(ptr, binary.size(), ptmFile);
705685
EXPECT_EQ(0, retVal);
706686

707-
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";
687+
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";
708688
EXPECT_EQ(expectedOutput, ptmFile.str());
709689
EXPECT_TRUE(decoder.getMockIga()->disasmWasCalled);
710690
EXPECT_FALSE(decoder.getMockIga()->asmWasCalled);
@@ -817,4 +797,4 @@ TEST(DecoderHelperTest, GivenProductFamilyWhenTranslatingToIgaGenBaseThenExpecte
817797
}
818798
}
819799

820-
} // namespace NEO
800+
} // namespace NEO

opencl/test/unit_test/offline_compiler/decoder/encoder_tests.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2018-2023 Intel Corporation
2+
* Copyright (C) 2018-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -8,6 +8,7 @@
88
#include "shared/offline_compiler/source/decoder/binary_decoder.h"
99
#include "shared/source/helpers/aligned_memory.h"
1010
#include "shared/source/helpers/array_count.h"
11+
#include "shared/test/common/helpers/stdout_capture.h"
1112
#include "shared/test/common/helpers/test_files.h"
1213

1314
#include "opencl/test/unit_test/offline_compiler/environment.h"
@@ -50,9 +51,10 @@ TEST(EncoderTests, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingThenError
5051
constexpr auto suppressMessages{false};
5152
MockEncoder encoder{suppressMessages};
5253

53-
::testing::internal::CaptureStdout();
54+
StdoutCapture capture;
55+
capture.captureStdout();
5456
const auto result = encoder.validateInput(args);
55-
const auto output{::testing::internal::GetCapturedStdout()};
57+
const auto output{capture.getCapturedStdout()};
5658

5759
EXPECT_EQ(-1, result);
5860

@@ -111,9 +113,10 @@ TEST(EncoderTests, GivenMissingDumpFlagAndArgHelperOutputEnabledWhenParsingValid
111113

112114
encoder.mockArgHelper->hasOutput = true;
113115

114-
::testing::internal::CaptureStdout();
116+
StdoutCapture capture;
117+
capture.captureStdout();
115118
const auto result = encoder.validateInput(args);
116-
const auto output{::testing::internal::GetCapturedStdout()};
119+
const auto output{capture.getCapturedStdout()};
117120

118121
EXPECT_EQ(0, result);
119122
EXPECT_TRUE(output.empty()) << output;
@@ -126,9 +129,10 @@ TEST(EncoderTests, GivenMissingPTMFileWhenEncodingThenErrorIsReturnedAndLogIsPri
126129
constexpr auto suppressMessages{false};
127130
MockEncoder encoder{suppressMessages};
128131

129-
::testing::internal::CaptureStdout();
132+
StdoutCapture capture;
133+
capture.captureStdout();
130134
const auto result = encoder.encode();
131-
const auto output{::testing::internal::GetCapturedStdout()};
135+
const auto output{capture.getCapturedStdout()};
132136

133137
EXPECT_EQ(-1, result);
134138
EXPECT_EQ("Error! Couldn't find PTM.txt", output);
@@ -586,4 +590,4 @@ TEST(EncoderTests, WhenProcessingDeviceBinaryAndAsmIsAvailableThenAseembleItWith
586590
EXPECT_EQ(encoder.filesMap["kernel_KernelHeap.asm"], encoder.getMockIga()->receivedAsm);
587591
}
588592

589-
} // namespace NEO
593+
} // namespace NEO

opencl/test/unit_test/offline_compiler/decoder/zebin_manipulator_tests.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 Intel Corporation
2+
* Copyright (C) 2022-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -10,7 +10,10 @@
1010
#include "shared/source/device_binary_format/elf/elf.h"
1111
#include "shared/source/device_binary_format/elf/elf_encoder.h"
1212
#include "shared/source/helpers/product_config_helper.h"
13+
#include "shared/test/common/helpers/stdout_capture.h"
14+
#include "shared/test/common/helpers/variable_backup.h"
1315
#include "shared/test/common/mocks/mock_elf.h"
16+
#include "shared/test/common/mocks/mock_io_functions.h"
1417
#include "shared/test/common/mocks/mock_modules_zebin.h"
1518
#include "shared/test/common/test_macros/test.h"
1619

@@ -107,6 +110,7 @@ version: '1.15'
107110
};
108111

109112
TEST(ZebinManipulatorTests, GivenValidZebinWhenItIsDisassembledAndAssembledBackThenItIsSameBinary) {
113+
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
110114
MockZebin<NEO::Elf::EI_CLASS_32> mockZebin32;
111115
MockZebin<NEO::Elf::EI_CLASS_64> mockZebin64;
112116
for (const auto zebin : {&mockZebin32.storage, &mockZebin64.storage}) {
@@ -210,9 +214,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenInvalidInputWhenValidatingInputT
210214
"asm/disasm",
211215
"-unknown_arg"};
212216

213-
testing::internal::CaptureStdout();
217+
StdoutCapture capture;
218+
capture.captureStdout();
214219
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
215-
const auto output{testing::internal::GetCapturedStdout()};
220+
const auto output{capture.getCapturedStdout()};
216221

217222
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
218223
EXPECT_EQ("Unknown argument -unknown_arg\n", output);
@@ -224,9 +229,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenMissingFileWhenValidatingInputTh
224229
"-dump",
225230
"./dump/"};
226231

227-
testing::internal::CaptureStdout();
232+
StdoutCapture capture;
233+
capture.captureStdout();
228234
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
229-
const auto output{testing::internal::GetCapturedStdout()};
235+
const auto output{capture.getCapturedStdout()};
230236

231237
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
232238
EXPECT_EQ("Error: Missing -file argument\n", output);
@@ -238,9 +244,10 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenMissingSecondPartOfTheArgumentWh
238244
"-arg"};
239245
for (const auto halfArg : {"-file", "-device", "-dump"}) {
240246
args[2] = halfArg;
241-
testing::internal::CaptureStdout();
247+
StdoutCapture capture;
248+
capture.captureStdout();
242249
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
243-
const auto output{testing::internal::GetCapturedStdout()};
250+
const auto output{capture.getCapturedStdout()};
244251

245252
EXPECT_EQ(OCLOC_INVALID_COMMAND_LINE, retVal);
246253
const auto expectedOutput = "Unknown argument " + std::string(halfArg) + "\n";
@@ -253,9 +260,11 @@ TEST_F(ZebinManipulatorValidateInputTests, GivenValidArgsButDumpNotSpecifiedWhen
253260
"asm/disasm",
254261
"-file",
255262
"binary.bin"};
256-
testing::internal::CaptureStdout();
263+
264+
StdoutCapture capture;
265+
capture.captureStdout();
257266
auto retVal = NEO::Zebin::Manipulator::validateInput(args, &iga, &argHelper, arguments);
258-
const auto output{testing::internal::GetCapturedStdout()};
267+
const auto output{capture.getCapturedStdout()};
259268

260269
EXPECT_EQ(OCLOC_SUCCESS, retVal);
261270
EXPECT_EQ("Warning: Path to dump -dump not specified. Using \"./dump/\" as dump folder.\n", output);
@@ -567,6 +576,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidIntelGTNotesSectionWhenGetIntelGTNotesThen
567576
}
568577

569578
TEST_F(ZebinEncoderTests, GivenInvalidSymtabFileWhenAppendSymtabThenErrorIsReturned) {
579+
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
580+
570581
MockElfEncoder elfEncoder;
571582
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
572583
sectionInfo.name = ".symtab";
@@ -578,6 +589,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidSymtabFileWhenAppendSymtabThenErrorIsRetur
578589
}
579590

580591
TEST_F(ZebinEncoderTests, GivenInvalidRelFileWhenAppendRelThenErrorIsReturned) {
592+
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
593+
581594
MockElfEncoder elfEncoder;
582595
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
583596
sectionInfo.name = ".rel.text";
@@ -589,6 +602,8 @@ TEST_F(ZebinEncoderTests, GivenInvalidRelFileWhenAppendRelThenErrorIsReturned) {
589602
}
590603

591604
TEST_F(ZebinEncoderTests, GivenInvalidRelaFileWhenAppendRelaThenErrorIsReturned) {
605+
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * { return NULL; });
606+
592607
MockElfEncoder elfEncoder;
593608
NEO::Zebin::Manipulator::SectionInfo sectionInfo;
594609
sectionInfo.name = ".rela.text";
@@ -649,4 +664,4 @@ TEST_F(ZebinEncoderTests, GivenMissingFileWhenCheckIfAllFilesExistThenErrorIsRet
649664
TEST_F(ZebinEncoderTests, WhenPrintHelpIsCalledThenHelpIsPrinted) {
650665
encoder.printHelp();
651666
EXPECT_FALSE(getOutput().empty());
652-
}
667+
}

opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 Intel Corporation
2+
* Copyright (C) 2020-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -69,38 +69,48 @@ class MockOclocArgHelper : public OclocArgHelper {
6969
return std::vector<char>(file.begin(), file.end());
7070
}
7171

72+
std::string getLog() {
73+
return messagePrinter.getLog().str();
74+
}
75+
7276
protected:
7377
bool fileExists(const std::string &filename) const override {
7478
if (filesMap.find(filename) != filesMap.end()) {
7579
return true;
7680
}
7781

78-
return OclocArgHelper::fileExists(filename);
82+
return OclocArgHelper::sourceFileExists(filename);
7983
}
8084

8185
std::unique_ptr<char[]> loadDataFromFile(const std::string &filename, size_t &retSize) override {
8286
if (callBaseLoadDataFromFile) {
8387
return OclocArgHelper::loadDataFromFile(filename, retSize);
8488
}
8589

90+
if (Source *s = findSourceFile(filename)) {
91+
auto size = s->length;
92+
std::unique_ptr<char[]> ret(new char[size]());
93+
memcpy_s(ret.get(), size, s->data, s->length);
94+
retSize = s->length;
95+
return ret;
96+
}
97+
8698
if (shouldLoadDataFromFileReturnZeroSize) {
8799
retSize = 0;
88100
return {};
89101
}
90102

91103
if (!fileExists(filename)) {
92104
return OclocArgHelper::loadDataFromFile(filename, retSize);
93-
}
94-
95-
const auto &file = filesMap[filename];
96-
97-
std::unique_ptr<char[]> result{new char[file.size() + 1]};
98-
std::copy(file.begin(), file.end(), result.get());
99-
result[file.size()] = '\0';
100-
101-
retSize = file.size() + 1;
105+
} else {
106+
const auto &file = filesMap[filename];
107+
std::unique_ptr<char[]> result{new char[file.size() + 1]};
108+
std::copy(file.begin(), file.end(), result.get());
109+
result[file.size()] = '\0';
110+
retSize = file.size() + 1;
102111

103-
return result;
112+
return result;
113+
}
104114
}
105115

106116
void saveOutput(const std::string &filename, const void *pData, const size_t &dataSize) override {

opencl/test/unit_test/offline_compiler/mock/mock_multi_command.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2023 Intel Corporation
2+
* Copyright (C) 2022-2025 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -32,8 +32,9 @@ class MockMultiCommand : public MultiCommand {
3232
using MultiCommand::splitLineInSeparateArgs;
3333

3434
MockMultiCommand() : MultiCommand{} {
35+
filesMap[clCopybufferFilename] = kernelSources;
3536
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
36-
uniqueHelper->setAllCallBase(true);
37+
uniqueHelper->setAllCallBase(false);
3738
argHelper = uniqueHelper.get();
3839
}
3940

@@ -53,6 +54,8 @@ class MockMultiCommand : public MultiCommand {
5354
std::unique_ptr<MockOclocArgHelper> uniqueHelper{};
5455
int singleBuildCalledCount{0};
5556
bool callBaseSingleBuild{true};
57+
const std::string clCopybufferFilename = "some_kernel.cl";
58+
std::string kernelSources = "example_kernel(){}";
5659
};
5760

58-
} // namespace NEO
61+
} // namespace NEO

opencl/test/unit_test/offline_compiler/mock/mock_offline_compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NEO {
1515

1616
MockOfflineCompiler::MockOfflineCompiler() : OfflineCompiler() {
1717
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
18-
uniqueHelper->setAllCallBase(true);
18+
uniqueHelper->setAllCallBase(false);
1919
argHelper = uniqueHelper.get();
2020

2121
auto uniqueFclFacadeMock = std::make_unique<MockOclocFclFacade>(argHelper);
@@ -88,4 +88,4 @@ void MockOfflineCompiler::createDir(const std::string &path) {
8888
}
8989
}
9090

91-
} // namespace NEO
91+
} // namespace NEO

0 commit comments

Comments
 (0)