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/. */
7 #include "gtest/gtest.h"
9 #include "BufferUnrotate.h"
11 using mozilla::gfx::BufferUnrotate
;
13 static unsigned char* GenerateBuffer(int bytesPerPixel
, int width
, int height
,
14 int stride
, int xBoundary
, int yBoundary
) {
15 unsigned char* buffer
= new unsigned char[stride
* height
];
16 for (int y
= 0; y
< height
; y
++) {
17 for (int x
= 0; x
< width
; x
++) {
18 int pos
= ((yBoundary
+ y
) % height
) * stride
+
19 ((xBoundary
+ x
) % width
) * bytesPerPixel
;
20 for (int i
= 0; i
< bytesPerPixel
; i
++) {
21 buffer
[pos
+ i
] = (x
+ y
+ i
* 2) % 256;
28 static bool CheckBuffer(unsigned char* buffer
, int bytesPerPixel
, int width
,
29 int height
, int stride
) {
32 for (int y
= 0; y
< height
; y
++) {
33 for (int x
= 0; x
< width
; x
++) {
34 int pos
= ((yBoundary
+ y
) % height
) * stride
+
35 ((xBoundary
+ x
) % width
) * bytesPerPixel
;
36 for (int i
= 0; i
< bytesPerPixel
; i
++) {
37 if (buffer
[pos
+ i
] != (x
+ y
+ i
* 2) % 256) {
38 printf("Buffer differs at %i, %i, is %i\n", x
, y
,
39 (int)buffer
[pos
+ i
]);
48 TEST(Gfx
, BufferUnrotateHorizontal
)
50 const int NUM_OF_TESTS
= 8;
51 int bytesPerPixelList
[2] = {2, 4};
52 int width
[NUM_OF_TESTS
] = {100, 100, 99, 99, 100, 100, 99, 99};
53 int height
[NUM_OF_TESTS
] = {100, 99, 100, 99, 100, 99, 100, 99};
54 int xBoundary
[NUM_OF_TESTS
] = {30, 30, 30, 30, 31, 31, 31, 31};
55 int yBoundary
[NUM_OF_TESTS
] = {0, 0, 0, 0};
57 for (int bytesPerId
= 0; bytesPerId
< 2; bytesPerId
++) {
58 int bytesPerPixel
= bytesPerPixelList
[bytesPerId
];
59 int stride
= 256 * bytesPerPixel
;
60 for (int testId
= 0; testId
< NUM_OF_TESTS
; testId
++) {
61 unsigned char* buffer
=
62 GenerateBuffer(bytesPerPixel
, width
[testId
], height
[testId
], stride
,
63 xBoundary
[testId
], yBoundary
[testId
]);
64 BufferUnrotate(buffer
, width
[testId
] * bytesPerPixel
, height
[testId
],
65 stride
, xBoundary
[testId
] * bytesPerPixel
,
68 EXPECT_TRUE(CheckBuffer(buffer
, bytesPerPixel
, width
[testId
],
69 height
[testId
], stride
));
75 TEST(Gfx
, BufferUnrotateVertical
)
77 const int NUM_OF_TESTS
= 8;
78 int bytesPerPixelList
[2] = {2, 4};
79 int width
[NUM_OF_TESTS
] = {100, 100, 99, 99, 100, 100, 99, 99};
80 int height
[NUM_OF_TESTS
] = {100, 99, 100, 99, 100, 99, 100, 99};
81 int xBoundary
[NUM_OF_TESTS
] = {0, 0, 0, 0};
82 int yBoundary
[NUM_OF_TESTS
] = {30, 30, 30, 30, 31, 31, 31, 31};
84 for (int bytesPerId
= 0; bytesPerId
< 2; bytesPerId
++) {
85 int bytesPerPixel
= bytesPerPixelList
[bytesPerId
];
86 int stride
= 256 * bytesPerPixel
;
87 for (int testId
= 0; testId
< NUM_OF_TESTS
; testId
++) {
88 unsigned char* buffer
=
89 GenerateBuffer(bytesPerPixel
, width
[testId
], height
[testId
], stride
,
90 xBoundary
[testId
], yBoundary
[testId
]);
91 BufferUnrotate(buffer
, width
[testId
] * bytesPerPixel
, height
[testId
],
92 stride
, xBoundary
[testId
] * bytesPerPixel
,
95 EXPECT_TRUE(CheckBuffer(buffer
, bytesPerPixel
, width
[testId
],
96 height
[testId
], stride
));
102 TEST(Gfx
, BufferUnrotateBoth
)
104 const int NUM_OF_TESTS
= 16;
105 int bytesPerPixelList
[2] = {2, 4};
106 int width
[NUM_OF_TESTS
] = {100, 100, 99, 99, 100, 100, 99, 99,
107 100, 100, 99, 99, 100, 100, 99, 99};
108 int height
[NUM_OF_TESTS
] = {100, 99, 100, 99, 100, 99, 100, 99,
109 100, 99, 100, 99, 100, 99, 100, 99};
110 int xBoundary
[NUM_OF_TESTS
] = {30, 30, 30, 30, 31, 31, 31, 31,
111 30, 30, 30, 30, 31, 31, 31, 31};
112 int yBoundary
[NUM_OF_TESTS
] = {30, 30, 30, 30, 30, 30, 30, 30,
113 31, 31, 31, 31, 31, 31, 31, 31};
115 for (int bytesPerId
= 0; bytesPerId
< 2; bytesPerId
++) {
116 int bytesPerPixel
= bytesPerPixelList
[bytesPerId
];
117 int stride
= 256 * bytesPerPixel
;
118 for (int testId
= 0; testId
< NUM_OF_TESTS
; testId
++) {
119 unsigned char* buffer
=
120 GenerateBuffer(bytesPerPixel
, width
[testId
], height
[testId
], stride
,
121 xBoundary
[testId
], yBoundary
[testId
]);
122 BufferUnrotate(buffer
, width
[testId
] * bytesPerPixel
, height
[testId
],
123 stride
, xBoundary
[testId
] * bytesPerPixel
,
126 EXPECT_TRUE(CheckBuffer(buffer
, bytesPerPixel
, width
[testId
],
127 height
[testId
], stride
));
133 TEST(Gfx
, BufferUnrotateUneven
)
135 const int NUM_OF_TESTS
= 16;
136 int bytesPerPixelList
[2] = {2, 4};
137 int width
[NUM_OF_TESTS
] = {10, 100, 99, 39, 100, 40, 99, 39,
138 100, 50, 39, 99, 74, 60, 99, 39};
139 int height
[NUM_OF_TESTS
] = {100, 39, 10, 99, 10, 99, 40, 99,
140 73, 39, 100, 39, 67, 99, 84, 99};
141 int xBoundary
[NUM_OF_TESTS
] = {0, 0, 30, 30, 99, 31, 0, 31,
142 30, 30, 30, 30, 31, 31, 31, 38};
143 int yBoundary
[NUM_OF_TESTS
] = {30, 30, 0, 30, 0, 30, 0, 30,
144 31, 31, 31, 31, 31, 31, 31, 98};
146 for (int bytesPerId
= 0; bytesPerId
< 2; bytesPerId
++) {
147 int bytesPerPixel
= bytesPerPixelList
[bytesPerId
];
148 int stride
= 256 * bytesPerPixel
;
149 for (int testId
= 0; testId
< NUM_OF_TESTS
; testId
++) {
150 unsigned char* buffer
=
151 GenerateBuffer(bytesPerPixel
, width
[testId
], height
[testId
], stride
,
152 xBoundary
[testId
], yBoundary
[testId
]);
153 BufferUnrotate(buffer
, width
[testId
] * bytesPerPixel
, height
[testId
],
154 stride
, xBoundary
[testId
] * bytesPerPixel
,
157 EXPECT_TRUE(CheckBuffer(buffer
, bytesPerPixel
, width
[testId
],
158 height
[testId
], stride
));