File: FixedSizeQueueTest.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (114 lines) | stat: -rw-r--r-- 2,593 bytes parent folder | download | duplicates (2)
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
110
111
112
113
114
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <gtest/gtest.h>

#include "Common/FixedSizeQueue.h"

TEST(FixedSizeQueue, Simple)
{
  Common::FixedSizeQueue<int, 5> q;

  EXPECT_EQ(0u, q.size());

  q.push(0);
  q.push(1);
  q.push(2);
  q.push(3);
  q.push(4);
  for (int i = 0; i < 1000; ++i)
  {
    EXPECT_EQ(i, q.front());
    EXPECT_EQ(i, q.pop_front());
    q.push(i + 5);
  }
  EXPECT_EQ(1000, q.pop_front());
  EXPECT_EQ(1001, q.pop_front());
  EXPECT_EQ(1002, q.pop_front());
  EXPECT_EQ(1003, q.pop_front());
  EXPECT_EQ(1004, q.pop_front());

  EXPECT_EQ(0u, q.size());
}

TEST(FixedSizeQueue, RingBuffer)
{
  // Testing if queue works when used as a ring buffer
  Common::FixedSizeQueue<int, 5> q;

  EXPECT_EQ(0u, q.size());

  q.push(0);
  q.push(1);
  q.push(2);
  q.push(3);
  q.push(4);
  q.push(5);

  EXPECT_EQ(5u, q.size());
  EXPECT_EQ(1, q.pop_front());

  EXPECT_EQ(4u, q.size());
}

// Local classes cannot have static fields,
// therefore this has to be declared in global scope.
class NonTrivialTypeTestData
{
public:
  static inline int num_objects = 0;
  static inline int total_constructed = 0;
  static inline int default_constructed = 0;
  static inline int total_destructed = 0;

  NonTrivialTypeTestData()
  {
    num_objects++;
    total_constructed++;
    default_constructed++;
  }

  NonTrivialTypeTestData(int /*val*/)
  {
    num_objects++;
    total_constructed++;
  }

  ~NonTrivialTypeTestData()
  {
    num_objects--;
    total_destructed++;
  }
};

TEST(FixedSizeQueue, NonTrivialTypes)
{
  // Testing if construction/destruction of non-trivial types happens as expected
  Common::FixedSizeQueue<NonTrivialTypeTestData, 2> q;

  EXPECT_EQ(0u, q.size());

  EXPECT_EQ(2, NonTrivialTypeTestData::num_objects);
  EXPECT_EQ(2, NonTrivialTypeTestData::total_constructed);
  EXPECT_EQ(2, NonTrivialTypeTestData::default_constructed);
  EXPECT_EQ(0, NonTrivialTypeTestData::total_destructed);

  q.emplace(4);
  q.emplace(6);
  q.emplace(8);

  EXPECT_EQ(2, NonTrivialTypeTestData::num_objects);
  EXPECT_EQ(2 + 3, NonTrivialTypeTestData::total_constructed);
  EXPECT_EQ(2, NonTrivialTypeTestData::default_constructed);
  EXPECT_EQ(3, NonTrivialTypeTestData::total_destructed);
  EXPECT_EQ(2u, q.size());

  q.pop();
  q.pop();

  EXPECT_EQ(2, NonTrivialTypeTestData::num_objects);
  EXPECT_EQ(2 + 3 + 2, NonTrivialTypeTestData::total_constructed);
  EXPECT_EQ(2 + 2, NonTrivialTypeTestData::default_constructed);
  EXPECT_EQ(3 + 2, NonTrivialTypeTestData::total_destructed);
  EXPECT_EQ(0u, q.size());
}