Commit 5f4a2d70 authored by kolusask's avatar kolusask
Browse files

Fixed CuckooHash Map and Set unit tests

parent 85367a03
Loading
Loading
Loading
Loading
+21 −6
Original line number Original line Diff line number Diff line
@@ -9,11 +9,22 @@
#include <vector>
#include <vector>




#ifdef HAVE_CUDA
#include <TNL/Devices/Cuda.h>
using Device = TNL::Devices::Cuda;
#else
#include <TNL/Devices/Host.h>
using Device = TNL::Devices::Host;
#endif


template<typename tPair>
template<typename tPair>
class CuckooHashMapTest : public ::testing::Test {
class CuckooHashMapTest : public ::testing::Test {
protected:
protected:
    using KeyType = typename std::tuple_element<0, tPair>::type;
    using KeyType = typename std::tuple_element<0, tPair>::type;
    using ValueType = typename std::tuple_element<1, tPair>::type;
    using ValueType = typename std::tuple_element<1, tPair>::type;
    using ArrayType = Array<Pair<KeyType, ValueType>, Device>;
    using MapType = CuckooHashMap<KeyType, ValueType, Device>;
};
};


using CuckooHashMapTypes = ::testing::Types<
using CuckooHashMapTypes = ::testing::Types<
@@ -40,28 +51,32 @@ TYPED_TEST_SUITE(CuckooHashMapTest, CuckooHashMapTypes);
TYPED_TEST(CuckooHashMapTest, correctQuery) {
TYPED_TEST(CuckooHashMapTest, correctQuery) {
    using KeyType = typename TestFixture::KeyType;
    using KeyType = typename TestFixture::KeyType;
    using ValueType = typename TestFixture::ValueType;
    using ValueType = typename TestFixture::ValueType;
    using ArrayType = typename TestFixture::ArrayType;
    using MapType = typename TestFixture::MapType;


    Array<Pair<KeyType, ValueType>> pairs = get_pairs<KeyType, ValueType>();
    ArrayType pairs = get_pairs<KeyType, ValueType, Device>();
    int tableSize = 1.25 * pairs.getSize();
    int tableSize = 1.25 * pairs.getSize();
    int nIterations = 7 * log(pairs.getSize()); 
    int nIterations = 7 * log(pairs.getSize()); 
    CuckooHashMap<KeyType, ValueType> map(pairs, 6);
    MapType map(pairs, 6);


    ValueType value;
    ValueType value;
    for (int i = 0; i < pairs.getSize(); i++) {
    for (int i = 0; i < pairs.getSize(); i++) {
        KeyType key = pairs[i].key;
        KeyType key = pairs.getElement(i).key;
        EXPECT_TRUE(map.find(key, value));
        EXPECT_TRUE(map.find(key, value));
        EXPECT_EQ(value, pairs[i].value);
        EXPECT_EQ(value, pairs.getElement(i).value);
    }
    }
}
}


TYPED_TEST(CuckooHashMapTest, wrongQuery) {
TYPED_TEST(CuckooHashMapTest, wrongQuery) {
    using KeyType = typename TestFixture::KeyType;
    using KeyType = typename TestFixture::KeyType;
    using ValueType = typename TestFixture::ValueType;
    using ValueType = typename TestFixture::ValueType;
    using ArrayType = typename TestFixture::ArrayType;
    using MapType = typename TestFixture::MapType;


    Array<Pair<KeyType, ValueType>> pairs = get_pairs<KeyType, ValueType>();
    ArrayType pairs = get_pairs<KeyType, ValueType, Device>();
    int tableSize = 1.25 * pairs.getSize();
    int tableSize = 1.25 * pairs.getSize();
    int nIterations = 7 * log(pairs.getSize()); 
    int nIterations = 7 * log(pairs.getSize()); 
    CuckooHashMap<KeyType, ValueType> map(pairs, 6);
    MapType map(pairs, 6);


    for (KeyType key = 8; key < 16; key++) {
    for (KeyType key = 8; key < 16; key++) {
        ValueType value;
        ValueType value;
+20 −5
Original line number Original line Diff line number Diff line
@@ -9,10 +9,21 @@
#include <vector>
#include <vector>




#ifdef HAVE_CUDA
#include <TNL/Devices/Cuda.h>
using Device = TNL::Devices::Cuda;
#else
#include <TNL/Devices/Host.h>
using Device = TNL::Devices::Host;
#endif


template<typename Key>
template<typename Key>
class CuckooHashSetTest : public ::testing::Test {
class CuckooHashSetTest : public ::testing::Test {
protected:
protected:
    using KeyType = Key;
    using KeyType = Key;
    using ArrayType = Array<KeyType, Device>;
    using SetType = CuckooHashSet<Key, Device>;
};
};


using CuckooHashSetTypes = ::testing::Types<int, long, float, double>;
using CuckooHashSetTypes = ::testing::Types<int, long, float, double>;
@@ -21,23 +32,27 @@ TYPED_TEST_SUITE(CuckooHashSetTest, CuckooHashSetTypes);


TYPED_TEST(CuckooHashSetTest, correctQuery) {
TYPED_TEST(CuckooHashSetTest, correctQuery) {
    using KeyType = typename TestFixture::KeyType;
    using KeyType = typename TestFixture::KeyType;
    using ArrayType = typename TestFixture::ArrayType;
    using SetType = typename TestFixture::SetType;


    Array<KeyType> values = get_values<KeyType>();
    ArrayType values = get_values<KeyType, Device>();
    int tableSize = 1.25 * values.getSize();
    int tableSize = 1.25 * values.getSize();
    int nIterations = 7 * log(values.getSize()); 
    int nIterations = 7 * log(values.getSize()); 
    CuckooHashSet<KeyType> set(values, 6);
    SetType set(values, 6);


    for (int i = 0; i < values.getSize(); i++)
    for (int i = 0; i < values.getSize(); i++)
        EXPECT_TRUE(set.contains(values[i]));
        EXPECT_TRUE(set.contains(values.getElement(i)));
}
}


TYPED_TEST(CuckooHashSetTest, wrongQuery) {
TYPED_TEST(CuckooHashSetTest, wrongQuery) {
    using KeyType = typename TestFixture::KeyType;
    using KeyType = typename TestFixture::KeyType;
    using ArrayType = typename TestFixture::ArrayType;
    using SetType = typename TestFixture::SetType;


    Array<KeyType> values = get_values<KeyType>();
    ArrayType values = get_values<KeyType, Device>();
    int tableSize = 1.25 * values.getSize();
    int tableSize = 1.25 * values.getSize();
    int nIterations = 7 * log(values.getSize()); 
    int nIterations = 7 * log(values.getSize()); 
    CuckooHashSet<KeyType> map(values, 6);
    SetType map(values, 6);


    for (KeyType key = 8; key < 16; key++)
    for (KeyType key = 8; key < 16; key++)
        EXPECT_FALSE(map.contains(key));
        EXPECT_FALSE(map.contains(key));
+18 −11
Original line number Original line Diff line number Diff line
#include "../CuckooHash/CuckooHashMap.h"
#include "../CuckooHash/CuckooHashMap.h"


#include <TNL/Algorithms/ParallelFor.h>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/Array.h>


using namespace TNL::Containers;
using namespace TNL::Containers;




template<typename KeyType, typename ValueType>
template<typename KeyType, typename ValueType, typename Device>
Array<Pair<KeyType, ValueType>> get_pairs() {
Array<Pair<KeyType, ValueType>, Device> get_pairs() {
    Array<Pair<KeyType, ValueType>> pairs(8);
    Array<Pair<KeyType, ValueType>, Device> pairs(8);
    for (ValueType i = 0; i < 8; i++)
    auto pView = pairs.getView();
        pairs[i] = {i, i + 1}; 
    auto fill_pairs = [pView] __cuda_callable__ (int i) mutable {
        pView[i] = { KeyType(i), ValueType(i + 1) };
    };
    TNL::Algorithms::ParallelFor<Device>::exec(0, 8, fill_pairs);
    return pairs;
    return pairs;
}
}


#include "CuckooHashMapTest.h"
#include "CuckooHashMapTest.h"
//#include "HashGraphV1MapTest.h"
//#include "HashGraphV1MapTest.h"


template<typename KeyType>
template<typename KeyType, typename Device>
Array<KeyType> get_values() {
Array<KeyType, Device> get_values() {
    Array<KeyType> values(8);
    Array<KeyType, Device> values(8);
    for (KeyType i = 0; i < 8; i++)
    auto vView = values.getView();
        values[i] = i;
    auto fill_values = [vView] __cuda_callable__ (int i) mutable {
        vView[i] = KeyType(i);
    };
    TNL::Algorithms::ParallelFor<Device>::exec(0, 8, fill_values);
    return values;
    return values;
}
}


#include "CuckooHashSetTest.h"
#include "CuckooHashSetTest.h"

//#include "HashGraphV1SetTest.h"


int main(int argc, char* argv[]) {
int main(int argc, char* argv[]) {
   ::testing::InitGoogleTest( &argc, argv );
   ::testing::InitGoogleTest( &argc, argv );