// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include #include #include namespace document { struct GlobalIdTest : public CppUnit::TestFixture { void testNormalUsage(); void testBucketIdConversion(); void testGidRangeConversion(); void testBucketOrderCmp(); void verifyGlobalIdRange(const std::vector& ids, uint32_t countBits); CPPUNIT_TEST_SUITE(GlobalIdTest); CPPUNIT_TEST(testNormalUsage); CPPUNIT_TEST(testBucketIdConversion); CPPUNIT_TEST(testGidRangeConversion); CPPUNIT_TEST(testBucketOrderCmp); CPPUNIT_TEST_SUITE_END(); }; CPPUNIT_TEST_SUITE_REGISTRATION(GlobalIdTest); void GlobalIdTest::testNormalUsage() { const char* emptystring = "\0\0\0\0\0\0\0\0\0\0\0\0"; const char* teststring = "1234567890ABCDEF"; CPPUNIT_ASSERT(strlen(teststring) > GlobalId::LENGTH); { // Test empty global id GlobalId id; for (uint32_t i=0; i& ids, uint32_t countBits) { BucketIdFactory factory; for (uint32_t i=0, n=ids.size(); i last.convertToBucketId().toKey())) { std::ostringstream msg; msg << gid << " should be in the range " << first << " - " << last; msg << ", as bucket " << gid.convertToBucketId() << " should be in the range " << first.convertToBucketId() << " - " << last.convertToBucketId() << "\n"; msg << ", reverted " << std::hex << gid.convertToBucketId().toKey() << " should be in the range " << first.convertToBucketId().toKey() << " - " << last.convertToBucketId().toKey() << "\n"; CPPUNIT_ASSERT_MESSAGE(msg.str(), gid.convertToBucketId().toKey() >= first.convertToBucketId().toKey()); CPPUNIT_ASSERT_MESSAGE(msg.str(), gid.convertToBucketId().toKey() <= last.convertToBucketId().toKey()); } } else { if ((gidKey >= first.convertToBucketId().toKey()) && (gidKey <= last.convertToBucketId().toKey())) { std::ostringstream msg; msg << gid << " should not be in the range " << first << " - " << last; CPPUNIT_ASSERT_MESSAGE(msg.str(), (gid.convertToBucketId().toKey() < first.convertToBucketId().toKey()) || (gid.convertToBucketId().toKey() > last.convertToBucketId().toKey())); } } } } } void GlobalIdTest::testGidRangeConversion() { // Generate a lot of random document ids used for test std::vector docIds; vespalib::RandomGen randomizer(0xdeadbabe); for (uint32_t j=0; j<100; ++j) { vespalib::asciistream name_space; vespalib::asciistream ost; for (uint32_t i=0, n=randomizer.nextUint32(1, 5); i GidMap; GidMap gidMap; gidMap[foo] = 666; gidMap[bar] = 777; gidMap[baz] = 888; GidMap::iterator it = gidMap.begin(); CPPUNIT_ASSERT(it->first == bar); CPPUNIT_ASSERT(it->second == 777); ++it; CPPUNIT_ASSERT(it->first == baz); CPPUNIT_ASSERT(it->second == 888); ++it; CPPUNIT_ASSERT(it->first == foo); CPPUNIT_ASSERT(it->second == 666); } } } // document