folly: avoid compile warning/failure due to lvalue-to-rvalue conversion
[folly.git] / folly / test / PaddedTest.cpp
index f5fde4845bd89d2b0ed807ee785dc784690de2f4..7cbcb7296c8880f727450f6741e66a89732e34fe 100644 (file)
@@ -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.
  * limitations under the License.
  */
 
-#include "folly/Padded.h"
+#include <folly/Padded.h>
 
 #include <glog/logging.h>
-#include <gtest/gtest.h>
+
+#include <folly/portability/GTest.h>
 
 using namespace folly;
-namespace ps = ::folly::padded;
 
 TEST(NodeTest, Padding) {
-  typedef ps::Node<int32_t, 64> IntNode;
+  typedef padded::Node<int32_t, 64> IntNode;
   EXPECT_EQ(16, IntNode::kElementCount);
   EXPECT_EQ(0, IntNode::kPaddingBytes);
   EXPECT_EQ(alignof(int32_t), alignof(IntNode));
@@ -50,7 +50,7 @@ TEST(NodeTest, Padding) {
     char c[7];
   };
   EXPECT_EQ(1, alignof(SevenBytes));
-  typedef ps::Node<SevenBytes, 64> SevenByteNode;
+  typedef padded::Node<SevenBytes, 64> SevenByteNode;
   EXPECT_EQ(9, SevenByteNode::kElementCount);  // 64 / 7
   EXPECT_EQ(1, SevenByteNode::kPaddingBytes);  // 64 % 7
   EXPECT_EQ(1, alignof(SevenByteNode));
@@ -77,7 +77,7 @@ TEST(NodeTest, Padding) {
 
 class IntPaddedTestBase : public ::testing::Test {
  protected:
-  typedef ps::Node<uint32_t, 64> IntNode;
+  typedef padded::Node<uint32_t, 64> IntNode;
   typedef std::vector<IntNode> IntNodeVec;
   IntNodeVec v_;
   int n_;
@@ -85,11 +85,11 @@ class IntPaddedTestBase : public ::testing::Test {
 
 class IntPaddedConstTest : public IntPaddedTestBase {
  protected:
-  void SetUp() {
+  void SetUp() override {
     v_.resize(4);
     n_ = 0;
     for (int i = 0; i < 4; i++) {
-      for (int j = 0; j < IntNode::kElementCount; ++j, ++n_) {
+      for (size_t j = 0; j < IntNode::kElementCount; ++j, ++n_) {
         v_[i].data()[j] = n_;
       }
     }
@@ -98,16 +98,16 @@ class IntPaddedConstTest : public IntPaddedTestBase {
 
 TEST_F(IntPaddedConstTest, Iteration) {
   int k = 0;
-  for (auto it = ps::cbegin(v_); it != ps::cend(v_); ++it, ++k) {
+  for (auto it = padded::cbegin(v_); it != padded::cend(v_); ++it, ++k) {
     EXPECT_EQ(k, *it);
   }
   EXPECT_EQ(n_, k);
 }
 
 TEST_F(IntPaddedConstTest, Arithmetic) {
-  EXPECT_EQ(64, ps::cend(v_) - ps::cbegin(v_));
+  EXPECT_EQ(64, padded::cend(v_) - padded::cbegin(v_));
   // Play around block boundaries
-  auto it = ps::cbegin(v_);
+  auto it = padded::cbegin(v_);
   EXPECT_EQ(0, *it);
   {
     auto i2 = it;
@@ -154,14 +154,14 @@ TEST_F(IntPaddedNonConstTest, Iteration) {
   n_ = 64;
 
   int k = 0;
-  for (auto it = ps::begin(v_); it != ps::end(v_); ++it, ++k) {
+  for (auto it = padded::begin(v_); it != padded::end(v_); ++it, ++k) {
     *it = k;
   }
   EXPECT_EQ(n_, k);
 
   k = 0;
   for (int i = 0; i < 4; i++) {
-    for (int j = 0; j < IntNode::kElementCount; ++j, ++k) {
+    for (size_t j = 0; j < IntNode::kElementCount; ++j, ++k) {
       EXPECT_EQ(k, v_[i].data()[j]);
     }
   }
@@ -174,7 +174,7 @@ class StructPaddedTestBase : public ::testing::Test {
     uint8_t y;
     uint8_t z;
   };
-  typedef ps::Node<Point, 64> PointNode;
+  typedef padded::Node<Point, 64> PointNode;
   typedef std::vector<PointNode> PointNodeVec;
   PointNodeVec v_;
   int n_;
@@ -182,11 +182,11 @@ class StructPaddedTestBase : public ::testing::Test {
 
 class StructPaddedConstTest : public StructPaddedTestBase {
  protected:
-  void SetUp() {
+  void SetUp() override {
     v_.resize(4);
     n_ = 0;
     for (int i = 0; i < 4; i++) {
-      for (int j = 0; j < PointNode::kElementCount; ++j, ++n_) {
+      for (size_t j = 0; j < PointNode::kElementCount; ++j, ++n_) {
         auto& point = v_[i].data()[j];
         point.x = n_;
         point.y = n_ + 1;
@@ -198,7 +198,7 @@ class StructPaddedConstTest : public StructPaddedTestBase {
 
 TEST_F(StructPaddedConstTest, Iteration) {
   int k = 0;
-  for (auto it = ps::cbegin(v_); it != ps::cend(v_); ++it, ++k) {
+  for (auto it = padded::cbegin(v_); it != padded::cend(v_); ++it, ++k) {
     EXPECT_EQ(k, it->x);
     EXPECT_EQ(k + 1, it->y);
     EXPECT_EQ(k + 2, it->z);
@@ -208,7 +208,7 @@ TEST_F(StructPaddedConstTest, Iteration) {
 
 class IntAdaptorTest : public IntPaddedConstTest {
  protected:
-  typedef ps::Adaptor<IntNodeVec> IntAdaptor;
+  typedef padded::Adaptor<IntNodeVec> IntAdaptor;
   IntAdaptor a_;
 };
 
@@ -232,3 +232,32 @@ TEST_F(IntAdaptorTest, Simple) {
   EXPECT_EQ(16, p.second);
   EXPECT_TRUE(v_ == p.first);
 }
+
+TEST_F(IntAdaptorTest, ResizeConstructor) {
+  IntAdaptor a(n_, 42);
+  EXPECT_EQ(n_, a.size());
+  for (int i = 0; i < n_; ++i) {
+    EXPECT_EQ(42, a[i]);
+  }
+}
+
+TEST_F(IntAdaptorTest, SimpleEmplaceBack) {
+  for (int i = 0; i < n_; ++i) {
+    EXPECT_EQ((i == 0), a_.empty());
+    EXPECT_EQ(i, a_.size());
+    a_.emplace_back(i);
+  }
+  EXPECT_EQ(n_, a_.size());
+
+  int k = 0;
+  for (auto it = a_.begin(); it != a_.end(); ++it, ++k) {
+    EXPECT_EQ(k, a_[k]);
+    EXPECT_EQ(k, *it);
+  }
+  EXPECT_EQ(n_, k);
+
+  auto p = a_.move();
+  EXPECT_TRUE(a_.empty());
+  EXPECT_EQ(16, p.second);
+  EXPECT_TRUE(v_ == p.first);
+}