aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/opengl/shared/emugl/common/unique_integer_map_unittest.cpp
blob: 8aee013273eacc33cee02f3d56b036217a93739b (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2014 The Android Open Source Project
//
// 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.

#include "emugl/common/unique_integer_map.h"

#include <gtest/gtest.h>

#include <stdio.h>

namespace emugl {

typedef UniqueIntegerMap<uintptr_t,uint32_t> MyMap;

TEST(UniqueIntegerMap, Empty) {
    MyMap map;

    EXPECT_TRUE(map.empty());
    EXPECT_EQ(0U, map.size());
    EXPECT_EQ(0U, map.find(0U));
    EXPECT_EQ(0U, map.find(1U));
    EXPECT_EQ(0U, map.find(2U));
    EXPECT_EQ(0U, map.find(4U));
}

TEST(UniqueIntegerMap, AddOne) {
    MyMap map;
    uintptr_t key1 = 1U;
    uint32_t val1 = map.add(key1);

    EXPECT_NE(0U, val1);
    EXPECT_EQ(val1, map.find(key1));
    EXPECT_EQ(key1, map.findKeyFor(val1));

    EXPECT_FALSE(map.empty());
    EXPECT_EQ(1U, map.size());

    EXPECT_EQ(0U, map.find(0));
    EXPECT_EQ(0U, map.findKeyFor(0));

    EXPECT_EQ(0U, map.find(key1 + 1));
    EXPECT_EQ(0U, map.findKeyFor(val1 + 1));
}

TEST(UniqueIntegerMap, AddMultiple) {
    MyMap map;
    const size_t kCount = 100;
    const size_t kKeyMultiplier = 3U;  // must be >= 2.
    uint32_t values[kCount];

    for (size_t n = 0; n < kCount; ++n) {
        uintptr_t key = 1U + n * kKeyMultiplier;
        values[n] = map.add(key);
        EXPECT_NE(0U, values[n]) << "key #" << n;
    }

    EXPECT_EQ(kCount, map.size());

    for (size_t n = 0; n < kCount; ++n) {
        uintptr_t key = 1U + n * kKeyMultiplier;
        EXPECT_EQ(values[n], map.find(key)) << "key #" << n;
        EXPECT_EQ(0U, map.find(key + 1U)) << "key #" << n;
    }

    for (size_t n = 0; n < kCount; ++n) {
        uintptr_t key = 1U + n * kKeyMultiplier;
        EXPECT_EQ(key, map.findKeyFor(values[n]));
    }
}

TEST(UniqueIntegerMap, Del) {
    MyMap map;
    const size_t kCount = 100;
    const size_t kKeyMultiplier = 3U;  // must be >= 2.
    uint32_t values[kCount];

    for (size_t n = 0; n < kCount; ++n) {
        uintptr_t key = 1U + n * kKeyMultiplier;
        values[n] = map.add(key);
    }

    for (size_t n = 0; n < kCount; ++n) {
        uintptr_t key = 1U + n * kKeyMultiplier;
        map.del(key);
        EXPECT_EQ(kCount - 1U - n, map.size());
        EXPECT_EQ(0U, map.find(key));
        EXPECT_EQ(0U, map.findKeyFor(values[n]));
    }

    EXPECT_TRUE(map.empty());
}

}  // namespace emugl