1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/tests/gtest/TestBufferRotation.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "gtest/gtest.h" 1.10 + 1.11 +#include "BufferUnrotate.h" 1.12 + 1.13 +static unsigned char* GenerateBuffer(int bytesPerPixel, 1.14 + int width, int height, 1.15 + int stride, int xBoundary, int yBoundary) 1.16 +{ 1.17 + unsigned char* buffer = new unsigned char[stride*height]; 1.18 + for (int y = 0; y < height; y++) { 1.19 + for (int x = 0; x < width; x++) { 1.20 + int pos = ((yBoundary + y) % height) * stride + 1.21 + ((xBoundary + x) % width) * bytesPerPixel; 1.22 + for (int i = 0; i < bytesPerPixel; i++) { 1.23 + buffer[pos+i] = (x+y+i*2)%256; 1.24 + } 1.25 + } 1.26 + } 1.27 + return buffer; 1.28 +} 1.29 + 1.30 +static bool CheckBuffer(unsigned char* buffer, int bytesPerPixel, 1.31 + int width, int height, int stride) 1.32 +{ 1.33 + int xBoundary = 0; 1.34 + int yBoundary = 0; 1.35 + for (int y = 0; y < height; y++) { 1.36 + for (int x = 0; x < width; x++) { 1.37 + int pos = ((yBoundary + y) % height) * stride + 1.38 + ((xBoundary + x) % width) * bytesPerPixel; 1.39 + for (int i = 0; i < bytesPerPixel; i++) { 1.40 + if (buffer[pos+i] != (x+y+i*2)%256) { 1.41 + printf("Buffer differs at %i, %i, is %i\n", x, y, (int)buffer[pos+i]); 1.42 + return false; 1.43 + } 1.44 + } 1.45 + } 1.46 + } 1.47 + return true; 1.48 +} 1.49 + 1.50 +TEST(Gfx, BufferUnrotateHorizontal) { 1.51 + const int NUM_OF_TESTS = 8; 1.52 + int bytesPerPixelList[2] = {2,4}; 1.53 + int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; 1.54 + int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; 1.55 + int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; 1.56 + int yBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; 1.57 + 1.58 + for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { 1.59 + int bytesPerPixel = bytesPerPixelList[bytesPerId]; 1.60 + int stride = 256 * bytesPerPixel; 1.61 + for (int testId = 0; testId < NUM_OF_TESTS; testId++) { 1.62 + unsigned char* buffer = GenerateBuffer(bytesPerPixel, 1.63 + width[testId], height[testId], stride, 1.64 + xBoundary[testId], yBoundary[testId]); 1.65 + BufferUnrotate(buffer, 1.66 + width[testId] * bytesPerPixel, height[testId], stride, 1.67 + xBoundary[testId] * bytesPerPixel, yBoundary[testId]); 1.68 + 1.69 + EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, 1.70 + width[testId], height[testId], stride)); 1.71 + delete[] buffer; 1.72 + } 1.73 + } 1.74 +} 1.75 + 1.76 +TEST(Gfx, BufferUnrotateVertical) { 1.77 + const int NUM_OF_TESTS = 8; 1.78 + int bytesPerPixelList[2] = {2,4}; 1.79 + int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; 1.80 + int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; 1.81 + int xBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; 1.82 + int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; 1.83 + 1.84 + for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { 1.85 + int bytesPerPixel = bytesPerPixelList[bytesPerId]; 1.86 + int stride = 256 * bytesPerPixel; 1.87 + for (int testId = 0; testId < NUM_OF_TESTS; testId++) { 1.88 + unsigned char* buffer = GenerateBuffer(bytesPerPixel, 1.89 + width[testId], height[testId], stride, 1.90 + xBoundary[testId], yBoundary[testId]); 1.91 + BufferUnrotate(buffer, width[testId] * bytesPerPixel, 1.92 + height[testId], stride, 1.93 + xBoundary[testId] * bytesPerPixel, yBoundary[testId]); 1.94 + 1.95 + EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, 1.96 + width[testId], height[testId], stride)); 1.97 + delete[] buffer; 1.98 + } 1.99 + } 1.100 +} 1.101 + 1.102 + 1.103 +TEST(Gfx, BufferUnrotateBoth) { 1.104 + const int NUM_OF_TESTS = 16; 1.105 + int bytesPerPixelList[2] = {2,4}; 1.106 + int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99}; 1.107 + int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99}; 1.108 + int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31, 30, 30, 30, 30, 31, 31, 31, 31}; 1.109 + int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31}; 1.110 + 1.111 + for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { 1.112 + int bytesPerPixel = bytesPerPixelList[bytesPerId]; 1.113 + int stride = 256 * bytesPerPixel; 1.114 + for (int testId = 0; testId < NUM_OF_TESTS; testId++) { 1.115 + unsigned char* buffer = GenerateBuffer(bytesPerPixel, 1.116 + width[testId], height[testId], stride, 1.117 + xBoundary[testId], yBoundary[testId]); 1.118 + BufferUnrotate(buffer, 1.119 + width[testId] * bytesPerPixel, height[testId], stride, 1.120 + xBoundary[testId] * bytesPerPixel, yBoundary[testId]); 1.121 + 1.122 + EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, 1.123 + width[testId], height[testId], stride)); 1.124 + delete[] buffer; 1.125 + } 1.126 + } 1.127 +} 1.128 + 1.129 +TEST(Gfx, BufferUnrotateUneven) { 1.130 + const int NUM_OF_TESTS = 16; 1.131 + int bytesPerPixelList[2] = {2,4}; 1.132 + int width[NUM_OF_TESTS] = {10, 100, 99, 39, 100, 40, 99, 39, 100, 50, 39, 99, 74, 60, 99, 39}; 1.133 + int height[NUM_OF_TESTS] = {100, 39, 10, 99, 10, 99, 40, 99, 73, 39, 100, 39, 67, 99, 84, 99}; 1.134 + int xBoundary[NUM_OF_TESTS] = {0, 0, 30, 30, 99, 31, 0, 31, 30, 30, 30, 30, 31, 31, 31, 38}; 1.135 + int yBoundary[NUM_OF_TESTS] = {30, 30, 0, 30, 0, 30, 0, 30, 31, 31, 31, 31, 31, 31, 31, 98}; 1.136 + 1.137 + for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { 1.138 + int bytesPerPixel = bytesPerPixelList[bytesPerId]; 1.139 + int stride = 256 * bytesPerPixel; 1.140 + for (int testId = 0; testId < NUM_OF_TESTS; testId++) { 1.141 + unsigned char* buffer = GenerateBuffer(bytesPerPixel, 1.142 + width[testId], height[testId], stride, 1.143 + xBoundary[testId], yBoundary[testId]); 1.144 + BufferUnrotate(buffer, 1.145 + width[testId]*bytesPerPixel, height[testId], stride, 1.146 + xBoundary[testId]*bytesPerPixel, yBoundary[testId]); 1.147 + 1.148 + EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId], height[testId], stride)); 1.149 + delete[] buffer; 1.150 + } 1.151 + } 1.152 +} 1.153 + 1.154 +