Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / remoting / base / util_unittest.cc
blob3f489b842e8a8ef36da8696151833ecb2c029443
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/webrtc/modules/desktop_capture/desktop_geometry.h"
11 namespace remoting {
13 TEST(ReplaceLfByCrLfTest, Basic) {
14 EXPECT_EQ("ab", ReplaceLfByCrLf("ab"));
15 EXPECT_EQ("\r\nab", ReplaceLfByCrLf("\nab"));
16 EXPECT_EQ("\r\nab\r\n", ReplaceLfByCrLf("\nab\n"));
17 EXPECT_EQ("\r\nab\r\ncd", ReplaceLfByCrLf("\nab\ncd"));
18 EXPECT_EQ("\r\nab\r\ncd\r\n", ReplaceLfByCrLf("\nab\ncd\n"));
19 EXPECT_EQ("\r\n\r\nab\r\n\r\ncd\r\n\r\n",
20 ReplaceLfByCrLf("\n\nab\n\ncd\n\n"));
23 TEST(ReplaceLfByCrLfTest, Speed) {
24 int kLineSize = 128;
25 std::string line(kLineSize, 'a');
26 line[kLineSize - 1] = '\n';
27 // Make a 10M string.
28 int kLineNum = 10 * 1024 * 1024 / kLineSize;
29 std::string buffer;
30 buffer.resize(kLineNum * kLineSize);
31 for (int i = 0; i < kLineNum; ++i) {
32 memcpy(&buffer[i * kLineSize], &line[0], kLineSize);
34 // Convert the string.
35 buffer = ReplaceLfByCrLf(buffer);
36 // Check the converted string.
37 EXPECT_EQ(static_cast<size_t>((kLineSize + 1) * kLineNum), buffer.size());
38 const char* p = &buffer[0];
39 for (int i = 0; i < kLineNum; ++i) {
40 EXPECT_EQ(0, memcmp(&line[0], p, kLineSize - 1));
41 p += kLineSize - 1;
42 EXPECT_EQ('\r', *p++);
43 EXPECT_EQ('\n', *p++);
47 TEST(ReplaceCrLfByLfTest, Basic) {
48 EXPECT_EQ("ab", ReplaceCrLfByLf("ab"));
49 EXPECT_EQ("\nab", ReplaceCrLfByLf("\r\nab"));
50 EXPECT_EQ("\nab\n", ReplaceCrLfByLf("\r\nab\r\n"));
51 EXPECT_EQ("\nab\ncd", ReplaceCrLfByLf("\r\nab\r\ncd"));
52 EXPECT_EQ("\nab\ncd\n", ReplaceCrLfByLf("\r\nab\r\ncd\n"));
53 EXPECT_EQ("\n\nab\n\ncd\n\n",
54 ReplaceCrLfByLf("\r\n\r\nab\r\n\r\ncd\r\n\r\n"));
55 EXPECT_EQ("\rab\rcd\r", ReplaceCrLfByLf("\rab\rcd\r"));
58 TEST(ReplaceCrLfByLfTest, Speed) {
59 int kLineSize = 128;
60 std::string line(kLineSize, 'a');
61 line[kLineSize - 2] = '\r';
62 line[kLineSize - 1] = '\n';
63 // Make a 10M string.
64 int kLineNum = 10 * 1024 * 1024 / kLineSize;
65 std::string buffer;
66 buffer.resize(kLineNum * kLineSize);
67 for (int i = 0; i < kLineNum; ++i) {
68 memcpy(&buffer[i * kLineSize], &line[0], kLineSize);
70 // Convert the string.
71 buffer = ReplaceCrLfByLf(buffer);
72 // Check the converted string.
73 EXPECT_EQ(static_cast<size_t>((kLineSize - 1) * kLineNum), buffer.size());
74 const char* p = &buffer[0];
75 for (int i = 0; i < kLineNum; ++i) {
76 EXPECT_EQ(0, memcmp(&line[0], p, kLineSize - 2));
77 p += kLineSize - 2;
78 EXPECT_EQ('\n', *p++);
82 TEST(StringIsUtf8Test, Basic) {
83 EXPECT_TRUE(StringIsUtf8("", 0));
84 EXPECT_TRUE(StringIsUtf8("\0", 1));
85 EXPECT_TRUE(StringIsUtf8("abc", 3));
86 EXPECT_TRUE(StringIsUtf8("\xc0\x80", 2));
87 EXPECT_TRUE(StringIsUtf8("\xe0\x80\x80", 3));
88 EXPECT_TRUE(StringIsUtf8("\xf0\x80\x80\x80", 4));
89 EXPECT_TRUE(StringIsUtf8("\xf8\x80\x80\x80\x80", 5));
90 EXPECT_TRUE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80", 6));
92 // Not enough continuation characters
93 EXPECT_FALSE(StringIsUtf8("\xc0", 1));
94 EXPECT_FALSE(StringIsUtf8("\xe0\x80", 2));
95 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80", 3));
96 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80", 4));
97 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80", 5));
99 // One more continuation character than needed
100 EXPECT_FALSE(StringIsUtf8("\xc0\x80\x80", 3));
101 EXPECT_FALSE(StringIsUtf8("\xe0\x80\x80\x80", 4));
102 EXPECT_FALSE(StringIsUtf8("\xf0\x80\x80\x80\x80", 5));
103 EXPECT_FALSE(StringIsUtf8("\xf8\x80\x80\x80\x80\x80", 6));
104 EXPECT_FALSE(StringIsUtf8("\xfc\x80\x80\x80\x80\x80\x80", 7));
106 // Invalid first byte
107 EXPECT_FALSE(StringIsUtf8("\xfe\x80\x80\x80\x80\x80\x80", 7));
108 EXPECT_FALSE(StringIsUtf8("\xff\x80\x80\x80\x80\x80\x80", 7));
110 // Invalid continuation byte
111 EXPECT_FALSE(StringIsUtf8("\xc0\x00", 2));
112 EXPECT_FALSE(StringIsUtf8("\xc0\x40", 2));
113 EXPECT_FALSE(StringIsUtf8("\xc0\xc0", 2));
116 } // namespace remoting