Skip to content

Commit 8f1004e

Browse files
Clean-up output after each test unit in offline compiler
Aditionally fix: - GivenSpecifiedOutputDirWithProductConfigValueWhenBuilding... ...MultiCommandThenSuccessIsReturned - GivenArgsWhenBuildingWithDeviceConfigValueThenBuildSucceeds tests which used files created by previously run tests. Now both of these tests are a separate units. Related-To: NEO-6606 Signed-off-by: Fabian Zwolinski <[email protected]>
1 parent 43725fe commit 8f1004e

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*
66
*/
77

8+
#pragma once
9+
810
#include "shared/offline_compiler/source/ocloc_arg_helper.h"
911
#include "shared/source/helpers/string.h"
1012

13+
#include "gtest/gtest.h"
14+
1115
#include <algorithm>
1216
#include <map>
1317
#include <string>
@@ -22,22 +26,47 @@ class MockOclocArgHelper : public OclocArgHelper {
2226
bool interceptOutput{false};
2327
bool shouldReturnReadingError{false};
2428
FilesMap interceptedFiles;
29+
std::vector<std::string> createdFiles{};
30+
bool callBaseFileExists = false;
31+
bool callBaseReadBinaryFile = false;
32+
bool callBaseLoadDataFromFile = false;
33+
bool callBaseSaveOutput = false;
2534

26-
MockOclocArgHelper(FilesMap &filesMap) : OclocArgHelper(
27-
0, nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr),
35+
MockOclocArgHelper(FilesMap &filesMap) : OclocArgHelper(0, nullptr, nullptr, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr),
2836
filesMap(filesMap){};
2937

38+
~MockOclocArgHelper() {
39+
cleanUpOutput();
40+
}
41+
42+
void setAllCallBase(bool value) {
43+
callBaseFileExists = value;
44+
callBaseReadBinaryFile = value;
45+
callBaseLoadDataFromFile = value;
46+
callBaseSaveOutput = value;
47+
}
48+
3049
protected:
3150
bool fileExists(const std::string &filename) const override {
51+
if (callBaseFileExists) {
52+
return OclocArgHelper::fileExists(filename);
53+
}
3254
return filesMap.find(filename) != filesMap.end();
3355
}
3456

3557
std::vector<char> readBinaryFile(const std::string &filename) override {
58+
if (callBaseReadBinaryFile) {
59+
return OclocArgHelper::readBinaryFile(filename);
60+
}
3661
auto file = filesMap[filename];
3762
return std::vector<char>(file.begin(), file.end());
3863
}
3964

4065
std::unique_ptr<char[]> loadDataFromFile(const std::string &filename, size_t &retSize) override {
66+
if (callBaseLoadDataFromFile) {
67+
return OclocArgHelper::loadDataFromFile(filename, retSize);
68+
}
69+
4170
if (shouldReturnReadingError) {
4271
return nullptr;
4372
}
@@ -62,7 +91,21 @@ class MockOclocArgHelper : public OclocArgHelper {
6291

6392
memcpy_s(fileContent.data(), fileContent.size(), pData, dataSize);
6493
} else {
94+
if (callBaseSaveOutput) {
95+
createdFiles.push_back(filename.c_str());
96+
}
6597
OclocArgHelper::saveOutput(filename, pData, dataSize);
6698
}
6799
}
100+
101+
void cleanUpOutput() {
102+
for (const auto &fileName : createdFiles) {
103+
int retVal = remove(fileName.c_str());
104+
EXPECT_EQ(0, retVal);
105+
if (retVal != 0) {
106+
auto errMsg = "Error deleting file: " + fileName;
107+
perror(errMsg.c_str());
108+
}
109+
}
110+
}
68111
};

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
*/
77

88
#pragma once
9+
910
#include "shared/offline_compiler/source/offline_compiler.h"
1011

12+
#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h"
13+
1114
#include <string>
1215

1316
namespace NEO {
@@ -48,7 +51,8 @@ class MockOfflineCompiler : public OfflineCompiler {
4851
using OfflineCompiler::useOptionsSuffix;
4952

5053
MockOfflineCompiler() : OfflineCompiler() {
51-
uniqueHelper = std::make_unique<OclocArgHelper>();
54+
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
55+
uniqueHelper->setAllCallBase(true);
5256
argHelper = uniqueHelper.get();
5357
}
5458
~MockOfflineCompiler() override = default;
@@ -79,14 +83,16 @@ class MockOfflineCompiler : public OfflineCompiler {
7983
}
8084

8185
void clearLog() {
82-
uniqueHelper = std::make_unique<OclocArgHelper>();
86+
uniqueHelper = std::make_unique<MockOclocArgHelper>(filesMap);
87+
uniqueHelper->setAllCallBase(true);
8388
argHelper = uniqueHelper.get();
8489
}
8590

91+
std::map<std::string, std::string> filesMap{};
8692
int buildSourceCodeStatus = 0;
8793
bool overrideBuildSourceCodeStatus = false;
8894
uint32_t generateElfBinaryCalled = 0u;
8995
uint32_t writeOutAllFilesCalled = 0u;
90-
std::unique_ptr<OclocArgHelper> uniqueHelper;
96+
std::unique_ptr<MockOclocArgHelper> uniqueHelper;
9197
};
9298
} // namespace NEO

