2017
[folly.git] / folly / test / stl_tests / StlVectorTest.cpp
index 7a999564f31deca6d1e681b57c6659a820c5b1f6..5d733255726b0475cafa17f6232f4af808119826 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 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.
@@ -25,9 +25,6 @@ GCC 4.7 is required.
 
 */
 
-// only compile if GCC is at least 4.7
-#if __GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 7
-
 #if 0
 #define USING_STD_VECTOR
 #endif
@@ -165,7 +162,7 @@ THOUGHTS:
 #include <vector>
 #define VECTOR_ std::vector
 #else
-#include "folly/FBVector.h"
+#include <folly/FBVector.h>
 #define VECTOR_ folly::fbvector
 #endif
 
@@ -184,12 +181,13 @@ THOUGHTS:
 #include <cstddef>
 #include <iomanip>
 
-#include "folly/ScopeGuard.h"
-#include "folly/Conv.h"
-#include <boost/preprocessor.hpp>
 #include <boost/iterator/iterator_adaptor.hpp>
-#include <gflags/gflags.h>
-#include <gtest/gtest.h>
+#include <boost/preprocessor.hpp>
+
+#include <folly/Conv.h>
+#include <folly/ScopeGuard.h>
+#include <folly/portability/GFlags.h>
+#include <folly/portability/GTest.h>
 
 using namespace std;
 using namespace folly;
@@ -335,11 +333,11 @@ template <> struct D4<true> {
 };
 
 template <Flags f>
