locked-queue-unittest.cc 1.92 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/utils/locked-queue-inl.h"
6 7 8 9
#include "testing/gtest/include/gtest/gtest.h"

namespace {

10
using Record = int;
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

}  // namespace

namespace v8 {
namespace internal {

TEST(LockedQueue, ConstructorEmpty) {
  LockedQueue<Record> queue;
  EXPECT_TRUE(queue.IsEmpty());
}

TEST(LockedQueue, SingleRecordEnqueueDequeue) {
  LockedQueue<Record> queue;
  EXPECT_TRUE(queue.IsEmpty());
  queue.Enqueue(1);
  EXPECT_FALSE(queue.IsEmpty());
  Record a = -1;
  bool success = queue.Dequeue(&a);
  EXPECT_TRUE(success);
  EXPECT_EQ(a, 1);
  EXPECT_TRUE(queue.IsEmpty());
}

TEST(LockedQueue, Peek) {
  LockedQueue<Record> queue;
  EXPECT_TRUE(queue.IsEmpty());
  queue.Enqueue(1);
  EXPECT_FALSE(queue.IsEmpty());
  Record a = -1;
  bool success = queue.Peek(&a);
  EXPECT_TRUE(success);
  EXPECT_EQ(a, 1);
  EXPECT_FALSE(queue.IsEmpty());
  success = queue.Dequeue(&a);
  EXPECT_TRUE(success);
  EXPECT_EQ(a, 1);
  EXPECT_TRUE(queue.IsEmpty());
}

TEST(LockedQueue, PeekOnEmpty) {
  LockedQueue<Record> queue;
  EXPECT_TRUE(queue.IsEmpty());
  Record a = -1;
  bool success = queue.Peek(&a);
  EXPECT_FALSE(success);
}

TEST(LockedQueue, MultipleRecords) {
  LockedQueue<Record> queue;
  EXPECT_TRUE(queue.IsEmpty());
  queue.Enqueue(1);
  EXPECT_FALSE(queue.IsEmpty());
  for (int i = 2; i <= 5; ++i) {
    queue.Enqueue(i);
    EXPECT_FALSE(queue.IsEmpty());
  }
  Record rec = 0;
  for (int i = 1; i <= 4; ++i) {
    EXPECT_FALSE(queue.IsEmpty());
    queue.Dequeue(&rec);
    EXPECT_EQ(i, rec);
  }
  for (int i = 6; i <= 12; ++i) {
    queue.Enqueue(i);
    EXPECT_FALSE(queue.IsEmpty());
  }
  for (int i = 5; i <= 12; ++i) {
    EXPECT_FALSE(queue.IsEmpty());
    queue.Dequeue(&rec);
    EXPECT_EQ(i, rec);
  }
  EXPECT_TRUE(queue.IsEmpty());
}

}  // namespace internal
}  // namespace v8