Test functionality of Source helper class

This change introduces unit tests for Source class,
which is a helper of ocloc_arg_helper.

The original functionality has been improved to preserve
last line even if it does not contain trailing new line sign.
Moreover, empty lines are skipped since this change.

Signed-off-by: Patryk Wrobel <patryk.wrobel@intel.com>
This commit is contained in:
Patryk Wrobel
2022-03-24 15:16:01 +00:00
committed by Compute-Runtime-Automation
parent be50afb930
commit 41a8972772
2 changed files with 71 additions and 2 deletions

View File

@ -2146,6 +2146,69 @@ TEST(OclocArgHelperTest, GivenOutputSuppressMessagesAndSaveItToFile) {
delete[] lenOutputs;
}
TEST(OclocArgHelperTest, GivenValidSourceFileWhenRequestingVectorOfStringsThenLinesAreStored) {
const char input[] = "First\nSecond\nThird";
const auto inputLength{sizeof(input)};
const auto filename{"some_file.txt"};
Source source{reinterpret_cast<const uint8_t *>(input), inputLength, filename};
std::vector<std::string> lines{};
source.toVectorOfStrings(lines);
ASSERT_EQ(3u, lines.size());
EXPECT_EQ("First", lines[0]);
EXPECT_EQ("Second", lines[1]);
EXPECT_EQ("Third", lines[2]);
}
TEST(OclocArgHelperTest, GivenSourceFileWithTabsWhenRequestingVectorOfStringsWithTabsReplacementThenLinesWithSpacesAreStored) {
const char input[] = "First\tWord\nSecond\tWord\nThird\tWord";
const auto inputLength{sizeof(input)};
const auto filename{"some_file.txt"};
Source source{reinterpret_cast<const uint8_t *>(input), inputLength, filename};
constexpr bool replaceTabs{true};
std::vector<std::string> lines{};
source.toVectorOfStrings(lines, replaceTabs);
ASSERT_EQ(3u, lines.size());
EXPECT_EQ("First Word", lines[0]);
EXPECT_EQ("Second Word", lines[1]);
EXPECT_EQ("Third Word", lines[2]);
}
TEST(OclocArgHelperTest, GivenSourceFileWithEmptyLinesWhenRequestingVectorOfStringsThenOnlyNonEmptyLinesAreStored) {
const char input[] = "First\n\n\nSecond\n";
const auto inputLength{sizeof(input)};
const auto filename{"some_file.txt"};
Source source{reinterpret_cast<const uint8_t *>(input), inputLength, filename};
std::vector<std::string> lines{};
source.toVectorOfStrings(lines);
ASSERT_EQ(2u, lines.size());
EXPECT_EQ("First", lines[0]);
EXPECT_EQ("Second", lines[1]);
}
TEST(OclocArgHelperTest, GivenSourceFileWhenRequestingBinaryVectorThenBinaryIsReturned) {
const char input[] = "A file content";
const auto inputLength{sizeof(input)};
const auto filename{"some_file.txt"};
Source source{reinterpret_cast<const uint8_t *>(input), inputLength, filename};
const auto binaryContent = source.toBinaryVector();
ASSERT_EQ(inputLength, binaryContent.size());
ASSERT_TRUE(std::equal(binaryContent.begin(), binaryContent.end(), input));
}
TEST(OclocArgHelperTest, GivenNoOutputPrintMessages) {
auto helper = WhiteBoxOclocArgHelper(0, nullptr, nullptr, nullptr,
0, nullptr, nullptr, nullptr,

View File

@ -25,13 +25,19 @@ void Source::toVectorOfStrings(std::vector<std::string> &lines, bool replaceTabs
if (replaceTabs && *file == '\t') {
line += ' ';
} else if (*file == '\n') {
if (!line.empty()) {
lines.push_back(line);
line = "";
}
} else {
line += *file;
}
file++;
}
if (!line.empty()) {
lines.push_back(std::move(line));
}
}
Output::Output(const std::string &name, const void *data, const size_t &size)