sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 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 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame^] | 11 | #include "webrtc/test/gmock.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 12 | #include "webrtc/modules/desktop_capture/differ_block.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | |
| 16 | // Run 900 times to mimic 1280x720. |
| 17 | // TODO(fbarchard): Remove benchmark once performance is non-issue. |
| 18 | static const int kTimesToRun = 900; |
| 19 | |
| 20 | static 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. |
| 27 | static const int kSizeOfBlock = kBlockSize * kBlockSize * kBytesPerPixel; |
| 28 | uint8_t block_buffer[kSizeOfBlock * 2 + 16]; |
| 29 | |
| 30 | void 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 | |
| 38 | TEST(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 | |
| 50 | TEST(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 | |
| 62 | TEST(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 | |
| 74 | TEST(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 |