• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/util/match.h"
16 
17 #include <stdlib.h>
18 
19 #include <memory>
20 
21 #include "gtest/gtest.h"
22 
23 namespace grpc_core {
24 namespace testing {
25 
TEST(MatchTest,Test)26 TEST(MatchTest, Test) {
27   EXPECT_EQ(Match(
28                 absl::variant<int, double>(1.9), [](int) -> int { abort(); },
29                 [](double x) -> int {
30                   EXPECT_EQ(x, 1.9);
31                   return 42;
32                 }),
33             42);
34   EXPECT_EQ(Match(
35                 absl::variant<int, double>(3),
36                 [](int x) -> int {
37                   EXPECT_EQ(x, 3);
38                   return 42;
39                 },
40                 [](double) -> int { abort(); }),
41             42);
42 }
43 
TEST(MatchTest,TestVoidReturn)44 TEST(MatchTest, TestVoidReturn) {
45   bool triggered = false;
46   Match(
47       absl::variant<int, double>(1.9), [](int) { abort(); },
48       [&triggered](double x) {
49         EXPECT_EQ(x, 1.9);
50         triggered = true;
51       });
52   EXPECT_TRUE(triggered);
53 }
54 
TEST(MatchTest,TestMutable)55 TEST(MatchTest, TestMutable) {
56   absl::variant<int, double> v = 1.9;
57   MatchMutable(&v, [](int*) { abort(); }, [](double* x) { *x = 0.0; });
58   EXPECT_EQ(v, (absl::variant<int, double>(0.0)));
59 }
60 
TEST(MatchTest,TestMutableWithReturn)61 TEST(MatchTest, TestMutableWithReturn) {
62   absl::variant<int, double> v = 1.9;
63   EXPECT_EQ(MatchMutable(
64                 &v, [](int*) -> int { abort(); },
65                 [](double* x) -> int {
66                   *x = 0.0;
67                   return 1;
68                 }),
69             1);
70   EXPECT_EQ(v, (absl::variant<int, double>(0.0)));
71 }
72 
73 }  // namespace testing
74 }  // namespace grpc_core
75 
main(int argc,char ** argv)76 int main(int argc, char** argv) {
77   ::testing::InitGoogleTest(&argc, argv);
78   return RUN_ALL_TESTS();
79 }
80