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