// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include #include #include using namespace ::testing; namespace storage { namespace { std::vector> getJudyArrayContents(const JudyArray& array) { std::vector> vals; for (JudyArray::const_iterator it = array.begin(); it != array.end(); ++it) { vals.push_back(std::make_pair(it.key(), it.value())); } return vals; } } TEST(JudyArrayTest, iterating) { JudyArray array; // Test that things are sane for empty document ASSERT_EQ(array.begin(), array.end()); // Add some values std::vector> values({ {3, 2}, {5, 12}, {15, 8}, {13, 10}, {7, 6}, {9, 4} }); for (uint32_t i=0; i> values1({ {3, 2}, {5, 12}, {15, 8}, {13, 10}, {7, 6}, {9, 4} }); for (uint32_t i=0; i> values2({ {4, 5}, {9, 40} }); for (uint32_t i=0; i StdMap; StdMap stdMap; boost::rand48 rnd(55); for (uint32_t checkpoint=0; checkpoint<50; ++checkpoint) { for (uint32_t opnr=0; opnr<500; ++opnr) { int optype = rnd() % 100; if (optype < 30) { // Insert JudyArray::key_type key(rnd() % 500); JudyArray::key_type value(rnd()); judyArray.insert(key, value); stdMap[key] = value; } else if (optype < 50) { // operator[] JudyArray::key_type key(rnd() % 500); JudyArray::key_type value(rnd()); judyArray[key] = value; stdMap[key] = value; } else if (optype < 70) { // erase() JudyArray::key_type key(rnd() % 500); EXPECT_EQ(stdMap.erase(key), judyArray.erase(key)); } else if (optype < 75) { // size() EXPECT_EQ(stdMap.size(), judyArray.size()); } else if (optype < 78) { // empty() EXPECT_EQ(stdMap.empty(), judyArray.empty()); } else { // find() JudyArray::key_type key(rnd() % 500); auto it = judyArray.find(key); auto it2 = stdMap.find(key); EXPECT_EQ(it2 == stdMap.end(), it == judyArray.end()); if (it != judyArray.end()) { EXPECT_EQ(it.key(), it2->first); EXPECT_EQ(it.value(), it2->second); } } } // Ensure judy array contents is equal to std::map's at this point StdMap tmpMap; for (JudyArray::const_iterator it = judyArray.begin(); it != judyArray.end(); ++it) { tmpMap[it.key()] = it.value(); } EXPECT_EQ(stdMap, tmpMap); } } } // storage