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