CacheTests.cpp
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <NovaDAQUtilities/CacheManager.h>
3 #include <NovaDAQUtilities/EnvVarCache.h>
4 #include <boost/lexical_cast.hpp>
5 #include <stdlib.h>
6 
7 using namespace novadaq;
8 
9 class CacheTests : public CppUnit::TestFixture
10 {
11  CPPUNIT_TEST_SUITE(CacheTests);
12  CPPUNIT_TEST(testEnvVarCacheBasic);
13  CPPUNIT_TEST(testEnvVarCacheNumbers);
14  CPPUNIT_TEST_SUITE_END();
15 
16 public:
17  void setUp();
18  void tearDown();
19 
20  void testEnvVarCacheBasic();
21  void testEnvVarCacheNumbers();
22 
23 private:
24 
25 };
26 
27 void
29 {
30 }
31 
32 void
34 {
35 }
36 
37 void
39 {
40  std::string sampleName("PHONY_BALONEY_NAME");
41  std::string sampleValue1("Fred Flintstone");
42  std::string sampleValue2("Barney Rubble");
43  std::string currentValue;
45 
46  // verify that there is initially no env var with our sample name
47  currentValue = theCache.getEnvVar(sampleName);
48  CPPUNIT_ASSERT(currentValue == "");
49 
50  // create an env var with our sample name and value
51  CPPUNIT_ASSERT(setenv(sampleName.c_str(), sampleValue1.c_str(), 1) == 0);
52 
53  // verify that we still get an empty string from the cache
54  // (because we haven't cleared the cache)
55  currentValue = theCache.getEnvVar(sampleName);
56  CPPUNIT_ASSERT(currentValue == "");
57 
58  // clear the cache and see if we get the new value from a new request
59  theCache.flush();
60  currentValue = theCache.getEnvVar(sampleName);
61  CPPUNIT_ASSERT(currentValue != "");
62  CPPUNIT_ASSERT(currentValue == sampleValue1);
63 
64  // asking a second time shouldn't change anything
65  currentValue = theCache.getEnvVar(sampleName);
66  CPPUNIT_ASSERT(currentValue != "");
67  CPPUNIT_ASSERT(currentValue == sampleValue1);
68 
69  // verify that clearing the cache through the cache manager also works
70  CPPUNIT_ASSERT(setenv(sampleName.c_str(), sampleValue2.c_str(), 1) == 0);
72  currentValue = theCache.getEnvVar(sampleName);
73  CPPUNIT_ASSERT(currentValue != "");
74  CPPUNIT_ASSERT(currentValue == sampleValue2);
75 
76  // remove the env var from the environment and verify that it still
77  // remains in the cache
78  CPPUNIT_ASSERT(unsetenv(sampleName.c_str()) == 0);
79  currentValue = theCache.getEnvVar(sampleName);
80  CPPUNIT_ASSERT(currentValue == sampleValue2);
81 
82  // clear the cache for later tests
83  theCache.flush();
84 }
85 
86 void
88 {
89  std::string sampleName("PHONY_NUMBERIC_ENVVAR");
90  long sampleLong = 1492;
91  double sampleDouble = 3.141592;
92  std::string longString = boost::lexical_cast<std::string>(sampleLong);
93  std::string doubleString = boost::lexical_cast<std::string>(sampleDouble);
94  long defaultLong = -99;
95  double defaultDouble = -123.0;
96  std::string currentValue;
97  long longValue;
98  double doubleValue;
100 
101  // verify that there is initially no env var with our sample name
102  currentValue = theCache.getEnvVar(sampleName);
103  CPPUNIT_ASSERT(currentValue == "");
104  longValue = theCache.getEnvVarAsLong(sampleName, defaultLong);
105  CPPUNIT_ASSERT(longValue == defaultLong);
106  doubleValue = theCache.getEnvVarAsDouble(sampleName, defaultDouble);
107  CPPUNIT_ASSERT(doubleValue == defaultDouble);
108 
109  // long value tests
110  CPPUNIT_ASSERT(setenv(sampleName.c_str(), longString.c_str(), 1) == 0);
111  theCache.flush();
112  currentValue = theCache.getEnvVar(sampleName);
113  CPPUNIT_ASSERT(currentValue != "");
114  longValue = theCache.getEnvVarAsLong(sampleName, defaultLong);
115  CPPUNIT_ASSERT(longValue == sampleLong);
116 
117  // double value tests
118  CPPUNIT_ASSERT(setenv(sampleName.c_str(), doubleString.c_str(), 1) == 0);
119  theCache.flush();
120  currentValue = theCache.getEnvVar(sampleName);
121  CPPUNIT_ASSERT(currentValue != "");
122  doubleValue = theCache.getEnvVarAsDouble(sampleName, defaultDouble);
123  CPPUNIT_ASSERT(doubleValue == sampleDouble);
124 
125  // cleanup
126  CPPUNIT_ASSERT(unsetenv(sampleName.c_str()) == 0);
127  theCache.flush();
128 }
129 
131 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities");
void flush()
Definition: Cache.cpp:8
static CacheManager & getInstance()
void tearDown()
Definition: CacheTests.cpp:33
static EnvVarCache & getInstance()
Definition: EnvVarCache.cpp:12
int getEnvVarAsLong(std::string name, long defaultValue)
Definition: EnvVarCache.cpp:48
void testEnvVarCacheNumbers()
Definition: CacheTests.cpp:87
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(CacheTests,"NovaDAQUtilities")
double getEnvVarAsDouble(std::string name, double defaultValue)
Definition: EnvVarCache.cpp:70
void setUp()
Definition: CacheTests.cpp:28
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities")
std::string getEnvVar(std::string name)
Definition: EnvVarCache.cpp:33
void testEnvVarCacheBasic()
Definition: CacheTests.cpp:38
enum BeamMode string