1/*
2 *  Copyright (c) 2013 The WebRTC 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#include "testing/gmock/include/gmock/gmock.h"
12#include "webrtc/modules/desktop_capture/differ_block.h"
13#include "webrtc/system_wrappers/interface/ref_count.h"
14
15namespace webrtc {
16
17// Run 900 times to mimic 1280x720.
18// TODO(fbarchard): Remove benchmark once performance is non-issue.
19static const int kTimesToRun = 900;
20
21static void GenerateData(uint8_t* data, int size) {
22  for (int i = 0; i < size; ++i) {
23    data[i] = i;
24  }
25}
26
27// Memory buffer large enough for 2 blocks aligned to 16 bytes.
28static const int kSizeOfBlock = kBlockSize * kBlockSize * kBytesPerPixel;
29uint8_t block_buffer[kSizeOfBlock * 2 + 16];
30
31void PrepareBuffers(uint8_t* &block1, uint8_t* &block2) {
32  block1 = reinterpret_cast<uint8_t*>
33      ((reinterpret_cast<uintptr_t>(&block_buffer[0]) + 15) & ~15);
34  GenerateData(block1, kSizeOfBlock);
35  block2 = block1 + kSizeOfBlock;
36  memcpy(block2, block1, kSizeOfBlock);
37}
38
39TEST(BlockDifferenceTestSame, BlockDifference) {
40  uint8_t* block1;
41  uint8_t* block2;
42  PrepareBuffers(block1, block2);
43
44  // These blocks should match.
45  for (int i = 0; i < kTimesToRun; ++i) {
46    int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel);
47    EXPECT_EQ(0, result);
48  }
49}
50
51TEST(BlockDifferenceTestLast, BlockDifference) {
52  uint8_t* block1;
53  uint8_t* block2;
54  PrepareBuffers(block1, block2);
55  block2[kSizeOfBlock-2] += 1;
56
57  for (int i = 0; i < kTimesToRun; ++i) {
58    int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel);
59    EXPECT_EQ(1, result);
60  }
61}
62
63TEST(BlockDifferenceTestMid, BlockDifference) {
64  uint8_t* block1;
65  uint8_t* block2;
66  PrepareBuffers(block1, block2);
67  block2[kSizeOfBlock/2+1] += 1;
68
69  for (int i = 0; i < kTimesToRun; ++i) {
70    int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel);
71    EXPECT_EQ(1, result);
72  }
73}
74
75TEST(BlockDifferenceTestFirst, BlockDifference) {
76  uint8_t* block1;
77  uint8_t* block2;
78  PrepareBuffers(block1, block2);
79  block2[0] += 1;
80
81  for (int i = 0; i < kTimesToRun; ++i) {
82    int result = BlockDifference(block1, block2, kBlockSize * kBytesPerPixel);
83    EXPECT_EQ(1, result);
84  }
85}
86
87}  // namespace webrtc
88