|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2020 Intel Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +#include "shared/offline_compiler/source/ocloc_validator.h" |
| 9 | +#include "shared/source/device_binary_format/device_binary_formats.h" |
| 10 | +#include "shared/test/unit_test/device_binary_format/zebin_tests.h" |
| 11 | + |
| 12 | +#include "opencl/test/unit_test/offline_compiler/mock/mock_argument_helper.h" |
| 13 | + |
| 14 | +#include "gtest/gtest.h" |
| 15 | + |
| 16 | +PRODUCT_FAMILY productFamily; |
| 17 | + |
| 18 | +TEST(OclocValidate, WhenFileArgIsMissingThenFail) { |
| 19 | + std::map<std::string, std::string> files; |
| 20 | + MockOclocArgHelper argHelper{files}; |
| 21 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 22 | + int res = NEO::Ocloc::validate({}, &argHelper); |
| 23 | + EXPECT_EQ(-1, res); |
| 24 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 25 | + EXPECT_STREQ("Error : Mandatory argument -file is missing.\n", oclocStdout.c_str()); |
| 26 | +} |
| 27 | + |
| 28 | +TEST(OclocValidate, WhenInputFileIsMissingThenFail) { |
| 29 | + MockOclocArgHelper::FilesMap files; |
| 30 | + MockOclocArgHelper argHelper{files}; |
| 31 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 32 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 33 | + EXPECT_EQ(-1, res); |
| 34 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 35 | + EXPECT_STREQ("Error : Input file missing : src.gen\n", oclocStdout.c_str()); |
| 36 | +} |
| 37 | + |
| 38 | +TEST(OclocValidate, WhenInputFileIsAvailableTheLogItsSize) { |
| 39 | + MockOclocArgHelper::FilesMap files{{"src.gen", "01234567"}}; |
| 40 | + MockOclocArgHelper argHelper{files}; |
| 41 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 42 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 43 | + EXPECT_NE(0, res); |
| 44 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 45 | + EXPECT_NE(nullptr, strstr(oclocStdout.c_str(), "Validating : src.gen (8 bytes).\n")) << oclocStdout; |
| 46 | +} |
| 47 | + |
| 48 | +TEST(OclocValidate, WhenInputFileIsNotZebinThenFail) { |
| 49 | + MockOclocArgHelper::FilesMap files{{"src.gen", "01234567"}}; |
| 50 | + MockOclocArgHelper argHelper{files}; |
| 51 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 52 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 53 | + EXPECT_EQ(-2, res); |
| 54 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 55 | + EXPECT_NE(nullptr, strstr(oclocStdout.c_str(), "Input is not a Zebin file (not elf or wrong elf object file type)")) << oclocStdout; |
| 56 | +} |
| 57 | + |
| 58 | +TEST(OclocValidate, WhenInputIsValidZebinThenReturnSucceed) { |
| 59 | + ZebinTestData::ValidEmptyProgram zebin; |
| 60 | + MockOclocArgHelper::FilesMap files{{"src.gen", MockOclocArgHelper::FileData(reinterpret_cast<const char *>(zebin.storage.data()), |
| 61 | + reinterpret_cast<const char *>(zebin.storage.data()) + zebin.storage.size())}}; |
| 62 | + MockOclocArgHelper argHelper{files}; |
| 63 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 64 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 65 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 66 | + EXPECT_EQ(0, res) << oclocStdout; |
| 67 | +} |
| 68 | + |
| 69 | +TEST(OclocValidate, WhenWarningsEmitedThenRedirectsThemToStdout) { |
| 70 | + ZebinTestData::ValidEmptyProgram zebin; |
| 71 | + zebin.removeSection(NEO::Elf::SHT_ZEBIN_ZEINFO, NEO::Elf::SectionsNamesZebin::zeInfo); |
| 72 | + MockOclocArgHelper::FilesMap files{{"src.gen", MockOclocArgHelper::FileData(reinterpret_cast<const char *>(zebin.storage.data()), |
| 73 | + reinterpret_cast<const char *>(zebin.storage.data()) + zebin.storage.size())}}; |
| 74 | + MockOclocArgHelper argHelper{files}; |
| 75 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 76 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 77 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 78 | + EXPECT_EQ(0, res) << oclocStdout; |
| 79 | + EXPECT_NE(nullptr, strstr(oclocStdout.c_str(), "Validator detected potential problems :\nDeviceBinaryFormat::Zebin : Expected at least one .ze_info section, got 0")) << oclocStdout; |
| 80 | +} |
| 81 | + |
| 82 | +TEST(OclocValidate, WhenErrorsEmitedThenRedirectsThemToStdout) { |
| 83 | + ZebinTestData::ValidEmptyProgram zebin; |
| 84 | + zebin.removeSection(NEO::Elf::SHT_ZEBIN_ZEINFO, NEO::Elf::SectionsNamesZebin::zeInfo); |
| 85 | + zebin.appendSection(NEO::Elf::SHT_ZEBIN_ZEINFO, NEO::Elf::SectionsNamesZebin::zeInfo, ArrayRef<const char>("kernels : \nkernels :\n").toArrayRef<const uint8_t>()); |
| 86 | + MockOclocArgHelper::FilesMap files{{"src.gen", MockOclocArgHelper::FileData(reinterpret_cast<const char *>(zebin.storage.data()), |
| 87 | + reinterpret_cast<const char *>(zebin.storage.data()) + zebin.storage.size())}}; |
| 88 | + MockOclocArgHelper argHelper{files}; |
| 89 | + argHelper.getPrinterRef() = MessagePrinter(true); |
| 90 | + int res = NEO::Ocloc::validate({"-file", "src.gen"}, &argHelper); |
| 91 | + std::string oclocStdout = argHelper.getPrinterRef().getLog().str(); |
| 92 | + EXPECT_EQ(static_cast<int>(NEO::DecodeError::InvalidBinary), res) << oclocStdout; |
| 93 | + EXPECT_NE(nullptr, strstr(oclocStdout.c_str(), "Validator detected errors :\nDeviceBinaryFormat::Zebin::.ze_info : Expected at most one kernels entry in global scope of .ze_info, got : 2")) << oclocStdout; |
| 94 | +} |
0 commit comments