opencl/test/unit_test/offline_compiler/offline_compiler_tests.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,14 @@ TEST_F(MultiCommandTests, GivenSpecifiedOutputDirWithProductConfigValueWhenBuild
185185
if (allEnabledDeviceConfigs.empty()) {
186186
GTEST_SKIP();
187187
}
188-
auto deviceMapConfig = allEnabledDeviceConfigs[0];
189-
auto configStr = oclocArgHelperWithoutInput->parseProductConfigFromValue(deviceMapConfig.config);
188+
189+
std::string configStr;
190+
for (auto &deviceMapConfig : allEnabledDeviceConfigs) {
191+
if (productFamily == deviceMapConfig.hwInfo->platform.eProductFamily) {
192+
configStr = oclocArgHelperWithoutInput->parseProductConfigFromValue(deviceMapConfig.config);
193+
break;
194+
}
195+
}
190196

191197
nameOfFileWithArgs = "test_files/ImAMulitiComandMinimalGoodFile.txt";
192198
std::vector<std::string> argv = {
@@ -705,15 +711,21 @@ TEST_F(OfflineCompilerTests, GivenArgsWhenBuildingWithDeviceConfigValueThenBuild
705711
if (allEnabledDeviceConfigs.empty()) {
706712
return;
707713
}
708-
auto deviceMapConfig = allEnabledDeviceConfigs[0];
709-
auto configString = oclocArgHelperWithoutInput->parseProductConfigFromValue(deviceMapConfig.config);
714+
715+
std::string configStr;
716+
for (auto &deviceMapConfig : allEnabledDeviceConfigs) {
717+
if (productFamily == deviceMapConfig.hwInfo->platform.eProductFamily) {
718+
configStr = oclocArgHelperWithoutInput->parseProductConfigFromValue(deviceMapConfig.config);
719+
break;
720+
}
721+
}
710722

711723
std::vector<std::string> argv = {
712724
"ocloc",
713725
"-file",
714726
"test_files/copybuffer.cl",
715727
"-device",
716-
configString};
728+
configStr};
717729

718730
pOfflineCompiler = OfflineCompiler::create(argv.size(), argv, true, retVal, oclocArgHelperWithoutInput.get());
719731

@@ -1207,9 +1219,6 @@ TEST(OfflineCompilerTest, givenSpvOnlyOptionPassedWhenCmdLineParsedThenGenerateO
12071219
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "bc") || compilerOutputExists("myOutputFileName", "spv"));
12081220
EXPECT_FALSE(compilerOutputExists("myOutputFileName", "bin"));
12091221
EXPECT_FALSE(compilerOutputExists("myOutputFileName", "gen"));
1210-
1211-
compilerOutputRemove("myOutputFileName", "bc");
1212-
compilerOutputRemove("myOutputFileName", "spv");
12131222
}
12141223

12151224
TEST(OfflineCompilerTest, GivenKernelWhenNoCharAfterKernelSourceThenBuildWithSuccess) {
@@ -1416,11 +1425,6 @@ TEST(OfflineCompilerTest, givenOutputFileOptionWhenSourceIsCompiledThenOutputFil
14161425
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "bc") || compilerOutputExists("myOutputFileName", "spv"));
14171426
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "bin"));
14181427
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "gen"));
1419-
1420-
compilerOutputRemove("myOutputFileName", "bc");
1421-
compilerOutputRemove("myOutputFileName", "spv");
1422-
compilerOutputRemove("myOutputFileName", "bin");
1423-
compilerOutputRemove("myOutputFileName", "gen");
14241428
}
14251429

14261430
TEST(OfflineCompilerTest, givenDebugDataAvailableWhenSourceIsBuiltThenDebugDataFileIsCreated) {
@@ -1459,12 +1463,6 @@ TEST(OfflineCompilerTest, givenDebugDataAvailableWhenSourceIsBuiltThenDebugDataF
14591463
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "gen"));
14601464
EXPECT_TRUE(compilerOutputExists("myOutputFileName", "dbg"));
14611465

1462-
compilerOutputRemove("myOutputFileName", "bc");
1463-
compilerOutputRemove("myOutputFileName", "spv");
1464-
compilerOutputRemove("myOutputFileName", "bin");
1465-
compilerOutputRemove("myOutputFileName", "gen");
1466-
compilerOutputRemove("myOutputFileName", "dbg");
1467-
14681466
NEO::setIgcDebugVars(gEnvironment->igcDebugVars);
14691467
}
14701468

opencl/test/unit_test/offline_compiler/offline_compiler_tests.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "shared/offline_compiler/source/ocloc_error_code.h"
1212
#include "shared/offline_compiler/source/offline_compiler.h"
1313

14+
#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h"
15+
1416
#include "gtest/gtest.h"
1517

1618
#include <cstdint>
@@ -22,7 +24,13 @@ class OfflineCompilerTests : public ::testing::Test {
2224
public:
2325
OfflineCompiler *pOfflineCompiler = nullptr;
2426
int retVal = OclocErrorCode::SUCCESS;
25-
std::unique_ptr<OclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<OclocArgHelper>();
27+
std::map<std::string, std::string> filesMap;
28+
std::unique_ptr<MockOclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<MockOclocArgHelper>(filesMap);
29+
30+
protected:
31+
void SetUp() override {
32+
oclocArgHelperWithoutInput->setAllCallBase(true);
33+
}
2634
};
2735

2836
class MultiCommandTests : public ::testing::Test {
@@ -34,6 +42,12 @@ class MultiCommandTests : public ::testing::Test {
3442
std::string nameOfFileWithArgs;
3543
std::string outFileList;
3644
int retVal = OclocErrorCode::SUCCESS;
37-
std::unique_ptr<OclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<OclocArgHelper>();
45+
std::map<std::string, std::string> filesMap;
46+
std::unique_ptr<MockOclocArgHelper> oclocArgHelperWithoutInput = std::make_unique<MockOclocArgHelper>(filesMap);
47+
48+
protected:
49+
void SetUp() override {
50+
oclocArgHelperWithoutInput->setAllCallBase(true);
51+
}
3852
};
3953
} // namespace NEO

0 commit comments

Comments
 (0)