1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include <wtf/Vector.h>
29#include <wtf/text/LineEnding.h>
30
31namespace TestWebKitAPI {
32
33const uint8_t null = 0;
34const uint8_t CR = '\r';
35const uint8_t LF = '\n';
36const uint8_t letterA = 'a';
37const uint8_t letterB = 'b';
38
39static const char* stringify(const Vector<uint8_t>& vector)
40{
41 static char buffer[100];
42 char* out = buffer;
43 for (auto character : vector) {
44 switch (character) {
45 case '\0':
46 *out++ = '<';
47 *out++ = '0';
48 *out++ = '>';
49 break;
50 case '\r':
51 *out++ = '<';
52 *out++ = 'C';
53 *out++ = 'R';
54 *out++ = '>';
55 break;
56 case '\n':
57 *out++ = '<';
58 *out++ = 'L';
59 *out++ = 'F';
60 *out++ = '>';
61 break;
62 default:
63 *out++ = character;
64 }
65 }
66 *out = '\0';
67 return buffer;
68}
69
70TEST(WTF, LineEndingNormalizeToLF)
71{
72 EXPECT_STREQ("", stringify(normalizeLineEndingsToLF({ })));
73
74 EXPECT_STREQ("<0>", stringify(normalizeLineEndingsToLF({ null })));
75 EXPECT_STREQ("a", stringify(normalizeLineEndingsToLF({ letterA })));
76 EXPECT_STREQ("<LF>", stringify(normalizeLineEndingsToLF({ CR })));
77 EXPECT_STREQ("<LF>", stringify(normalizeLineEndingsToLF({ LF })));
78
79 EXPECT_STREQ("<LF>", stringify(normalizeLineEndingsToLF({ CR, LF })));
80 EXPECT_STREQ("<LF><LF>", stringify(normalizeLineEndingsToLF({ LF, LF })));
81 EXPECT_STREQ("<LF><LF>", stringify(normalizeLineEndingsToLF({ CR, CR })));
82 EXPECT_STREQ("<LF><LF>", stringify(normalizeLineEndingsToLF({ LF, CR })));
83
84 EXPECT_STREQ("a<LF>", stringify(normalizeLineEndingsToLF({ letterA, CR })));
85 EXPECT_STREQ("a<LF>", stringify(normalizeLineEndingsToLF({ letterA, LF })));
86 EXPECT_STREQ("a<LF>", stringify(normalizeLineEndingsToLF({ letterA, CR, LF })));
87 EXPECT_STREQ("a<LF><LF>", stringify(normalizeLineEndingsToLF({ letterA, LF, CR })));
88
89 EXPECT_STREQ("a<LF>b", stringify(normalizeLineEndingsToLF({ letterA, CR, letterB })));
90 EXPECT_STREQ("a<LF>b", stringify(normalizeLineEndingsToLF({ letterA, LF, letterB })));
91 EXPECT_STREQ("a<LF>b", stringify(normalizeLineEndingsToLF({ letterA, CR, LF, letterB })));
92 EXPECT_STREQ("a<LF><LF>b", stringify(normalizeLineEndingsToLF({ letterA, LF, CR, letterB })));
93}
94
95TEST(WTF, LineEndingNormalizeToCRLF)
96{
97 EXPECT_STREQ("", stringify(normalizeLineEndingsToCRLF({ })));
98
99 EXPECT_STREQ("<0>", stringify(normalizeLineEndingsToCRLF({ null })));
100 EXPECT_STREQ("a", stringify(normalizeLineEndingsToCRLF({ letterA })));
101 EXPECT_STREQ("<CR><LF>", stringify(normalizeLineEndingsToCRLF({ CR })));
102 EXPECT_STREQ("<CR><LF>", stringify(normalizeLineEndingsToCRLF({ LF })));
103
104 EXPECT_STREQ("<CR><LF>", stringify(normalizeLineEndingsToCRLF({ CR, LF })));
105 EXPECT_STREQ("<CR><LF><CR><LF>", stringify(normalizeLineEndingsToCRLF({ LF, LF })));
106 EXPECT_STREQ("<CR><LF><CR><LF>", stringify(normalizeLineEndingsToCRLF({ CR, CR })));
107 EXPECT_STREQ("<CR><LF><CR><LF>", stringify(normalizeLineEndingsToCRLF({ LF, CR })));
108
109 EXPECT_STREQ("a<CR><LF>", stringify(normalizeLineEndingsToCRLF({ letterA, CR })));
110 EXPECT_STREQ("a<CR><LF>", stringify(normalizeLineEndingsToCRLF({ letterA, LF })));
111 EXPECT_STREQ("a<CR><LF>", stringify(normalizeLineEndingsToCRLF({ letterA, CR, LF })));
112 EXPECT_STREQ("a<CR><LF><CR><LF>", stringify(normalizeLineEndingsToCRLF({ letterA, LF, CR })));
113
114 EXPECT_STREQ("a<CR><LF>b", stringify(normalizeLineEndingsToCRLF({ letterA, CR, letterB })));
115 EXPECT_STREQ("a<CR><LF>b", stringify(normalizeLineEndingsToCRLF({ letterA, LF, letterB })));
116 EXPECT_STREQ("a<CR><LF>b", stringify(normalizeLineEndingsToCRLF({ letterA, CR, LF, letterB })));
117 EXPECT_STREQ("a<CR><LF><CR><LF>b", stringify(normalizeLineEndingsToCRLF({ letterA, LF, CR, letterB })));
118}
119
120} // namespace TestWebKitAPI
121