YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/id_mapping-test.cc
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include <vector>
34
35
#include <gtest/gtest.h>
36
37
#include "yb/common/id_mapping.h"
38
#include "yb/util/random.h"
39
#include "yb/util/test_util.h"
40
41
namespace yb {
42
// Basic unit test for IdMapping.
43
1
TEST(TestIdMapping, TestSimple) {
44
1
  IdMapping m;
45
1
  ASSERT_EQ(-1, m.get(1));
46
1
  m.set(1, 10);
47
1
  m.set(2, 20);
48
1
  m.set(3, 30);
49
1
  ASSERT_EQ(10, m.get(1));
50
1
  ASSERT_EQ(20, m.get(2));
51
1
  ASSERT_EQ(30, m.get(3));
52
1
}
53
54
// Insert enough entries in the mapping so that it is forced to rehash
55
// itself.
56
1
TEST(TestIdMapping, TestRehash) {
57
1
  IdMapping m;
58
59
1.00k
  for (int i = 0; i < 1000; i++) {
60
1.00k
    m.set(i, i * 10);
61
1.00k
  }
62
1.00k
  for (int i = 0; i < 1000; i++) {
63
1.00k
    ASSERT_EQ(i * 10, m.get(i));
64
1.00k
  }
65
1
}
66
67
// Generate a random sequence of keys.
68
1
TEST(TestIdMapping, TestRandom) {
69
1
  Random r(SeedRandom());
70
1
  IdMapping m;
71
1
  std::vector<int> picked;
72
1.00k
  for (int i = 0; i < 1000; i++) {
73
1.00k
    int32_t k = r.Next32();
74
1.00k
    m.set(k, i);
75
1.00k
    picked.push_back(k);
76
1.00k
  }
77
78
1.00k
  for (size_t i = 0; i < picked.size(); i++) {
79
1.00k
    ASSERT_EQ(i, m.get(picked[i]));
80
1.00k
  }
81
1
}
82
83
// Regression test for a particular bad sequence of inserts
84
// that caused a crash on a previous implementation.
85
//
86
// The particular issue here is that we have many inserts
87
// which have the same key modulo the initial capacity.
88
1
TEST(TestIdMapping, TestBadSequence) {
89
1
  IdMapping m;
90
1
  m.set(0, 0);
91
1
  m.set(4, 0);
92
1
  m.set(128, 0); // 0 modulo 64 and 128
93
1
  m.set(129, 0); // 1
94
1
  m.set(130, 0); // 2
95
1
  m.set(131, 0); // 3
96
1
}
97
98
1
TEST(TestIdMapping, TestReinsert) {
99
1
  IdMapping m;
100
1
  m.set(0, 0);
101
1
  ASSERT_DEATH({
102
1
    m.set(0, 1);
103
1
  },
104
1
  "Cannot insert duplicate keys");
105
1
}
106
107
} // namespace yb