Skip to content

Commit 50efd09

Browse files
Improve code coverage of binary encoder and decoder
This change contains ULTs, which improve the coverage of BinaryEncoder and BinaryDecoder classes. Related-To: NEO-6834 Signed-off-by: Patryk Wrobel <[email protected]>
1 parent f09fd52 commit 50efd09

File tree

8 files changed

+399
-48
lines changed

8 files changed

+399
-48
lines changed

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

Lines changed: 191 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
*
66
*/
77

8+
#include "shared/offline_compiler/source/decoder/translate_platform_base.h"
89
#include "shared/source/helpers/array_count.h"
910
#include "shared/test/common/helpers/test_files.h"
1011

1112
#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h"
1213
#include "opencl/test/unit_test/test_files/patch_list.h"
1314

1415
#include "gtest/gtest.h"
16+
#include "igad.h"
17+
#include "igfxfmid.h"
1518
#include "mock/mock_decoder.h"
1619

20+
#include <array>
1721
#include <fstream>
22+
#include <utility>
1823

1924
SProgramBinaryHeader createProgramBinaryHeader(const uint32_t numberOfKernels, const uint32_t patchListSize) {
2025
return SProgramBinaryHeader{MAGIC_CL, 0, 0, 0, numberOfKernels, 0, patchListSize};
@@ -30,8 +35,9 @@ SKernelBinaryHeaderCommon createKernelBinaryHeaderCommon(const uint32_t kernelNa
3035
}
3136

3237
namespace NEO {
38+
3339
TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) {
34-
std::vector<std::string> args = {
40+
const std::vector<std::string> args = {
3541
"ocloc",
3642
"decoder",
3743
"-file",
@@ -45,6 +51,117 @@ TEST(DecoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) {
4551
EXPECT_EQ(0, decoder.validateInput(args));
4652
}
4753

54+
TEST(DecoderTests, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingThenErrorIsReported) {
55+
const std::array<std::string, 4> flagsToTest = {
56+
"-file", "-device", "-patch", "-dump"};
57+
58+
for (const auto &flag : flagsToTest) {
59+
const std::vector<std::string> args = {
60+
"ocloc",
61+
"decoder",
62+
flag};
63+
64+
constexpr auto suppressMessages{false};
65+
MockDecoder decoder{suppressMessages};
66+
67+
::testing::internal::CaptureStdout();
68+
const auto result = decoder.validateInput(args);
69+
const auto output{::testing::internal::GetCapturedStdout()};
70+
71+
EXPECT_EQ(-1, result);
72+
73+
const std::string expectedErrorMessage{"Unknown argument " + flag + "\n"};
74+
EXPECT_EQ(expectedErrorMessage, output);
75+
}
76+
}
77+
78+
TEST(DecoderTests, GivenIgnoreIsaPaddingFlagWhenParsingValidListOfParametersThenReturnValueIsZeroAndInternalFlagIsSet) {
79+
const std::vector<std::string> args = {
80+
"ocloc",
81+
"decoder",
82+
"-file",
83+
"test_files/binary.bin",
84+
"-patch",
85+
"test_files/patch",
86+
"-dump",
87+
"test_files/created",
88+
"-ignore_isa_padding"};
89+
90+
MockDecoder decoder;
91+
EXPECT_EQ(0, decoder.validateInput(args));
92+
EXPECT_TRUE(decoder.ignoreIsaPadding);
93+
}
94+
95+
TEST(DecoderTests, GivenQuietModeFlagWhenParsingValidListOfParametersThenReturnValueIsZeroAndMessagesAreSuppressed) {
96+
const std::vector<std::string> args = {
97+
"ocloc",
98+
"decoder",
99+
"-file",
100+
"test_files/binary.bin",
101+
"-patch",
102+
"test_files/patch",
103+
"-dump",
104+
"test_files/created",
105+
"-q"};
106+
107+
constexpr auto suppressMessages{false};
108+
MockDecoder decoder{suppressMessages};
109+
110+
EXPECT_EQ(0, decoder.validateInput(args));
111+
EXPECT_TRUE(decoder.argHelper->getPrinterRef().isSuppressed());
112+
}
113+
114+
TEST(DecoderTests, GivenMissingDumpFlagWhenParsingValidListOfParametersThenReturnValueIsZeroAndWarningAboutCreationOfDefaultDirectoryIsPrinted) {
115+
const std::vector<std::string> args = {
116+
"ocloc",
117+
"decoder",
118+
"-file",
119+
"test_files/binary.bin",
120+
"-device",
121+
"pvc",
122+
"-patch",
123+
"test_files/patch"};
124+
125+
constexpr auto suppressMessages{false};
126+
MockDecoder decoder{suppressMessages};
127+
decoder.getMockIga()->isKnownPlatformReturnValue = true;
128+
129+
::testing::internal::CaptureStdout();
130+
const auto result = decoder.validateInput(args);
131+
const auto output{::testing::internal::GetCapturedStdout()};
132+
133+
EXPECT_EQ(0, result);
134+
135+
const std::string expectedErrorMessage{"Warning : Path to dump folder not specificed - using ./dump as default.\n"};
136+
EXPECT_EQ(expectedErrorMessage, output);
137+
}
138+
139+
TEST(DecoderTests, GivenMissingDumpFlagAndArgHelperOutputEnabledWhenParsingValidListOfParametersThenReturnValueIsZeroAndDefaultDirectoryWarningIsNotEmitted) {
140+
const std::vector<std::string> args = {
141+
"ocloc",
142+
"decoder",
143+
"-file",
144+
"test_files/binary.bin",
145+
"-device",
146+
"pvc",
147+
"-patch",
148+
"test_files/patch"};
149+
150+
constexpr auto suppressMessages{false};
151+
MockDecoder decoder{suppressMessages};
152+
decoder.mockArgHelper->hasOutput = true;
153+
decoder.getMockIga()->isKnownPlatformReturnValue = true;
154+
155+
::testing::internal::CaptureStdout();
156+
const auto result = decoder.validateInput(args);
157+
const auto output{::testing::internal::GetCapturedStdout()};
158+
159+
EXPECT_EQ(0, result);
160+
EXPECT_TRUE(output.empty()) << output;
161+
162+
decoder.mockArgHelper->hasOutput = false;
163+
}
164+
48165
TEST(DecoderTests, GivenValidSizeStringWhenGettingSizeThenProperOutcomeIsExpectedAndExceptionIsNotThrown) {
49166
MockDecoder decoder;
50167
EXPECT_EQ(static_cast<uint8_t>(1), decoder.getSize("uint8_t"));
@@ -301,4 +418,77 @@ TEST(DecoderTests, givenPatchtokensBinaryFormatWhenTryingToGetDevBinaryThenRawDa
301418
std::string dataString(static_cast<const char *>(data), dataSize);
302419
EXPECT_STREQ("CTNI\n\n\n\n\n\n\n", dataString.c_str());
303420
}
421+
422+
TEST(DecoderHelperTest, GivenTextSeparatedByTabsWhenSearchingForExistingTextThenItsIndexIsReturned) {
423+
const std::vector<std::string> lines = {"Some\tNice\tText"};
424+
const auto position = findPos(lines, "Nice");
425+
426+
EXPECT_EQ(0u, position);
427+
}
428+
429+
TEST(DecoderHelperTest, GivenTextSeparatedByNewLinesWhenSearchingForExistingTextThenItsIndexIsReturned) {
430+
const std::vector<std::string> lines = {"Some\nNice\nText"};
431+
const auto position = findPos(lines, "Nice");
432+
433+
EXPECT_EQ(0u, position);
434+
}
435+
436+
TEST(DecoderHelperTest, GivenTextSeparatedByCarriageReturnWhenSearchingForExistingTextThenItsIndexIsReturned) {
437+
const std::vector<std::string> lines = {"Some\rNice\rText"};
438+
const auto position = findPos(lines, "Nice");
439+
440+
EXPECT_EQ(0u, position);
441+
}
442+
443+
TEST(DecoderHelperTest, GivenOnlyMatchingSubstringWhenSearchingForExistingTextThenInvalidIndexIsReturned) {
444+
const std::vector<std::string> lines = {"Carpet"};
445+
const auto position = findPos(lines, "Car");
446+
447+
EXPECT_EQ(lines.size(), position);
448+
}
449+
450+
TEST(DecoderHelperTest, GivenPathEndedBySlashWhenCallingAddSlashThenNothingIsDone) {
451+
std::string path{"./some/path/"};
452+
addSlash(path);
453+
454+
EXPECT_EQ("./some/path/", path);
455+
}
456+
457+
TEST(DecoderHelperTest, GivenPathEndedByBackSlashWhenCallingAddSlashThenNothingIsDone) {
458+
std::string path{".\\some\\path\\"};
459+
addSlash(path);
460+
461+
EXPECT_EQ(".\\some\\path\\", path);
462+
}
463+
464+
TEST(DecoderHelperTest, GivenGfxCoreFamilyWhenTranslatingToIgaGenBaseThenExpectedIgaGenBaseIsReturned) {
465+
constexpr static std::array translations = {
466+
std::pair{IGFX_GEN8_CORE, IGA_GEN8},
467+
std::pair{IGFX_GEN9_CORE, IGA_GEN9},
468+
std::pair{IGFX_GEN11_CORE, IGA_GEN11},
469+
std::pair{IGFX_GEN11LP_CORE, IGA_GEN11},
470+
std::pair{IGFX_UNKNOWN_CORE, IGA_GEN_INVALID}};
471+
472+
for (const auto &[input, expectedOutput] : translations) {
473+
EXPECT_EQ(expectedOutput, translateToIgaGen(input));
474+
}
475+
}
476+
477+
TEST(DecoderHelperTest, GivenProductFamilyWhenTranslatingToIgaGenBaseThenExpectedIgaGenBaseIsReturned) {
478+
constexpr static std::array translations = {
479+
std::pair{IGFX_BROADWELL, IGA_GEN8},
480+
std::pair{IGFX_CHERRYVIEW, IGA_GEN8lp},
481+
std::pair{IGFX_SKYLAKE, IGA_GEN9},
482+
std::pair{IGFX_BROXTON, IGA_GEN9lp},
483+
std::pair{IGFX_KABYLAKE, IGA_GEN9p5},
484+
std::pair{IGFX_COFFEELAKE, IGA_GEN9p5},
485+
std::pair{IGFX_ICELAKE, IGA_GEN11},
486+
std::pair{IGFX_ICELAKE_LP, IGA_GEN11},
487+
std::pair{IGFX_UNKNOWN, IGA_GEN_INVALID}};
488+
489+
for (const auto &[input, expectedOutput] : translations) {
490+
EXPECT_EQ(expectedOutput, translateToIgaGen(input));
491+
}
492+
}
493+
304494
} // namespace NEO

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

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#include "gtest/gtest.h"
1313
#include "mock/mock_encoder.h"
1414

15+
#include <cstdint>
1516
#include <fstream>
17+
#include <sstream>
1618

1719
namespace NEO {
20+
1821
TEST(EncoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) {
19-
std::vector<std::string> args = {
22+
const std::vector<std::string> args = {
2023
"ocloc",
2124
"asm",
2225
"-dump",
@@ -28,6 +31,130 @@ TEST(EncoderTests, WhenParsingValidListOfParametersThenReturnValueIsZero) {
2831
EXPECT_EQ(0, encoder.validateInput(args));
2932
}
3033

34+
TEST(EncoderTests, GivenFlagsWhichRequireMoreArgsWithoutThemWhenParsingThenErrorIsReported) {
35+
const std::array<std::string, 3> flagsToTest = {
36+
"-dump", "-device", "-out"};
37+
38+
for (const auto &flag : flagsToTest) {
39+
const std::vector<std::string> args = {
40+
"ocloc",
41+
"asm",
42+
flag};
43+
44+
constexpr auto suppressMessages{false};
45+
MockEncoder encoder{suppressMessages};
46+
47+
::testing::internal::CaptureStdout();
48+
const auto result = encoder.validateInput(args);
49+
const auto output{::testing::internal::GetCapturedStdout()};
50+
51+
EXPECT_EQ(-1, result);
52+
53+
const std::string expectedErrorMessage{"Unknown argument " + flag + "\n"};
54+
EXPECT_EQ(expectedErrorMessage, output);
55+
}
56+
}
57+
58+
TEST(EncoderTests, GivenIgnoreIsaPaddingFlagWhenParsingValidListOfParametersThenReturnValueIsZeroAndInternalFlagIsSet) {
59+
const std::vector<std::string> args = {
60+
"ocloc",
61+
"asm",
62+
"-dump",
63+
"test_files/dump",
64+
"-out",
65+
"test_files/binary_gen.bin",
66+
"-ignore_isa_padding"};
67+
68+
MockEncoder encoder;
69+
EXPECT_EQ(0, encoder.validateInput(args));
70+
EXPECT_TRUE(encoder.ignoreIsaPadding);
71+
}
72+
73+
TEST(EncoderTests, GivenQuietModeFlagWhenParsingValidListOfParametersThenReturnValueIsZeroAndMessagesAreSuppressed) {
74+
const std::vector<std::string> args = {
75+
"ocloc",
76+
"asm",
77+
"-dump",
78+
"test_files/dump",
79+
"-out",
80+
"test_files/binary_gen.bin",
81+
"-q"};
82+
83+
constexpr auto suppressMessages{false};
84+
MockEncoder encoder{suppressMessages};
85+
86+
EXPECT_EQ(0, encoder.validateInput(args));
87+
EXPECT_TRUE(encoder.argHelper->getPrinterRef().isSuppressed());
88+
}
89+
90+
TEST(EncoderTests, GivenMissingDumpFlagAndArgHelperOutputEnabledWhenParsingValidListOfParametersThenReturnValueIsZeroAndDefaultDirectoryIsNotUsedAsDumpPath) {
91+
const std::vector<std::string> args = {
92+
"ocloc",
93+
"asm",
94+
"-out",
95+
"test_files/binary_gen.bin",
96+
"-device",
97+
"pvc"};
98+
99+
constexpr auto suppressMessages{false};
100+
MockEncoder encoder{suppressMessages};
101+
encoder.mockArgHelper->hasOutput = true;
102+
encoder.getMockIga()->isKnownPlatformReturnValue = true;
103+
104+
::testing::internal::CaptureStdout();
105+
const auto result = encoder.validateInput(args);
106+
const auto output{::testing::internal::GetCapturedStdout()};
107+
108+
EXPECT_EQ(0, result);
109+
EXPECT_TRUE(output.empty()) << output;
110+
EXPECT_TRUE(encoder.pathToDump.empty()) << encoder.pathToDump;
111+
112+
encoder.mockArgHelper->hasOutput = false;
113+
}
114+
115+
TEST(EncoderTests, GivenMissingPTMFileWhenEncodingThenErrorIsReturnedAndLogIsPrinted) {
116+
constexpr auto suppressMessages{false};
117+
MockEncoder encoder{suppressMessages};
118+
119+
::testing::internal::CaptureStdout();
120+
const auto result = encoder.encode();
121+
const auto output{::testing::internal::GetCapturedStdout()};
122+
123+
EXPECT_EQ(-1, result);
124+
EXPECT_EQ("Error! Couldn't find PTM.txt", output);
125+
}
126+
127+
TEST(EncoderTests, GivenMissingSourceFileWhenTryingToCopyBinaryThenErrorIsReturned) {
128+
MockEncoder encoder;
129+
encoder.callBaseCopyBinaryToBinary = true;
130+
131+
std::stringstream outputStream;
132+
EXPECT_FALSE(encoder.copyBinaryToBinary("bad_source.bin", outputStream, nullptr));
133+
}
134+
135+
TEST(EncoderTests, GivenValidSourceFileWhenTryingToCopyBinaryThenItIsCopied) {
136+
MockEncoder encoder;
137+
encoder.callBaseCopyBinaryToBinary = true;
138+
encoder.filesMap["good_source.bin"] = "TEXT!";
139+
140+
std::stringstream outputStream;
141+
ASSERT_TRUE(encoder.copyBinaryToBinary("good_source.bin", outputStream, nullptr));
142+
EXPECT_EQ("TEXT!", outputStream.str());
143+
}
144+
145+
TEST(EncoderTests, GivenValidSourceFileAndOutputLengthArgumentWhenTryingToCopyBinaryThenItIsCopiedAndLengthIsSet) {
146+
MockEncoder encoder;
147+
encoder.callBaseCopyBinaryToBinary = true;
148+
encoder.filesMap["good_source.bin"] = "TEXT!";
149+
150+
std::stringstream outputStream;
151+
uint32_t outputLength{};
152+
ASSERT_TRUE(encoder.copyBinaryToBinary("good_source.bin", outputStream, &outputLength));
153+
154+
EXPECT_EQ("TEXT!", outputStream.str());
155+
EXPECT_EQ(5u, outputLength);
156+
}
157+
31158
TEST(EncoderTests, WhenMissingParametersThenErrorCodeIsReturned) {
32159
std::vector<std::string> args = {
33160
"ocloc",
@@ -449,4 +576,4 @@ TEST(EncoderTests, WhenProcessingDeviceBinaryAndAsmIsAvailableThenAseembleItWith
449576
EXPECT_EQ(encoder.filesMap["kernel_KernelHeap.asm"], encoder.getMockIga()->receivedAsm);
450577
}
451578

452-
} // namespace NEO
579+
} // namespace NEO

0 commit comments

Comments
 (0)