2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
12 #include "vp8/encoder/boolhuff.h"
13 #include "vp8/decoder/dboolhuff.h"
21 #include <sys/types.h>
23 #include "third_party/googletest/src/include/gtest/gtest.h"
24 #include "vpx/vpx_integer.h"
27 const int num_tests = 10;
31 explicit ACMRandom(int seed) { Reset(seed); }
33 void Reset(int seed) { srand(seed); }
35 uint8_t Rand8(void) { return (rand() >> 8) & 0xff; }
37 int PseudoUniform(int range) { return (rand() >> 8) % range; }
39 int operator()(int n) { return PseudoUniform(n); }
41 static int DeterministicSeed(void) { return 0xbaba; }
45 TEST(VP8, TestBitIO) {
46 ACMRandom rnd(ACMRandom::DeterministicSeed());
47 for (int n = 0; n < num_tests; ++n) {
48 for (int method = 0; method <= 7; ++method) { // we generate various proba
49 const int bits_to_test = 1000;
50 uint8_t probas[bits_to_test];
52 for (int i = 0; i < bits_to_test; ++i) {
53 const int parity = i & 1;
55 (method == 0) ? 0 : (method == 1) ? 255 :
57 (method == 3) ? rnd.Rand8() :
58 (method == 4) ? (parity ? 0 : 255) :
59 // alternate between low and high proba:
60 (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
62 (parity ? rnd(64) : 255 - rnd(64)) :
63 (parity ? rnd(32) : 255 - rnd(32));
65 for (int bit_method = 0; bit_method <= 3; ++bit_method) {
66 const int random_seed = 6432;
67 const int buffer_size = 10000;
68 ACMRandom bit_rnd(random_seed);
70 uint8_t bw_buffer[buffer_size];
71 vp8_start_encode(&bw, bw_buffer, bw_buffer + buffer_size);
73 int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
74 for (int i = 0; i < bits_to_test; ++i) {
75 if (bit_method == 2) {
77 } else if (bit_method == 3) {
80 vp8_encode_bool(&bw, bit, static_cast<int>(probas[i]));
86 vp8dx_start_decode(&br, bw_buffer, buffer_size);
87 bit_rnd.Reset(random_seed);
88 for (int i = 0; i < bits_to_test; ++i) {
89 if (bit_method == 2) {
91 } else if (bit_method == 3) {
94 GTEST_ASSERT_EQ(vp8dx_decode_bool(&br, probas[i]), bit)
95 << "pos: "<< i << " / " << bits_to_test
96 << " bit_method: " << bit_method
97 << " method: " << method;