From f62aec2dc86e41c6b5b61b891f2cb4909cac1fa4 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 1 Dec 2022 11:40:24 +0000 Subject: [PATCH] tint: Add missing tests / benchmarks For enums generated by intrinsic.def. Change-Id: I1072248b31e2a365a558736f903d25f6e6184962 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112381 Commit-Queue: Ben Clayton Kokoro: Kokoro Auto-Submit: Ben Clayton Reviewed-by: Dan Sinclair --- src/tint/BUILD.gn | 1 + src/tint/CMakeLists.txt | 5 +- src/tint/ast/access_bench.cc | 51 ++++++++ src/tint/ast/access_bench.cc.tmpl | 29 +++++ src/tint/ast/access_test.cc | 82 ++++++++++++ src/tint/ast/access_test.cc.tmpl | 30 +++++ src/tint/ast/interpolate_attribute_bench.cc | 67 ++++++++++ .../ast/interpolate_attribute_bench.cc.tmpl | 27 ++++ src/tint/ast/interpolate_attribute_test.cc | 123 ++++++++++++++++++ .../ast/interpolate_attribute_test.cc.tmpl | 45 +++++++ 10 files changed, 457 insertions(+), 3 deletions(-) create mode 100644 src/tint/ast/access_bench.cc create mode 100644 src/tint/ast/access_bench.cc.tmpl create mode 100644 src/tint/ast/access_test.cc create mode 100644 src/tint/ast/access_test.cc.tmpl create mode 100644 src/tint/ast/interpolate_attribute_bench.cc create mode 100644 src/tint/ast/interpolate_attribute_bench.cc.tmpl create mode 100644 src/tint/ast/interpolate_attribute_test.cc.tmpl diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 3741764d47..27ae6560f4 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -1016,6 +1016,7 @@ if (tint_build_unittests) { tint_unittests_source_set("tint_unittests_ast_src") { sources = [ + "ast/access_test.cc", "ast/address_space_test.cc", "ast/alias_test.cc", "ast/array_test.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index adcb118fc9..e846756395 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -535,11 +535,11 @@ list(APPEND TINT_LIB_SRCS writer/writer.h ) -tint_generated(ast/access) +tint_generated(ast/access BENCH TEST) tint_generated(ast/address_space BENCH TEST) tint_generated(ast/builtin_value BENCH TEST) tint_generated(ast/extension BENCH TEST) -tint_generated(ast/interpolate_attribute) +tint_generated(ast/interpolate_attribute BENCH TEST) tint_generated(ast/texel_format BENCH TEST) tint_generated(resolver/init_conv_intrinsic) tint_generated(sem/builtin_type) @@ -809,7 +809,6 @@ if(TINT_BUILD_TESTS) ast/increment_decrement_statement_test.cc ast/index_accessor_expression_test.cc ast/int_literal_expression_test.cc - ast/interpolate_attribute_test.cc ast/invariant_attribute_test.cc ast/location_attribute_test.cc ast/loop_statement_test.cc diff --git a/src/tint/ast/access_bench.cc b/src/tint/ast/access_bench.cc new file mode 100644 index 0000000000..46b4f93604 --- /dev/null +++ b/src/tint/ast/access_bench.cc @@ -0,0 +1,51 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/ast/access_bench.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/ast/access.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::ast { +namespace { + +void AccessParser(::benchmark::State& state) { + std::array kStrings{ + "ccad", "3", "rVad", "read", "1ead", + "rqaJ", "rlla77", "reqqdppriHHe", "rv_wcit", "reabGwrte", + "read_write", "read_vriite", "re8d_wriWWe", "Meadxxrite", "wggte", + "VtX", "writ3", "write", "writE", "TTrPte", + "wxxidd", + }; + for (auto _ : state) { + for (auto& str : kStrings) { + auto result = ParseAccess(str); + benchmark::DoNotOptimize(result); + } + } +} + +BENCHMARK(AccessParser); + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/access_bench.cc.tmpl b/src/tint/ast/access_bench.cc.tmpl new file mode 100644 index 0000000000..e4ef341cd0 --- /dev/null +++ b/src/tint/ast/access_bench.cc.tmpl @@ -0,0 +1,29 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate access_bench.cc + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "access") -}} + +#include "src/tint/ast/access.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::ast { +namespace { + +{{ Eval "BenchmarkParseEnum" $enum }} + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/access_test.cc b/src/tint/ast/access_test.cc new file mode 100644 index 0000000000..017915d793 --- /dev/null +++ b/src/tint/ast/access_test.cc @@ -0,0 +1,82 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/ast/access_test.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/ast/access.h" + +#include + +#include "src/tint/ast/test_helper.h" +#include "src/tint/utils/string.h" + +namespace tint::ast { +namespace { + +namespace parse_print_tests { + +struct Case { + const char* string; + Access value; +}; + +inline std::ostream& operator<<(std::ostream& out, Case c) { + return out << "'" << std::string(c.string) << "'"; +} + +static constexpr Case kValidCases[] = { + {"read", Access::kRead}, + {"read_write", Access::kReadWrite}, + {"write", Access::kWrite}, +}; + +static constexpr Case kInvalidCases[] = { + {"ccad", Access::kUndefined}, {"3", Access::kUndefined}, + {"rVad", Access::kUndefined}, {"read1write", Access::kUndefined}, + {"reaJqqrite", Access::kUndefined}, {"rea7ll_write", Access::kUndefined}, + {"wrqHtpp", Access::kUndefined}, {"ve", Access::kUndefined}, + {"Grbe", Access::kUndefined}, +}; + +using AccessParseTest = testing::TestWithParam; + +TEST_P(AccessParseTest, Parse) { + const char* string = GetParam().string; + Access expect = GetParam().value; + EXPECT_EQ(expect, ParseAccess(string)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, AccessParseTest, testing::ValuesIn(kValidCases)); +INSTANTIATE_TEST_SUITE_P(InvalidCases, AccessParseTest, testing::ValuesIn(kInvalidCases)); + +using AccessPrintTest = testing::TestWithParam; + +TEST_P(AccessPrintTest, Print) { + Access value = GetParam().value; + const char* expect = GetParam().string; + EXPECT_EQ(expect, utils::ToString(value)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, AccessPrintTest, testing::ValuesIn(kValidCases)); + +} // namespace parse_print_tests + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/access_test.cc.tmpl b/src/tint/ast/access_test.cc.tmpl new file mode 100644 index 0000000000..9b4b71a007 --- /dev/null +++ b/src/tint/ast/access_test.cc.tmpl @@ -0,0 +1,30 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate access_test.cc + +To update the generated file, run: + ./tools/run gen + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} +{{- $enum := (Sem.Enum "access") -}} + +#include "src/tint/ast/access.h" + +#include + +#include "src/tint/ast/test_helper.h" +#include "src/tint/utils/string.h" + +namespace tint::ast { +namespace { + +{{ Eval "TestParsePrintEnum" $enum}} + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/interpolate_attribute_bench.cc b/src/tint/ast/interpolate_attribute_bench.cc new file mode 100644 index 0000000000..6382139892 --- /dev/null +++ b/src/tint/ast/interpolate_attribute_bench.cc @@ -0,0 +1,67 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/ast/interpolate_attribute_bench.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + +#include "src/tint/ast/interpolate_attribute.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::ast { +namespace { + +void InterpolationTypeParser(::benchmark::State& state) { + std::array kStrings{ + "ccat", "3", "fVat", "flat", "1lat", + "fqaJ", "flla77", "lippeHHr", "cin", "lbGea", + "linear", "liveaii", "liWWe8r", "xxiner", "perggpctXve", + "ueVspXtve", "3erspective", "perspective", "perspectivE", "peTTspectiPe", + "pxxdrspectve", + }; + for (auto _ : state) { + for (auto& str : kStrings) { + auto result = ParseInterpolationType(str); + benchmark::DoNotOptimize(result); + } + } +} + +BENCHMARK(InterpolationTypeParser); + +void InterpolationSamplingParser(::benchmark::State& state) { + std::array kStrings{ + "44enter", "cSSVVter", "centRr", "center", "ent9r", "cente", "VentORr", + "cenyroi", "77errtrllnid", "04entroid", "centroid", "enooid", "centzzd", "ceiippr1i", + "saXXple", "55IImpnn99", "aHHrrmplSS", "sample", "kkle", "jagRR", "smbe", + }; + for (auto _ : state) { + for (auto& str : kStrings) { + auto result = ParseInterpolationSampling(str); + benchmark::DoNotOptimize(result); + } + } +} + +BENCHMARK(InterpolationSamplingParser); + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/interpolate_attribute_bench.cc.tmpl b/src/tint/ast/interpolate_attribute_bench.cc.tmpl new file mode 100644 index 0000000000..b75fe44cf4 --- /dev/null +++ b/src/tint/ast/interpolate_attribute_bench.cc.tmpl @@ -0,0 +1,27 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate interpolate_attribute_bench.cc + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} + +#include "src/tint/ast/interpolate_attribute.h" + +#include + +#include "benchmark/benchmark.h" + +namespace tint::ast { +namespace { + +{{ Eval "BenchmarkParseEnum" (Sem.Enum "interpolation_type")}} + +{{ Eval "BenchmarkParseEnum" (Sem.Enum "interpolation_sampling")}} + +} // namespace +} // namespace tint::ast diff --git a/src/tint/ast/interpolate_attribute_test.cc b/src/tint/ast/interpolate_attribute_test.cc index d8b6601970..6c1204f544 100644 --- a/src/tint/ast/interpolate_attribute_test.cc +++ b/src/tint/ast/interpolate_attribute_test.cc @@ -12,9 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. +//////////////////////////////////////////////////////////////////////////////// +// File generated by tools/src/cmd/gen +// using the template: +// src/tint/ast/interpolate_attribute_test.cc.tmpl +// +// Do not modify this file directly +//////////////////////////////////////////////////////////////////////////////// + #include "src/tint/ast/interpolate_attribute.h" +#include + #include "src/tint/ast/test_helper.h" +#include "src/tint/utils/string.h" namespace tint::ast { namespace { @@ -28,5 +39,117 @@ TEST_F(InterpolateAttributeTest, Creation) { EXPECT_EQ(InterpolationSampling::kCenter, d->sampling); } +namespace interpolation_type_tests { + +namespace parse_print_tests { + +struct Case { + const char* string; + InterpolationType value; +}; + +inline std::ostream& operator<<(std::ostream& out, Case c) { + return out << "'" << std::string(c.string) << "'"; +} + +static constexpr Case kValidCases[] = { + {"flat", InterpolationType::kFlat}, + {"linear", InterpolationType::kLinear}, + {"perspective", InterpolationType::kPerspective}, +}; + +static constexpr Case kInvalidCases[] = { + {"ccat", InterpolationType::kUndefined}, {"3", InterpolationType::kUndefined}, + {"fVat", InterpolationType::kUndefined}, {"1inear", InterpolationType::kUndefined}, + {"lnqqar", InterpolationType::kUndefined}, {"linell77", InterpolationType::kUndefined}, + {"perppHqective", InterpolationType::kUndefined}, {"cespctve", InterpolationType::kUndefined}, + {"ebGpective", InterpolationType::kUndefined}, +}; + +using InterpolationTypeParseTest = testing::TestWithParam; + +TEST_P(InterpolationTypeParseTest, Parse) { + const char* string = GetParam().string; + InterpolationType expect = GetParam().value; + EXPECT_EQ(expect, ParseInterpolationType(string)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, InterpolationTypeParseTest, testing::ValuesIn(kValidCases)); +INSTANTIATE_TEST_SUITE_P(InvalidCases, + InterpolationTypeParseTest, + testing::ValuesIn(kInvalidCases)); + +using InterpolationTypePrintTest = testing::TestWithParam; + +TEST_P(InterpolationTypePrintTest, Print) { + InterpolationType value = GetParam().value; + const char* expect = GetParam().string; + EXPECT_EQ(expect, utils::ToString(value)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, InterpolationTypePrintTest, testing::ValuesIn(kValidCases)); + +} // namespace parse_print_tests + +} // namespace interpolation_type_tests + +namespace interpolation_sampling_tests { + +namespace parse_print_tests { + +struct Case { + const char* string; + InterpolationSampling value; +}; + +inline std::ostream& operator<<(std::ostream& out, Case c) { + return out << "'" << std::string(c.string) << "'"; +} + +static constexpr Case kValidCases[] = { + {"center", InterpolationSampling::kCenter}, + {"centroid", InterpolationSampling::kCentroid}, + {"sample", InterpolationSampling::kSample}, +}; + +static constexpr Case kInvalidCases[] = { + {"cevteii", InterpolationSampling::kUndefined}, {"ceWWt8r", InterpolationSampling::kUndefined}, + {"xxentr", InterpolationSampling::kUndefined}, {"ceXggrid", InterpolationSampling::kUndefined}, + {"ceXriu", InterpolationSampling::kUndefined}, {"centr3id", InterpolationSampling::kUndefined}, + {"sEmple", InterpolationSampling::kUndefined}, {"amTTlPP", InterpolationSampling::kUndefined}, + {"ddamxxl", InterpolationSampling::kUndefined}, +}; + +using InterpolationSamplingParseTest = testing::TestWithParam; + +TEST_P(InterpolationSamplingParseTest, Parse) { + const char* string = GetParam().string; + InterpolationSampling expect = GetParam().value; + EXPECT_EQ(expect, ParseInterpolationSampling(string)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, + InterpolationSamplingParseTest, + testing::ValuesIn(kValidCases)); +INSTANTIATE_TEST_SUITE_P(InvalidCases, + InterpolationSamplingParseTest, + testing::ValuesIn(kInvalidCases)); + +using InterpolationSamplingPrintTest = testing::TestWithParam; + +TEST_P(InterpolationSamplingPrintTest, Print) { + InterpolationSampling value = GetParam().value; + const char* expect = GetParam().string; + EXPECT_EQ(expect, utils::ToString(value)); +} + +INSTANTIATE_TEST_SUITE_P(ValidCases, + InterpolationSamplingPrintTest, + testing::ValuesIn(kValidCases)); + +} // namespace parse_print_tests + +} // namespace interpolation_sampling_tests + } // namespace } // namespace tint::ast diff --git a/src/tint/ast/interpolate_attribute_test.cc.tmpl b/src/tint/ast/interpolate_attribute_test.cc.tmpl new file mode 100644 index 0000000000..1adc9ab04e --- /dev/null +++ b/src/tint/ast/interpolate_attribute_test.cc.tmpl @@ -0,0 +1,45 @@ +{{- /* +-------------------------------------------------------------------------------- +Template file for use with tools/src/cmd/gen to generate interpolate_attribute_test.cc + +See: +* tools/src/cmd/gen for structures used by this template +* https://golang.org/pkg/text/template/ for documentation on the template syntax +-------------------------------------------------------------------------------- +*/ -}} + +{{- Import "src/tint/templates/enums.tmpl.inc" -}} + +#include "src/tint/ast/interpolate_attribute.h" + +#include + +#include "src/tint/ast/test_helper.h" +#include "src/tint/utils/string.h" + +namespace tint::ast { +namespace { + +using InterpolateAttributeTest = TestHelper; + +TEST_F(InterpolateAttributeTest, Creation) { + auto* d = + create(InterpolationType::kLinear, InterpolationSampling::kCenter); + EXPECT_EQ(InterpolationType::kLinear, d->type); + EXPECT_EQ(InterpolationSampling::kCenter, d->sampling); +} + +namespace interpolation_type_tests { + +{{ Eval "TestParsePrintEnum" (Sem.Enum "interpolation_type")}} + +} // namespace interpolation_type_tests + +namespace interpolation_sampling_tests { + +{{ Eval "TestParsePrintEnum" (Sem.Enum "interpolation_sampling")}} + +} // namespace interpolation_sampling_tests + +} // namespace +} // namespace tint::ast