Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work in one example #1

Open
gusevfe opened this issue Jun 20, 2024 · 0 comments
Open

Does not work in one example #1

gusevfe opened this issue Jun 20, 2024 · 0 comments
Assignees

Comments

@gusevfe
Copy link

gusevfe commented Jun 20, 2024

Hello.

I've found an example of data, when encode-decode round trip does not give the identical result. It consists of four bytes: 0x00, 0x00, 0x80, 0x40.

The patch to add this test case is:

diff --git a/tests/test.cpp b/tests/test.cpp
index 078a6d4..bc35c4f 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -52,6 +52,7 @@ static void encode_decode_uint8()
     const uint8_t literalszeros[] = { 0x55, 0x00 };
     const uint8_t literalsones[]  = { 0xAA, 0xFF };
     const uint8_t mixed[]         = { 0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x
FF, 0xAA, 0x00 };
+    const uint8_t weird[]         = { 0x00, 0x00, 0x80, 0x40 };

     assert_true( roundtrip( zeros, 0xFFu ) );
     assert_true( roundtrip( ones ) );
@@ -63,6 +64,7 @@ static void encode_decode_uint8()
     assert_true( roundtrip( literalszeros ) );
     assert_true( roundtrip( literalsones ) );
     assert_true( roundtrip( mixed ) );
+    assert_true( roundtrip( weird ) );
 }

 static void encode_decode_uint16()

The test program then produces this result:

> ./obj/test
check failed! (file tests/test.cpp, line 67): roundtrip( weird )
Total tests: 45, Tests failed: 1

It was possible to fix this with the following change:

diff --git a/src/brle.h b/src/brle.h
index 70f7b83..e4391ba 100644
--- a/src/brle.h
+++ b/src/brle.h
@@ -409,6 +409,10 @@ constexpr auto decode( InputIt input, InputIt last, OutputIt output ) -> OutputI
             bit_count = bit_count - data_bits;
             bits      = in >> ( detail::literal_size - bit_count );
         }
+
+        if( bit_count == 0) {
+            bits = 0;
+        }
     }

     return output;

But I am not completely sure if this is a correct approach.

@PG1003 PG1003 self-assigned this Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants