Jlm
MatchTypeTests.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Helge Bahmann <hcb@chaoticmind.net>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #include <cassert>
7 
8 #include <gtest/gtest.h>
9 
10 #include <jlm/rvsdg/MatchType.hpp>
11 
12 namespace
13 {
14 
15 class Base
16 {
17 public:
18  virtual ~Base() = default;
19 };
20 
21 class X final : public Base
22 {
23 public:
24  ~X() override = default;
25 };
26 
27 class Y final : public Base
28 {
29 public:
30  ~Y() override = default;
31 };
32 
33 class Z final : public Base
34 {
35 public:
36  ~Z() override = default;
37 };
38 
39 class U final : public Base
40 {
41 public:
42  ~U() override = default;
43 };
44 
45 int
46 Discriminate1(const Base & obj)
47 {
48  int result = -1;
50  obj,
51  [&result](const X &)
52  {
53  result = 0;
54  },
55  [&result](const Y &)
56  {
57  result = 1;
58  },
59  [&result](const Z &)
60  {
61  result = 2;
62  });
63  return result;
64 }
65 
66 int
67 Discriminate2(const Base & obj)
68 {
69  int result = 42;
71  obj,
72  [&result](const X &)
73  {
74  result = 0;
75  },
76  [&result](const Y &)
77  {
78  result = 1;
79  },
80  [&result](const Z &)
81  {
82  result = 2;
83  },
84  [&result]()
85  {
86  result = -1;
87  });
88  return result;
89 }
90 
91 int
92 Discriminate3(const Base & obj)
93 {
95  obj,
96  [](const X &)
97  {
98  return 0;
99  },
100  [](const Y &)
101  {
102  return 1;
103  },
104  [](const Z &)
105  {
106  return 2;
107  },
108  []()
109  {
110  return -1;
111  });
112 }
113 
114 int
115 Discriminate4(const Base & obj)
116 {
118  obj,
119  [](const X &)
120  {
121  return 0;
122  },
123  [](const Y &)
124  {
125  return 1;
126  },
127  [](const Z &)
128  {
129  return 2;
130  });
131 }
132 
133 int
134 HandleX(const X &)
135 {
136  return 0;
137 }
138 
139 int
140 HandleY(const Y &)
141 {
142  return 1;
143 }
144 
145 int
146 Default()
147 {
148  return 2;
149 }
150 
151 int
152 Discriminate5(const Base & obj)
153 {
154  return jlm::rvsdg::MatchTypeWithDefault(obj, HandleX, HandleY, Default);
155 }
156 
157 }
158 
159 TEST(MatchTypeTests, TestBasicTypeMatch)
160 {
161  EXPECT_EQ(Discriminate1(X()), 0);
162  EXPECT_EQ(Discriminate1(Y()), 1);
163  EXPECT_EQ(Discriminate1(Z()), 2);
164  EXPECT_EQ(Discriminate1(U()), -1);
165 
166  EXPECT_EQ(Discriminate2(X()), 0);
167  EXPECT_EQ(Discriminate2(Y()), 1);
168  EXPECT_EQ(Discriminate2(Z()), 2);
169  EXPECT_EQ(Discriminate2(U()), -1);
170 
171  EXPECT_EQ(Discriminate3(X()), 0);
172  EXPECT_EQ(Discriminate3(Y()), 1);
173  EXPECT_EQ(Discriminate3(Z()), 2);
174  EXPECT_EQ(Discriminate3(U()), -1);
175 
176  EXPECT_EQ(Discriminate4(X()), 0);
177  EXPECT_EQ(Discriminate4(Y()), 1);
178  EXPECT_EQ(Discriminate4(Z()), 2);
179  EXPECT_THROW(Discriminate4(U()), std::logic_error);
180 
181  EXPECT_EQ(Discriminate5(X()), 0);
182  EXPECT_EQ(Discriminate5(Y()), 1);
183  EXPECT_EQ(Discriminate5(Z()), 2);
184 }
TEST(MatchTypeTests, TestBasicTypeMatch)
void MatchTypeWithDefault(T &obj, const Fns &... fns)
Pattern match over subclass type of given object with default handler.
void MatchTypeOrFail(T &obj, const Fns &... fns)
Pattern match over subclass type of given object.
void MatchType(T &obj, const Fns &... fns)
Pattern match over subclass type of given object.