Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / remoting / base / util_unittest.cc
bloba269c076ce53ab37a64267118cdbff07ae07422c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <algorithm>
7 #include "remoting/base/util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/skia/include/core/SkRect.h"
10 #include "third_party/skia/include/core/SkSize.h"
12 static const int kWidth = 32 ;
13 static const int kHeight = 24 ;
14 static const int kBytesPerPixel = 4;
15 static const int kYStride = kWidth;
16 static const int kUvStride = kWidth / 2;
17 static const int kRgbStride = kWidth * kBytesPerPixel;
18 static const uint32 kFillColor = 0xffffff;
20 namespace remoting {
22 class YuvToRgbTester {
23 public:
24 YuvToRgbTester() {
25 yuv_buffer_size_ = (kYStride + kUvStride) * kHeight;
26 yuv_buffer_.reset(new uint8[yuv_buffer_size_]);
27 yplane_ = yuv_buffer_.get();
28 uplane_ = yplane_ + (kYStride * kHeight);
29 vplane_ = uplane_ + (kUvStride * kHeight / 2);
31 rgb_buffer_size_ = kWidth * kHeight * kBytesPerPixel;
32 rgb_buffer_.reset(new uint8[rgb_buffer_size_]);
34 ResetYuvBuffer();
35 ResetRgbBuffer();
38 ~YuvToRgbTester() {}
40 void ResetYuvBuffer() {
41 memset(yuv_buffer_.get(), 0, yuv_buffer_size_);
44 void ResetRgbBuffer() {
45 memset(rgb_buffer_.get(), 0, rgb_buffer_size_);
48 void FillRgbBuffer(const SkIRect& rect) {
49 uint32* ptr = reinterpret_cast<uint32*>(
50 rgb_buffer_.get() + (rect.top() * kRgbStride) +
51 (rect.left() * kBytesPerPixel));
52 int width = rect.width();
53 for (int height = rect.height(); height > 0; --height) {
54 std::fill(ptr, ptr + width, kFillColor);
55 ptr += kRgbStride / kBytesPerPixel;
59 // Check the the desination buffer is filled within expected bounds.
60 void CheckRgbBuffer(const SkIRect& rect) {
61 uint32* ptr = reinterpret_cast<uint32*>(rgb_buffer_.get());
62 for (int y = 0; y < kHeight; ++y) {
63 if (y < rect.top() || rect.bottom() <= y) {
64 // The whole line should be intact.
65 EXPECT_EQ((ptrdiff_t)kWidth,
66 std::count(ptr, ptr + kWidth, 0u));
67 } else {
68 // The space before the painted rectangle should be intact.
69 EXPECT_EQ((ptrdiff_t)rect.left(),
70 std::count(ptr, ptr + rect.left(), 0u));
72 // All pixels of the target rectangle should be touched.
73 EXPECT_EQ(ptr + rect.right(),
74 std::find(ptr + rect.left(), ptr + rect.right(), 0u));
76 // The space after the painted rectangle should be intact.
77 EXPECT_EQ((ptrdiff_t)kWidth - rect.right(),
78 std::count(ptr + rect.right(), ptr + kWidth, 0u));
80 ptr += kRgbStride / kBytesPerPixel;
84 void RunTest(const SkISize dest_size, const SkIRect& rect) {
85 ASSERT_TRUE(SkIRect::MakeSize(dest_size).contains(rect));
87 // Reset buffers.
88 ResetYuvBuffer();
89 ResetRgbBuffer();
90 FillRgbBuffer(rect);
92 // RGB -> YUV
93 ConvertRGB32ToYUVWithRect(rgb_buffer_.get(),
94 yplane_,
95 uplane_,
96 vplane_,
99 kWidth,
100 kHeight,
101 kRgbStride,
102 kYStride,
103 kUvStride);
105 // Reset RGB buffer and do opposite conversion.
106 ResetRgbBuffer();
107 ConvertAndScaleYUVToRGB32Rect(yplane_,
108 uplane_,
109 vplane_,
110 kYStride,
111 kUvStride,
112 SkISize::Make(kWidth, kHeight),
113 SkIRect::MakeWH(kWidth, kHeight),
114 rgb_buffer_.get(),
115 kRgbStride,
116 dest_size,
117 SkIRect::MakeSize(dest_size),
118 rect);
120 // Check if it worked out.
121 CheckRgbBuffer(rect);
124 void TestBasicConversion() {
125 // Whole buffer.
126 RunTest(SkISize::Make(kWidth, kHeight), SkIRect::MakeWH(kWidth, kHeight));
129 private:
130 size_t yuv_buffer_size_;
131 scoped_ptr<uint8[]> yuv_buffer_;
132 uint8* yplane_;
133 uint8* uplane_;
134 uint8* vplane_;
136 size_t rgb_buffer_size_;
137 scoped_ptr<uint8[]> rgb_buffer_;
139 DISALLOW_COPY_AND_ASSIGN(YuvToRgbTester);
142 TEST(YuvToRgbTest, BasicConversion) {
143 YuvToRgbTester tester;
144 tester.TestBasicConversion();
147 TEST(YuvToRgbTest, Clipping) {
148 YuvToRgbTester tester;
150 SkISize dest_size = SkISize::Make(kWidth, kHeight);
151 SkIRect rect = SkIRect::MakeLTRB(0, 0, kWidth - 1, kHeight - 1);
152 for (int i = 0; i < 16; ++i) {
153 SkIRect dest_rect = rect;
154 if ((i & 1) != 0)
155 dest_rect.fLeft += 1;
156 if ((i & 2) != 0)
157 dest_rect.fTop += 1;
158 if ((i & 4) != 0)
159 dest_rect.fRight += 1;
160 if ((i & 8) != 0)
161 dest_rect.fBottom += 1;
163 tester.RunTest(dest_size, dest_rect);
167 TEST(YuvToRgbTest, ClippingAndScaling) {
168 YuvToRgbTester tester;
170 SkISize dest_size = SkISize::Make(kWidth - 10, kHeight - 10);
171 SkIRect rect = SkIRect::MakeLTRB(5, 5, kWidth - 11, kHeight - 11);
172 for (int i = 0; i < 16; ++i) {
173 SkIRect dest_rect = rect;
174 if ((i & 1) != 0)
175 dest_rect.fLeft += 1;
176 if ((i & 2) != 0)
177 dest_rect.fTop += 1;
178 if ((i & 4) != 0)
179 dest_rect.fRight += 1;
180 if ((i & 8) != 0)
181 dest_rect.fBottom += 1;
183 tester.RunTest(dest_size, dest_rect);
187 TEST(ReplaceLfByCrLfTest, Basic) {
188 EXPECT_EQ("ab", ReplaceLfByCrLf("ab"));
189 EXPECT_EQ("\r\nab", ReplaceLfByCrLf("\nab"));
190 EXPECT_EQ("\r\nab\r\n", ReplaceLfByCrLf("\nab\n"));
191 EXPECT_EQ("\r\nab\r\ncd", ReplaceLfByCrLf("\nab\ncd"));
192 EXPECT_EQ("\r\nab\r\ncd\r\n", ReplaceLfByCrLf("\nab\ncd\n"));
193 EXPECT_EQ("\r\n\r\nab\r\n\r\ncd\r\n\r\n",
194 ReplaceLfByCrLf("\n\nab\n\ncd\n\n"));
197 TEST(ReplaceLfByCrLfTest, Speed) {
198 int kLineSize = 128;
199 std::string line(kLineSize, 'a');
200 line[kLineSize - 1] = '\n';
201 // Make a 10M string.
202 int kLineNum = 10 * 1024 * 1024 / kLineSize;
203 std::string buffer;
204 buffer.resize(kLineNum * kLineSize);
205 for (int i = 0; i < kLineNum; ++i) {
206 memcpy(&buffer[i * kLineSize], &line[0], kLineSize);
208 // Convert the string.
209 buffer = ReplaceLfByCrLf(buffer);
210 // Check the converted string.
211 EXPECT_EQ(static_cast<size_t>((kLineSize + 1) * kLineNum), buffer.size());
212 const char* p = &buffer[0];
213 for (int i = 0; i < kLineNum; ++i) {
214 EXPECT_EQ(0, memcmp(&line[0], p, kLineSize - 1));
215 p += kLineSize - 1;
216 EXPECT_EQ('\r', *p++);
217 EXPECT_EQ('\n', *p++);
221 TEST(ReplaceCrLfByLfTest, Basic) {
222 EXPECT_EQ("ab", ReplaceCrLfByLf("ab"));
223 EXPECT_EQ("\nab", ReplaceCrLfByLf("\r\nab"));
224 EXPECT_EQ("\nab\n", ReplaceCrLfByLf("\r\nab\r\n"));
225 EXPECT_EQ("\nab\ncd", ReplaceCrLfByLf("\r\nab\r\ncd"));
226 EXPECT_EQ("\nab\ncd\n", ReplaceCrLfByLf("\r\nab\r\ncd\n"));
227 EXPECT_EQ("\n\nab\n\ncd\n\n",
228 ReplaceCrLfByLf("\r\n\r\nab\r\n\r\ncd\r\n\r\n"));
229 EXPECT_EQ("\rab\rcd\r", ReplaceCrLfByLf("\rab\rcd\r"));
232 TEST(ReplaceCrLfByLfTest, Speed) {
233 int kLineSize = 128;
234 std::string line(kLineSize, 'a');
235 line[kLineSize - 2] = '\r';
236 line[kLineSize - 1] = '\n';
237 // Make a 10M string.
238 int kLineNum = 10 * 1024 * 1024 / kLineSize;
239 std::string buffer;
240 buffer.resize(kLineNum * kLineSize);
241 for (int i = 0; i < kLineNum; ++i) {
242 memcpy(&buffer[i * kLineSize], &line[0], kLineSize);
244 // Convert the string.
245 buffer = ReplaceCrLfByLf(buffer);
246 // Check the converted string.
247 EXPECT_EQ(static_cast<size_t>((kLineSize - 1) * kLineNum), buffer.size());
248 const char* p = &buffer[0];
249 for (int i = 0; i < kLineNum; ++i) {
250 EXPECT_EQ(0, memcmp(&line[0], p, kLineSize - 2));
251 p += kLineSize - 2;
252 EXPECT_EQ('\n', *p++);
256 TEST(StringIsUtf8Test, Basic) {
257 EXPECT_TRUE(StringIsUtf8("", 0));
258 EXPECT_TRUE(StringIsUtf8("\0", 1));
259 EXPECT_TRUE(StringIsUtf8("abc", 3));
260 EXPECT_TRUE(StringIsUtf8("\xc0\x80", 2));
261 EXPECT_TRUE(StringIsUtf8("\xe0\x80\x80", 3));
262 EXPECT_TRUE(StringIsUtf8("\xf0\x80\x80\x80", 4));
263 EXPECT_TRUE(StringIsUtf8("\xf8\x80\x80\x80\x80", 5));
264 EXPECT_TRUE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80", 6));
266 // Not enough continuation characters
267 EXPECT_FALSE(StringIsUtf8("\xc0", 1));
268 EXPECT_FALSE(StringIsUtf8("\xe0\x80", 2));
269 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80", 3));
270 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80", 4));
271 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80", 5));
273 // One more continuation character than needed
274 EXPECT_FALSE(StringIsUtf8("\xc0\x80\x80", 3));
275 EXPECT_FALSE(StringIsUtf8("\xe0\x80\x80\x80", 4));
276 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80\x80\x80", 5));
277 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80\x80\x80", 6));
278 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80\x80", 7));
280 // Invalid first byte
281 EXPECT_FALSE(StringIsUtf8("\xfe\x80\x80\x80\x80\x80\x80", 7));
282 EXPECT_FALSE(StringIsUtf8("\xff\x80\x80\x80\x80\x80\x80", 7));
284 // Invalid continuation byte
285 EXPECT_FALSE(StringIsUtf8("\xc0\x00", 2));
286 EXPECT_FALSE(StringIsUtf8("\xc0\x40", 2));
287 EXPECT_FALSE(StringIsUtf8("\xc0\xc0", 2));
290 } // namespace remoting