tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bitwriter_buffer_test.cc (2873B)


      1 /*
      2 * Copyright (c) 2025, Alliance for Open Media. All rights reserved.
      3 *
      4 * This source code is subject to the terms of the BSD 2 Clause License and
      5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
      6 * was not distributed with this source code in the LICENSE file, you can
      7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
      8 * Media Patent License 1.0 was not distributed with this source code in the
      9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
     10 */
     11 
     12 #include <cstdint>
     13 
     14 #include "gtest/gtest.h"
     15 
     16 #include "aom_dsp/bitwriter_buffer.h"
     17 
     18 namespace {
     19 
     20 // Test the examples in Table 25 in ITU-T H.274 (V3) (09/2023) and a few more.
     21 //
     22 // Bit string    codeNum
     23 //     1            0
     24 //    010           1
     25 //    011           2
     26 //   00100          3
     27 //   00101          4
     28 //   00110          5
     29 //   00111          6
     30 //  0001000         7
     31 //  0001001         8
     32 //  0001010         9
     33 //  0001011        10
     34 //  0001100        11
     35 //  0001101        12
     36 //  0001110        13
     37 //  0001111        14
     38 TEST(BitwriterBufferTest, UvlcOneByte) {
     39  static constexpr struct {
     40    uint32_t bit_offset;
     41    uint8_t byte;
     42  } kExpected[] = {
     43    { 1, 0x80 },  // 0
     44    { 3, 0x40 },  // 1
     45    { 3, 0x60 },  // 2
     46    { 5, 0x20 },  // 3
     47    { 5, 0x28 },  // 4
     48    { 5, 0x30 },  // 5
     49    { 5, 0x38 },  // 6
     50    { 7, 0x10 },  // 7
     51    { 7, 0x12 },  // 8
     52    { 7, 0x14 },  // 9
     53    { 7, 0x16 },  // 10
     54    { 7, 0x18 },  // 11
     55    { 7, 0x1a },  // 12
     56    { 7, 0x1c },  // 13
     57    { 7, 0x1e },  // 14
     58  };
     59  uint8_t dst[1];
     60 
     61  for (int i = 0; i < 15; i++) {
     62    struct aom_write_bit_buffer wb = { dst, 0 };
     63    aom_wb_write_uvlc(&wb, i);
     64    ASSERT_EQ(wb.bit_offset, kExpected[i].bit_offset);
     65    EXPECT_EQ(wb.bit_buffer[0], kExpected[i].byte);
     66  }
     67 }
     68 
     69 // Tests two values with the maximum number (31) of leading zero bits.
     70 TEST(BitwriterBufferTest, Uvlc31LeadingZeros) {
     71  uint8_t dst[8];
     72 
     73  // 2^31 - 1
     74  {
     75    struct aom_write_bit_buffer wb = { dst, 0 };
     76    aom_wb_write_uvlc(&wb, 0x7fffffff);
     77    ASSERT_EQ(wb.bit_offset, 63u);
     78    EXPECT_EQ(wb.bit_buffer[0], 0x00);
     79    EXPECT_EQ(wb.bit_buffer[1], 0x00);
     80    EXPECT_EQ(wb.bit_buffer[2], 0x00);
     81    EXPECT_EQ(wb.bit_buffer[3], 0x01);
     82    EXPECT_EQ(wb.bit_buffer[4], 0x00);
     83    EXPECT_EQ(wb.bit_buffer[5], 0x00);
     84    EXPECT_EQ(wb.bit_buffer[6], 0x00);
     85    EXPECT_EQ(wb.bit_buffer[7], 0x00);
     86  }
     87 
     88  // 2^32 - 2
     89  {
     90    struct aom_write_bit_buffer wb = { dst, 0 };
     91    aom_wb_write_uvlc(&wb, 0xfffffffe);
     92    ASSERT_EQ(wb.bit_offset, 63u);
     93    EXPECT_EQ(wb.bit_buffer[0], 0x00);
     94    EXPECT_EQ(wb.bit_buffer[1], 0x00);
     95    EXPECT_EQ(wb.bit_buffer[2], 0x00);
     96    EXPECT_EQ(wb.bit_buffer[3], 0x01);
     97    EXPECT_EQ(wb.bit_buffer[4], 0xff);
     98    EXPECT_EQ(wb.bit_buffer[5], 0xff);
     99    EXPECT_EQ(wb.bit_buffer[6], 0xff);
    100    EXPECT_EQ(wb.bit_buffer[7], 0xfe);
    101  }
    102 }
    103 
    104 }  // namespace