1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125

media / parsers / vp8_bool_decoder_unittest.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/parsers/vp8_bool_decoder.h"

#include <stddef.h>
#include <stdint.h>

#include <limits>

#include "testing/gtest/include/gtest/gtest.h"

namespace media {

const static size_t NUM_BITS_TO_TEST = 100;

namespace {

// 100 zeros with probability of 0x80.
const uint8_t kDataZerosAndEvenProbabilities[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00};

// 100 ones with probability of 0x80.
const uint8_t kDataOnesAndEvenProbabilities[] = {
    0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xf0, 0x20};

// [0, 1, 0, 1, ..., 1] with probability [0, 1, 2, 3, ..., 99].
const uint8_t kDataParitiesAndIncreasingProbabilities[] = {
    0x00, 0x02, 0x08, 0x31, 0x8e, 0xca, 0xab, 0xe2, 0xc8, 0x31, 0x12,
    0xb3, 0x2c, 0x19, 0x90, 0xc6, 0x6a, 0xeb, 0x17, 0x52, 0x30};

}  // namespace

class Vp8BoolDecoderTest : public ::testing::Test {
 public:
  Vp8BoolDecoderTest() = default;

  Vp8BoolDecoderTest(const Vp8BoolDecoderTest&) = delete;
  Vp8BoolDecoderTest& operator=(const Vp8BoolDecoderTest&) = delete;

 protected:
  // Fixture member, the bool decoder to be tested.
  Vp8BoolDecoder bd_;
};

#define INITIALIZE(data) ASSERT_TRUE(bd_.Initialize(data, sizeof(data)));

TEST_F(Vp8BoolDecoderTest, DecodeBoolsWithZerosAndEvenProbabilities) {
  INITIALIZE(kDataZerosAndEvenProbabilities);
  ASSERT_EQ(0u, bd_.BitOffset());
  for (size_t i = 0; i < NUM_BITS_TO_TEST; ++i) {
    bool out = true;
    ASSERT_TRUE(bd_.ReadBool(&out, 0x80));
    ASSERT_FALSE(out);
    ASSERT_EQ(i, bd_.BitOffset());
  }
}

TEST_F(Vp8BoolDecoderTest, DecodeLiteralsWithZerosAndEvenProbabilities) {
  INITIALIZE(kDataZerosAndEvenProbabilities);

  int value = 1;
  ASSERT_TRUE(bd_.ReadLiteral(1, &value));
  ASSERT_EQ(0, value);

  value = 1;
  ASSERT_TRUE(bd_.ReadLiteral(32, &value));
  ASSERT_EQ(0, value);

  value = 1;
  ASSERT_TRUE(bd_.ReadLiteralWithSign(1, &value));
  ASSERT_EQ(0, value);

  value = 1;
  ASSERT_TRUE(bd_.ReadLiteralWithSign(31, &value));
  ASSERT_EQ(0, value);
}

TEST_F(Vp8BoolDecoderTest, DecodeBoolsWithOnesAndEvenProbabilities) {
  INITIALIZE(kDataOnesAndEvenProbabilities);

  ASSERT_EQ(0u, bd_.BitOffset());
  for (size_t i = 0; i < NUM_BITS_TO_TEST; ++i) {
    bool out = false;
    ASSERT_TRUE(bd_.ReadBool(&out, 0x80));
    ASSERT_TRUE(out);
    ASSERT_EQ(i + 1, bd_.BitOffset());
  }
}

TEST_F(Vp8BoolDecoderTest, DecodeLiteralsWithOnesAndEvenProbabilities) {
  INITIALIZE(kDataOnesAndEvenProbabilities);

  int value = 0;
  ASSERT_TRUE(bd_.ReadLiteral(1, &value));
  EXPECT_EQ(1, value);

  value = 0;
  ASSERT_TRUE(bd_.ReadLiteral(31, &value));
  EXPECT_EQ(0x7FFFFFFF, value);

  value = 0;
  ASSERT_TRUE(bd_.ReadLiteralWithSign(1, &value));
  EXPECT_EQ(-1, value);

  value = 0;
  ASSERT_TRUE(bd_.ReadLiteralWithSign(31, &value));
  EXPECT_EQ(-0x7FFFFFFF, value);
}

TEST_F(Vp8BoolDecoderTest, DecodeBoolsWithParitiesAndIncreasingProbabilities) {
  INITIALIZE(kDataParitiesAndIncreasingProbabilities);

  for (size_t i = 0; i < NUM_BITS_TO_TEST; ++i) {
    bool out = !(i & 1);
    ASSERT_LE(i, std::numeric_limits<uint8_t>::max());
    ASSERT_TRUE(bd_.ReadBool(&out, static_cast<uint8_t>(i)));
    EXPECT_EQ(out, !!(i & 1));
  }
}

}  // namespace media