flags.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <gtest/gtest.h>
14 #include "../../include/ecl/utilities/flags.hpp"
15 
20 #define FLAGS_DEBUG
21 
22 /*****************************************************************************
23 ** Namespaces
24 *****************************************************************************/
25 
26 namespace ecl {
27 namespace utilities {
28 namespace tests {
29 
30 /*****************************************************************************
31 ** Enum
32 *****************************************************************************/
33 
34 enum Setting
35 {
36  Nada = 0x00, // the empty flag.
37  Fast = 0x01, // Group speed
38  Slow = 0x02,
39  Bright = 0x10, // Group colour
40  Dark = 0x20,
41  Red = 0x40,
42  Gold = 0x80,
43  FastGold = 0x81, // Combinations (convenient setting and checking (w/ testFlag()))
44 };
45 
46 /*****************************************************************************
47 ** Helper Piper
48 *****************************************************************************/
52 Flags<Setting> operator|(Setting flagOne, Setting flagTwo) { return Flags<Setting>(flagOne) | flagTwo; }
53 
54 enum Property
55 {
56  Fixed = 0x01,
57  Dynamic = 0x02
58 };
59 
64 typedef Flags<Setting> Settings;
65 
66 /*****************************************************************************
67 ** Illegal combination handler
68 *****************************************************************************/
72 class A
73 {
74 public:
75  void configure(Settings s)
76  {
77  if ( ( s.testFlag(Gold) && s_.testFlag(Dark) ) ||
78  ( s_.testFlag(Gold) && s.testFlag(Dark) ) ||
79  ( s.testFlag(Gold) && s.testFlag(Dark) ) )
80  {
81  #ifdef FLAGS_DEBUG
82  std::cout << "This combination is not permitted." << std::endl;
83  #endif
84  return;
85  } else {
86  s_ = s;
87  }
88  }
89  Settings settings() { return s_; }
90 
91 private:
92  Settings s_;
93 };
94 
95 }}} // namespaces
96 
97 /*****************************************************************************
98 ** Using
99 *****************************************************************************/
100 
101 using namespace ecl::utilities::tests;
102 
103 /*****************************************************************************
104 ** Tests
105 *****************************************************************************/
106 
107 TEST(Flags, initialisation) {
108 
109  Settings settings;
110  settings = Fast | Red;
111  // settings = Fixed; // <-- Illegal call, incompatible enum used.
112 #ifdef FLAGS_DEBUG
113  std::cout << "Fast & Red: " << settings << std::endl;
114 #endif
115  EXPECT_EQ(Fast|Red,settings);
116 }
117 
118 TEST(Flags, Operators) {
119  Settings settings;
120  settings = Fast | Red;
121  settings &= Fast; // <-- Mask out everything but Fast
122 #ifdef FLAGS_DEBUG
123  std::cout << "Fast: " << settings << std::endl;
124 #endif
125 
126  settings |= Gold; // <-- Add gold
127 #ifdef FLAGS_DEBUG
128  std::cout << "Fast & Gold: " << settings << std::endl;
129 #endif
130  EXPECT_EQ(FastGold,settings);
131 }
132 
133 TEST(Flags, tests) {
134  Settings settings;
135  settings = Fast | Gold;
136  EXPECT_EQ(settings.testFlag(Gold),true);
137  EXPECT_EQ(settings.testFlag(Red),false);
138  EXPECT_EQ(settings.testFlag(FastGold),true);
139  settings.clear();
140  EXPECT_EQ(settings.testFlag(Nada),true);
141 
142 #ifdef FLAGS_DEBUG
143  if ( settings.testFlag(Gold) ) {
144  std::cout << "Gold Set" << std::endl;
145  } else {
146  std::cout << "Gold Not Set" << std::endl;
147  }
148  if ( settings.testFlag(Red) ) {
149  std::cout << "Red Set" << std::endl;
150  } else {
151  std::cout << "Red Not Set" << std::endl;
152  }
153  if ( settings.testFlag(FastGold) ) {
154  std::cout << "Fast & Gold Set" << std::endl;
155  } else {
156  std::cout << "Fast & Gold Not Set" << std::endl;
157  }
158 #endif
159 }
160 TEST(Flags, combinations) {
161  A a;
162  a.configure(Gold|Dark); // fails to configure
163  EXPECT_EQ(0,a.settings());
164  a.configure(Gold);
165  EXPECT_EQ(Gold,a.settings());
166 }
167 
168 /*****************************************************************************
169 ** Main program
170 *****************************************************************************/
171 
172 int main(int argc, char **argv) {
173  testing::InitGoogleTest(&argc,argv);
174  return RUN_ALL_TESTS();
175 }
Embedded control libraries.
TEST(TypeTests, fundamentals)
int main(int argc, char **argv)


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Mon Feb 28 2022 22:18:41