00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "fennel/common/CommonPreamble.h"
00024 #include "fennel/common/PseudoUuid.h"
00025 #include "fennel/common/TraceSource.h"
00026 #include "fennel/test/TestBase.h"
00027 #include <boost/test/test_tools.hpp>
00028 #include <iostream>
00029
00030 using namespace fennel;
00031 using std::string;
00032
00035 class PseudoUuidTest : public TestBase, public TraceSource
00036 {
00037 void testGeneration();
00038 void testInvalid();
00039 void testComparison();
00040 void testParsing();
00041 void testFormatting();
00042 void testCopy();
00043
00044 public:
00045 explicit PseudoUuidTest()
00046 : TraceSource(shared_from_this(),"PseudoUuidTest")
00047 {
00048 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testGeneration);
00049 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testInvalid);
00050 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testComparison);
00051 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testParsing);
00052 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testFormatting);
00053 FENNEL_UNIT_TEST_CASE(PseudoUuidTest, testCopy);
00054 }
00055 };
00056
00057 void PseudoUuidTest::testGeneration()
00058 {
00059 PseudoUuid uuid;
00060
00061 uuid.generate();
00062
00063 for (int i = 0; i < PseudoUuid::UUID_LENGTH; i++) {
00064 if (uuid.getByte(i) != 0) {
00065
00066 return;
00067 }
00068 }
00069
00070 BOOST_ERROR("PseudoUuid::generate() generated all-zero UUID");
00071 }
00072
00073 void PseudoUuidTest::testInvalid()
00074 {
00075 PseudoUuid uuid;
00076
00077 uuid.generateInvalid();
00078
00079 for (int i = 0; i < PseudoUuid::UUID_LENGTH; i++) {
00080 BOOST_CHECK_MESSAGE(
00081 uuid.getByte(i) == (uint8_t)0xFF, "invalid UUID not all 0xFF");
00082 }
00083 }
00084
00085 void PseudoUuidTest::testComparison()
00086 {
00087 PseudoUuid uuid1("00010203-0405-0607-0809-0A0B0C0D0E0F");
00088 PseudoUuid uuid2("00010203-0405-0607-0809-0A0B0C0D0E0F");
00089 PseudoUuid uuid3("0F0E0D0C-0B0A-0908-0706-050403020100");
00090
00091 BOOST_CHECK(uuid1 == uuid2);
00092 BOOST_CHECK(uuid1 != uuid3);
00093 BOOST_CHECK(uuid2 != uuid3);
00094 }
00095
00096 void PseudoUuidTest::testParsing()
00097 {
00098 PseudoUuid uuid1("00010203-0405-0607-0809-0A0B0C0D0E0F");
00099
00100 for (int i = 0; i < PseudoUuid::UUID_LENGTH; i++) {
00101 BOOST_CHECK_EQUAL(i, uuid1.getByte(i));
00102 }
00103
00104 PseudoUuid uuid2("00000000-0000-0000-0000-000000000000");
00105
00106 for (int i = 0; i < PseudoUuid::UUID_LENGTH; i++) {
00107 BOOST_CHECK_EQUAL(0, uuid2.getByte(i));
00108 }
00109
00110 PseudoUuid uuid3("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
00111
00112 for (int i = 0; i < PseudoUuid::UUID_LENGTH; i++) {
00113 BOOST_CHECK_EQUAL((uint8_t)0xff, uuid3.getByte(i));
00114 }
00115
00116 BOOST_CHECK_THROW(
00117 PseudoUuid("bad"), FennelExcn);
00118 BOOST_CHECK_THROW(
00119 PseudoUuid("00112233-44-55-6677-8899-AABBCCDDEEFF"), FennelExcn);
00120 }
00121
00122 void PseudoUuidTest::testFormatting()
00123 {
00124 string exp1 = "12345678-9abc-def0-1234-56789abcdef0";
00125 string exp2 = "00000000-0000-0000-0000-000000000000";
00126
00127 PseudoUuid uuid1(exp1), uuid2(exp2);
00128
00129 string got1 = uuid1.toString();
00130 string got2 = uuid2.toString();
00131
00132 BOOST_CHECK_EQUAL(exp1, got1);
00133 BOOST_CHECK_EQUAL(exp2, got2);
00134 }
00135
00136 void PseudoUuidTest::testCopy()
00137 {
00138 PseudoUuid uuid1("00010203-0405-0607-0809-0A0B0C0D0E0F");
00139 PseudoUuid uuid2(uuid1);
00140 PseudoUuid uuid3 = uuid1;
00141
00142 BOOST_CHECK_EQUAL(uuid1, uuid2);
00143 BOOST_CHECK_EQUAL(uuid1, uuid3);
00144
00145 PseudoUuid uuid4;
00146
00147 uuid4.generateInvalid();
00148
00149 PseudoUuid uuid5 = uuid4;
00150 PseudoUuid uuid6(uuid4);
00151
00152 BOOST_CHECK_EQUAL(uuid4, uuid5);
00153 BOOST_CHECK_EQUAL(uuid4, uuid6);
00154 }
00155 FENNEL_UNIT_TEST_SUITE(PseudoUuidTest)
00156
00157