flags.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <gtest/gtest.h>
00014 #include "../../include/ecl/utilities/flags.hpp"
00015 
00020 #define FLAGS_DEBUG
00021 
00022 /*****************************************************************************
00023 ** Namespaces
00024 *****************************************************************************/
00025 
00026 namespace ecl {
00027 namespace utilities {
00028 namespace tests {
00029 
00030 /*****************************************************************************
00031 ** Enum
00032 *****************************************************************************/
00033 
00034 enum Setting
00035 {
00036     Nada = 0x00, // the empty flag.
00037     Fast = 0x01, // Group speed
00038     Slow = 0x02,
00039     Bright = 0x10, // Group colour
00040     Dark = 0x20,
00041     Red = 0x40,
00042     Gold = 0x80,
00043     FastGold = 0x81, // Combinations (convenient setting and checking (w/ testFlag()))
00044 };
00045 
00046 /*****************************************************************************
00047 ** Helper Piper
00048 *****************************************************************************/
00052 Flags<Setting> operator|(Setting flagOne, Setting flagTwo) { return Flags<Setting>(flagOne) | flagTwo; }
00053 
00054 enum Property
00055 {
00056     Fixed = 0x01,
00057     Dynamic = 0x02
00058 };
00059 
00064 typedef Flags<Setting> Settings;
00065 
00066 /*****************************************************************************
00067 ** Illegal combination handler
00068 *****************************************************************************/
00072 class A
00073 {
00074 public:
00075     void configure(Settings s)
00076     {
00077         if ( ( s.testFlag(Gold) && s_.testFlag(Dark) ) ||
00078              ( s_.testFlag(Gold) && s.testFlag(Dark) ) ||
00079              ( s.testFlag(Gold) && s.testFlag(Dark) ) )
00080         {
00081             #ifdef FLAGS_DEBUG
00082               std::cout << "This combination is not permitted." << std::endl;
00083             #endif
00084             return;
00085         } else {
00086             s_ = s;
00087         }
00088     }
00089     Settings settings() { return s_; }
00090 
00091 private:
00092     Settings s_;
00093 };
00094 
00095 }}} // namespaces
00096 
00097 /*****************************************************************************
00098 ** Using
00099 *****************************************************************************/
00100 
00101 using namespace ecl::utilities::tests;
00102 
00103 /*****************************************************************************
00104 ** Tests
00105 *****************************************************************************/
00106 
00107 TEST(Flags, initialisation) {
00108 
00109   Settings settings;
00110   settings = Fast | Red;
00111   // settings = Fixed; // <-- Illegal call, incompatible enum used.
00112 #ifdef FLAGS_DEBUG
00113   std::cout << "Fast & Red: " << settings << std::endl;
00114 #endif
00115   EXPECT_EQ(Fast|Red,settings);
00116 }
00117 
00118 TEST(Flags, Operators) {
00119   Settings settings;
00120   settings = Fast | Red;
00121   settings &= Fast; // <-- Mask out everything but Fast
00122 #ifdef FLAGS_DEBUG
00123   std::cout << "Fast: " << settings << std::endl;
00124 #endif
00125 
00126   settings |= Gold; // <-- Add gold
00127 #ifdef FLAGS_DEBUG
00128   std::cout << "Fast & Gold: " << settings << std::endl;
00129 #endif
00130   EXPECT_EQ(FastGold,settings);
00131 }
00132 
00133 TEST(Flags, tests) {
00134   Settings settings;
00135   settings = Fast | Gold;
00136   EXPECT_EQ(settings.testFlag(Gold),true);
00137   EXPECT_EQ(settings.testFlag(Red),false);
00138   EXPECT_EQ(settings.testFlag(FastGold),true);
00139   settings.clear();
00140   EXPECT_EQ(settings.testFlag(Nada),true);
00141 
00142 #ifdef FLAGS_DEBUG
00143   if ( settings.testFlag(Gold) ) {
00144       std::cout << "Gold Set" << std::endl;
00145   } else {
00146       std::cout << "Gold Not Set" << std::endl;
00147   }
00148   if ( settings.testFlag(Red) ) {
00149       std::cout << "Red Set" << std::endl;
00150   } else {
00151       std::cout << "Red Not Set" << std::endl;
00152   }
00153   if ( settings.testFlag(FastGold) ) {
00154       std::cout << "Fast & Gold Set" << std::endl;
00155   } else {
00156       std::cout << "Fast & Gold Not Set" << std::endl;
00157   }
00158 #endif
00159 }
00160 TEST(Flags, combinations) {
00161     A a;
00162     a.configure(Gold|Dark); // fails to configure
00163     EXPECT_EQ(0,a.settings());
00164     a.configure(Gold);
00165     EXPECT_EQ(Gold,a.settings());
00166 }
00167 
00168 /*****************************************************************************
00169 ** Main program
00170 *****************************************************************************/
00171 
00172 int main(int argc, char **argv) {
00173         testing::InitGoogleTest(&argc,argv);
00174         return RUN_ALL_TESTS();
00175 }


ecl_utilities
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:12:20