#include "precompiled_unittests_core.h" #include "core/Seeder.h" #include "core/UUID.h" #include "core/AdaptersInterfaces/Adapter_SafeRandom.h" namespace ITF { // gtest & gmock use namespace testing using namespace testing; class Adapter_SafeRandom_Mock : public Adapter_SafeRandom { public: ErrorCode generate(size_t len, ITF::u8* buffer) const override { for (size_t i(0); i < len; ++i) { buffer[i] = m_Seeder.GetU32(0xff); } return Error_Ok; } private: ITF::Seeder m_Seeder; }; //anonymous TEST(UUID, test_NullIds) { UUID uuid; //default constructor should be clean EXPECT_EQ(UUID::Nil, uuid); EXPECT_BTRUE(uuid.isNil()); //going to valid and then back to null EXPECT_BTRUE(uuid.fromString("e5d5eda2-a754-4dba-81e7-52c74ee7674d")); EXPECT_BFALSE(uuid.isNil()); uuid.makeNil(); EXPECT_EQ(UUID::Nil, uuid); EXPECT_BTRUE(uuid.isNil()); //check null ids as string EXPECT_STREQ(uuid.toString().cStr(), "00000000-0000-0000-0000-000000000000"); } //converts UUIDs to and from strings, checking for validity TEST(UUID, test_StringConversion) { UUID uuid; const char* validIdStr = "e5d5eda2-a754-4dba-81e7-52c74ee7674d"; //convert from long strings EXPECT_EQ(uuid.fromString(validIdStr), true); EXPECT_BFALSE(uuid.isNil()); EXPECT_STREQ(uuid.toString().cStr(), validIdStr); const char* bracketsTest = "{12345678-1234-5678-1234-567812345678}"; const char* bracketsTestClean = "12345678-1234-5678-1234-567812345678"; EXPECT_EQ(uuid.fromString(bracketsTest), true); EXPECT_BFALSE(uuid.isNil()); EXPECT_STREQ(uuid.toString().cStr(), bracketsTestClean); } //checks if different id versions are being reported correctly TEST(UUID, test_Versioning) { const String8 validIdStrVersions[] = { "ba5bdcde-9751-11e4-b100-123b93f75cba" , "df6a0fe0-9752-21e4-b4a9-0800200c9a66" , "c478211b-224d-30b1-9116-c06048999ce2" , "dcafcc69-ec43-43f2-9a4f-fe3fcc82a5e2" , "12345678-1234-5678-1234-567812345678"}; for (size_t version=0; version < ITF_ARRAY_SIZE(validIdStrVersions); ++version) { UUID uuid(validIdStrVersions[version]); EXPECT_EQ(uuid.toString(), validIdStrVersions[version]); } } //checks comparison operators (=, ==, !=, < and >) between UUIDs TEST(UUID, test_ComparisonOperators) { //check comparisons between a valid and a null id UUID uuidA; UUID uuidB("ba5bdcde-9751-11e4-b100-123b93f75cba"); EXPECT_BTRUE(uuidA < uuidB); EXPECT_BTRUE(uuidB > uuidA); EXPECT_NE(uuidA, uuidB); //equality comparison of a copy constructor UUID uuidC(uuidB); EXPECT_EQ(uuidB, uuidC); //comparisons between 2 valid and different ids UUID uuidD("dcafcc69-ec43-43f2-9a4f-fe3fcc82a5e2"); EXPECT_NE(uuidB, uuidD); EXPECT_BTRUE( uuidD > uuidB); EXPECT_BTRUE( uuidB < uuidD); EXPECT_BFALSE(uuidD < uuidB); EXPECT_BFALSE(uuidB > uuidD); //comparisons following an assignment operator uuidD = uuidB; EXPECT_EQ(uuidB, uuidD); EXPECT_BFALSE(uuidD > uuidB); EXPECT_BFALSE(uuidB < uuidD); } //checks the creation of random uuids (v4) TEST(UUID, test_MakeRandom) { UUID uuid; Adapter_SafeRandom_Mock sra; //check if a number of random ids are generated, unique and valid std::set uniqueIds; for (size_t i = 0; i < 10; ++i) { uuid.makeRandom(); EXPECT_BFALSE(uuid.isNil()); EXPECT_EQ(uniqueIds.find(uuid), uniqueIds.end()); uniqueIds.insert(uuid); } } }