Dynamic MPMCQueue: Eliminate cases of enqueue indefinite blocking and failure in...
[folly.git] / folly / test / ChecksumTest.cpp
index 4c5039a2198dc0285186b010eeab899f910a9d8e..16489dce970410fa9d54e974945f00671dede894 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <folly/Checksum.h>
 
+#include <boost/crc.hpp>
 
 #include <folly/Benchmark.h>
 #include <folly/Hash.h>
@@ -89,6 +90,17 @@ void testCRC32CContinuation(
   }
 }
 
+void testMatchesBoost32Type() {
+  for (auto expected : expectedResults) {
+    boost::crc_32_type result;
+    result.process_bytes(buffer + expected.offset, expected.length);
+    const uint32_t boostResult = result.checksum();
+    const uint32_t follyResult =
+        folly::crc32_type(buffer + expected.offset, expected.length);
+    EXPECT_EQ(follyResult, boostResult);
+  }
+}
+
 } // namespace
 
 TEST(Checksum, crc32c_software) {
@@ -109,6 +121,19 @@ TEST(Checksum, crc32c_hardware) {
   }
 }
 
+TEST(Checksum, crc32c_hardware_eq) {
+  if (folly::detail::crc32c_hw_supported()) {
+    for (int i = 0; i < 1000; i++) {
+      auto sw = folly::detail::crc32c_sw(buffer, i, 0);
+      auto hw = folly::detail::crc32c_hw(buffer, i, 0);
+      EXPECT_EQ(sw, hw);
+    }
+  } else {
+    LOG(WARNING) << "skipping hardware-accelerated CRC-32C tests"
+                 << " (not supported on this CPU)";
+  }
+}
+
 TEST(Checksum, crc32c_continuation_hardware) {
   if (folly::detail::crc32c_hw_supported()) {
     testCRC32CContinuation(folly::detail::crc32c_hw);
@@ -169,6 +194,11 @@ TEST(Checksum, crc32_continuation) {
   }
 }
 
+TEST(Checksum, crc32_type) {
+  // Test that crc32_type matches boost::crc_32_type
+  testMatchesBoost32Type();
+}
+
 void benchmarkHardwareCRC32C(unsigned long iters, size_t blockSize) {
   if (folly::detail::crc32c_hw_supported()) {
     uint32_t checksum;