summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/crc/crc_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'vespalib/src/tests/crc/crc_test.cpp')
-rw-r--r--vespalib/src/tests/crc/crc_test.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/vespalib/src/tests/crc/crc_test.cpp b/vespalib/src/tests/crc/crc_test.cpp
new file mode 100644
index 00000000000..8afeed487ee
--- /dev/null
+++ b/vespalib/src/tests/crc/crc_test.cpp
@@ -0,0 +1,78 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include <vespa/vespalib/testkit/testapp.h>
+#include <vespa/vespalib/util/crc.h>
+#include <boost/crc.hpp>
+#include <vector>
+
+using namespace vespalib;
+
+class Test : public TestApp
+{
+public:
+ int Main() override;
+ void testCorrectNess();
+ void testBenchmark(bool our, size_t bufSz, size_t numRep);
+};
+
+int
+Test::Main()
+{
+ TEST_INIT("crc_test");
+ testCorrectNess();
+ if (_argc >= 2) {
+ testBenchmark(false, 1024, 1000*1000);
+ } else {
+ testBenchmark(true, 1024, 1000*1000);
+ }
+ TEST_DONE();
+}
+
+void Test::testCorrectNess()
+{
+ const char *a[7] = { "", "a", "ab", "abc", "abcd", "abcde", "doc:crawler:http://www.ntnu.no/" };
+ for (size_t i(0); i < sizeof(a)/sizeof(a[0]); i++) {
+ uint32_t vespaCrc32 = crc_32_type::crc(a[i], strlen(a[i]));
+ boost::crc_32_type calculator;
+ calculator.process_bytes(a[i], strlen(a[i]));
+ EXPECT_EQUAL(vespaCrc32, calculator.checksum());
+ vespalib::crc_32_type calculator2;
+ calculator2.process_bytes(a[i], strlen(a[i]));
+ EXPECT_EQUAL(vespaCrc32, calculator2.checksum());
+ EXPECT_EQUAL(calculator.checksum(), calculator2.checksum());
+ }
+ vespalib::crc_32_type calculator2;
+ boost::crc_32_type calculator;
+ for (size_t i(0); i < sizeof(a)/sizeof(a[0]); i++) {
+ calculator.process_bytes(a[i], strlen(a[i]));
+ calculator2.process_bytes(a[i], strlen(a[i]));
+ EXPECT_EQUAL(calculator.checksum(), calculator2.checksum());
+ }
+ EXPECT_EQUAL(calculator.checksum(), calculator2.checksum());
+}
+
+void Test::testBenchmark(bool our, size_t bufSz, size_t numRep)
+{
+ std::vector<char> a(numRep+bufSz);
+ for(size_t i(0), m(a.size()); i < m; i++) {
+ a[i] = i&0xff;
+ }
+ uint32_t sum(0);
+ if (our) {
+ for (size_t i(0); i < (numRep); i++) {
+ //sum ^= crc_32_type::crc(&a[i], bufSz);
+ vespalib::crc_32_type calculator;
+ calculator.process_bytes(&a[i], bufSz);
+ sum ^=calculator.checksum();
+ }
+ } else {
+ for (size_t i(0); i < (numRep); i++) {
+ boost::crc_32_type calculator;
+ calculator.process_bytes(&a[i], bufSz);
+ sum ^=calculator.checksum();
+ }
+ }
+ printf("sum = %x\n", sum);
+}
+
+TEST_APPHOOK(Test)