Ben Clayton | 0619b8c | 2023-11-03 00:21:53 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Dawn & Tint Authors |
| 2 | // |
| 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
| 5 | // |
| 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
| 8 | // |
| 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // GEN_BUILD:CONDITION(tint_build_wgsl_reader && tint_build_wgsl_writer) |
| 29 | |
| 30 | #include <string> |
| 31 | #include <unordered_set> |
| 32 | |
Ben Clayton | b098803 | 2023-11-22 17:47:28 +0000 | [diff] [blame] | 33 | #include "src/tint/cmd/fuzz/wgsl/fuzz.h" |
Ben Clayton | 0619b8c | 2023-11-03 00:21:53 +0000 | [diff] [blame] | 34 | #include "src/tint/lang/wgsl/reader/parser/parser.h" |
| 35 | #include "src/tint/lang/wgsl/writer/writer.h" |
| 36 | |
| 37 | namespace tint::program { |
| 38 | |
| 39 | #define ASSERT_EQ(A, B) \ |
| 40 | do { \ |
| 41 | decltype(A) assert_a = (A); \ |
| 42 | decltype(B) assert_b = (B); \ |
| 43 | if (assert_a != assert_b) { \ |
| 44 | TINT_ICE() << "ASSERT_EQ(" #A ", " #B ") failed:\n" \ |
| 45 | << #A << " was: " << assert_a << "\n" \ |
| 46 | << #B << " was: " << assert_b << "\n"; \ |
| 47 | } \ |
| 48 | } while (false) |
| 49 | |
| 50 | #define ASSERT_TRUE(A) \ |
| 51 | do { \ |
| 52 | decltype(A) assert_a = (A); \ |
Ben Clayton | 89274f7 | 2024-01-03 10:53:42 +0000 | [diff] [blame] | 53 | if (assert_a != Success) { \ |
Ben Clayton | 0619b8c | 2023-11-03 00:21:53 +0000 | [diff] [blame] | 54 | TINT_ICE() << "ASSERT_TRUE(" #A ") failed:\n" << #A << " was: " << assert_a << "\n"; \ |
| 55 | } \ |
| 56 | } while (false) |
| 57 | |
| 58 | void CloneContextFuzzer(const tint::Program& src) { |
| 59 | // Clone the src program to dst |
| 60 | tint::Program dst(src.Clone()); |
| 61 | |
| 62 | // Expect the printed strings to match |
| 63 | ASSERT_EQ(tint::Program::printer(src), tint::Program::printer(dst)); |
| 64 | |
| 65 | // Check that none of the AST nodes or type pointers in dst are found in src |
| 66 | std::unordered_set<const tint::ast::Node*> src_nodes; |
| 67 | for (auto* src_node : src.ASTNodes().Objects()) { |
| 68 | src_nodes.emplace(src_node); |
| 69 | } |
| 70 | std::unordered_set<const tint::core::type::Type*> src_types; |
| 71 | for (auto* src_type : src.Types()) { |
| 72 | src_types.emplace(src_type); |
| 73 | } |
| 74 | for (auto* dst_node : dst.ASTNodes().Objects()) { |
| 75 | ASSERT_EQ(src_nodes.count(dst_node), 0u); |
| 76 | } |
| 77 | for (auto* dst_type : dst.Types()) { |
| 78 | ASSERT_EQ(src_types.count(dst_type), 0u); |
| 79 | } |
| 80 | |
| 81 | tint::wgsl::writer::Options wgsl_options; |
| 82 | |
| 83 | auto src_wgsl = tint::wgsl::writer::Generate(src, wgsl_options); |
| 84 | ASSERT_TRUE(src_wgsl); |
| 85 | |
| 86 | auto dst_wgsl = tint::wgsl::writer::Generate(dst, wgsl_options); |
| 87 | ASSERT_TRUE(dst_wgsl); |
| 88 | |
| 89 | ASSERT_EQ(src_wgsl->wgsl, dst_wgsl->wgsl); |
| 90 | } |
| 91 | |
| 92 | } // namespace tint::program |
| 93 | |
| 94 | TINT_WGSL_PROGRAM_FUZZER(tint::program::CloneContextFuzzer); |