| 1 |
/* |
| 2 |
libheif bitstream unit tests |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2024 Brad Hards <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "catch_amalgamated.hpp" |
| 28 |
#include "error.h" |
| 29 |
#include <cstdint> |
| 30 |
#include <iostream> |
| 31 |
#include <memory> |
| 32 |
#include <bitstream.h> |
| 33 |
|
| 34 |
|
| 35 |
TEST_CASE("read bits") { |
| 36 |
std::vector<uint8_t> byteArray{0x7f, 0xf1, 0b01000001, 0b10000111, 0b10001111}; |
| 37 |
BitReader uut(byteArray.data(), (int)byteArray.size()); |
| 38 |
uint32_t byte0 = uut.get_bits(8); |
| 39 |
REQUIRE(byte0 == 0x7f); |
| 40 |
uint32_t byte1_high = uut.get_bits(4); |
| 41 |
REQUIRE(byte1_high == 0x0f); |
| 42 |
uint32_t byte1_low = uut.get_bits(4); |
| 43 |
REQUIRE(byte1_low == 0x01); |
| 44 |
uint32_t byte2_partial1 = uut.get_bits(3); |
| 45 |
REQUIRE(byte2_partial1 == 0x02); |
| 46 |
uint32_t byte2_partial2 = uut.get_bits(3); |
| 47 |
REQUIRE(byte2_partial2 == 0x00); |
| 48 |
uint32_t byte2_3_overlap = uut.get_bits(11); |
| 49 |
REQUIRE(byte2_3_overlap == 0b0000001100001111); |
| 50 |
} |
| 51 |
|
| 52 |
TEST_CASE("read uint8") { |
| 53 |
std::vector<uint8_t> byteArray{0x7f, 0xf1, 0b01000001, 0b10000111, 0b10001111}; |
| 54 |
BitReader uut(byteArray.data(), (int)byteArray.size()); |
| 55 |
uint8_t byte0 = uut.get_bits8(8); |
| 56 |
REQUIRE(byte0 == 0x7f); |
| 57 |
uint8_t byte1_high = uut.get_bits8(4); |
| 58 |
REQUIRE(byte1_high == 0x0f); |
| 59 |
uint8_t byte1_low = uut.get_bits8(4); |
| 60 |
REQUIRE(byte1_low == 0x01); |
| 61 |
uint8_t byte2_partial1 = uut.get_bits8(3); |
| 62 |
REQUIRE(byte2_partial1 == 0x02); |
| 63 |
uint8_t byte2_partial2 = uut.get_bits8(3); |
| 64 |
REQUIRE(byte2_partial2 == 0x00); |
| 65 |
uint8_t byte2_3_overlap = uut.get_bits8(8); |
| 66 |
REQUIRE((int)byte2_3_overlap == 0b1100001); |
| 67 |
} |
| 68 |
|
| 69 |
TEST_CASE("read uint32") { |
| 70 |
std::vector<uint8_t> byteArray{0x7f, 0b11110001, 0b01000001, 0b10000111, 0b10001111}; |
| 71 |
BitReader uut(byteArray.data(), (int)byteArray.size()); |
| 72 |
uint32_t byte0 = uut.get_bits32(8); |
| 73 |
REQUIRE(byte0 == 0x7f); |
| 74 |
uint32_t byte1_high = uut.get_bits(1); |
| 75 |
REQUIRE(byte1_high == 0x01); |
| 76 |
uint32_t overlap = uut.get_bits32(30); |
| 77 |
REQUIRE(overlap == 0b111000101000001100001111000111); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_CASE("read float") { |
| 81 |
std::vector<uint8_t> byteArray{0x40, 0x00, 0x00, 0x00}; |
| 82 |
std::shared_ptr<StreamReader_memory> stream = std::make_shared<StreamReader_memory>(byteArray.data(), (int)byteArray.size(), false); |
| 83 |
BitstreamRange uut(stream, byteArray.size(), nullptr); |
| 84 |
float f = uut.read_float32(); |
| 85 |
REQUIRE(f == 2.0); |
| 86 |
} |