aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/tests/eval/array_array_map/array_array_map_test.cpp
blob: 0aaa85a65fc1a0d980e24f45b4d0f38a27aee46b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/eval/eval/array_array_map.h>
#include <vespa/vespalib/gtest/gtest.h>

using namespace vespalib;
using namespace vespalib::eval;

std::vector<int> ints(std::vector<int> vec) { return vec; }

template <typename T>
ConstArrayRef<T> refs(const std::vector<T> &vec) { return ConstArrayRef<T>(vec); }

//-----------------------------------------------------------------------------

TEST(ArrayArrayMapTest, simple_map_can_be_created_and_used) {
    // ctor params: 'keys_per_entry', 'values_per_entry', 'expected_entries'
    ArrayArrayMap<int,int> map(2,3,5);
    EXPECT_EQ(map.size(), 0);
    EXPECT_FALSE(map.lookup(refs(ints({1, 2}))).valid());
    auto tag = map.add_entry(refs(ints({1, 2})));
    EXPECT_EQ(map.size(), 1);
    auto values = map.get_values(tag);
    ASSERT_EQ(values.size(), 3);
    values[0] = 10;
    values[1] = 20;
    values[2] = 30;
    EXPECT_FALSE(map.lookup(refs(ints({2, 1}))).valid());
    auto tag2 = map.lookup(refs(ints({1, 2})));
    ASSERT_TRUE(tag2.valid());
    EXPECT_EQ(map.get_values(tag2)[1], 20);
}

TEST(ArrayArrayMapTest, lookup_or_add_entry_works) {
    ArrayArrayMap<int,int> map(2,3,5);
    auto res1 = map.lookup_or_add_entry(refs(ints({1, 2})));
    auto res2 = map.lookup_or_add_entry(refs(ints({1, 2})));
    EXPECT_TRUE(res1.second);
    EXPECT_FALSE(res2.second);
    EXPECT_EQ(map.get_values(res1.first).begin(), map.get_values(res2.first).begin());
    EXPECT_EQ(map.get_values(res1.first).size(), 3);
}

TEST(ArrayArrayMapTest, each_entry_works) {
    ArrayArrayMap<int,int> map(2,3,5);
    auto res1 = map.add_entry(refs(ints({1, 2})));
    auto res2 = map.add_entry(refs(ints({2, 1})));
    map.get_values(res1)[0] = 10;
    map.get_values(res2)[0] = 20;
    EXPECT_EQ(map.size(), 2);
    bool first = true;
    // Note: insert order is guaranteed here
    map.each_entry([&](const auto &keys, const auto &values)
                   {
                       if (first) {
                           EXPECT_EQ(keys[0], 1);
                           EXPECT_EQ(keys[1], 2);
                           EXPECT_EQ(values[0], 10);
                           first = false;
                       } else {
                           EXPECT_EQ(keys[0], 2);
                           EXPECT_EQ(keys[1], 1);
                           EXPECT_EQ(values[0], 20);
                       } 
                   });
}

//-----------------------------------------------------------------------------

GTEST_MAIN_RUN_ALL_TESTS()