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