michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #include "BufferUnrotate.h" michael@0: michael@0: static unsigned char* GenerateBuffer(int bytesPerPixel, michael@0: int width, int height, michael@0: int stride, int xBoundary, int yBoundary) michael@0: { michael@0: unsigned char* buffer = new unsigned char[stride*height]; michael@0: for (int y = 0; y < height; y++) { michael@0: for (int x = 0; x < width; x++) { michael@0: int pos = ((yBoundary + y) % height) * stride + michael@0: ((xBoundary + x) % width) * bytesPerPixel; michael@0: for (int i = 0; i < bytesPerPixel; i++) { michael@0: buffer[pos+i] = (x+y+i*2)%256; michael@0: } michael@0: } michael@0: } michael@0: return buffer; michael@0: } michael@0: michael@0: static bool CheckBuffer(unsigned char* buffer, int bytesPerPixel, michael@0: int width, int height, int stride) michael@0: { michael@0: int xBoundary = 0; michael@0: int yBoundary = 0; michael@0: for (int y = 0; y < height; y++) { michael@0: for (int x = 0; x < width; x++) { michael@0: int pos = ((yBoundary + y) % height) * stride + michael@0: ((xBoundary + x) % width) * bytesPerPixel; michael@0: for (int i = 0; i < bytesPerPixel; i++) { michael@0: if (buffer[pos+i] != (x+y+i*2)%256) { michael@0: printf("Buffer differs at %i, %i, is %i\n", x, y, (int)buffer[pos+i]); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: TEST(Gfx, BufferUnrotateHorizontal) { michael@0: const int NUM_OF_TESTS = 8; michael@0: int bytesPerPixelList[2] = {2,4}; michael@0: int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; michael@0: int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; michael@0: int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; michael@0: int yBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; michael@0: michael@0: for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { michael@0: int bytesPerPixel = bytesPerPixelList[bytesPerId]; michael@0: int stride = 256 * bytesPerPixel; michael@0: for (int testId = 0; testId < NUM_OF_TESTS; testId++) { michael@0: unsigned char* buffer = GenerateBuffer(bytesPerPixel, michael@0: width[testId], height[testId], stride, michael@0: xBoundary[testId], yBoundary[testId]); michael@0: BufferUnrotate(buffer, michael@0: width[testId] * bytesPerPixel, height[testId], stride, michael@0: xBoundary[testId] * bytesPerPixel, yBoundary[testId]); michael@0: michael@0: EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, michael@0: width[testId], height[testId], stride)); michael@0: delete[] buffer; michael@0: } michael@0: } michael@0: } michael@0: michael@0: TEST(Gfx, BufferUnrotateVertical) { michael@0: const int NUM_OF_TESTS = 8; michael@0: int bytesPerPixelList[2] = {2,4}; michael@0: int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; michael@0: int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; michael@0: int xBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; michael@0: int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; michael@0: michael@0: for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { michael@0: int bytesPerPixel = bytesPerPixelList[bytesPerId]; michael@0: int stride = 256 * bytesPerPixel; michael@0: for (int testId = 0; testId < NUM_OF_TESTS; testId++) { michael@0: unsigned char* buffer = GenerateBuffer(bytesPerPixel, michael@0: width[testId], height[testId], stride, michael@0: xBoundary[testId], yBoundary[testId]); michael@0: BufferUnrotate(buffer, width[testId] * bytesPerPixel, michael@0: height[testId], stride, michael@0: xBoundary[testId] * bytesPerPixel, yBoundary[testId]); michael@0: michael@0: EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, michael@0: width[testId], height[testId], stride)); michael@0: delete[] buffer; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: TEST(Gfx, BufferUnrotateBoth) { michael@0: const int NUM_OF_TESTS = 16; michael@0: int bytesPerPixelList[2] = {2,4}; michael@0: int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99}; michael@0: int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99}; michael@0: int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31, 30, 30, 30, 30, 31, 31, 31, 31}; michael@0: int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31}; michael@0: michael@0: for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { michael@0: int bytesPerPixel = bytesPerPixelList[bytesPerId]; michael@0: int stride = 256 * bytesPerPixel; michael@0: for (int testId = 0; testId < NUM_OF_TESTS; testId++) { michael@0: unsigned char* buffer = GenerateBuffer(bytesPerPixel, michael@0: width[testId], height[testId], stride, michael@0: xBoundary[testId], yBoundary[testId]); michael@0: BufferUnrotate(buffer, michael@0: width[testId] * bytesPerPixel, height[testId], stride, michael@0: xBoundary[testId] * bytesPerPixel, yBoundary[testId]); michael@0: michael@0: EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, michael@0: width[testId], height[testId], stride)); michael@0: delete[] buffer; michael@0: } michael@0: } michael@0: } michael@0: michael@0: TEST(Gfx, BufferUnrotateUneven) { michael@0: const int NUM_OF_TESTS = 16; michael@0: int bytesPerPixelList[2] = {2,4}; michael@0: int width[NUM_OF_TESTS] = {10, 100, 99, 39, 100, 40, 99, 39, 100, 50, 39, 99, 74, 60, 99, 39}; michael@0: int height[NUM_OF_TESTS] = {100, 39, 10, 99, 10, 99, 40, 99, 73, 39, 100, 39, 67, 99, 84, 99}; michael@0: int xBoundary[NUM_OF_TESTS] = {0, 0, 30, 30, 99, 31, 0, 31, 30, 30, 30, 30, 31, 31, 31, 38}; michael@0: int yBoundary[NUM_OF_TESTS] = {30, 30, 0, 30, 0, 30, 0, 30, 31, 31, 31, 31, 31, 31, 31, 98}; michael@0: michael@0: for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { michael@0: int bytesPerPixel = bytesPerPixelList[bytesPerId]; michael@0: int stride = 256 * bytesPerPixel; michael@0: for (int testId = 0; testId < NUM_OF_TESTS; testId++) { michael@0: unsigned char* buffer = GenerateBuffer(bytesPerPixel, michael@0: width[testId], height[testId], stride, michael@0: xBoundary[testId], yBoundary[testId]); michael@0: BufferUnrotate(buffer, michael@0: width[testId]*bytesPerPixel, height[testId], stride, michael@0: xBoundary[testId]*bytesPerPixel, yBoundary[testId]); michael@0: michael@0: EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId], height[testId], stride)); michael@0: delete[] buffer; michael@0: } michael@0: } michael@0: } michael@0: michael@0: