diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespalib/src/tests/compress |
Publish
Diffstat (limited to 'vespalib/src/tests/compress')
-rw-r--r-- | vespalib/src/tests/compress/.gitignore | 1 | ||||
-rw-r--r-- | vespalib/src/tests/compress/CMakeLists.txt | 8 | ||||
-rw-r--r-- | vespalib/src/tests/compress/DESC | 1 | ||||
-rw-r--r-- | vespalib/src/tests/compress/FILES | 1 | ||||
-rw-r--r-- | vespalib/src/tests/compress/compress_test.cpp | 141 |
5 files changed, 152 insertions, 0 deletions
diff --git a/vespalib/src/tests/compress/.gitignore b/vespalib/src/tests/compress/.gitignore new file mode 100644 index 00000000000..439cf005e2f --- /dev/null +++ b/vespalib/src/tests/compress/.gitignore @@ -0,0 +1 @@ +vespalib_compress_test_app diff --git a/vespalib/src/tests/compress/CMakeLists.txt b/vespalib/src/tests/compress/CMakeLists.txt new file mode 100644 index 00000000000..eb6c403f1d9 --- /dev/null +++ b/vespalib/src/tests/compress/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_compress_test_app + SOURCES + compress_test.cpp + DEPENDS + vespalib +) +vespa_add_test(NAME vespalib_compress_test_app COMMAND vespalib_compress_test_app) diff --git a/vespalib/src/tests/compress/DESC b/vespalib/src/tests/compress/DESC new file mode 100644 index 00000000000..f7e01ae9c4a --- /dev/null +++ b/vespalib/src/tests/compress/DESC @@ -0,0 +1 @@ +commpress test. Take a look at commpress.cpp for details. diff --git a/vespalib/src/tests/compress/FILES b/vespalib/src/tests/compress/FILES new file mode 100644 index 00000000000..be7a6df2fee --- /dev/null +++ b/vespalib/src/tests/compress/FILES @@ -0,0 +1 @@ +compress_test.cpp diff --git a/vespalib/src/tests/compress/compress_test.cpp b/vespalib/src/tests/compress/compress_test.cpp new file mode 100644 index 00000000000..7cc458e2290 --- /dev/null +++ b/vespalib/src/tests/compress/compress_test.cpp @@ -0,0 +1,141 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include <vespa/fastos/fastos.h> +#include <vespa/log/log.h> +LOG_SETUP("compress_test"); +#include <vespa/vespalib/testkit/testapp.h> +#include <vespa/vespalib/util/compress.h> +#include <vespa/vespalib/util/exceptions.h> + +using namespace vespalib; +using compress::Integer; + +class CompressTest : public TestApp +{ +private: + void verifyPositiveNumber(uint64_t n, const uint8_t * expected, size_t sz); + void verifyNumber(int64_t n, const uint8_t * expected, size_t sz); + void requireThatPositiveNumberCompressCorrectly(); + void requireThatNumberCompressCorrectly(); +public: + int Main(); +}; + +void +CompressTest::verifyPositiveNumber(uint64_t n, const uint8_t * expected, size_t sz) { + uint8_t buf[8]; + EXPECT_EQUAL(sz, Integer::compressPositive(n, buf)); + EXPECT_EQUAL(sz, Integer::compressedPositiveLength(n)); + for (size_t i(0); i < sz; i++) { + EXPECT_EQUAL(expected[i], buf[i]); + } + uint64_t v(0); + EXPECT_EQUAL(sz, Integer::decompressPositive(v, expected)); + EXPECT_EQUAL(n, v); +} + +void +CompressTest::verifyNumber(int64_t n, const uint8_t * expected, size_t sz) { + uint8_t buf[8]; + EXPECT_EQUAL(sz, Integer::compress(n, buf)); + EXPECT_EQUAL(sz, Integer::compressedLength(n)); + for (size_t i(0); i < sz; i++) { + EXPECT_EQUAL(expected[i], buf[i]); + } + int64_t v(0); + EXPECT_EQUAL(sz, Integer::decompress(v, expected)); + EXPECT_EQUAL(n, v); +} + +#define VERIFY_POSITIVE(n, p) verifyPositiveNumber(n, p, sizeof(p)) +void +CompressTest::requireThatPositiveNumberCompressCorrectly() +{ + const uint8_t zero[1] = {0}; + VERIFY_POSITIVE(0, zero); + const uint8_t one[1] = {0x01}; + VERIFY_POSITIVE(1, one); + const uint8_t x3f[1] = {0x3f}; + VERIFY_POSITIVE(0x3f, x3f); + const uint8_t x40[2] = {0x80,0x40}; + VERIFY_POSITIVE(0x40, x40); + const uint8_t x3fff[2] = {0xbf, 0xff}; + VERIFY_POSITIVE(0x3fff, x3fff); + const uint8_t x4000[4] = {0xc0, 0x00, 0x40, 0x00}; + VERIFY_POSITIVE(0x4000, x4000); + const uint8_t x3fffffff[4] = {0xff, 0xff, 0xff, 0xff}; + VERIFY_POSITIVE(0x3fffffff, x3fffffff); + const uint8_t x40000000[4] = {0,0,0,0}; + try { + VERIFY_POSITIVE(0x40000000, x40000000); + EXPECT_TRUE(false); + } catch (const IllegalArgumentException & e) { + EXPECT_EQUAL("Number '1073741824' too big, must extend encoding", e.getMessage()); + } + try { + VERIFY_POSITIVE(-1, x40000000); + EXPECT_TRUE(false); + } catch (const IllegalArgumentException & e) { + EXPECT_EQUAL("Number '18446744073709551615' too big, must extend encoding", e.getMessage()); + } +} + +#define VERIFY_NUMBER(n, p) verifyNumber(n, p, sizeof(p)) +void +CompressTest::requireThatNumberCompressCorrectly() +{ + const uint8_t zero[1] = {0}; + VERIFY_NUMBER(0, zero); + const uint8_t one[1] = {0x01}; + VERIFY_NUMBER(1, one); + const uint8_t x1f[1] = {0x1f}; + VERIFY_NUMBER(0x1f, x1f); + const uint8_t x20[2] = {0x40,0x20}; + VERIFY_NUMBER(0x20, x20); + const uint8_t x1fff[2] = {0x5f, 0xff}; + VERIFY_NUMBER(0x1fff, x1fff); + const uint8_t x2000[4] = {0x60, 0x00, 0x20, 0x00}; + VERIFY_NUMBER(0x2000, x2000); + const uint8_t x1fffffff[4] = {0x7f, 0xff, 0xff, 0xff}; + VERIFY_NUMBER(0x1fffffff, x1fffffff); + const uint8_t x20000000[4] = {0,0,0,0}; + try { + VERIFY_NUMBER(0x20000000, x20000000); + EXPECT_TRUE(false); + } catch (const IllegalArgumentException & e) { + EXPECT_EQUAL("Number '536870912' too big, must extend encoding", e.getMessage()); + } + const uint8_t mzero[1] = {0x81}; + VERIFY_NUMBER(-1, mzero); + const uint8_t mone[1] = {0x82}; + VERIFY_NUMBER(-2, mone); + const uint8_t mx1f[1] = {0x9f}; + VERIFY_NUMBER(-0x1f, mx1f); + const uint8_t mx20[2] = {0xc0,0x20}; + VERIFY_NUMBER(-0x20, mx20); + const uint8_t mx1fff[2] = {0xdf, 0xff}; + VERIFY_NUMBER(-0x1fff, mx1fff); + const uint8_t mx2000[4] = {0xe0, 0x00, 0x20, 0x00}; + VERIFY_NUMBER(-0x2000, mx2000); + const uint8_t mx1fffffff[4] = {0xff, 0xff, 0xff, 0xff}; + VERIFY_NUMBER(-0x1fffffff, mx1fffffff); + const uint8_t mx20000000[4] = {0,0,0,0}; + try { + VERIFY_NUMBER(-0x20000000, mx20000000); + EXPECT_TRUE(false); + } catch (const IllegalArgumentException & e) { + EXPECT_EQUAL("Number '-536870912' too big, must extend encoding", e.getMessage()); + } +} + +int +CompressTest::Main() +{ + TEST_INIT("compress_test"); + + requireThatPositiveNumberCompressCorrectly(); + requireThatNumberCompressCorrectly(); + + TEST_DONE(); +} + +TEST_APPHOOK(CompressTest) |