/src/mozilla-central/gfx/tests/gtest/TestBufferRotation.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "gtest/gtest.h" |
8 | | |
9 | | #include "BufferUnrotate.h" |
10 | | |
11 | | static unsigned char* GenerateBuffer(int bytesPerPixel, |
12 | | int width, int height, |
13 | | int stride, int xBoundary, int yBoundary) |
14 | 0 | { |
15 | 0 | unsigned char* buffer = new unsigned char[stride*height]; |
16 | 0 | for (int y = 0; y < height; y++) { |
17 | 0 | for (int x = 0; x < width; x++) { |
18 | 0 | int pos = ((yBoundary + y) % height) * stride + |
19 | 0 | ((xBoundary + x) % width) * bytesPerPixel; |
20 | 0 | for (int i = 0; i < bytesPerPixel; i++) { |
21 | 0 | buffer[pos+i] = (x+y+i*2)%256; |
22 | 0 | } |
23 | 0 | } |
24 | 0 | } |
25 | 0 | return buffer; |
26 | 0 | } |
27 | | |
28 | | static bool CheckBuffer(unsigned char* buffer, int bytesPerPixel, |
29 | | int width, int height, int stride) |
30 | 0 | { |
31 | 0 | int xBoundary = 0; |
32 | 0 | int yBoundary = 0; |
33 | 0 | for (int y = 0; y < height; y++) { |
34 | 0 | for (int x = 0; x < width; x++) { |
35 | 0 | int pos = ((yBoundary + y) % height) * stride + |
36 | 0 | ((xBoundary + x) % width) * bytesPerPixel; |
37 | 0 | for (int i = 0; i < bytesPerPixel; i++) { |
38 | 0 | if (buffer[pos+i] != (x+y+i*2)%256) { |
39 | 0 | printf("Buffer differs at %i, %i, is %i\n", x, y, (int)buffer[pos+i]); |
40 | 0 | return false; |
41 | 0 | } |
42 | 0 | } |
43 | 0 | } |
44 | 0 | } |
45 | 0 | return true; |
46 | 0 | } |
47 | | |
48 | 0 | TEST(Gfx, BufferUnrotateHorizontal) { |
49 | 0 | const int NUM_OF_TESTS = 8; |
50 | 0 | int bytesPerPixelList[2] = {2,4}; |
51 | 0 | int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; |
52 | 0 | int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; |
53 | 0 | int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; |
54 | 0 | int yBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; |
55 | 0 |
|
56 | 0 | for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { |
57 | 0 | int bytesPerPixel = bytesPerPixelList[bytesPerId]; |
58 | 0 | int stride = 256 * bytesPerPixel; |
59 | 0 | for (int testId = 0; testId < NUM_OF_TESTS; testId++) { |
60 | 0 | unsigned char* buffer = GenerateBuffer(bytesPerPixel, |
61 | 0 | width[testId], height[testId], stride, |
62 | 0 | xBoundary[testId], yBoundary[testId]); |
63 | 0 | BufferUnrotate(buffer, |
64 | 0 | width[testId] * bytesPerPixel, height[testId], stride, |
65 | 0 | xBoundary[testId] * bytesPerPixel, yBoundary[testId]); |
66 | 0 |
|
67 | 0 | EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, |
68 | 0 | width[testId], height[testId], stride)); |
69 | 0 | delete[] buffer; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | 0 | TEST(Gfx, BufferUnrotateVertical) { |
75 | 0 | const int NUM_OF_TESTS = 8; |
76 | 0 | int bytesPerPixelList[2] = {2,4}; |
77 | 0 | int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99}; |
78 | 0 | int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99}; |
79 | 0 | int xBoundary[NUM_OF_TESTS] = {0, 0, 0, 0}; |
80 | 0 | int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31}; |
81 | 0 |
|
82 | 0 | for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { |
83 | 0 | int bytesPerPixel = bytesPerPixelList[bytesPerId]; |
84 | 0 | int stride = 256 * bytesPerPixel; |
85 | 0 | for (int testId = 0; testId < NUM_OF_TESTS; testId++) { |
86 | 0 | unsigned char* buffer = GenerateBuffer(bytesPerPixel, |
87 | 0 | width[testId], height[testId], stride, |
88 | 0 | xBoundary[testId], yBoundary[testId]); |
89 | 0 | BufferUnrotate(buffer, width[testId] * bytesPerPixel, |
90 | 0 | height[testId], stride, |
91 | 0 | xBoundary[testId] * bytesPerPixel, yBoundary[testId]); |
92 | 0 |
|
93 | 0 | EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, |
94 | 0 | width[testId], height[testId], stride)); |
95 | 0 | delete[] buffer; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | |
101 | 0 | TEST(Gfx, BufferUnrotateBoth) { |
102 | 0 | const int NUM_OF_TESTS = 16; |
103 | 0 | int bytesPerPixelList[2] = {2,4}; |
104 | 0 | int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99, 100, 100, 99, 99}; |
105 | 0 | int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99, 100, 99}; |
106 | 0 | int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31, 30, 30, 30, 30, 31, 31, 31, 31}; |
107 | 0 | int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 30, 30, 30, 30, 31, 31, 31, 31, 31, 31, 31, 31}; |
108 | 0 |
|
109 | 0 | for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { |
110 | 0 | int bytesPerPixel = bytesPerPixelList[bytesPerId]; |
111 | 0 | int stride = 256 * bytesPerPixel; |
112 | 0 | for (int testId = 0; testId < NUM_OF_TESTS; testId++) { |
113 | 0 | unsigned char* buffer = GenerateBuffer(bytesPerPixel, |
114 | 0 | width[testId], height[testId], stride, |
115 | 0 | xBoundary[testId], yBoundary[testId]); |
116 | 0 | BufferUnrotate(buffer, |
117 | 0 | width[testId] * bytesPerPixel, height[testId], stride, |
118 | 0 | xBoundary[testId] * bytesPerPixel, yBoundary[testId]); |
119 | 0 |
|
120 | 0 | EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, |
121 | 0 | width[testId], height[testId], stride)); |
122 | 0 | delete[] buffer; |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | 0 | TEST(Gfx, BufferUnrotateUneven) { |
128 | 0 | const int NUM_OF_TESTS = 16; |
129 | 0 | int bytesPerPixelList[2] = {2,4}; |
130 | 0 | int width[NUM_OF_TESTS] = {10, 100, 99, 39, 100, 40, 99, 39, 100, 50, 39, 99, 74, 60, 99, 39}; |
131 | 0 | int height[NUM_OF_TESTS] = {100, 39, 10, 99, 10, 99, 40, 99, 73, 39, 100, 39, 67, 99, 84, 99}; |
132 | 0 | int xBoundary[NUM_OF_TESTS] = {0, 0, 30, 30, 99, 31, 0, 31, 30, 30, 30, 30, 31, 31, 31, 38}; |
133 | 0 | int yBoundary[NUM_OF_TESTS] = {30, 30, 0, 30, 0, 30, 0, 30, 31, 31, 31, 31, 31, 31, 31, 98}; |
134 | 0 |
|
135 | 0 | for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) { |
136 | 0 | int bytesPerPixel = bytesPerPixelList[bytesPerId]; |
137 | 0 | int stride = 256 * bytesPerPixel; |
138 | 0 | for (int testId = 0; testId < NUM_OF_TESTS; testId++) { |
139 | 0 | unsigned char* buffer = GenerateBuffer(bytesPerPixel, |
140 | 0 | width[testId], height[testId], stride, |
141 | 0 | xBoundary[testId], yBoundary[testId]); |
142 | 0 | BufferUnrotate(buffer, |
143 | 0 | width[testId]*bytesPerPixel, height[testId], stride, |
144 | 0 | xBoundary[testId]*bytesPerPixel, yBoundary[testId]); |
145 | 0 |
|
146 | 0 | EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId], height[testId], stride)); |
147 | 0 | delete[] buffer; |
148 | 0 | } |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | |