Ensure curly-braces around control-flow
[folly.git] / folly / futures / test / FSMTest.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/futures/detail/FSM.h>
18 #include <folly/portability/GTest.h>
19
20 using namespace folly::futures::detail;
21
22 enum class State { A, B };
23
24 TEST(FSM, example) {
25   FSM<State> fsm(State::A);
26   int count = 0;
27   int unprotectedCount = 0;
28
29   // somebody set up us the switch
30   auto tryTransition = [&]{
31     switch (fsm.getState()) {
32     case State::A:
33       return fsm.updateState(State::A, State::B, [&]{ count++; });
34     case State::B:
35       return fsm.updateState(State::B, State::A,
36                              [&]{ count--; }, [&]{ unprotectedCount--; });
37     }
38     return false; // unreachable
39   };
40
41   // keep retrying until success (like a cas)
42   while (!tryTransition()) {
43     ;
44   }
45   EXPECT_EQ(State::B, fsm.getState());
46   EXPECT_EQ(1, count);
47   EXPECT_EQ(0, unprotectedCount);
48
49   while (!tryTransition()) {
50     ;
51   }
52   EXPECT_EQ(State::A, fsm.getState());
53   EXPECT_EQ(0, count);
54   EXPECT_EQ(-1, unprotectedCount);
55 }
56
57 TEST(FSM, magicMacrosExample) {
58   struct MyFSM {
59     FSM<State> fsm_;
60     int count = 0;
61     int unprotectedCount = 0;
62     MyFSM() : fsm_(State::A) {}
63     void twiddle() {
64       FSM_START(fsm_)
65         FSM_CASE(fsm_, State::A, State::B, [&]{ count++; });
66         FSM_CASE2(fsm_, State::B, State::A,
67                   [&]{ count--; }, [&]{ unprotectedCount--; });
68       FSM_END
69     }
70   };
71
72   MyFSM fsm;
73
74   fsm.twiddle();
75   EXPECT_EQ(State::B, fsm.fsm_.getState());
76   EXPECT_EQ(1, fsm.count);
77   EXPECT_EQ(0, fsm.unprotectedCount);
78
79   fsm.twiddle();
80   EXPECT_EQ(State::A, fsm.fsm_.getState());
81   EXPECT_EQ(0, fsm.count);
82   EXPECT_EQ(-1, fsm.unprotectedCount);
83 }
84
85
86 TEST(FSM, ctor) {
87   FSM<State> fsm(State::A);
88   EXPECT_EQ(State::A, fsm.getState());
89 }
90
91 TEST(FSM, update) {
92   FSM<State> fsm(State::A);
93   EXPECT_TRUE(fsm.updateState(State::A, State::B, []{}));
94   EXPECT_EQ(State::B, fsm.getState());
95 }
96
97 TEST(FSM, badUpdate) {
98   FSM<State> fsm(State::A);
99   EXPECT_FALSE(fsm.updateState(State::B, State::A, []{}));
100 }
101
102 TEST(FSM, actionOnUpdate) {
103   FSM<State> fsm(State::A);
104   int count = 0;
105   fsm.updateState(State::A, State::B, [&]{ count++; });
106   EXPECT_EQ(1, count);
107 }
108
109 TEST(FSM, noActionOnBadUpdate) {
110   FSM<State> fsm(State::A);
111   int count = 0;
112   fsm.updateState(State::B, State::A, [&]{ count++; });
113   EXPECT_EQ(0, count);
114 }
115
116 TEST(FSM, stateTransitionAfterAction) {
117   FSM<State> fsm(State::A);
118   fsm.updateState(State::A, State::B,
119                   [&]{ EXPECT_EQ(State::A, fsm.getState()); });
120 }