diff --git a/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp b/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp index 415288977f..2509ac3e3f 100644 --- a/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp +++ b/opencl/test/unit_test/offline_compiler/ocloc_api_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -12,6 +12,7 @@ #include "environment.h" #include "gtest/gtest.h" +#include "hw_cmds.h" #include @@ -43,6 +44,26 @@ TEST(OclocApiTests, WhenGoodArgsAreGivenThenSuccessIsReturned) { EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4])); } +TEST(OclocApiTests, WhenGoodFamilyNameIsProvidedThenSuccessIsReturned) { + const char *argv[] = { + "ocloc", + "-file", + "test_files/copybuffer.cl", + "-device", + NEO::familyName[NEO::DEFAULT_PLATFORM::hwInfo.platform.eRenderCoreFamily]}; + unsigned int argc = sizeof(argv) / sizeof(const char *); + + testing::internal::CaptureStdout(); + int retVal = oclocInvoke(argc, argv, + 0, nullptr, nullptr, nullptr, + 0, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr); + std::string output = testing::internal::GetCapturedStdout(); + + EXPECT_EQ(retVal, NEO::SUCCESS); + EXPECT_EQ(std::string::npos, output.find("Command was: ocloc -file test_files/copybuffer.cl -device "s + argv[4])); +} + TEST(OclocApiTests, WhenArgsWithMissingFileAreGivenThenErrorMessageIsProduced) { const char *argv[] = { "ocloc", diff --git a/shared/offline_compiler/source/ocloc_fatbinary.cpp b/shared/offline_compiler/source/ocloc_fatbinary.cpp index a054caba97..2e0dcc0f1a 100644 --- a/shared/offline_compiler/source/ocloc_fatbinary.cpp +++ b/shared/offline_compiler/source/ocloc_fatbinary.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -29,7 +29,7 @@ bool requestedFatBinary(const std::vector &args) { const bool hasMoreArgs = (argIndex + 1 < args.size()); if ((ConstStringRef("-device") == currArg) && hasMoreArgs) { ConstStringRef deviceArg(args[argIndex + 1]); - return deviceArg.contains("*") || deviceArg.contains("-") || deviceArg.contains(",") || deviceArg.contains("gen"); + return deviceArg.contains("*") || deviceArg.contains("-") || deviceArg.contains(",") || deviceArg.containsCaseInsensitive("gen"); } } return false; @@ -112,7 +112,7 @@ std::vector getTargetPlatformsForFatbinary(ConstStringRef device if (range.size() == 1) { // open range , from-max or min-to - if (range[0].contains("gen")) { + if (range[0].containsCaseInsensitive("gen")) { auto coreIdList = asGfxCoreIdList(range[0]); if (coreIdList.empty()) { argHelper->printf("Unknown device : %s\n", set.str().c_str()); @@ -197,7 +197,7 @@ std::vector getTargetPlatformsForFatbinary(ConstStringRef device requestedPlatforms.insert(requestedPlatforms.end(), from, to); } } - } else if (set.contains("gen")) { + } else if (set.containsCaseInsensitive("gen")) { if (set.size() == genArg.size()) { argHelper->printf("Invalid gen-based device : %s - gen should be followed by a number\n", set.str().c_str()); } else { diff --git a/shared/source/device_binary_format/yaml/yaml_parser.h b/shared/source/device_binary_format/yaml/yaml_parser.h index e1284e001b..b18c08000c 100644 --- a/shared/source/device_binary_format/yaml/yaml_parser.h +++ b/shared/source/device_binary_format/yaml/yaml_parser.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Intel Corporation + * Copyright (C) 2020-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -595,7 +595,7 @@ inline bool YamlParser::readValueChecked(const Node &node, bool &outValue) case 1: return true; case 3: - return equalsCaseInsesitive(ConstStringRef("es"), ConstStringRef(token.cstrref().begin() + 1, 2)); + return equalsCaseInsensitive(ConstStringRef("es"), ConstStringRef(token.cstrref().begin() + 1, 2)); } break; } @@ -618,7 +618,7 @@ inline bool YamlParser::readValueChecked(const Node &node, bool &outValue) if (token.len != 4) { return false; } - return equalsCaseInsesitive(ConstStringRef("rue"), ConstStringRef(token.cstrref().begin() + 1, 3)); + return equalsCaseInsensitive(ConstStringRef("rue"), ConstStringRef(token.cstrref().begin() + 1, 3)); } case 'f': case 'F': { @@ -626,7 +626,7 @@ inline bool YamlParser::readValueChecked(const Node &node, bool &outValue) if (token.len != 5) { return false; } - return equalsCaseInsesitive(ConstStringRef("alse"), ConstStringRef(token.cstrref().begin() + 1, 4)); + return equalsCaseInsensitive(ConstStringRef("alse"), ConstStringRef(token.cstrref().begin() + 1, 4)); } case 'o': case 'O': { @@ -638,7 +638,7 @@ inline bool YamlParser::readValueChecked(const Node &node, bool &outValue) return ((token.cstrref()[1] == 'n') | (token.cstrref()[1] == 'N')); case 3: outValue = false; - return equalsCaseInsesitive(ConstStringRef("ff"), ConstStringRef(token.cstrref().begin() + 1, 2)); + return equalsCaseInsensitive(ConstStringRef("ff"), ConstStringRef(token.cstrref().begin() + 1, 2)); } break; } diff --git a/shared/source/utilities/const_stringref.h b/shared/source/utilities/const_stringref.h index fdeb8f5c71..b3837c588e 100644 --- a/shared/source/utilities/const_stringref.h +++ b/shared/source/utilities/const_stringref.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019-2020 Intel Corporation + * Copyright (C) 2019-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -137,6 +137,24 @@ class ConstStringRef { return false; } + constexpr bool containsCaseInsensitive(const char *subString) const noexcept { + const char *findBeg = ptr; + const char *findEnd = ptr + len; + while (findBeg != findEnd) { + const char *lhs = findBeg; + const char *rhs = subString; + while ((lhs < findEnd) && (std::tolower(*lhs) == std::tolower(*rhs)) && ('\0' != *rhs)) { + ++lhs; + ++rhs; + } + if ('\0' == *rhs) { + return true; + } + ++findBeg; + } + return false; + } + constexpr bool startsWith(const char *subString) const noexcept { const char *findEnd = ptr + len; const char *lhs = ptr; @@ -207,7 +225,7 @@ constexpr bool operator!=(const char *lhs, const ConstStringRef &rhs) { return false == equals(rhs, lhs); } -constexpr bool equalsCaseInsesitive(const ConstStringRef &lhs, const ConstStringRef &rhs) { +constexpr bool equalsCaseInsensitive(const ConstStringRef &lhs, const ConstStringRef &rhs) { if (lhs.size() != rhs.size()) { return false; } @@ -223,4 +241,4 @@ constexpr bool equalsCaseInsesitive(const ConstStringRef &lhs, const ConstString return true; } -} // namespace NEO \ No newline at end of file +} // namespace NEO diff --git a/shared/test/unit_test/utilities/const_stringref_tests.cpp b/shared/test/unit_test/utilities/const_stringref_tests.cpp index 6aeeac6ffb..ea0861840f 100644 --- a/shared/test/unit_test/utilities/const_stringref_tests.cpp +++ b/shared/test/unit_test/utilities/const_stringref_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2020 Intel Corporation + * Copyright (C) 2017-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -115,6 +115,28 @@ TEST(ConstStringRef, WhenCopyAsignedThenIdenticalAsOrigin) { EXPECT_EQ(a, b); } +TEST(ConstStringRef, WhenCheckingForInclusionCaseInsensitivelyThenDoesNotReadOutOfBounds) { + static constexpr ConstStringRef str1("Text", 2); + ConstStringRef substr1("tex"); + EXPECT_FALSE(str1.containsCaseInsensitive(substr1.data())); + + static constexpr ConstStringRef str2("AabAac"); + ConstStringRef substr2("aac"); + EXPECT_TRUE(str2.containsCaseInsensitive(substr2.data())); + + static constexpr ConstStringRef str3("AabAac"); + ConstStringRef substr3("aacd"); + EXPECT_FALSE(str3.containsCaseInsensitive(substr3.data())); +} + +TEST(ConstStringRef, GivenConstStringRefWithDifferentCasesWhenCheckingIfOneContainsTheOtherOneCaseInsensitivelyThenTrueIsReturned) { + static constexpr ConstStringRef str1("TexT"); + static constexpr ConstStringRef str2("tEXt"); + + EXPECT_FALSE(str1.contains(str2.data())); + EXPECT_TRUE(str1.containsCaseInsensitive(str2.data())); +} + TEST(ConstStringRef, WhenCheckingForInclusionThenDoesNotReadOutOfBounds) { static constexpr ConstStringRef str1("Text", 2); ConstStringRef substr1("Tex"); @@ -189,19 +211,19 @@ TEST(ConstStringRefTruncated, GivenNegativeLengthThenCountFromRight) { TEST(ConstStringRefEqualsCaseInsesitive, WhenSizesDifferReturnFalse) { ConstStringRef lhs = ConstStringRef::fromArray("\0"); ConstStringRef rhs = ConstStringRef::fromArray("\0\0"); - EXPECT_FALSE(equalsCaseInsesitive(lhs, rhs)); + EXPECT_FALSE(equalsCaseInsensitive(lhs, rhs)); } TEST(ConstStringRefEqualsCaseInsesitive, WhenStringsDontMatchThenReturnFalse) { - EXPECT_FALSE(equalsCaseInsesitive(ConstStringRef("abc"), ConstStringRef("abd"))); + EXPECT_FALSE(equalsCaseInsensitive(ConstStringRef("abc"), ConstStringRef("abd"))); } TEST(ConstStringRefEqualsCaseInsesitive, WhenStringsIdenticalThenReturnTrue) { - EXPECT_TRUE(equalsCaseInsesitive(ConstStringRef("abc"), ConstStringRef("abc"))); + EXPECT_TRUE(equalsCaseInsensitive(ConstStringRef("abc"), ConstStringRef("abc"))); } TEST(ConstStringRefEqualsCaseInsesitive, WhenStringsDifferOnlyByCaseThenReturnTrue) { - EXPECT_TRUE(equalsCaseInsesitive(ConstStringRef("aBc"), ConstStringRef("Abc"))); + EXPECT_TRUE(equalsCaseInsensitive(ConstStringRef("aBc"), ConstStringRef("Abc"))); } TEST(ConstStringStartsWith, GivenRightPrefixThenReturnsTrue) {