1 /*
2  * Copyright (c) 2016, 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 <math.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
17 
18 #include "test/acm_random.h"
19 #include "aom/aom_integer.h"
20 #include "aom_dsp/bitreader.h"
21 #include "aom_dsp/bitwriter.h"
22 
23 using libaom_test::ACMRandom;
24 
TEST(AV1,TestAccounting)25 TEST(AV1, TestAccounting) {
26   const int kBufferSize = 10000;
27   const int kSymbols = 1024;
28   aom_writer bw;
29   uint8_t bw_buffer[kBufferSize];
30   aom_start_encode(&bw, bw_buffer);
31   for (int i = 0; i < kSymbols; i++) {
32     aom_write(&bw, 0, 32);
33     aom_write(&bw, 0, 32);
34     aom_write(&bw, 0, 32);
35   }
36   aom_stop_encode(&bw);
37   aom_reader br;
38   aom_reader_init(&br, bw_buffer, bw.pos);
39 
40   Accounting accounting;
41   aom_accounting_init(&accounting);
42   br.accounting = &accounting;
43   for (int i = 0; i < kSymbols; i++) {
44     aom_read(&br, 32, "A");
45   }
46   // Consecutive symbols that are the same are coalesced.
47   GTEST_ASSERT_EQ(accounting.syms.num_syms, 1);
48   GTEST_ASSERT_EQ(accounting.syms.syms[0].samples, (unsigned int)kSymbols);
49 
50   aom_accounting_reset(&accounting);
51   GTEST_ASSERT_EQ(accounting.syms.num_syms, 0);
52 
53   // Should record 2 * kSymbols accounting symbols.
54   aom_reader_init(&br, bw_buffer, bw.pos);
55   br.accounting = &accounting;
56   for (int i = 0; i < kSymbols; i++) {
57     aom_read(&br, 32, "A");
58     aom_read(&br, 32, "B");
59     aom_read(&br, 32, "B");
60   }
61   GTEST_ASSERT_EQ(accounting.syms.num_syms, kSymbols * 2);
62   uint32_t tell_frac = aom_reader_tell_frac(&br);
63   for (int i = 0; i < accounting.syms.num_syms; i++) {
64     tell_frac -= accounting.syms.syms[i].bits;
65   }
66   GTEST_ASSERT_EQ(tell_frac, 0U);
67 
68   GTEST_ASSERT_EQ(aom_accounting_dictionary_lookup(&accounting, "A"),
69                   aom_accounting_dictionary_lookup(&accounting, "A"));
70 
71   // Check for collisions. The current aom_accounting_hash function returns
72   // the same hash code for AB and BA.
73   GTEST_ASSERT_NE(aom_accounting_dictionary_lookup(&accounting, "AB"),
74                   aom_accounting_dictionary_lookup(&accounting, "BA"));
75 }
76