refactor: Modernize writeDataToFile function

Signed-off-by: Marcel Skierkowski <marcel.skierkowski@intel.com>
This commit is contained in:
Marcel Skierkowski
2025-04-11 11:24:23 +00:00
committed by Compute-Runtime-Automation
parent dd3d294f87
commit e82be94368
18 changed files with 74 additions and 58 deletions

View File

@@ -267,8 +267,7 @@ TEST_F(BuiltInTests, WhenBuildingListOfBuiltinsThenBuiltinsHaveBeenGenerated) {
#define GENERATE_NEW_HASH_FOR_BUILT_INS 0
#if GENERATE_NEW_HASH_FOR_BUILT_INS
std::cout << "writing builtins to file: " << hashName << std::endl;
const char *pData = allBuiltIns.c_str();
writeDataToFile(hashName.c_str(), pData, allBuiltIns.length());
writeDataToFile(hashName.c_str(), allBuiltIns);
#endif
}
}

View File

@@ -54,15 +54,18 @@ class OclocApiTest : public ::testing::Test {
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());
constexpr unsigned char mockByteArray[] = {0x01, 0x02, 0x03, 0x04};
std::string_view byteArrayView(reinterpret_cast<const char *>(mockByteArray), sizeof(mockByteArray));
writeDataToFile(spvFile.c_str(), byteArrayView);
writeDataToFile(binFile.c_str(), byteArrayView);
writeDataToFile(dbgFile.c_str(), byteArrayView);
writeDataToFile(clCopybufferFilename.c_str(), kernelSources);
}
const std::string clCopybufferFilename = "some_kernel.cl";
std::string kernelSources = "example_kernel(){}";
const std::string_view kernelSources = "example_kernel(){}";
};
TEST_F(OclocApiTest, WhenGoodArgsAreGivenThenSuccessIsReturned) {
VariableBackup<decltype(NEO::IoFunctions::fopenPtr)> mockFopen(&NEO::IoFunctions::fopenPtr, [](const char *filename, const char *mode) -> FILE * {

View File

@@ -27,10 +27,13 @@ class OclocTest : public ::testing::Test {
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());
constexpr unsigned char mockByteArray[] = {0x01, 0x02, 0x03, 0x04};
std::string_view byteArrayView(reinterpret_cast<const char *>(mockByteArray), sizeof(mockByteArray));
writeDataToFile(spvFile.c_str(), byteArrayView);
writeDataToFile(binFile.c_str(), byteArrayView);
writeDataToFile(dbgFile.c_str(), byteArrayView);
}
protected:

View File

@@ -38,10 +38,13 @@ class OfflineCompilerTests : public ::testing::Test {
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());
constexpr unsigned char mockByteArray[] = {0x01, 0x02, 0x03, 0x04};
std::string_view byteArrayView(reinterpret_cast<const char *>(mockByteArray), sizeof(mockByteArray));
writeDataToFile(spvFile.c_str(), byteArrayView);
writeDataToFile(binFile.c_str(), byteArrayView);
writeDataToFile(dbgFile.c_str(), byteArrayView);
filesMap[clCopybufferFilename] = OfflineCompilerTests::kernelSources;
oclocArgHelperWithoutInput->setAllCallBase(false);
@@ -67,10 +70,14 @@ class MultiCommandTests : public ::testing::Test {
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());
constexpr unsigned char mockByteArray[] = {0x01, 0x02, 0x03, 0x04};
std::string_view byteArrayView(reinterpret_cast<const char *>(mockByteArray), sizeof(mockByteArray));
writeDataToFile(spvFile.c_str(), byteArrayView);
writeDataToFile(binFile.c_str(), byteArrayView);
writeDataToFile(dbgFile.c_str(), byteArrayView);
filesMap[clCopybufferFilename] = kernelSources;
oclocArgHelperWithoutInput->setAllCallBase(false);
}

View File

@@ -37,10 +37,14 @@ 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());
constexpr unsigned char mockByteArray[] = {0x01, 0x02, 0x03, 0x04};
std::string_view byteArrayView(reinterpret_cast<const char *>(mockByteArray), sizeof(mockByteArray));
writeDataToFile(spvFile.c_str(), byteArrayView);
writeDataToFile(binFile.c_str(), byteArrayView);
writeDataToFile(dbgFile.c_str(), byteArrayView);
MockCompilerDebugVars igcDebugVars{gEnvironment->igcDebugVars};
igcDebugVars.binaryToReturn = binaryToReturn;
igcDebugVars.binaryToReturnSize = sizeof(binaryToReturn);

View File

@@ -210,6 +210,6 @@ void OclocArgHelper::saveOutput(const std::string &filename, const void *pData,
if (outputEnabled()) {
addOutput(filename, pData, dataSize);
} else {
writeDataToFile(filename.c_str(), pData, dataSize);
writeDataToFile(filename.c_str(), std::string_view(static_cast<const char *>(pData), dataSize));
}
}

