blob: cc664f5a0e243dbf259ec9e0f9b6f911ecf349b9 [file] [log] [blame]
Luca Versari99fddff2022-05-25 10:22:32 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#include "lifetime_analysis/object_set.h"
6
7#include "gmock/gmock.h"
8#include "gtest/gtest.h"
9#include "lifetime_analysis/object.h"
10#include "lifetime_annotations/lifetime.h"
11#include "lifetime_annotations/lifetime_annotations.h"
12#include "lifetime_annotations/test/run_on_code.h"
13#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
14
15namespace clang {
16namespace tidy {
17namespace lifetimes {
18namespace {
19
20using testing::UnorderedElementsAre;
21
22TEST(ObjectSet, AccessObjects) {
23 runOnCodeWithLifetimeHandlers(
24 "",
25 [](const clang::ASTContext& ast_context,
26 const LifetimeAnnotationContext&) {
Luca Versaria12c9d52023-02-23 08:40:48 -080027 Object object_static(Lifetime::Static(), ast_context.IntTy,
28 std::nullopt);
Martin Brænne65bae0a2022-07-01 06:08:42 -070029 ObjectSet object_set = {&object_static};
Luca Versari99fddff2022-05-25 10:22:32 -070030
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070031 EXPECT_THAT(object_set, UnorderedElementsAre(&object_static));
Luca Versari99fddff2022-05-25 10:22:32 -070032 },
33 {});
34}
35
36TEST(ObjectSet, Contains) {
37 runOnCodeWithLifetimeHandlers(
38 "",
39 [](const clang::ASTContext& ast_context,
40 const LifetimeAnnotationContext&) {
Luca Versaria12c9d52023-02-23 08:40:48 -080041 Object o1(Lifetime::CreateLocal(), ast_context.IntTy, std::nullopt);
42 Object o2(Lifetime::CreateLocal(), ast_context.IntTy, std::nullopt);
Luca Versari99fddff2022-05-25 10:22:32 -070043
Martin Brænne65bae0a2022-07-01 06:08:42 -070044 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains(&o1));
45 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains(&o2));
46 EXPECT_FALSE(ObjectSet({&o1}).Contains(&o2));
Luca Versari99fddff2022-05-25 10:22:32 -070047
Martin Brænne65bae0a2022-07-01 06:08:42 -070048 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains(ObjectSet()));
49 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains(ObjectSet{&o1}));
50 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains(ObjectSet{&o2}));
51 EXPECT_TRUE(ObjectSet({&o1, &o2}).Contains({&o1, &o2}));
52 EXPECT_TRUE(ObjectSet({&o1}).Contains(ObjectSet{&o1}));
53 EXPECT_FALSE(ObjectSet({&o1}).Contains(ObjectSet{&o2}));
Luca Versari99fddff2022-05-25 10:22:32 -070054 EXPECT_TRUE(ObjectSet().Contains(ObjectSet()));
55 },
56 {});
57}
58
59TEST(ObjectSet, Union) {
60 runOnCodeWithLifetimeHandlers(
61 "",
62 [](const clang::ASTContext& ast_context,
63 const LifetimeAnnotationContext&) {
Luca Versaria12c9d52023-02-23 08:40:48 -080064 Object object_static(Lifetime::Static(), ast_context.IntTy,
65 std::nullopt);
Martin Brænne65bae0a2022-07-01 06:08:42 -070066 ObjectSet set_1 = {&object_static};
Luca Versaria12c9d52023-02-23 08:40:48 -080067 Object object_local(Lifetime::CreateLocal(), ast_context.IntTy,
68 std::nullopt);
Martin Brænne65bae0a2022-07-01 06:08:42 -070069 ObjectSet set_2 = {&object_local};
Luca Versari99fddff2022-05-25 10:22:32 -070070
71 ObjectSet set_union = set_1.Union(set_2);
72
73 EXPECT_THAT(set_union,
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070074 UnorderedElementsAre(&object_static, &object_local));
Luca Versari99fddff2022-05-25 10:22:32 -070075 },
76 {});
77}
78
79TEST(ObjectSet, Add) {
80 runOnCodeWithLifetimeHandlers(
81 "",
82 [](const clang::ASTContext& ast_context,
83 const LifetimeAnnotationContext&) {
Luca Versaria12c9d52023-02-23 08:40:48 -080084 Object o1(Lifetime::CreateLocal(), ast_context.IntTy, std::nullopt);
85 Object o2(Lifetime::CreateLocal(), ast_context.IntTy, std::nullopt);
86 Object o3(Lifetime::CreateLocal(), ast_context.IntTy, std::nullopt);
Luca Versari99fddff2022-05-25 10:22:32 -070087
88 {
Martin Brænne65bae0a2022-07-01 06:08:42 -070089 ObjectSet object_set = {&o1};
90 object_set.Add(&o2);
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070091 EXPECT_THAT(object_set, UnorderedElementsAre(&o1, &o2));
Luca Versari99fddff2022-05-25 10:22:32 -070092 }
93 {
Martin Brænne65bae0a2022-07-01 06:08:42 -070094 ObjectSet object_set = {&o1, &o2};
95 object_set.Add(&o2);
Martin Brænne4d8cdfd2022-07-01 06:16:36 -070096 EXPECT_THAT(object_set, UnorderedElementsAre(&o1, &o2));
Luca Versari99fddff2022-05-25 10:22:32 -070097 }
98 {
Martin Brænne65bae0a2022-07-01 06:08:42 -070099 ObjectSet object_set = {&o1};
100 object_set.Add({&o2, &o3});
Martin Brænne4d8cdfd2022-07-01 06:16:36 -0700101 EXPECT_THAT(object_set, UnorderedElementsAre(&o1, &o2, &o3));
Luca Versari99fddff2022-05-25 10:22:32 -0700102 }
103 {
Martin Brænne65bae0a2022-07-01 06:08:42 -0700104 ObjectSet object_set = {&o1, &o2};
105 object_set.Add({&o2, &o3});
Martin Brænne4d8cdfd2022-07-01 06:16:36 -0700106 EXPECT_THAT(object_set, UnorderedElementsAre(&o1, &o2, &o3));
Luca Versari99fddff2022-05-25 10:22:32 -0700107 }
108 },
109 {});
110}
111
112TEST(ObjectSet, Equality) {
113 runOnCodeWithLifetimeHandlers(
114 "",
115 [](const clang::ASTContext& ast_context,
116 const LifetimeAnnotationContext&) {
Luca Versaria12c9d52023-02-23 08:40:48 -0800117 Object object_static(Lifetime::Static(), ast_context.IntTy,
118 std::nullopt);
119 Object object_local(Lifetime::CreateLocal(), ast_context.IntTy,
120 std::nullopt);
Martin Brænne65bae0a2022-07-01 06:08:42 -0700121 ObjectSet set_1 = {&object_static};
122 ObjectSet set_2 = {&object_static};
123 ObjectSet set_3 = {&object_static, &object_local};
Luca Versari99fddff2022-05-25 10:22:32 -0700124
125 EXPECT_EQ(set_1, set_2);
126 EXPECT_NE(set_1, set_3);
127 },
128 {});
129}
130
131} // namespace
132} // namespace lifetimes
133} // namespace tidy
134} // namespace clang