Fixes RCU test cases error (loads should use Consume ordering)
[folly.git] / folly / test / GroupVarintTest.cpp
index cdc693bca77e439d4e68ddc24eff6bab349a705e..be95a37f987bd3b4917957b9b98573c86962b451 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2012-present 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 <stdarg.h>
-#include "folly/GroupVarint.h"
+#include <folly/GroupVarint.h>
 
-#include <gtest/gtest.h>
+#include <algorithm>
+#include <cstdarg>
+
+// On platforms where it's not supported, GroupVarint will be compiled out.
+#if HAVE_GROUP_VARINT
+
+#include <folly/portability/GTest.h>
 
 using namespace folly;
 
@@ -35,8 +40,6 @@ class StringAppender {
 
 typedef GroupVarintEncoder<uint32_t, StringAppender> GroupVarint32Encoder;
 typedef GroupVarintEncoder<uint64_t, StringAppender> GroupVarint64Encoder;
-typedef GroupVarintDecoder<uint32_t> GroupVarint32Decoder;
-typedef GroupVarintDecoder<uint32_t> GroupVarint64Decoder;
 
 // Expected bytes follow, terminate with -1
 void testGroupVarint32(uint32_t a, uint32_t b, uint32_t c, uint32_t d, ...) {
@@ -53,7 +56,10 @@ void testGroupVarint32(uint32_t a, uint32_t b, uint32_t c, uint32_t d, ...) {
   EXPECT_EQ(expectedBytes.size(), size);
 
   std::vector<char> foundBytes;
-  foundBytes.resize(size + 4);
+
+  // ssse3 decoding requires that the source buffer have length >= 17,
+  // so that it can read 128 bits from &start[1] via _mm_loadu_si128.
+  foundBytes.resize(std::max<size_t>(size + 4, 17UL));
   char* start = &(foundBytes.front());
   char* p = GroupVarint32::encode(start, a, b, c, d);
   EXPECT_EQ((void*)(start + size), (void*)p);
@@ -113,7 +119,7 @@ void testGroupVarint64(uint64_t a, uint64_t b, uint64_t c, uint64_t d,
   EXPECT_EQ(e, fe);
 }
 
-}  // namespace
+} // namespace
 
 TEST(GroupVarint, GroupVarint32) {
   EXPECT_EQ(0, GroupVarint32::maxSize(0));
@@ -259,3 +265,4 @@ TEST(GroupVarint, GroupVarintDecoder) {
   }
 }
 
+#endif