X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Ftest%2FProducerConsumerQueueTest.cpp;h=2bc8ae981e85159504d1725cf5bd0ebe68c576c0;hp=bf28dcb41c56115386d041f13e370581aa8a4d3c;hb=7fc3f9174951941df7a7a32acf320185716c26a6;hpb=c51f93205176b9bb3792a1c8bedcf5228ce669ac diff --git a/folly/test/ProducerConsumerQueueTest.cpp b/folly/test/ProducerConsumerQueueTest.cpp index bf28dcb4..2bc8ae98 100644 --- a/folly/test/ProducerConsumerQueueTest.cpp +++ b/folly/test/ProducerConsumerQueueTest.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -14,31 +14,33 @@ * limitations under the License. */ -#include "folly/ProducerConsumerQueue.h" +#include -#include -#include #include #include #include #include +#include + #include +#include + ////////////////////////////////////////////////////////////////////// namespace { -template struct TestTraits { +template struct TestTraits { T limit() const { return 1 << 24; } T generate() const { return rand() % 26; } }; -template<> struct TestTraits { - int limit() const { return 1 << 22; } +template <> struct TestTraits { + unsigned int limit() const { return 1 << 22; } std::string generate() const { return std::string(12, ' '); } }; -template +template struct PerfTest { typedef typename QueueType::value_type T; @@ -61,7 +63,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())) { } } @@ -87,13 +92,13 @@ struct PerfTest { TestTraits traits_; }; -template void doTest(const char* name) { +template void doTest(const char* name) { LOG(INFO) << " testing: " << name; std::unique_ptr const t(new TestType()); (*t)(); } -template +template void perfTestType(const char* type) { const size_t size = 0xfffe; @@ -102,7 +107,7 @@ void perfTestType(const char* type) { "ProducerConsumerQueue"); } -template +template struct CorrectnessTest { typedef typename QueueType::value_type T; @@ -112,7 +117,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()); } } @@ -157,11 +162,11 @@ struct CorrectnessTest { } else { goto again; } + EXPECT_EQ(*data, expect); } else { + EXPECT_EQ(*data, expect); queue_.popFront(); } - - EXPECT_EQ(*data, expect); } } @@ -192,7 +197,7 @@ struct CorrectnessTest { std::atomic done_; }; -template +template void correctnessTestType(const std::string& type) { LOG(INFO) << "Type: " << type; doTest,0xfffe,Pop> >( @@ -200,15 +205,15 @@ void correctnessTestType(const std::string& type) { } 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 ////////////////////////////////////////////////////////////////////// @@ -266,3 +271,25 @@ TEST(PCQ, Destructor) { } EXPECT_EQ(DtorChecker::numInstances, 0); } + +TEST(PCQ, EmptyFull) { + folly::ProducerConsumerQueue 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 queue(3); + EXPECT_EQ(queue.capacity(), 2); // PCQ max size is buffer size - 1. +}