HexUtilTests.cpp
Go to the documentation of this file.
1 #include <cppunit/extensions/HelperMacros.h>
2 #include <NovaDAQUtilities/HexUtils.h>
3 
4 using namespace novadaq;
5 
6 class HexUtilTests : public CppUnit::TestFixture
7 {
8  CPPUNIT_TEST_SUITE(HexUtilTests);
9  CPPUNIT_TEST(testFormat);
10  CPPUNIT_TEST(testValidity);
11  CPPUNIT_TEST(testParse);
12  CPPUNIT_TEST_SUITE_END();
13 
14 public:
15  void setUp();
16  void tearDown();
17 
18  void testFormat();
19  void testValidity();
20  void testParse();
21 
22 private:
23 
24 };
25 
26 void
28 {
29  // flush stdout to get progress markers to display in a timely way
30  std::cout.flush();
31 }
32 
33 void
35 {
36  // flush stdout to get progress markers to display in a timely way
37  std::cout.flush();
38 }
39 
40 void
42 {
43  int value;
44  //int32_t value32;
45  int64_t value64;
46  std::string hexString;
47  std::string decimalString;
48 
49  value = 1;
50  hexString = HexUtils::format(value);
51  CPPUNIT_ASSERT(hexString == "0x00000001");
52 
53  value = -1;
54  hexString = HexUtils::format(value);
55  CPPUNIT_ASSERT(hexString == "0xFFFFFFFF");
56 
57  value = 0x1234;
58  hexString = HexUtils::format(value, 5);
59  CPPUNIT_ASSERT(hexString == "0x01234");
60 
61  value64 = -1;
62  hexString = HexUtils::format(value64);
63  CPPUNIT_ASSERT(hexString == "0xFFFFFFFFFFFFFFFF");
64 
65  decimalString = "100";
66  hexString = HexUtils::format(decimalString, 2);
67  CPPUNIT_ASSERT(hexString == "0x64");
68 
69  decimalString = "100";
70  hexString = HexUtils::format(decimalString);
71  CPPUNIT_ASSERT(hexString == "0x00000064");
72 
73  decimalString = "-1";
74  hexString = HexUtils::format(decimalString);
75  CPPUNIT_ASSERT(hexString == "0xFFFFFFFF");
76 
77  decimalString = "-1";
78  hexString = HexUtils::format(decimalString, 16);
79  CPPUNIT_ASSERT(hexString == "0xFFFFFFFFFFFFFFFF");
80 
81  decimalString = "abc";
82  try {
83  hexString = HexUtils::format(decimalString);
84  CPPUNIT_ASSERT(0);
85  }
86  catch (...) {}
87 
88  decimalString = "99999";
89  hexString = HexUtils::format(decimalString, 2);
90  CPPUNIT_ASSERT(hexString.length() > 4);
91 
92  decimalString = "9999999999";
93  try {
94  hexString = HexUtils::format(decimalString, 2);
95  CPPUNIT_ASSERT(0);
96  }
97  catch (...) {}
98 }
99 
100 void
102 {
103  CPPUNIT_ASSERT(! HexUtils::isValidHex("0"));
104  CPPUNIT_ASSERT(! HexUtils::isValidHex("1"));
105 
106  CPPUNIT_ASSERT(HexUtils::isValidHex(" 0x01 "));
107  CPPUNIT_ASSERT(HexUtils::isValidHex("0x80000000"));
108  CPPUNIT_ASSERT(HexUtils::isValidHex("0xffffffff"));
109  CPPUNIT_ASSERT(HexUtils::isValidHex("0x123456789abcd"));
110 
111  CPPUNIT_ASSERT(! HexUtils::isValidHex("0xabcdefghij"));
112  CPPUNIT_ASSERT(! HexUtils::isValidHex("0x0t"));
113 }
114 
115 void
117 {
118  int32_t value32;
119  int64_t value64;
120  std::string hexString;
121 
122  hexString = "0x0";
123  CPPUNIT_ASSERT(HexUtils::parse(hexString, value32));
124  CPPUNIT_ASSERT(value32 == 0);
125 
126  hexString = "0x80000000";
127  CPPUNIT_ASSERT(HexUtils::parse(hexString, value32));
128  CPPUNIT_ASSERT(value32 == -2147483648);
129 
130  hexString = "0xffffffff";
131  CPPUNIT_ASSERT(HexUtils::parse(hexString, value32));
132  CPPUNIT_ASSERT(value32 == -1);
133 
134  hexString = "0x123456789abc";
135  CPPUNIT_ASSERT(! HexUtils::parse(hexString, value32));
136 
137  hexString = "0x123456789abc";
138  CPPUNIT_ASSERT(HexUtils::parse(hexString, value64));
139 
140  hexString = "0x0t";
141  CPPUNIT_ASSERT(! HexUtils::parse(hexString, value32));
142 
143  hexString = " abc ";
144  CPPUNIT_ASSERT(! HexUtils::parse(hexString, value32));
145 }
146 
148 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities");
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("NovaDAQUtilities")
void testValidity()
bool isValidHex(const std::string &hexString)
Definition: HexUtils.cpp:74
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
void testFormat()
OStream cout
Definition: OStream.cxx:6
bool parse(const std::string &hexString, int32_t &decimalValue)
Definition: HexUtils.cpp:96
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(HexUtilTests,"NovaDAQUtilities")
enum BeamMode string