Fix -Wsign-compare
[folly.git] / folly / test / small_vector_test.cpp
index 18cb57bf4783fe21a2157f216aa0a30e111c5d82..0e014a2c2aef4930a3d9ed1cb30a4d8acac60a69 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 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/small_vector.h"
+#include <folly/small_vector.h>
 
 #include <gtest/gtest.h>
 #include <string>
 
 #include <boost/algorithm/string.hpp>
 
-#include "folly/Conv.h"
+#include <folly/Conv.h>
 
 using folly::small_vector;
 using namespace folly::small_vector_policy;
 
-#if defined(__x86_64__)
+#if FOLLY_X64
 
 static_assert(sizeof(small_vector<int>) == 16,
               "Object size is not what we expect for small_vector<int>");
@@ -50,19 +50,14 @@ static_assert(sizeof(small_vector<int32_t,1,uint8_t>) ==
                 8 + 1,
               "small_vector<int32_t,1,uint32_t> is wrong size");
 
-static_assert(sizeof(small_vector<int32_t,1,OneBitMutex>) == 16,
-              "OneBitMutex took more space than expected");
-
 static_assert(sizeof(small_vector<int16_t,4,uint16_t>) == 10,
               "Sizeof unexpectedly large");
-static_assert(sizeof(small_vector<int16_t,4,uint16_t,OneBitMutex>) == 10,
-              "Sizeof unexpectedly large");
-static_assert(sizeof(small_vector<int16_t,4,NoHeap,uint16_t,
-                                  OneBitMutex>) == 10,
-              "Sizeof unexpectedly large");
 
 #endif
 
+static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(std::unique_ptr<int>),
+              "std::unique_ptr<> is trivially copyable");
+
 namespace {
 
 struct NontrivialType {
@@ -84,8 +79,8 @@ struct NontrivialType {
 
   int32_t a;
 };
-static_assert(!boost::has_trivial_copy<NontrivialType>::value,
-              "NontrivialType isn't trivially copyable");
+static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NontrivialType),
+              "NontrivialType is trivially copyable");
 
 int NontrivialType::ctored = 0;
 
@@ -141,13 +136,16 @@ struct NoncopyableCounter {
   ~NoncopyableCounter() {
     --alive;
   }
-  NoncopyableCounter(NoncopyableCounter&&) { ++alive; }
+  NoncopyableCounter(NoncopyableCounter&&) noexcept { ++alive; }
   NoncopyableCounter(NoncopyableCounter const&) = delete;
   NoncopyableCounter& operator=(NoncopyableCounter const&) const = delete;
   NoncopyableCounter& operator=(NoncopyableCounter&&) { return *this; }
 };
 int NoncopyableCounter::alive = 0;
 
+static_assert(!FOLLY_IS_TRIVIALLY_COPYABLE(NoncopyableCounter),
+              "NoncopyableCounter is trivially copyable");
+
 // Check that throws don't break the basic guarantee for some cases.
 // Uses the method for testing exception safety described at
 // http://www.boost.org/community/exception_safety.html, to force all
@@ -286,7 +284,7 @@ TEST(small_vector, Insert) {
   folly::small_vector<int> someVec(3, 3);
   someVec.insert(someVec.begin(), 12, 12);
   EXPECT_EQ(someVec.size(), 15);
-  for (int i = 0; i < someVec.size(); ++i) {
+  for (size_t i = 0; i < someVec.size(); ++i) {
     if (i < 12) {
       EXPECT_EQ(someVec[i], 12);
     } else {
@@ -426,7 +424,7 @@ TEST(small_vector, GrowShrinkGrow) {
   auto capacity = vec.capacity();
 
   auto oldSize = vec.size();
-  for (int i = 0; i < oldSize; ++i) {
+  for (size_t i = 0; i < oldSize; ++i) {
     vec.erase(vec.begin() + (std::rand() % vec.size()));
     EXPECT_EQ(vec.capacity(), capacity);
   }
@@ -472,9 +470,10 @@ TEST(small_vector, Iteration) {
 }
 
 TEST(small_vector, NonCopyableType) {
-  folly::small_vector<std::unique_ptr<std::string>,2> vec;
+  folly::small_vector<NontrivialType,2> vec;
+
   for (int i = 0; i < 10; ++i) {
-    vec.emplace(vec.begin(), new std::string("asd"));
+    vec.emplace(vec.begin(), 13);
   }
   EXPECT_EQ(vec.size(), 10);
   auto vec2 = std::move(vec);
@@ -522,7 +521,7 @@ TEST(small_vector, NoHeap) {
     std::size_t,folly::small_vector_policy::NoHeap> Vector;
 
   Vector v;
-  EXPECT_EQ(v.max_size(), 10);
+  static_assert(v.max_size() == 10, "max_size is incorrect");
 
   for (int i = 0; i < 10; ++i) {
     v.push_back(folly::to<std::string>(i));
@@ -538,10 +537,6 @@ TEST(small_vector, NoHeap) {
   EXPECT_TRUE(caught);
 
   // Check max_size works right with various policy combinations.
-  folly::small_vector<std::string,32,uint32_t,NoHeap,OneBitMutex> v2;
-  EXPECT_EQ(v2.max_size(), 32);
-  folly::small_vector<std::string,32,uint32_t,OneBitMutex> v3;
-  EXPECT_EQ(v3.max_size(), (1ul << 30) - 1);
   folly::small_vector<std::string,32,uint32_t> v4;
   EXPECT_EQ(v4.max_size(), (1ul << 31) - 1);
 
@@ -551,7 +546,8 @@ TEST(small_vector, NoHeap) {
    * pointer.
    */
   folly::small_vector<char,1,NoHeap> notsosmall;
-  EXPECT_EQ(notsosmall.max_size(), sizeof(char*));
+  static_assert(notsosmall.max_size() == sizeof(char*),
+                "max_size is incorrect");
   caught = false;
   try {
     notsosmall.push_back(12);
@@ -568,8 +564,6 @@ TEST(small_vector, MaxSize) {
   EXPECT_EQ(vec.max_size(), 127);
   folly::small_vector<int,2,uint16_t> vec2;
   EXPECT_EQ(vec2.max_size(), (1 << 15) - 1);
-  folly::small_vector<int,2,uint16_t,OneBitMutex> vec3;
-  EXPECT_EQ(vec3.max_size(), (1 << 14) - 1);
 }
 
 TEST(small_vector, AllHeap) {
@@ -594,18 +588,10 @@ TEST(small_vector, AllHeap) {
 
 TEST(small_vector, Basic) {
   typedef folly::small_vector<int,3,uint32_t
-#ifdef __x86_64__
-    ,OneBitMutex
-#endif
   > Vector;
 
   Vector a;
 
-#ifdef __x86_64__
-  a.lock();
-  a.unlock();
-#endif
-
   a.push_back(12);
   EXPECT_EQ(a.front(), 12);
   EXPECT_EQ(a.size(), 1);
@@ -672,9 +658,6 @@ TEST(small_vector, Capacity) {
   EXPECT_EQ(vec2.size(), 3);
   EXPECT_GT(vec2.capacity(), 2);
 
-  // Both have grown by the minimum amount
-  EXPECT_EQ(vec.capacity(), vec2.capacity());
-
   // Test capacity heapifying logic
   folly::small_vector<unsigned char, 1> vec3;
   const size_t hc_size = 1000000;