/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/ProducerConsumerQueue.h"
+#include <folly/ProducerConsumerQueue.h>
-#include <gtest/gtest.h>
-#include <vector>
#include <atomic>
#include <chrono>
#include <memory>
#include <thread>
+#include <vector>
+
#include <glog/logging.h>
+#include <folly/portability/GTest.h>
+
//////////////////////////////////////////////////////////////////////
namespace {
-template<class T> struct TestTraits {
+template <class T> struct TestTraits {
T limit() const { return 1 << 24; }
T generate() const { return rand() % 26; }
};
-template<> struct TestTraits<std::string> {
- int limit() const { return 1 << 22; }
+template <> struct TestTraits<std::string> {
+ unsigned int limit() const { return 1 << 22; }
std::string generate() const { return std::string(12, ' '); }
};
-template<class QueueType, size_t Size, bool Pop = false>
+template <class QueueType, size_t Size, bool Pop = false>
struct PerfTest {
typedef typename QueueType::value_type T;
}
void producer() {
- for (int i = 0; i < traits_.limit(); ++i) {
+ // This is written differently than you might expect so that
+ // it does not run afoul of -Wsign-compare, regardless of the
+ // signedness of this loop's upper bound.
+ for (auto i = traits_.limit(); i > 0; --i) {
while (!queue_.write(traits_.generate())) {
}
}
TestTraits<T> traits_;
};
-template<class TestType> void doTest(const char* name) {
+template <class TestType> void doTest(const char* name) {
LOG(INFO) << " testing: " << name;
std::unique_ptr<TestType> const t(new TestType());
(*t)();
}
-template<class T, bool Pop = false>
+template <class T, bool Pop = false>
void perfTestType(const char* type) {
const size_t size = 0xfffe;
"ProducerConsumerQueue");
}
-template<class QueueType, size_t Size, bool Pop>
+template <class QueueType, size_t Size, bool Pop>
struct CorrectnessTest {
typedef typename QueueType::value_type T;
{
const size_t testSize = traits_.limit();
testData_.reserve(testSize);
- for (int i = 0; i < testSize; ++i) {
+ for (size_t i = 0; i < testSize; ++i) {
testData_.push_back(traits_.generate());
}
}
} else {
goto again;
}
+ EXPECT_EQ(*data, expect);
} else {
+ EXPECT_EQ(*data, expect);
queue_.popFront();
}
-
- EXPECT_EQ(*data, expect);
}
}
std::atomic<bool> done_;
};
-template<class T, bool Pop = false>
+template <class T, bool Pop = false>
void correctnessTestType(const std::string& type) {
LOG(INFO) << "Type: " << type;
doTest<CorrectnessTest<folly::ProducerConsumerQueue<T>,0xfffe,Pop> >(
}
struct DtorChecker {
- static int numInstances;
+ static unsigned int numInstances;
DtorChecker() { ++numInstances; }
- DtorChecker(const DtorChecker& o) { ++numInstances; }
+ DtorChecker(const DtorChecker& /* o */) { ++numInstances; }
~DtorChecker() { --numInstances; }
};
-int DtorChecker::numInstances = 0;
+unsigned int DtorChecker::numInstances = 0;
-}
+} // namespace
//////////////////////////////////////////////////////////////////////
}
EXPECT_EQ(DtorChecker::numInstances, 0);
}
+
+TEST(PCQ, EmptyFull) {
+ folly::ProducerConsumerQueue<int> queue(3);
+ EXPECT_TRUE(queue.isEmpty());
+ EXPECT_FALSE(queue.isFull());
+
+ EXPECT_TRUE(queue.write(1));
+ EXPECT_FALSE(queue.isEmpty());
+ EXPECT_FALSE(queue.isFull());
+
+ EXPECT_TRUE(queue.write(2));
+ EXPECT_FALSE(queue.isEmpty());
+ EXPECT_TRUE(queue.isFull()); // Tricky: full after 2 writes, not 3.
+
+ EXPECT_FALSE(queue.write(3));
+ EXPECT_EQ(queue.sizeGuess(), 2);
+}
+
+TEST(PCQ, Capacity) {
+ folly::ProducerConsumerQueue<int> queue(3);
+ EXPECT_EQ(queue.capacity(), 2); // PCQ max size is buffer size - 1.
+}