2020-12-03 18:10:39 +00:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2021-04-07 11:16:01 +00:00
|
|
|
#include <unordered_set>
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-04-07 11:16:01 +00:00
|
|
|
#include "gtest/gtest-spi.h"
|
2021-01-26 16:57:10 +00:00
|
|
|
#include "src/program_builder.h"
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace {
|
|
|
|
|
2021-04-16 20:30:21 +00:00
|
|
|
struct Allocator {
|
|
|
|
template <typename T, typename... ARGS>
|
|
|
|
T* Create(ARGS&&... args) {
|
|
|
|
return alloc.Create<T>(this, std::forward<ARGS>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BlockAllocator<Cloneable> alloc;
|
|
|
|
};
|
2020-12-12 13:00:34 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
struct Node : public Castable<Node, Cloneable> {
|
|
|
|
Node(Allocator* alloc, Symbol n) : allocator(alloc), name(n) {}
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator* const allocator;
|
2021-03-04 10:42:55 +00:00
|
|
|
Symbol name;
|
2021-05-19 13:25:08 +00:00
|
|
|
Node* a = nullptr;
|
|
|
|
Node* b = nullptr;
|
|
|
|
Node* c = nullptr;
|
|
|
|
std::vector<Node*> vec;
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
Node* Clone(CloneContext* ctx) const override {
|
|
|
|
auto* out = allocator->Create<Node>(ctx->Clone(name));
|
2020-12-03 18:10:39 +00:00
|
|
|
out->a = ctx->Clone(a);
|
|
|
|
out->b = ctx->Clone(b);
|
|
|
|
out->c = ctx->Clone(c);
|
2021-02-16 23:09:31 +00:00
|
|
|
out->vec = ctx->Clone(vec);
|
2020-12-03 18:10:39 +00:00
|
|
|
return out;
|
|
|
|
}
|
2021-04-16 20:30:21 +00:00
|
|
|
};
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
struct Replaceable : public Castable<Replaceable, Node> {
|
|
|
|
Replaceable(Allocator* alloc, Symbol n) : Base(alloc, n) {}
|
2020-12-03 18:10:39 +00:00
|
|
|
};
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
struct Replacement : public Castable<Replacement, Replaceable> {
|
|
|
|
Replacement(Allocator* alloc, Symbol n) : Base(alloc, n) {}
|
2021-01-26 16:57:10 +00:00
|
|
|
};
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-04-16 20:30:21 +00:00
|
|
|
struct NotANode : public Castable<NotANode, Cloneable> {
|
|
|
|
explicit NotANode(Allocator* alloc) : allocator(alloc) {}
|
2021-02-16 22:27:11 +00:00
|
|
|
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator* const allocator;
|
|
|
|
NotANode* Clone(CloneContext*) const override {
|
|
|
|
return allocator->Create<NotANode>();
|
2021-02-16 22:27:11 +00:00
|
|
|
}
|
2021-04-16 20:30:21 +00:00
|
|
|
};
|
2021-02-16 22:27:11 +00:00
|
|
|
|
2021-04-19 16:50:23 +00:00
|
|
|
struct ProgramNode : public Castable<ProgramNode, Cloneable> {
|
|
|
|
ProgramNode(Allocator* alloc, ProgramID id, ProgramID cloned_id)
|
|
|
|
: allocator(alloc), program_id(id), cloned_program_id(cloned_id) {}
|
|
|
|
|
|
|
|
Allocator* const allocator;
|
|
|
|
ProgramID const program_id;
|
|
|
|
ProgramID const cloned_program_id;
|
|
|
|
|
|
|
|
ProgramNode* Clone(CloneContext*) const override {
|
|
|
|
return allocator->Create<ProgramNode>(cloned_program_id, cloned_program_id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ProgramID ProgramIDOf(const ProgramNode* node) {
|
|
|
|
return node->program_id;
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
using CloneContextNodeTest = ::testing::Test;
|
2021-04-16 20:30:21 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, Clone) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().New("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().New("a"));
|
|
|
|
original_root->a->b = a.Create<Node>(builder.Symbols().New("a->b"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().New("b"));
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
2021-04-16 20:30:21 +00:00
|
|
|
original_root->b->b = a.Create<Node>(builder.Symbols().New("b->b"));
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->c = original_root->b; // Aliased
|
2021-01-26 16:57:10 +00:00
|
|
|
Program original(std::move(builder));
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ N <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N └───┘ N
|
2020-12-03 18:10:39 +00:00
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2020-12-15 12:32:18 +00:00
|
|
|
auto* cloned_root = CloneContext(&cloned, &original).Clone(original_root);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->a->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->b->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->c, nullptr);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, original_root->a);
|
|
|
|
EXPECT_NE(cloned_root->a->b, original_root->a->b);
|
|
|
|
EXPECT_NE(cloned_root->b, original_root->b);
|
|
|
|
EXPECT_NE(cloned_root->b->a, original_root->b->a);
|
|
|
|
EXPECT_NE(cloned_root->b->b, original_root->b->b);
|
|
|
|
EXPECT_NE(cloned_root->c, original_root->c);
|
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->a->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->a->b->name, cloned.Symbols().Get("a->b"));
|
|
|
|
EXPECT_EQ(cloned_root->b->name, cloned.Symbols().Get("b"));
|
|
|
|
EXPECT_EQ(cloned_root->b->b->name, cloned.Symbols().Get("b->b"));
|
2021-02-16 23:09:31 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
EXPECT_NE(cloned_root->b->a, cloned_root->a); // De-aliased
|
|
|
|
EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased
|
|
|
|
|
2021-04-16 20:30:21 +00:00
|
|
|
EXPECT_EQ(cloned_root->b->a->name, cloned_root->a->name);
|
|
|
|
EXPECT_EQ(cloned_root->c->name, cloned_root->b->name);
|
2020-12-03 18:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().New("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().New("a"));
|
|
|
|
original_root->a->b = a.Create<Replaceable>(builder.Symbols().New("a->b"));
|
|
|
|
original_root->b = a.Create<Replaceable>(builder.Symbols().New("b"));
|
2020-12-03 18:10:39 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
|
|
|
original_root->c = original_root->b; // Aliased
|
2021-01-26 16:57:10 +00:00
|
|
|
Program original(std::move(builder));
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ R <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// R └───┘
|
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
// R: Replaceable
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2021-02-26 19:33:56 +00:00
|
|
|
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([&](Replaceable* in) {
|
2021-04-16 20:30:21 +00:00
|
|
|
auto out_name = cloned.Symbols().Register(
|
|
|
|
"replacement:" + original.Symbols().NameFor(in->name));
|
|
|
|
auto b_name = cloned.Symbols().Register(
|
|
|
|
"replacement-child:" + original.Symbols().NameFor(in->name));
|
|
|
|
auto* out = a.Create<Replacement>(out_name);
|
|
|
|
out->b = a.Create<Node>(b_name);
|
2021-02-26 19:33:56 +00:00
|
|
|
out->c = ctx.Clone(in->a);
|
|
|
|
return out;
|
|
|
|
});
|
|
|
|
auto* cloned_root = ctx.Clone(original_root);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭─────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N <──────┐ R <───────────────┘
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// R │ N |
|
2020-12-03 18:10:39 +00:00
|
|
|
// ╭────┼────╮ └────────────┘
|
|
|
|
// (a) (b) (c)
|
2021-02-17 13:17:39 +00:00
|
|
|
// N
|
2020-12-03 18:10:39 +00:00
|
|
|
//
|
2021-02-17 13:17:39 +00:00
|
|
|
// N: Node
|
2020-12-03 18:10:39 +00:00
|
|
|
// R: Replacement
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->a->a, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->a->b, nullptr); // Replaced
|
|
|
|
EXPECT_EQ(cloned_root->a->b->a, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->a->b->b, nullptr); // From replacement
|
|
|
|
EXPECT_EQ(cloned_root->a->b->c, nullptr); // From replacement
|
|
|
|
EXPECT_EQ(cloned_root->a->c, nullptr);
|
|
|
|
EXPECT_NE(cloned_root->b, nullptr);
|
|
|
|
EXPECT_EQ(cloned_root->b->a, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->b->b, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->b->c, nullptr); // From replacement
|
|
|
|
EXPECT_NE(cloned_root->c, nullptr);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, original_root->a);
|
|
|
|
EXPECT_NE(cloned_root->a->b, original_root->a->b);
|
|
|
|
EXPECT_NE(cloned_root->b, original_root->b);
|
|
|
|
EXPECT_NE(cloned_root->b->a, original_root->b->a);
|
|
|
|
EXPECT_NE(cloned_root->c, original_root->c);
|
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->a->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->a->b->name, cloned.Symbols().Get("replacement:a->b"));
|
|
|
|
EXPECT_EQ(cloned_root->a->b->b->name,
|
|
|
|
cloned.Symbols().Get("replacement-child:a->b"));
|
|
|
|
EXPECT_EQ(cloned_root->b->name, cloned.Symbols().Get("replacement:b"));
|
|
|
|
EXPECT_EQ(cloned_root->b->b->name,
|
|
|
|
cloned.Symbols().Get("replacement-child:b"));
|
2021-02-16 23:09:31 +00:00
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
EXPECT_NE(cloned_root->b->c, cloned_root->a); // De-aliased
|
|
|
|
EXPECT_NE(cloned_root->c, cloned_root->b); // De-aliased
|
|
|
|
|
2021-04-16 20:30:21 +00:00
|
|
|
EXPECT_EQ(cloned_root->b->c->name, cloned_root->a->name);
|
|
|
|
EXPECT_EQ(cloned_root->c->name, cloned_root->b->name);
|
|
|
|
|
|
|
|
EXPECT_FALSE(Is<Replacement>(cloned_root->a));
|
|
|
|
EXPECT_TRUE(Is<Replacement>(cloned_root->a->b));
|
|
|
|
EXPECT_FALSE(Is<Replacement>(cloned_root->a->b->b));
|
|
|
|
EXPECT_TRUE(Is<Replacement>(cloned_root->b));
|
|
|
|
EXPECT_FALSE(Is<Replacement>(cloned_root->b->b));
|
2020-12-03 18:10:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Symbols) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().New("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().New("a"));
|
|
|
|
original_root->a->b = a.Create<Node>(builder.Symbols().New("a->b"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().New("b"));
|
2021-03-04 10:42:55 +00:00
|
|
|
original_root->b->a = original_root->a; // Aliased
|
2021-04-16 20:30:21 +00:00
|
|
|
original_root->b->b = a.Create<Node>(builder.Symbols().New("b->b"));
|
2021-03-04 10:42:55 +00:00
|
|
|
original_root->c = original_root->b; // Aliased
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// N <──────┐ N <───────────────┘
|
|
|
|
// ╭────┼────╮ │ ╭────┼────╮
|
|
|
|
// (a) (b) (c) │ (a) (b) (c)
|
|
|
|
// N └───┘ N
|
|
|
|
//
|
|
|
|
// N: Node
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 10:29:54 +00:00
|
|
|
auto* cloned_root = CloneContext(&cloned, &original, false)
|
2021-03-04 10:42:55 +00:00
|
|
|
.ReplaceAll([&](Symbol sym) {
|
|
|
|
auto in = original.Symbols().NameFor(sym);
|
|
|
|
auto out = "transformed<" + in + ">";
|
2021-04-13 20:07:57 +00:00
|
|
|
return cloned.Symbols().New(out);
|
2021-03-04 10:42:55 +00:00
|
|
|
})
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("transformed<root>"));
|
|
|
|
EXPECT_EQ(cloned_root->a->name, cloned.Symbols().Get("transformed<a>"));
|
|
|
|
EXPECT_EQ(cloned_root->a->b->name, cloned.Symbols().Get("transformed<a->b>"));
|
|
|
|
EXPECT_EQ(cloned_root->b->name, cloned.Symbols().Get("transformed<b>"));
|
|
|
|
EXPECT_EQ(cloned_root->b->b->name, cloned.Symbols().Get("transformed<b->b>"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithoutTransform) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-04-08 14:47:37 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_node = a.Create<Node>(builder.Symbols().New("root"));
|
2021-04-08 14:47:37 +00:00
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([&](Node*) {
|
2021-04-16 20:30:21 +00:00
|
|
|
return a.Create<Replacement>(builder.Symbols().New("<unexpected-node>"));
|
2021-04-08 14:47:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
auto* cloned_node = ctx.CloneWithoutTransform(original_node);
|
|
|
|
EXPECT_NE(cloned_node, original_node);
|
|
|
|
EXPECT_EQ(cloned_node->name, cloned.Symbols().Get("root"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplace) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().New("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().New("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().New("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().New("c"));
|
2021-01-26 16:57:10 +00:00
|
|
|
Program original(std::move(builder));
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// Replaced
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
ProgramBuilder cloned;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* replacement = a.Create<Node>(cloned.Symbols().New("replacement"));
|
2021-01-26 16:57:10 +00:00
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.Replace(original_root->b, replacement)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->a, replacement);
|
|
|
|
EXPECT_EQ(cloned_root->b, replacement);
|
|
|
|
EXPECT_NE(cloned_root->c, replacement);
|
2021-02-16 23:09:31 +00:00
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->a->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->b->name, cloned.Symbols().Get("replacement"));
|
|
|
|
EXPECT_EQ(cloned_root->c->name, cloned.Symbols().Get("c"));
|
2021-02-16 23:09:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithRemove) {
|
2021-05-18 14:26:17 +00:00
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.Remove(original_root->vec, original_root->b)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 2u);
|
2021-05-19 13:25:08 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->vec[0], cloned_root->a);
|
|
|
|
EXPECT_NE(cloned_root->vec[1], cloned_root->c);
|
2021-05-18 14:26:17 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("c"));
|
|
|
|
}
|
|
|
|
|
2021-05-26 11:31:32 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertFront) {
|
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertFront(original_root->vec, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->vec[0], cloned_root->a);
|
|
|
|
EXPECT_NE(cloned_root->vec[1], cloned_root->b);
|
|
|
|
EXPECT_NE(cloned_root->vec[2], cloned_root->c);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, cloned.Symbols().Get("b"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertFront_Empty) {
|
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->vec = {};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertFront(original_root->vec, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:12:12 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertBack) {
|
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertBack(original_root->vec, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("b"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, cloned.Symbols().Get("c"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertBack_Empty) {
|
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->vec = {};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertBack(original_root->vec, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertFrontAndBack_Empty) {
|
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->vec = {};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion_front =
|
|
|
|
a.Create<Node>(cloned.Symbols().New("insertion_front"));
|
|
|
|
auto* insertion_back = a.Create<Node>(cloned.Symbols().New("insertion_back"));
|
|
|
|
|
|
|
|
auto* cloned_root = CloneContext(&cloned, &original)
|
|
|
|
.InsertBack(original_root->vec, insertion_back)
|
|
|
|
.InsertFront(original_root->vec, insertion_front)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 2u);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("insertion_front"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("insertion_back"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertBefore) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-02-16 23:09:31 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
2021-02-16 23:09:31 +00:00
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
2021-02-16 23:09:31 +00:00
|
|
|
|
2021-03-31 21:00:26 +00:00
|
|
|
auto* cloned_root =
|
|
|
|
CloneContext(&cloned, &original)
|
|
|
|
.InsertBefore(original_root->vec, original_root->b, insertion)
|
|
|
|
.Clone(original_root);
|
2021-02-16 23:09:31 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
2021-05-19 13:25:08 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->vec[0], cloned_root->a);
|
|
|
|
EXPECT_NE(cloned_root->vec[2], cloned_root->b);
|
|
|
|
EXPECT_NE(cloned_root->vec[3], cloned_root->c);
|
2021-02-16 23:09:31 +00:00
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, cloned.Symbols().Get("b"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertAfter) {
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator a;
|
|
|
|
|
2021-03-31 21:00:26 +00:00
|
|
|
ProgramBuilder builder;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
2021-03-31 21:00:26 +00:00
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* insertion = a.Create<Node>(cloned.Symbols().New("insertion"));
|
2021-03-31 21:00:26 +00:00
|
|
|
|
|
|
|
auto* cloned_root =
|
|
|
|
CloneContext(&cloned, &original)
|
|
|
|
.InsertAfter(original_root->vec, original_root->b, insertion)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
2021-05-19 13:25:08 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->vec[0], cloned_root->a);
|
|
|
|
EXPECT_NE(cloned_root->vec[1], cloned_root->b);
|
|
|
|
EXPECT_NE(cloned_root->vec[3], cloned_root->c);
|
2021-03-31 21:00:26 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name, cloned.Symbols().Get("b"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, cloned.Symbols().Get("insertion"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithInsertBeforeAndAfterRemoved) {
|
2021-05-18 21:28:58 +00:00
|
|
|
Allocator a;
|
|
|
|
|
|
|
|
ProgramBuilder builder;
|
|
|
|
auto* original_root = a.Create<Node>(builder.Symbols().Register("root"));
|
|
|
|
original_root->a = a.Create<Node>(builder.Symbols().Register("a"));
|
|
|
|
original_root->b = a.Create<Node>(builder.Symbols().Register("b"));
|
|
|
|
original_root->c = a.Create<Node>(builder.Symbols().Register("c"));
|
|
|
|
original_root->vec = {original_root->a, original_root->b, original_root->c};
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
auto* insertion_before =
|
|
|
|
a.Create<Node>(cloned.Symbols().New("insertion_before"));
|
|
|
|
auto* insertion_after =
|
|
|
|
a.Create<Node>(cloned.Symbols().New("insertion_after"));
|
|
|
|
|
|
|
|
auto* cloned_root =
|
|
|
|
CloneContext(&cloned, &original)
|
|
|
|
.InsertBefore(original_root->vec, original_root->b, insertion_before)
|
|
|
|
.InsertAfter(original_root->vec, original_root->b, insertion_after)
|
|
|
|
.Remove(original_root->vec, original_root->b)
|
|
|
|
.Clone(original_root);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->vec.size(), 4u);
|
2021-05-19 13:25:08 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_root->vec[0], cloned_root->a);
|
|
|
|
EXPECT_NE(cloned_root->vec[3], cloned_root->c);
|
2021-05-18 21:28:58 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(cloned_root->name, cloned.Symbols().Get("root"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[0]->name, cloned.Symbols().Get("a"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[1]->name,
|
|
|
|
cloned.Symbols().Get("insertion_before"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[2]->name, cloned.Symbols().Get("insertion_after"));
|
|
|
|
EXPECT_EQ(cloned_root->vec[3]->name, cloned.Symbols().Get("c"));
|
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneIntoSameBuilder) {
|
2021-05-04 18:06:31 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
CloneContext ctx(&builder);
|
|
|
|
Allocator allocator;
|
|
|
|
auto* original = allocator.Create<Node>(builder.Symbols().New());
|
|
|
|
auto* cloned_a = ctx.Clone(original);
|
|
|
|
auto* cloned_b = ctx.Clone(original);
|
|
|
|
EXPECT_NE(original, cloned_a);
|
|
|
|
EXPECT_NE(original, cloned_b);
|
2021-05-19 13:25:08 +00:00
|
|
|
|
|
|
|
EXPECT_NE(cloned_a, cloned_b);
|
2021-05-04 18:06:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplaceAll_SameTypeTwice) {
|
|
|
|
std::string node_name = TypeInfo::Of<Node>().name;
|
2021-04-16 20:30:21 +00:00
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
Program original;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([](Node*) { return nullptr; });
|
|
|
|
ctx.ReplaceAll([](Node*) { return nullptr; });
|
|
|
|
},
|
2021-04-16 20:30:21 +00:00
|
|
|
"internal compiler error: ReplaceAll() called with a handler for type " +
|
|
|
|
node_name + " that is already handled by a handler for type " +
|
|
|
|
node_name);
|
2021-03-04 10:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplaceAll_BaseThenDerived) {
|
|
|
|
std::string node_name = TypeInfo::Of<Node>().name;
|
|
|
|
std::string replaceable_name = TypeInfo::Of<Replaceable>().name;
|
2021-04-16 20:30:21 +00:00
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
Program original;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([](Node*) { return nullptr; });
|
|
|
|
ctx.ReplaceAll([](Replaceable*) { return nullptr; });
|
|
|
|
},
|
2021-04-16 20:30:21 +00:00
|
|
|
"internal compiler error: ReplaceAll() called with a handler for type " +
|
|
|
|
replaceable_name + " that is already handled by a handler for type " +
|
|
|
|
node_name);
|
2021-03-04 10:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplaceAll_DerivedThenBase) {
|
|
|
|
std::string node_name = TypeInfo::Of<Node>().name;
|
|
|
|
std::string replaceable_name = TypeInfo::Of<Replaceable>().name;
|
2021-04-16 20:30:21 +00:00
|
|
|
|
2021-03-04 10:42:55 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
Program original;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([](Replaceable*) { return nullptr; });
|
|
|
|
ctx.ReplaceAll([](Node*) { return nullptr; });
|
|
|
|
},
|
2021-04-16 20:30:21 +00:00
|
|
|
"internal compiler error: ReplaceAll() called with a handler for type " +
|
|
|
|
node_name + " that is already handled by a handler for type " +
|
|
|
|
replaceable_name);
|
2021-03-04 10:42:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TEST_F(CloneContextNodeTest, CloneWithReplace_WithNotANode) {
|
2021-02-17 20:13:34 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
2021-04-16 20:30:21 +00:00
|
|
|
Allocator allocator;
|
2021-02-17 20:13:34 +00:00
|
|
|
ProgramBuilder builder;
|
2021-03-04 10:42:55 +00:00
|
|
|
auto* original_root =
|
2021-04-16 20:30:21 +00:00
|
|
|
allocator.Create<Node>(builder.Symbols().New("root"));
|
|
|
|
original_root->a = allocator.Create<Node>(builder.Symbols().New("a"));
|
|
|
|
original_root->b = allocator.Create<Node>(builder.Symbols().New("b"));
|
|
|
|
original_root->c = allocator.Create<Node>(builder.Symbols().New("c"));
|
2021-02-17 20:13:34 +00:00
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
// root
|
|
|
|
// ╭──────────────────┼──────────────────╮
|
|
|
|
// (a) (b) (c)
|
|
|
|
// Replaced
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 20:30:21 +00:00
|
|
|
auto* replacement = allocator.Create<NotANode>();
|
2021-02-17 20:13:34 +00:00
|
|
|
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.Replace(original_root->b, replacement);
|
|
|
|
|
|
|
|
ctx.Clone(original_root);
|
|
|
|
},
|
|
|
|
"internal compiler error");
|
2021-02-16 22:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:06:31 +00:00
|
|
|
using CloneContextTest = ::testing::Test;
|
|
|
|
|
|
|
|
TEST_F(CloneContextTest, CloneWithReplaceAll_SymbolsTwice) {
|
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
Program original;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
ctx.ReplaceAll([](Symbol s) { return s; });
|
|
|
|
ctx.ReplaceAll([](Symbol s) { return s; });
|
|
|
|
},
|
|
|
|
"internal compiler error: ReplaceAll(const SymbolTransform&) called "
|
|
|
|
"multiple times on the same CloneContext");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CloneContextTest, CloneNewUnnamedSymbols) {
|
2021-04-07 11:16:01 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
Symbol old_a = builder.Symbols().New();
|
|
|
|
Symbol old_b = builder.Symbols().New();
|
|
|
|
Symbol old_c = builder.Symbols().New();
|
2021-04-13 20:07:57 +00:00
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_a), "tint_symbol");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_b), "tint_symbol_1");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_c), "tint_symbol_2");
|
2021-04-07 11:16:01 +00:00
|
|
|
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 10:29:54 +00:00
|
|
|
CloneContext ctx(&cloned, &original, false);
|
2021-04-07 11:16:01 +00:00
|
|
|
Symbol new_x = cloned.Symbols().New();
|
2021-04-13 20:07:57 +00:00
|
|
|
Symbol new_a = ctx.Clone(old_a);
|
2021-04-07 11:16:01 +00:00
|
|
|
Symbol new_y = cloned.Symbols().New();
|
2021-04-13 20:07:57 +00:00
|
|
|
Symbol new_b = ctx.Clone(old_b);
|
2021-04-07 11:16:01 +00:00
|
|
|
Symbol new_z = cloned.Symbols().New();
|
2021-04-13 20:07:57 +00:00
|
|
|
Symbol new_c = ctx.Clone(old_c);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_x), "tint_symbol");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_a), "tint_symbol_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_y), "tint_symbol_2");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_b), "tint_symbol_1_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_z), "tint_symbol_3");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_c), "tint_symbol_2_1");
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:06:31 +00:00
|
|
|
TEST_F(CloneContextTest, CloneNewSymbols) {
|
2021-04-13 20:07:57 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
Symbol old_a = builder.Symbols().New("a");
|
|
|
|
Symbol old_b = builder.Symbols().New("b");
|
|
|
|
Symbol old_c = builder.Symbols().New("c");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_a), "a");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_b), "b");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_c), "c");
|
|
|
|
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
2021-04-16 10:29:54 +00:00
|
|
|
CloneContext ctx(&cloned, &original, false);
|
2021-04-13 20:07:57 +00:00
|
|
|
Symbol new_x = cloned.Symbols().New("a");
|
|
|
|
Symbol new_a = ctx.Clone(old_a);
|
|
|
|
Symbol new_y = cloned.Symbols().New("b");
|
|
|
|
Symbol new_b = ctx.Clone(old_b);
|
|
|
|
Symbol new_z = cloned.Symbols().New("c");
|
|
|
|
Symbol new_c = ctx.Clone(old_c);
|
|
|
|
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_x), "a");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_a), "a_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_y), "b");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_b), "b_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_z), "c");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_c), "c_1");
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:06:31 +00:00
|
|
|
TEST_F(CloneContextTest, CloneNewSymbols_AfterCloneSymbols) {
|
2021-04-13 20:07:57 +00:00
|
|
|
ProgramBuilder builder;
|
|
|
|
Symbol old_a = builder.Symbols().New("a");
|
|
|
|
Symbol old_b = builder.Symbols().New("b");
|
|
|
|
Symbol old_c = builder.Symbols().New("c");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_a), "a");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_b), "b");
|
|
|
|
EXPECT_EQ(builder.Symbols().NameFor(old_c), "c");
|
|
|
|
|
|
|
|
Program original(std::move(builder));
|
|
|
|
|
|
|
|
ProgramBuilder cloned;
|
|
|
|
CloneContext ctx(&cloned, &original);
|
|
|
|
Symbol new_x = cloned.Symbols().New("a");
|
|
|
|
Symbol new_a = ctx.Clone(old_a);
|
|
|
|
Symbol new_y = cloned.Symbols().New("b");
|
|
|
|
Symbol new_b = ctx.Clone(old_b);
|
|
|
|
Symbol new_z = cloned.Symbols().New("c");
|
|
|
|
Symbol new_c = ctx.Clone(old_c);
|
2021-04-07 11:16:01 +00:00
|
|
|
|
2021-04-13 20:07:57 +00:00
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_x), "a_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_a), "a");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_y), "b_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_b), "b");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_z), "c_1");
|
|
|
|
EXPECT_EQ(cloned.Symbols().NameFor(new_c), "c");
|
2021-04-07 11:16:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 18:06:31 +00:00
|
|
|
TEST_F(CloneContextTest, ProgramIDs) {
|
2021-04-19 16:50:23 +00:00
|
|
|
ProgramBuilder dst;
|
|
|
|
Program src(ProgramBuilder{});
|
|
|
|
CloneContext ctx(&dst, &src);
|
|
|
|
Allocator allocator;
|
2021-05-04 18:06:31 +00:00
|
|
|
auto* cloned = ctx.Clone(allocator.Create<ProgramNode>(src.ID(), dst.ID()));
|
|
|
|
EXPECT_EQ(cloned->program_id, dst.ID());
|
2021-04-19 16:50:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:26:17 +00:00
|
|
|
TEST_F(CloneContextTest, ProgramIDs_Clone_ObjectNotOwnedBySrc) {
|
2021-04-19 16:50:23 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder dst;
|
|
|
|
Program src(ProgramBuilder{});
|
|
|
|
CloneContext ctx(&dst, &src);
|
|
|
|
Allocator allocator;
|
|
|
|
ctx.Clone(allocator.Create<ProgramNode>(ProgramID::New(), dst.ID()));
|
|
|
|
},
|
2021-06-24 11:27:36 +00:00
|
|
|
R"(internal compiler error: TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, a))");
|
2021-04-19 16:50:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:26:17 +00:00
|
|
|
TEST_F(CloneContextTest, ProgramIDs_Clone_ObjectNotOwnedByDst) {
|
2021-04-19 16:50:23 +00:00
|
|
|
EXPECT_FATAL_FAILURE(
|
|
|
|
{
|
|
|
|
ProgramBuilder dst;
|
|
|
|
Program src(ProgramBuilder{});
|
|
|
|
CloneContext ctx(&dst, &src);
|
|
|
|
Allocator allocator;
|
|
|
|
ctx.Clone(allocator.Create<ProgramNode>(src.ID(), ProgramID::New()));
|
|
|
|
},
|
2021-06-24 11:27:36 +00:00
|
|
|
R"(internal compiler error: TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, dst, out))");
|
2021-04-19 16:50:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 18:10:39 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-05-19 13:25:08 +00:00
|
|
|
TINT_INSTANTIATE_TYPEINFO(Node);
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(Replaceable);
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(Replacement);
|
2021-03-02 20:51:18 +00:00
|
|
|
TINT_INSTANTIATE_TYPEINFO(NotANode);
|
2021-04-19 16:50:23 +00:00
|
|
|
TINT_INSTANTIATE_TYPEINFO(ProgramNode);
|
2020-12-03 18:10:39 +00:00
|
|
|
|
|
|
|
} // namespace tint
|