//////////////////////////////////////////////////////////////////////////////// /// @brief test suite for TRI_associative_multi_pointer_t /// /// @file /// /// DISCLAIMER /// /// Copyright 2012 triagens GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is triAGENS GmbH, Cologne, Germany /// /// @author Max Neunhoeffer /// @author Copyright 2014, triAGENS GmbH, Cologne, Germany //////////////////////////////////////////////////////////////////////////////// #include "Basics/Common.h" #include "gtest/gtest.h" #include "Basics/conversions.h" #include "Basics/fasthash.h" #include "Basics/hashes.h" #include "Basics/tri-strings.h" #include "Containers/AssocMulti.h" using namespace std; // ----------------------------------------------------------------------------- // --SECTION-- private macros // ----------------------------------------------------------------------------- #define ELEMENT(name, v, k) \ data_container_t name; \ name.key = k; \ name.value = v; struct data_container_t { int value; int key; data_container_t() : value(0), key(0){}; data_container_t(int key, int value) : value(value), key(key){}; }; struct AssocMultiTestHelper { static inline uint64_t HashKey(void const* e) { int const* key = (int const*)e; return fasthash64(key, sizeof(int), 0x12345678); } static inline uint64_t HashElement(void const* e, bool byKey) { data_container_t const* element = (data_container_t const*)e; if (byKey) { return fasthash64(&element->key, sizeof(element->key), 0x12345678); } else { return fasthash64(&element->value, sizeof(element->value), 0x12345678); } } bool IsEqualKeyElement(void* userData, void const* k, void const* r) const { int const* key = (int const*)k; data_container_t const* element = (data_container_t const*)r; return *key == element->key; } bool IsEqualElementElement(void* userData, void const* l, void const* r) const { data_container_t const* left = (data_container_t const*)l; data_container_t const* right = (data_container_t const*)r; return left->value == right->value; } bool IsEqualElementElementByKey(void* userData, void const* l, void const* r) const { data_container_t const* left = (data_container_t const*)l; data_container_t const* right = (data_container_t const*)r; return left->key == right->key; } }; #define INIT_MULTI \ arangodb::containers::AssocMulti a1( \ (AssocMultiTestHelper())); // ----------------------------------------------------------------------------- // --SECTION-- test suite // ----------------------------------------------------------------------------- TEST(AssociativeMultiPointerTest, tst_init) { INIT_MULTI EXPECT_EQ(0U, a1.size()); } //////////////////////////////////////////////////////////////////////////////// /// @brief test unique insertion //////////////////////////////////////////////////////////////////////////////// TEST(AssociativeMultiPointerTest, tst_insert_few) { INIT_MULTI void* r = nullptr; ELEMENT(e1, 1, 123); EXPECT_EQ(r, a1.insert(nullptr, &e1, true, false)); EXPECT_EQ(1U, a1.size()); EXPECT_EQ(&e1, a1.lookup(nullptr, &e1)); EXPECT_EQ(&e1, a1.remove(nullptr, &e1)); EXPECT_EQ(0U, a1.size()); EXPECT_EQ(r, a1.lookup(nullptr, &e1)); } // Note MODULUS must be a divisor of NUMBER_OF_ELEMENTS // and NUMBER_OF_ELEMENTS must be a multiple of 3. #define NUMBER_OF_ELEMENTS 3000 #define MODULUS 10 TEST(AssociativeMultiPointerTest, tst_insert_delete_many) { INIT_MULTI unsigned int i, j; ELEMENT(e, 0, 0); vector v; data_container_t* n = nullptr; data_container_t* p; data_container_t* one_more; // Put in some data: for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { p = new data_container_t(i % MODULUS, i); v.push_back(p); EXPECT_EQ(n, a1.insert(nullptr, p, true, false)); } one_more = new data_container_t(NUMBER_OF_ELEMENTS % MODULUS, NUMBER_OF_ELEMENTS); // Now check it is there (by element): for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { p = static_cast(a1.lookup(nullptr, v[i])); EXPECT_EQ(p, v[i]); } // This should not be there: p = static_cast(a1.lookup(nullptr, one_more)); EXPECT_EQ(n, p); // Now check by key: std::vector* res = nullptr; for (i = 0; i < MODULUS; i++) { int* space = new int[NUMBER_OF_ELEMENTS / MODULUS](); res = a1.lookupByKey(nullptr, &i); EXPECT_EQ(res->size(), (NUMBER_OF_ELEMENTS / MODULUS)); // Now check its contents: for (j = 0; j < res->size(); j++) { data_container_t* q = static_cast(res->at(j)); EXPECT_EQ((q->value % MODULUS), i); EXPECT_EQ(space[(q->value - i) / MODULUS], 0); space[(q->value - i) / MODULUS] = 1; } delete[] space; delete res; } // Delete some data: for (i = 0; i < v.size(); i += 3) { EXPECT_EQ(v[i], a1.remove(nullptr, v[i])); } for (i = 0; i < v.size(); i += 3) { EXPECT_EQ(n, a1.remove(nullptr, v[i])); } // Now check which are there (by element): for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { p = static_cast(a1.lookup(nullptr, v[i])); if (i % 3 == 0) { EXPECT_EQ(p, n); } else { EXPECT_EQ(p, v[i]); } } // This should not be there: p = static_cast(a1.lookup(nullptr, one_more)); EXPECT_EQ(n, p); // Delete some more: for (i = 1; i < v.size(); i += 3) { EXPECT_EQ(v[i], a1.remove(nullptr, v[i])); } for (i = 1; i < v.size(); i += 3) { EXPECT_EQ(n, a1.remove(nullptr, v[i])); } // Now check which are there (by element): for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { p = static_cast(a1.lookup(nullptr, v[i])); if (i % 3 == 2) { EXPECT_EQ(p, v[i]); } else { EXPECT_EQ(p, n); } } // This should not be there: p = static_cast(a1.lookup(nullptr, one_more)); EXPECT_EQ(n, p); // Delete the rest: for (i = 2; i < v.size(); i += 3) { EXPECT_EQ(v[i], a1.remove(nullptr, v[i])); } for (i = 2; i < v.size(); i += 3) { EXPECT_EQ(n, a1.remove(nullptr, v[i])); } // Now check which are there (by element): for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { p = static_cast(a1.lookup(nullptr, v[i])); EXPECT_EQ(p, n); } // This should not be there: p = static_cast(a1.lookup(nullptr, one_more)); EXPECT_EQ(n, p); // Pull down data again: for (i = 0; i < NUMBER_OF_ELEMENTS; i++) { delete v[i]; } v.clear(); delete one_more; }