summaryrefslogtreecommitdiffstats
path: root/cc/base/random_access_list_container_unittest.cc
blob: c02ca784aef65921a87a46eef31bb8df98e084dc (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
104
105
106
107
108
109
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/base/random_access_list_container.h"

#include <stddef.h>

#include <algorithm>
#include <vector>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
namespace {

class Base {
 public:
  virtual ~Base() {}
  int get_value() const { return value_; }

 protected:
  explicit Base(int value) : value_(value) {}

  int value_;
};

const int kMagicNumberOne = 1;
const int kMagicNumberTwo = 2;
const int kMagicNumberThree = 3;

class Derived1 : public Base {
 public:
  Derived1() : Base(kMagicNumberOne) {}
};

class Derived2 : public Base {
 public:
  Derived2() : Base(kMagicNumberTwo) {}
};

class Derived3 : public Base {
 public:
  Derived3() : Base(kMagicNumberThree) {}
};

size_t LargestDerivedElementSize() {
  static_assert(sizeof(Derived1) >= sizeof(Derived2),
                "Derived2 is larger than Derived1");
  static_assert(sizeof(Derived1) >= sizeof(Derived3),
                "Derived3 is larger than Derived1");
  return sizeof(Derived1);
}

TEST(RandomAccessListContainerTest, RandomAccess) {
  RandomAccessListContainer<Base> list(LargestDerivedElementSize(), 1);

  list.AllocateAndConstruct<Derived1>();
  list.AllocateAndConstruct<Derived2>();
  list.AllocateAndConstruct<Derived3>();
  list.AllocateAndConstruct<Derived1>();
  list.AllocateAndConstruct<Derived2>();
  list.AllocateAndConstruct<Derived3>();

  EXPECT_EQ(kMagicNumberOne, list[0]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[1]->get_value());
  EXPECT_EQ(kMagicNumberThree, list[2]->get_value());
  EXPECT_EQ(kMagicNumberOne, list[3]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[4]->get_value());
  EXPECT_EQ(kMagicNumberThree, list[5]->get_value());

  list.RemoveLast();
  list.RemoveLast();
  list.RemoveLast();

  EXPECT_EQ(kMagicNumberOne, list[0]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[1]->get_value());
  EXPECT_EQ(kMagicNumberThree, list[2]->get_value());

  list.AllocateAndConstruct<Derived3>();
  list.AllocateAndConstruct<Derived2>();
  list.AllocateAndConstruct<Derived1>();

  EXPECT_EQ(kMagicNumberOne, list[0]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[1]->get_value());
  EXPECT_EQ(kMagicNumberThree, list[2]->get_value());
  EXPECT_EQ(kMagicNumberThree, list[3]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[4]->get_value());
  EXPECT_EQ(kMagicNumberOne, list[5]->get_value());
}

TEST(RandomAccessListContainerTest, Clear) {
  RandomAccessListContainer<Base> list(LargestDerivedElementSize(), 1);

  list.AllocateAndConstruct<Derived1>();
  list.AllocateAndConstruct<Derived2>();

  EXPECT_EQ(kMagicNumberOne, list[0]->get_value());
  EXPECT_EQ(kMagicNumberTwo, list[1]->get_value());

  list.clear();
  list.AllocateAndConstruct<Derived3>();

  EXPECT_EQ(kMagicNumberThree, list[0]->get_value());
}

}  // namespace
}  // namespace cc