View File

@@ -22,7 +22,6 @@
#include "shared/source/os_interface/os_context.h"
#include <cstdint>
namespace NEO {
BuiltIns::BuiltIns() {
@@ -49,7 +48,7 @@ const SipKernel &BuiltIns::getSipKernel(SipKernelType type, Device &device) {
if (NEO::debugManager.flags.DumpSipHeaderFile.get() != "unk") {
std::string name = NEO::debugManager.flags.DumpSipHeaderFile.get() + "_header.bin";
writeDataToFile(name.c_str(), stateSaveAreaHeader.data(), stateSaveAreaHeader.size());
writeDataToFile(name.c_str(), std::string_view(stateSaveAreaHeader.data(), stateSaveAreaHeader.size()));
}
const auto allocType = AllocationType::kernelIsaInternal;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -16,17 +16,15 @@
size_t writeDataToFile(
const char *filename,
const void *pData,
size_t dataSize) {
std::string_view data) {
FILE *fp = nullptr;
size_t nsize = 0;
DEBUG_BREAK_IF(nullptr == pData);
DEBUG_BREAK_IF(nullptr == filename);
fopen_s(&fp, filename, "wb");
if (fp) {
nsize = fwrite(pData, sizeof(unsigned char), dataSize, fp);
nsize = fwrite(data.data(), sizeof(unsigned char), data.size(), fp);
fclose(fp);
}

View File

@@ -14,6 +14,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
std::unique_ptr<char[]> loadDataFromFile(
const char *filename,
@@ -21,8 +22,7 @@ std::unique_ptr<char[]> loadDataFromFile(
size_t writeDataToFile(
const char *filename,
const void *pData,
size_t dataSize);
std::string_view data);
bool fileExists(const std::string &fileName);
bool fileExistsHasSize(const std::string &fileName);

View File

@@ -58,5 +58,5 @@ void dumpFileIncrement(const char *data, size_t dataSize, const std::string &fil
filenameWithExt = filename + "_" + std::to_string(suffix) + extension;
suffix++;
}
writeDataToFile(filenameWithExt.c_str(), data, dataSize);
writeDataToFile(filenameWithExt.c_str(), std::string_view(data, dataSize));
}

View File

@@ -22,15 +22,13 @@ extern std::map<std::string, std::stringstream> virtualFileList;
size_t writeDataToFile(
const char *filename,
const void *pData,
size_t dataSize) {
std::string_view data) {
DEBUG_BREAK_IF(nullptr == pData);
DEBUG_BREAK_IF(nullptr == filename);
NEO::virtualFileList[filename] << std::string(static_cast<const char *>(pData), dataSize);
NEO::virtualFileList[filename] << data;
return dataSize;
return data.size();
}
bool fileExists(const std::string &fileName) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2024 Intel Corporation
* Copyright (C) 2022-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -48,7 +48,7 @@ class TestFileLogger : public NEO::FileLogger<debugLevel> {
NEO::FileLogger<debugLevel>::writeToFile(filename, str, length, mode);
return;
}
writeDataToFile(filename.c_str(), str, length);
writeDataToFile(filename.c_str(), std::string_view(str, length));
}
int32_t createdFilesCount() {

View File

@@ -386,8 +386,8 @@ TEST(DebugSettingsManager, givenDisabledDebugManagerWhenCreateThenOnlyReleaseVar
}
TEST(DebugSettingsManager, givenEnabledDebugManagerWhenCreateThenAllVariablesAreRead) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
constexpr std::string_view data = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, data);
SettingsReader *reader = MockSettingsReader::createFileReader();
EXPECT_NE(nullptr, reader);

View File

@@ -19,8 +19,8 @@
namespace NEO {
TEST(DebugSettingsManager, givenDisabledDebugManagerAndMockEnvVariableWhenCreateThenAllVariablesAreRead) {
const char data[] = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
constexpr std::string_view data = "LogApiCalls = 1\nMakeAllBuffersResident = 1";
writeDataToFile(SettingsReader::settingsFileName, data);
SettingsReader *reader = MockSettingsReader::createFileReader();

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2024 Intel Corporation
* Copyright (C) 2018-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -20,7 +20,8 @@ TEST(FileIO, GivenNonEmptyFileWhenCheckingIfHasSizeThenReturnTrue) {
ASSERT_FALSE(virtualFileExists(fileName.c_str()));
writeDataToFile(fileName.c_str(), "TEST", 4);
constexpr std::string_view data = "TEST";
writeDataToFile(fileName.c_str(), data);
EXPECT_TRUE(virtualFileExists(fileName.c_str()));
EXPECT_TRUE(fileExistsHasSize(fileName.c_str()));
@@ -35,7 +36,8 @@ TEST(FileIO, GivenEmptyFileWhenCheckingIfHasSizeThenReturnFalse) {
ASSERT_FALSE(virtualFileExists(fileName.c_str()));
writeDataToFile(fileName.c_str(), "", 0);
constexpr std::string_view data = "";
writeDataToFile(fileName.c_str(), data);
EXPECT_TRUE(virtualFileExists(fileName.c_str()));
EXPECT_FALSE(fileExistsHasSize(fileName.c_str()));

View File

@@ -37,8 +37,9 @@ TEST(CpuInfo, givenProcCpuinfoFileExistsWhenIsCpuFlagPresentIsCalledThenValidVal
VariableBackup<const char *> pathPrefixBackup(&Os::sysFsProcPathPrefix, ".");
std::string cpuinfoFile = "cpuinfo";
EXPECT_FALSE(virtualFileExists(cpuinfoFile));
std::string cpuinfoData = "processor\t\t: 0\nFeatures\t\t: flag1 flag2 flag3\n";
writeDataToFile(cpuinfoFile.c_str(), cpuinfoData.data(), cpuinfoData.length());
constexpr std::string_view cpuinfoData = "processor\t\t: 0\nFeatures\t\t: flag1 flag2 flag3\n";
writeDataToFile(cpuinfoFile.c_str(), cpuinfoData);
EXPECT_TRUE(virtualFileExists(cpuinfoFile));
CpuInfo::getCpuFlagsFunc = mockGetCpuFlags;

View File

@@ -36,8 +36,8 @@ TEST(SettingsReader, GivenNoSettingsFileWhenCreatingSettingsReaderThenOsReaderIs
TEST(SettingsReader, GivenSettingsFileExistsWhenCreatingSettingsReaderThenReaderIsCreated) {
ASSERT_FALSE(virtualFileExists(SettingsReader::settingsFileName));
const char data[] = "ProductFamilyOverride = test";
writeDataToFile(SettingsReader::settingsFileName, &data, sizeof(data));
constexpr std::string_view data = "ProductFamilyOverride = test";
writeDataToFile(SettingsReader::settingsFileName, data);
auto reader = std::unique_ptr<SettingsReader>(MockSettingsReader::create(ApiSpecificConfig::getRegistryPath()));
EXPECT_NE(nullptr, reader.get());
@@ -50,7 +50,8 @@ TEST(SettingsReader, GivenSettingsFileExistsWhenCreatingSettingsReaderThenReader
TEST(SettingsReader, WhenCreatingFileReaderThenReaderIsCreated) {
ASSERT_FALSE(virtualFileExists(SettingsReader::settingsFileName));
char data = 0;
writeDataToFile(SettingsReader::settingsFileName, &data, 0);
std::string_view emptyView(&data, 0);
writeDataToFile(SettingsReader::settingsFileName, emptyView);
auto reader = std::unique_ptr<SettingsReader>(MockSettingsReader::createFileReader());
EXPECT_NE(nullptr, reader.get());
@@ -62,7 +63,8 @@ TEST(SettingsReader, WhenCreatingFileReaderUseNeoFileIfNoDefault) {
ASSERT_FALSE(virtualFileExists(SettingsReader::settingsFileName));
ASSERT_FALSE(virtualFileExists(SettingsReader::neoSettingsFileName));
char data = 0;
writeDataToFile(SettingsReader::neoSettingsFileName, &data, 0);
std::string_view emptyView(&data, 0);
writeDataToFile(SettingsReader::neoSettingsFileName, emptyView);
auto reader = std::unique_ptr<SettingsReader>(MockSettingsReader::createFileReader());
EXPECT_NE(nullptr, reader.get());

View File

@@ -37,8 +37,8 @@ TEST(CpuInfo, givenProcCpuinfoFileExistsWhenIsCpuFlagPresentIsCalledThenValidVal
VariableBackup<const char *> pathPrefixBackup(&Os::sysFsProcPathPrefix, ".");
std::string cpuinfoFile = "cpuinfo";
EXPECT_FALSE(virtualFileExists(cpuinfoFile));
std::string cpuinfoData = "processor\t\t: 0\nFeatures\t\t: flag1 flag2 flag3\n";
writeDataToFile(cpuinfoFile.c_str(), cpuinfoData.data(), cpuinfoData.length());
constexpr std::string_view cpuinfoData = "processor\t\t: 0\nFeatures\t\t: flag1 flag2 flag3\n";
writeDataToFile(cpuinfoFile.c_str(), cpuinfoData);
EXPECT_TRUE(virtualFileExists(cpuinfoFile));
CpuInfo::getCpuFlagsFunc = mockGetCpuFlags;