9d946269051a39721f5ba78f1fe56f2401b13652
[profile/ivi/libvpx.git] / vp8 / encoder / boolcoder_test.cc
1 /*
2  *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3  *
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.
9  */
10
11 extern "C"
12 {
13 #include "vp8/encoder/boolhuff.h"
14 #include "vp8/decoder/dboolhuff.h"
15 }
16
17 #include <math.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24
25 #include "third_party/googletest/src/include/gtest/gtest.h"
26
27 typedef unsigned char uint8_t;
28
29 namespace
30 {
31 const int num_tests = 10;
32
33 class ACMRandom
34 {
35 public:
36     ACMRandom(int seed) { Reset(seed);}
37
38     void Reset(int seed) { srand(seed); }
39
40     uint8_t Rand8(void) { return (rand() >> 8) & 0xff; }
41
42     int PseudoUniform(int range) { return (rand() >> 8) % range; }
43
44     int operator()(int n) { return PseudoUniform(n); }
45
46     static int DeterministicSeed(void) { return 0xbaba; }
47 };
48 }
49
50 TEST(VP8, TestBitIO)
51 {
52     ACMRandom rnd(ACMRandom::DeterministicSeed());
53     for (int n = 0; n < num_tests; ++n)
54     {
55         for (int method = 0; method <= 7; ++method)
56         {   // we generate various proba
57             const int bits_to_test = 1000;
58
59             uint8_t probas[bits_to_test];
60
61             for (int i = 0; i < bits_to_test; ++i)
62             {
63                 const int parity = i & 1;
64                 probas[i] =
65                         (method == 0) ? 0 :
66                         (method == 1) ? 255 :
67                         (method == 2) ? 128 :
68                         (method == 3) ? rnd.Rand8() :
69                         (method == 4) ? (parity ? 0 : 255) :
70                         // alternate between low and high proba:
71                         (method == 5) ? (parity ? rnd(128) : 255 - rnd(128)) :
72                         (method == 6) ?
73                                 (parity ? rnd(64) : 255 - rnd(64)) :
74                                 (parity ? rnd(32) : 255 - rnd(32));
75             }
76             for (int bit_method = 0; bit_method <= 3; ++bit_method)
77             {
78                 const int random_seed = 6432;
79                 const int buffer_size = 10000;
80                 ACMRandom bit_rnd(random_seed);
81                 BOOL_CODER bw;
82                 uint8_t bw_buffer[buffer_size];
83                 vp8_start_encode(&bw, bw_buffer, bw_buffer + buffer_size);
84
85                 int bit = (bit_method == 0) ? 0 : (bit_method == 1) ? 1 : 0;
86                 for (int i = 0; i < bits_to_test; ++i)
87                 {
88                     if (bit_method == 2)
89                     {
90                         bit = (i & 1);
91                     }
92                     else if (bit_method == 3)
93                     {
94                         bit = bit_rnd(2);
95                     }
96                     vp8_encode_bool(&bw, bit, (int) probas[i]);
97                 }
98
99                 vp8_stop_encode(&bw);
100
101                 BOOL_DECODER br;
102                 vp8dx_start_decode(&br, bw_buffer, buffer_size);
103                 bit_rnd.Reset(random_seed);
104                 for (int i = 0; i < bits_to_test; ++i)
105                 {
106                     if (bit_method == 2)
107                     {
108                         bit = (i & 1);
109                     }
110                     else if (bit_method == 3)
111                     {
112                         bit = bit_rnd(2);
113                     }
114                     GTEST_ASSERT_EQ(vp8dx_decode_bool(&br, probas[i]), bit)
115                       << "pos: "
116                       << i << " / " << bits_to_test << " bit_method: "
117                       << bit_method << " method: " << method;
118                 }
119
120             }
121         }
122     }
123 }
124
125 int main(int argc, char **argv)
126 {
127     ::testing::InitGoogleTest(&argc, argv);
128     return RUN_ALL_TESTS();
129 }