Files
TheRock/rocm-libraries/shared/rocroller/test/unit/SourceMatcherTest.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

329 lines
12 KiB
C++

// Copyright Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <gtest/gtest-spi.h>
#include "SimpleFixture.hpp"
#include "SourceMatcher.hpp"
#include <rocRoller/Utilities/Utils.hpp>
using namespace rocRoller;
namespace rocRollerTest
{
class SourceMatcherTest : public SimpleFixture
{
};
TEST_F(SourceMatcherTest, NormalizedSourceLinesBasic)
{
using ::testing::ElementsAre;
EXPECT_THAT(Generated(NormalizedSourceLines("some code\nmore code", true)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code\nmore code", true)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\nmore code", false)),
ElementsAre("some code", "more code"));
}
TEST_F(SourceMatcherTest, NormalizedSourceLinesString)
{
using ::testing::ElementsAre;
EXPECT_THAT(Generated(NormalizedSourceLines("some code \"string constant\"\n"
"more code",
true)),
ElementsAre("some code \"string constant\"", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines("some code \"unterminated string constant\n"
"more code",
true)),
ElementsAre("some code \"unterminated string constant\nmore code"));
EXPECT_THAT(Generated(NormalizedSourceLines("some code \"string \\\"constant data\\\"\"\n"
"more code",
true)),
ElementsAre("some code \"string \\\"constant data\\\"\"", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines("\"nothing but string constant\"", true)),
ElementsAre("\"nothing but string constant\""));
}
TEST_F(SourceMatcherTest, NormalizedSourceLinesComment)
{
using ::testing::ElementsAre;
using ::testing::IsEmpty;
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\n"
"more code",
true)),
ElementsAre("some code // comment", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\n"
"more code",
false)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\n"
"more code // ending comment",
false)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\n"
"more code // ending comment\n",
false)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code /* multi\n"
" line comment */ more code",
false)),
ElementsAre("some code more code"));
EXPECT_THAT(Generated(NormalizedSourceLines(" some code // comment\n"
"more code /* unterminated comment",
false)),
ElementsAre("some code", "more code"));
EXPECT_THAT(Generated(NormalizedSourceLines("// only comments and whitespace\n"
" /* another comment */\n \n",
false)),
IsEmpty());
EXPECT_THAT(Generated(NormalizedSourceLines("// only comments and whitespace\n"
" /* another comment */\n \n",
true)),
ElementsAre("// only comments and whitespace", "/* another comment */"));
}
TEST_F(SourceMatcherTest, NormalizedSourceLinesLonger)
{
using ::testing::ElementsAre;
std::string input
= ".amdgcn_target \"amdgcn-amd-amdhsa--gfx90a+xnack\"\n"
"\n"
".set .amdgcn.next_free_vgpr, 0 // Needed if we ever place 2 kernels in one file.\n"
" .set .amdgcn.next_free_sgpr, 0\n"
".text\n"
".globl hello_world\n"
" .p2align 8\n"
".type hello_world,@function\n"
" hello_world:\n\n";
EXPECT_THAT(
Generated(NormalizedSourceLines(input, true)),
ElementsAre(
".amdgcn_target \"amdgcn-amd-amdhsa--gfx90a+xnack\"",
".set .amdgcn.next_free_vgpr, 0 // Needed if we ever place 2 kernels in one file.",
".set .amdgcn.next_free_sgpr, 0",
".text",
".globl hello_world",
".p2align 8",
".type hello_world,@function",
"hello_world:"));
EXPECT_THAT(Generated(NormalizedSourceLines(input, false)),
ElementsAre(".amdgcn_target \"amdgcn-amd-amdhsa--gfx90a+xnack\"",
".set .amdgcn.next_free_vgpr, 0",
".set .amdgcn.next_free_sgpr, 0",
".text",
".globl hello_world",
".p2align 8",
".type hello_world,@function",
"hello_world:"));
}
TEST_F(SourceMatcherTest, NormalizedSourceLinesYAMLDoc)
{
#ifdef ROCROLLER_TESTS_USE_YAML_CPP
using ::testing::ElementsAre;
std::string input = R"(
.amdgpu_metadata
---
amdhsa.version: [1, 2]
foo:
baz: 7
bar: 2
...
.end_amdgpu_metadata
)";
std::string doc = R"(---
amdhsa.version:
- 1
- 2
foo:
bar: 2
baz: 7
...
)";
EXPECT_THAT(Generated(NormalizedSourceLines(input, false)),
ElementsAre(".amdgpu_metadata", doc, ".end_amdgpu_metadata"));
#else
GTEST_SKIP() << "Skipping NormalizedYAML test (Only implemented for yaml-cpp)";
#endif
}
TEST_F(SourceMatcherTest, NormalizedSourceLinesUnterminatedYAMLDoc)
{
#ifdef ROCROLLER_TESTS_USE_YAML_CPP
using ::testing::ElementsAre;
std::string input = R"(
.amdgpu_metadata
---
amdhsa.version:
- 1
- 2
)";
std::string doc = R"(---
amdhsa.version:
- 1
- 2
...
)";
EXPECT_THAT(Generated(NormalizedSourceLines(input, false)),
ElementsAre(".amdgpu_metadata", doc));
#else
GTEST_SKIP() << "Skipping NormalizedYAML test (Only implemented for yaml-cpp)";
#endif
}
TEST_F(SourceMatcherTest, NormalizedYAML)
{
#ifdef ROCROLLER_TESTS_USE_YAML_CPP
std::string input1 = "{x: 7, y: 2, a: [4,5,6]}";
std::string input2 = R"(
x: 7
a: [4,5,6]
y: 2
)";
std::string input3 = R"(# a comment
x: 7
a: [4,5,6]
y: 2
)";
std::string input4 = "---\n{x: 7, y: 2, a: [4,5,6]}";
std::string expected = R"(---
a:
- 4
- 5
- 6
x: 7
y: 2
...
)";
EXPECT_EQ(expected, NormalizedYAML(input1));
EXPECT_EQ(expected, NormalizedYAML(input2));
EXPECT_EQ(expected, NormalizedYAML(input3));
EXPECT_EQ(expected, NormalizedYAML(input4));
EXPECT_EQ(NormalizedYAML(input1), NormalizedYAML(NormalizedYAML(input1)));
EXPECT_EQ(NormalizedYAML(expected), NormalizedYAML(input1));
#else
GTEST_SKIP() << "Skipping NormalizedYAML test (Only implemented for yaml-cpp)";
#endif
}
TEST_F(SourceMatcherTest, MatchesSource)
{
std::string programA = "#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
"std::cout << \"Hello, world!\" << std::endl;\n"
"return 0;\n"
"}\n";
std::string programB = "/**\n"
" *\n"
" * My Awesome program!\n"
" *\n"
" */\n"
"#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
" std::cout << \"Hello, world!\" << std::endl;\n"
" return 0; // Success!\n"
"}\n";
std::string programC = "#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
"std::cout << \"Hello, world!\" << std::endl;\n"
"return 0;\n"
"}\n";
EXPECT_THAT(programA, MatchesSource(programA));
EXPECT_THAT(programB, MatchesSource(programB));
EXPECT_THAT(programB, MatchesSource(programA));
EXPECT_THAT(programA, MatchesSource(programB));
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programC, MatchesSource(programA)), "Hello");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programA, MatchesSource(programC)), "Hello");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programC, MatchesSource(programB)), "Hello");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programB, MatchesSource(programC)), "Hello");
}
TEST_F(SourceMatcherTest, MatchesSourceIncludingComments)
{
std::string programA = "/**\n"
" *\n"
" * My Awesome program!\n"
" *\n"
" */\n"
"#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
"std::cout << \"Hello, world!\" << std::endl;\n"
"return 0; // Success!\n"
"}\n";
std::string programB = "/**\n"
" *\n"
" * My Awesome program!\n"
" *\n"
" */\n"
"#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
" std::cout << \"Hello, world!\" << std::endl;\n"
" return 0; // Success!\n"
"}\n";
std::string programC = "#include <iostream>\n"
"int main(int argc, const char * argv[])\n"
"{\n"
"std::cout << \"Hello, world!\" << std::endl;\n"
"return 0;\n"
"}\n";
EXPECT_THAT(programA, MatchesSourceIncludingComments(programA));
EXPECT_THAT(programB, MatchesSourceIncludingComments(programB));
EXPECT_THAT(programB, MatchesSourceIncludingComments(programA));
EXPECT_THAT(programA, MatchesSourceIncludingComments(programB));
EXPECT_THAT(programC, MatchesSource(programA));
EXPECT_THAT(programA, MatchesSource(programC));
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programC, MatchesSourceIncludingComments(programA)),
"Awesome");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programA, MatchesSourceIncludingComments(programC)),
"Awesome");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programC, MatchesSourceIncludingComments(programB)),
"Awesome");
EXPECT_NONFATAL_FAILURE(EXPECT_THAT(programB, MatchesSourceIncludingComments(programC)),
"Awesome");
}
}