Copyright 2014->2015
[folly.git] / folly / test / ProducerConsumerQueueTest.cpp
index bf28dcb41c56115386d041f13e370581aa8a4d3c..6ade4a1abe5670d1c55163759938c05a985dcff3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "folly/ProducerConsumerQueue.h"
+#include <folly/ProducerConsumerQueue.h>
 
 #include <gtest/gtest.h>
 #include <vector>
@@ -34,7 +34,7 @@ template<class T> struct TestTraits {
 };
 
 template<> struct TestTraits<std::string> {
-  int limit() const { return 1 << 22; }
+  unsigned int limit() const { return 1 << 22; }
   std::string generate() const { return std::string(12, ' '); }
 };
 
@@ -61,7 +61,10 @@ struct PerfTest {
   }
 
   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())) {
       }
     }
@@ -112,7 +115,7 @@ struct CorrectnessTest {
   {
     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());
     }
   }
@@ -200,13 +203,13 @@ void correctnessTestType(const std::string& type) {
 }
 
 struct DtorChecker {
-  static int numInstances;
+  static unsigned int numInstances;
   DtorChecker() { ++numInstances; }
   DtorChecker(const DtorChecker& o) { ++numInstances; }
   ~DtorChecker() { --numInstances; }
 };
 
-int DtorChecker::numInstances = 0;
+unsigned int DtorChecker::numInstances = 0;
 
 }
 
@@ -266,3 +269,20 @@ TEST(PCQ, Destructor) {
   }
   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);
+}