Read/write compressed signed integers.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/ce89b9c4 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/ce89b9c4 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/ce89b9c4 Branch: refs/heads/master Commit: ce89b9c40ea86b45c181852a12edeaa7bea2d054 Parents: 3e3c648 Author: Marvin Humphrey <[email protected]> Authored: Mon Apr 18 15:43:38 2016 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Mon Apr 18 15:43:38 2016 -0700 ---------------------------------------------------------------------- core/Lucy/Store/InStream.c | 10 ++++ core/Lucy/Store/InStream.cfh | 11 ++++ core/Lucy/Store/OutStream.c | 10 ++++ core/Lucy/Store/OutStream.cfh | 10 ++++ core/Lucy/Test/Store/TestIOPrimitives.c | 82 +++++++++++++++++++++++++++- 5 files changed, 122 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/ce89b9c4/core/Lucy/Store/InStream.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/InStream.c b/core/Lucy/Store/InStream.c index 09da76f..d04652d 100644 --- a/core/Lucy/Store/InStream.c +++ b/core/Lucy/Store/InStream.c @@ -477,6 +477,11 @@ InStream_Read_C32_IMP(InStream *self) { return SI_read_cu32(self); } +int32_t +InStream_Read_CI32_IMP(InStream *self) { + return (int32_t)SI_read_cu32(self); +} + uint32_t InStream_Read_CU32_IMP(InStream *self) { return SI_read_cu32(self); @@ -501,6 +506,11 @@ InStream_Read_C64_IMP(InStream *self) { return SI_read_cu64(self); } +int64_t +InStream_Read_CI64_IMP(InStream *self) { + return (int64_t)SI_read_cu64(self); +} + uint64_t InStream_Read_CU64_IMP(InStream *self) { return SI_read_cu64(self); http://git-wip-us.apache.org/repos/asf/lucy/blob/ce89b9c4/core/Lucy/Store/InStream.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/InStream.cfh b/core/Lucy/Store/InStream.cfh index 5fd5dab..921be77 100644 --- a/core/Lucy/Store/InStream.cfh +++ b/core/Lucy/Store/InStream.cfh @@ -176,6 +176,11 @@ class Lucy::Store::InStream inherits Clownfish::Obj { uint32_t Read_C32(InStream *self); + /** Read in a compressed 32-bit signed integer. + */ + int32_t + Read_CI32(InStream *self); + /** Read in a compressed 32-bit unsigned integer. */ uint32_t @@ -187,6 +192,12 @@ class Lucy::Store::InStream inherits Clownfish::Obj { final uint64_t Read_C64(InStream *self); + /** Read a 64-bit signed integer, using the same encoding as a CI32 but + * occupying as many as 10 bytes. + */ + final int64_t + Read_CI64(InStream *self); + /** Read a 64-bit unsigned integer, using the same encoding as a CU32 but * occupying as many as 10 bytes. */ http://git-wip-us.apache.org/repos/asf/lucy/blob/ce89b9c4/core/Lucy/Store/OutStream.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/OutStream.c b/core/Lucy/Store/OutStream.c index 8839791..3f1d941 100644 --- a/core/Lucy/Store/OutStream.c +++ b/core/Lucy/Store/OutStream.c @@ -295,6 +295,11 @@ OutStream_Write_C32_IMP(OutStream *self, uint32_t value) { } void +OutStream_Write_CI32_IMP(OutStream *self, int32_t value) { + SI_write_cu32(self, OutStream_IVARS(self), (uint32_t)value); +} + +void OutStream_Write_CU32_IMP(OutStream *self, uint32_t value) { SI_write_cu32(self, OutStream_IVARS(self), value); } @@ -323,6 +328,11 @@ OutStream_Write_C64_IMP(OutStream *self, uint64_t value) { } void +OutStream_Write_CI64_IMP(OutStream *self, int64_t value) { + SI_write_cu64(self, OutStream_IVARS(self), (uint64_t)value); +} + +void OutStream_Write_CU64_IMP(OutStream *self, uint64_t value) { SI_write_cu64(self, OutStream_IVARS(self), value); } http://git-wip-us.apache.org/repos/asf/lucy/blob/ce89b9c4/core/Lucy/Store/OutStream.cfh ---------------------------------------------------------------------- diff --git a/core/Lucy/Store/OutStream.cfh b/core/Lucy/Store/OutStream.cfh index 3726101..cb67003 100644 --- a/core/Lucy/Store/OutStream.cfh +++ b/core/Lucy/Store/OutStream.cfh @@ -118,6 +118,11 @@ class Lucy::Store::OutStream inherits Clownfish::Obj { final void Write_C32(OutStream *self, uint32_t value); + /** Write a signed 32-bit integer using a compressed format. + */ + final void + Write_CI32(OutStream *self, int32_t value); + /** Write an unsigned 32-bit integer using a compressed format. */ final void @@ -128,6 +133,11 @@ class Lucy::Store::OutStream inherits Clownfish::Obj { final void Write_C64(OutStream *self, uint64_t value); + /** Write a signed 64-bit integer using a compressed format. + */ + final void + Write_CI64(OutStream *self, int64_t value); + /** Write an unsigned 64-bit integer using a compressed format. */ final void http://git-wip-us.apache.org/repos/asf/lucy/blob/ce89b9c4/core/Lucy/Test/Store/TestIOPrimitives.c ---------------------------------------------------------------------- diff --git a/core/Lucy/Test/Store/TestIOPrimitives.c b/core/Lucy/Test/Store/TestIOPrimitives.c index 3c09dde..8d0f76f 100644 --- a/core/Lucy/Test/Store/TestIOPrimitives.c +++ b/core/Lucy/Test/Store/TestIOPrimitives.c @@ -242,6 +242,45 @@ test_u64(TestBatchRunner *runner) { } static void +test_ci32(TestBatchRunner *runner) { + int64_t *ints = TestUtils_random_i64s(NULL, 1000, INT32_MIN, INT32_MAX); + RAMFile *file = RAMFile_new(NULL, false); + OutStream *outstream = OutStream_open((Obj*)file); + InStream *instream; + size_t i; + + // Test boundaries. + ints[0] = 0; + ints[1] = 1; + ints[2] = -1; + ints[3] = INT32_MAX; + ints[4] = INT32_MIN; + + for (i = 0; i < 1000; i++) { + OutStream_Write_CI32(outstream, (int32_t)ints[i]); + } + OutStream_Close(outstream); + + instream = InStream_open((Obj*)file); + for (i = 0; i < 1000; i++) { + int32_t got = InStream_Read_CI32(instream); + if ((int64_t)got != ints[i]) { + FAIL(runner, "ci32 round trip failed: %" PRId32 ", %" PRId64, + got, ints[i]); + break; + } + } + if (i == 1000) { + PASS(runner, "ci32 round trip"); + } + + DECREF(instream); + DECREF(outstream); + DECREF(file); + FREEMEM(ints); +} + +static void test_cu32(TestBatchRunner *runner) { uint64_t *ints = TestUtils_random_u64s(NULL, 1000, 0, UINT32_MAX); RAMFile *file = RAMFile_new(NULL, false); @@ -280,6 +319,45 @@ test_cu32(TestBatchRunner *runner) { } static void +test_ci64(TestBatchRunner *runner) { + int64_t *ints = TestUtils_random_i64s(NULL, 1000, INT64_MIN, INT64_MAX); + RAMFile *file = RAMFile_new(NULL, false); + OutStream *outstream = OutStream_open((Obj*)file); + InStream *instream; + uint32_t i; + + // Test boundaries. + ints[0] = 0; + ints[1] = 1; + ints[2] = -1; + ints[3] = INT64_MAX; + ints[4] = INT64_MIN; + + for (i = 0; i < 1000; i++) { + OutStream_Write_CI64(outstream, ints[i]); + } + OutStream_Close(outstream); + + instream = InStream_open((Obj*)file); + for (i = 0; i < 1000; i++) { + int64_t got = InStream_Read_CI64(instream); + if (got != ints[i]) { + FAIL(runner, "ci64 round trip failed: %" PRId64 ", %" PRId64, + got, ints[i]); + break; + } + } + if (i == 1000) { + PASS(runner, "ci64 round trip"); + } + + DECREF(instream); + DECREF(outstream); + DECREF(file); + FREEMEM(ints); +} + +static void test_cu64(TestBatchRunner *runner) { uint64_t *ints = TestUtils_random_u64s(NULL, 1000, 0, UINT64_MAX); RAMFile *file = RAMFile_new(NULL, false); @@ -421,7 +499,7 @@ test_f64(TestBatchRunner *runner) { void TestIOPrimitives_Run_IMP(TestIOPrimitives *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 11); + TestBatchRunner_Plan(runner, (TestBatch*)self, 13); srand((unsigned int)time((time_t*)NULL)); test_i8(runner); test_u8(runner); @@ -429,7 +507,9 @@ TestIOPrimitives_Run_IMP(TestIOPrimitives *self, TestBatchRunner *runner) { test_u32(runner); test_i64(runner); test_u64(runner); + test_ci32(runner); test_cu32(runner); + test_ci64(runner); test_cu64(runner); test_f32(runner); test_f64(runner);
