| // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| // Exceptions. See /LICENSE for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| #include "lifetime_annotations/lifetime_substitutions.h" |
| |
| #include "gtest/gtest.h" |
| #include "lifetime_annotations/lifetime.h" |
| |
| namespace clang { |
| namespace tidy { |
| namespace lifetimes { |
| namespace { |
| |
| TEST(LifetimeSubstitutions, EmptySubstitution) { |
| LifetimeSubstitutions r; |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| |
| EXPECT_EQ(r.Substitute(l1), l1); |
| } |
| |
| TEST(LifetimeSubstitutions, SingleSubstitution) { |
| LifetimeSubstitutions s; |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| Lifetime l2 = Lifetime::CreateVariable(); |
| |
| s.Add(l1, l2); |
| |
| EXPECT_EQ(s.Substitute(l1), l2); |
| EXPECT_EQ(s.Substitute(l2), l2); |
| } |
| |
| TEST(LifetimeSubstitutions, Constants) { |
| LifetimeSubstitutions s; |
| EXPECT_EQ(s.Substitute(Lifetime::Static()), Lifetime::Static()); |
| Lifetime local = Lifetime::CreateLocal(); |
| EXPECT_EQ(s.Substitute(local), local); |
| } |
| |
| TEST(LifetimeSubstitutions, SelfSubstitution) { |
| LifetimeSubstitutions s; |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| |
| s.Add(l1, l1); |
| |
| EXPECT_EQ(s.Substitute(l1), l1); |
| } |
| |
| TEST(LifetimeSubstitutions, Cycle) { |
| LifetimeSubstitutions s; |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| Lifetime l2 = Lifetime::CreateVariable(); |
| |
| s.Add(l1, l2); |
| s.Add(l2, l1); |
| |
| EXPECT_EQ(s.Substitute(l1), l2); |
| EXPECT_EQ(s.Substitute(l2), l2); |
| } |
| |
| TEST(LifetimeSubstitutions, LargerCycle) { |
| LifetimeSubstitutions s; |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| Lifetime l2 = Lifetime::CreateVariable(); |
| Lifetime l3 = Lifetime::CreateVariable(); |
| |
| s.Add(l1, l2); |
| s.Add(l2, l3); |
| s.Add(l3, l1); |
| |
| EXPECT_EQ(s.Substitute(l1), l3); |
| EXPECT_EQ(s.Substitute(l2), l3); |
| EXPECT_EQ(s.Substitute(l3), l3); |
| } |
| |
| TEST(LifetimeSubstitutions, SubstituteIsTransitive) { |
| // Check that Substitute() performs transitive substitutions by adding |
| // substitutions in an order that will prevent Add() from preemptively |
| // performing all of those transitive substitutions. |
| |
| Lifetime l1 = Lifetime::CreateVariable(); |
| Lifetime l2 = Lifetime::CreateVariable(); |
| Lifetime l3 = Lifetime::CreateVariable(); |
| Lifetime l4 = Lifetime::CreateVariable(); |
| |
| LifetimeSubstitutions s; |
| s.Add(l1, l2); |
| s.Add(l3, l4); |
| s.Add(l2, l3); |
| |
| EXPECT_EQ(s.Substitute(l1), l4); |
| } |
| |
| } // namespace |
| } // namespace lifetimes |
| } // namespace tidy |
| } // namespace clang |