-struct Delete : D0<f & DC_DELETE>
-              , D1<f & CC_DELETE>
-              , D2<f & MC_DELETE>
-              , D3<f & CA_DELETE>
-              , D4<f & MA_DELETE> {
+struct Delete : D0<(f & DC_DELETE) != 0>
+              , D1<(f & CC_DELETE) != 0>
+              , D2<(f & MC_DELETE) != 0>
+              , D3<(f & CA_DELETE) != 0>
+              , D4<(f & MA_DELETE) != 0> {
   Delete() = default;
   Delete(const Delete&) = default;
   Delete(Delete&&) = default;
@@ -347,11 +345,11 @@ struct Delete : D0<f & DC_DELETE>
   Delete& operator=(Delete&&) = default;
 
   explicit Delete(std::nullptr_t)
-      : D0<f & DC_DELETE>(nullptr)
-      , D1<f & CC_DELETE>(nullptr)
-      , D2<f & MC_DELETE>(nullptr)
-      , D3<f & CA_DELETE>(nullptr)
-      , D4<f & MA_DELETE>(nullptr)
+      : D0<(f & DC_DELETE) != 0>(nullptr)
+      , D1<(f & CC_DELETE) != 0>(nullptr)
+      , D2<(f & MC_DELETE) != 0>(nullptr)
+      , D3<(f & CA_DELETE) != 0>(nullptr)
+      , D4<(f & MA_DELETE) != 0>(nullptr)
       {}
 };
 
@@ -381,20 +379,20 @@ struct DataTicker : Ticker {
   DataTicker() noexcept(f & DC_NOEXCEPT) {
     if (!(f & DC_NOEXCEPT)) Tick("Data()");
   }
-  DataTicker(const DataTicker&) noexcept(f & CC_NOEXCEPT) {
+  DataTicker(const DataTicker&) noexcept((f & CC_NOEXCEPT) != 0) {
     if (!(f & CC_NOEXCEPT)) Tick("Data(const Data&)");
   }
-  DataTicker(DataTicker&&) noexcept(f & MC_NOEXCEPT) {
+  DataTicker(DataTicker&&) noexcept((f & MC_NOEXCEPT) != 0) {
     if (!(f & MC_NOEXCEPT)) Tick("Data(Data&&)");
   }
-  explicit DataTicker(std::nullptr_t) noexcept(f & OC_NOEXCEPT) {
+  explicit DataTicker(std::nullptr_t) noexcept((f & OC_NOEXCEPT) != 0) {
     if (!(f & OC_NOEXCEPT)) Tick("Data(int)");
   }
   ~DataTicker() noexcept {}
-  void operator=(const DataTicker&) noexcept(f & CA_NOEXCEPT) {
+  void operator=(const DataTicker&) noexcept((f & CA_NOEXCEPT) != 0) {
     if (!(f & CA_NOEXCEPT)) Tick("op=(const Data&)");
   }
-  void operator=(DataTicker&&) noexcept(f & MA_NOEXCEPT) {
+  void operator=(DataTicker&&) noexcept((f & MA_NOEXCEPT) != 0) {
     if (!(f & MA_NOEXCEPT)) Tick("op=(Data&&)");
   }
 };
@@ -515,7 +513,7 @@ bool Tracker::Print = false;
 // Data
 
 template <Flags f = 0, size_t pad = 0>
-struct Data : DataTracker<f & IS_RELOCATABLE>,
+struct Data : DataTracker<(f & IS_RELOCATABLE) != 0>,
               Counter, DataTicker<f>, Delete<f> {
   static const Flags flags = f;
   char spacehog[pad ? pad : 1];
@@ -524,7 +522,7 @@ struct Data : DataTracker<f & IS_RELOCATABLE>,
   Data(const Data&) = default;
   Data(Data&&) = default;
   /* implicit */ Data(int i)
-    : DataTracker<f & IS_RELOCATABLE>(i), Counter()
+    : DataTracker<(f & IS_RELOCATABLE) != 0>(i), Counter()
     , DataTicker<f>(nullptr)
     , Delete<f>(nullptr)
   {}
@@ -540,7 +538,7 @@ namespace folly {
 template <Flags f, size_t pad>
 struct IsRelocatable<Data<f, pad>>
   : std::integral_constant<bool,
-      f & IS_RELOCATABLE
+      (f & IS_RELOCATABLE) != 0
     > {};
 };
 
@@ -552,19 +550,19 @@ template <typename T>
 struct isPropCopy : true_type {};
 template <Flags f, size_t pad>
 struct isPropCopy<Data<f, pad>> :
-  std::integral_constant<bool, f & PROP_COPY> {};
+  std::integral_constant<bool, (f & PROP_COPY) != 0> {};
 
 template <typename T>
 struct isPropMove : true_type {};
 template <Flags f, size_t pad>
 struct isPropMove<Data<f, pad>> :
-  std::integral_constant<bool, f & PROP_MOVE> {};
+  std::integral_constant<bool, (f & PROP_MOVE) != 0> {};
 
 template <typename T>
 struct isPropSwap : true_type {};
 template <Flags f, size_t pad>
 struct isPropSwap<Data<f, pad>> :
-  std::integral_constant<bool, f & PROP_SWAP> {};
+  std::integral_constant<bool, (f & PROP_SWAP) != 0> {};
 
 
 struct AllocTracker {
@@ -582,6 +580,7 @@ template <class T>
 struct Alloc : AllocTracker, Ticker {
   typedef typename std::allocator<T>::pointer pointer;
   typedef typename std::allocator<T>::const_pointer const_pointer;
+  typedef typename std::allocator<T>::difference_type difference_type;
   typedef typename std::allocator<T>::size_type size_type;
   typedef typename std::allocator<T>::value_type value_type;
 
@@ -590,7 +589,7 @@ struct Alloc : AllocTracker, Ticker {
 
   std::allocator<T> a;
   int id;
-  explicit Alloc(int i = 8) : a(a), id(i) {}
+  explicit Alloc(int i = 8) : a(), id(i) {}
   Alloc(const Alloc& o) : a(o.a), id(o.id) {}
   Alloc(Alloc&& o) : a(move(o.a)), id(o.id) {}
   Alloc& operator=(const Alloc&) = default;
@@ -766,6 +765,9 @@ struct special_move_assignable<Data<f, pad>>
 // Timing
 
 uint64_t ReadTSC() {
+#ifdef _MSC_VER
+   return __rdtsc();
+#else
    unsigned reslo, reshi;
 
     __asm__ __volatile__  (
@@ -779,6 +781,7 @@ uint64_t ReadTSC() {
      ::: "%eax", "%ebx", "%ecx", "%edx");
 
    return ((uint64_t)reshi << 32) | reslo;
+#endif
 }
 
 //-----------------------------------------------------------------------------
@@ -794,8 +797,8 @@ uint64_t ReadTSC() {
   e25, e26, e27, e28, e29, e30, e31, e32, e33, e34, e35, e36, e37, e38, e39,  \
   e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, e50, e51, e52, e53, e54,  \
   e55, e56, e57, e58, e59, e60, e61, e62, e63, size, ...) size
-#define IBOOST_PP_VARIADIC_TO_SEQ(args...) \
-  BOOST_PP_TUPLE_TO_SEQ(IBOOST_PP_VARIADIC_SIZE(args), (args))
+#define IBOOST_PP_VARIADIC_TO_SEQ(...) \
+  BOOST_PP_TUPLE_TO_SEQ(IBOOST_PP_VARIADIC_SIZE(__VA_ARGS__), (__VA_ARGS__))
 
 //-----------------------------------------------------------------------------
 // STL_TEST
@@ -822,7 +825,7 @@ uint64_t ReadTSC() {
 #define TYPIFY(r, d, name) BOOST_PP_CAT(TYPIFY_, name)
 #define ARGIFY(r, d, name) TYPIFY(r, d, name) name
 
-#define MAKE_TEST(ref, name, types, restriction, argseq, rawargs...)     \
+#define MAKE_TEST(ref, name, types, restriction, argseq, ...)            \
   template <class Vector> void test_ ## name ## 2 (std::false_type) {}   \
   template <class Vector> void test_ ## name ## 2 (std::true_type) {     \
     BOOST_PP_SEQ_FOR_EACH(GEN_LOOPER, _, argseq)                         \
@@ -830,7 +833,7 @@ uint64_t ReadTSC() {
     BOOST_PP_SEQ_FOR_EACH(GEN_VMAKER, _, argseq)                         \
     {                                                                    \
     test_ ## name <Vector, typename Vector::value_type,                  \
-      typename Vector::allocator_type> ( rawargs );                      \
+      typename Vector::allocator_type> ( __VA_ARGS__ );                  \
     if (::testing::Test::HasFatalFailure()) return;                      \
     }                                                                    \
     BOOST_PP_SEQ_FOR_EACH(GEN_UMAKER, _, BOOST_PP_SEQ_REVERSE(argseq))   \
@@ -867,19 +870,19 @@ uint64_t ReadTSC() {
     if (!one) FAIL() << "No tests qualified to run";                     \
   }
 
-#define DECL(name, args...)                                                   \
+#define DECL(name, ...)                                                       \
   template <class Vector, typename T, typename Allocator>                     \
   void test_ ## name (BOOST_PP_SEQ_ENUM(BOOST_PP_SEQ_TRANSFORM(               \
-    ARGIFY, _, IBOOST_PP_VARIADIC_TO_SEQ(args))))
+    ARGIFY, _, IBOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))))
 
-#define STL_TEST_I(ref, name, restriction, args...)                           \
-  DECL(name, args);                                                           \
+#define STL_TEST_I(ref, name, restriction, ...)                               \
+  DECL(name, __VA_ARGS__);                                                    \
   MAKE_TEST(ref, name, TEST_TYPES, restriction,                               \
-    IBOOST_PP_VARIADIC_TO_SEQ(args), args)                                    \
-  DECL(name, args)
+    IBOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__), __VA_ARGS__)                      \
+  DECL(name, __VA_ARGS__)
 
-#define STL_TEST(ref, name, restriction, args...) \
-  STL_TEST_I(ref, name, restriction, z, ## args, ticks)
+#define STL_TEST(ref, name, restriction, ...) \
+  STL_TEST_I(ref, name, restriction, z, ## __VA_ARGS__, ticks)
 
 //-----------------------------------------------------------------------------
 // Test Types
@@ -2722,25 +2725,7 @@ STL_TEST("attach", attach, is_destructible, a) {
 
 int main(int argc, char** argv) {
   testing::InitGoogleTest(&argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
+  gflags::ParseCommandLineFlags(&argc, &argv, true);
 
   return RUN_ALL_TESTS();
 }
-
-#else // GCC 4.7 guard
-
-#include <gflags/gflags.h>
-#include <gtest/gtest.h>
-
-// do nothing
-TEST(placeholder, gccversion) {}
-
-int main(int argc, char** argv) {
-  testing::InitGoogleTest(&argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
-
-  return RUN_ALL_TESTS();
-}
-
-#endif // GCC 4.7 guard
-