blob: 1b6fcef3a486c82222ba8472ade12558a5f6db0f [file] [log] [blame]
miloyip8d392822015-04-18 21:41:38 +08001// Tencent is pleased to support the open source community by making RapidJSON available.
2//
3// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
miloyipa32d8b72015-04-15 18:18:49 +08004//
miloyip8d392822015-04-18 21:41:38 +08005// Licensed under the MIT License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
miloyipa32d8b72015-04-15 18:18:49 +08007//
miloyip8d392822015-04-18 21:41:38 +08008// http://opensource.org/licenses/MIT
miloyipa32d8b72015-04-15 18:18:49 +08009//
miloyip8d392822015-04-18 21:41:38 +080010// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
miloyipa32d8b72015-04-15 18:18:49 +080014
15// Since Travis CI installs old Valgrind 3.7.0, which fails with some SSE4.2
16// The unit tests prefix with SIMD should be skipped by Valgrind test
17
18// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
19// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
20#if defined(__SSE4_2__)
21# define RAPIDJSON_SSE42
22#elif defined(__SSE2__)
23# define RAPIDJSON_SSE2
24#endif
25
miloyipee505262015-04-15 18:34:18 +080026#define RAPIDJSON_NAMESPACE rapidjson_simd
27
miloyipa32d8b72015-04-15 18:18:49 +080028#include "unittest.h"
29
30#include "rapidjson/reader.h"
Milo Yip6a6d9c72016-02-14 17:37:53 +080031#include "rapidjson/writer.h"
miloyipa32d8b72015-04-15 18:18:49 +080032
Milo Yipcefae772016-02-03 13:29:25 +080033#ifdef __GNUC__
34RAPIDJSON_DIAG_PUSH
35RAPIDJSON_DIAG_OFF(effc++)
36#endif
37
miloyipee505262015-04-15 18:34:18 +080038using namespace rapidjson_simd;
miloyipa32d8b72015-04-15 18:18:49 +080039
40#ifdef RAPIDJSON_SSE2
41#define SIMD_SUFFIX(name) name##_SSE2
42#elif defined(RAPIDJSON_SSE42)
43#define SIMD_SUFFIX(name) name##_SSE42
44#else
45#define SIMD_SUFFIX(name) name
46#endif
47
Milo Yip0571a212015-04-15 22:36:00 +080048template <typename StreamType>
49void TestSkipWhitespace() {
Milo Yip74c8dcf2015-12-18 18:34:04 +080050 for (size_t step = 1; step < 32; step++) {
Milo Yip4d3c64a2015-04-15 21:07:30 +080051 char buffer[1025];
52 for (size_t i = 0; i < 1024; i++)
53 buffer[i] = " \t\r\n"[i % 4];
54 for (size_t i = 0; i < 1024; i += step)
55 buffer[i] = 'X';
56 buffer[1024] = '\0';
miloyipa32d8b72015-04-15 18:18:49 +080057
Milo Yip0571a212015-04-15 22:36:00 +080058 StreamType s(buffer);
Milo Yip4d3c64a2015-04-15 21:07:30 +080059 size_t i = 0;
60 for (;;) {
61 SkipWhitespace(s);
62 if (s.Peek() == '\0')
63 break;
64 EXPECT_EQ(i, s.Tell());
65 EXPECT_EQ('X', s.Take());
66 i += step;
67 }
miloyipa32d8b72015-04-15 18:18:49 +080068 }
69}
Milo Yip0571a212015-04-15 22:36:00 +080070
71TEST(SIMD, SIMD_SUFFIX(SkipWhitespace)) {
72 TestSkipWhitespace<StringStream>();
73 TestSkipWhitespace<InsituStringStream>();
74}
Milo Yipd258f592016-02-03 12:51:02 +080075
Milo Yip48378b72016-02-15 20:21:36 +080076TEST(SIMD, SIMD_SUFFIX(SkipWhitespace_EncodedMemoryStream)) {
77 for (size_t step = 1; step < 32; step++) {
78 char buffer[1024];
79 for (size_t i = 0; i < 1024; i++)
80 buffer[i] = " \t\r\n"[i % 4];
81 for (size_t i = 0; i < 1024; i += step)
82 buffer[i] = 'X';
83
84 MemoryStream ms(buffer, 1024);
85 EncodedInputStream<UTF8<>, MemoryStream> s(ms);
86 size_t i = 0;
87 for (;;) {
88 SkipWhitespace(s);
89 if (s.Peek() == '\0')
90 break;
91 //EXPECT_EQ(i, s.Tell());
92 EXPECT_EQ('X', s.Take());
93 i += step;
94 }
95 }
96}
97
Milo Yipd258f592016-02-03 12:51:02 +080098struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnescapedStringHandler> {
Milo Yip021d7462016-02-03 13:10:55 +080099 bool String(const char* str, size_t length, bool) {
Milo Yipd258f592016-02-03 12:51:02 +0800100 memcpy(buffer, str, length + 1);
101 return true;
102 }
103 char buffer[1024 + 5];
104};
105
Milo Yip7c726402016-02-03 13:48:39 +0800106template <unsigned parseFlags, typename StreamType>
Milo Yipd258f592016-02-03 12:51:02 +0800107void TestScanCopyUnescapedString() {
108 for (size_t step = 0; step < 1024; step++) {
109 char json[1024 + 5];
110 char *p = json;
111 *p ++= '\"';
112 for (size_t i = 0; i < step; i++)
113 *p++= "ABCD"[i % 4];
114 *p++ = '\\';
115 *p++ = '\\';
116 *p++ = '\"';
117 *p++ = '\0';
118
119 StreamType s(json);
120 Reader reader;
121 ScanCopyUnescapedStringHandler h;
Milo Yip7c726402016-02-03 13:48:39 +0800122 reader.Parse<parseFlags>(s, h);
Milo Yipd258f592016-02-03 12:51:02 +0800123 EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0);
124 EXPECT_EQ('\\', h.buffer[step]); // escaped
125 EXPECT_EQ('\0', h.buffer[step + 1]);
126 }
127}
128
129TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
Milo Yip7c726402016-02-03 13:48:39 +0800130 TestScanCopyUnescapedString<kParseDefaultFlags, StringStream>();
131 TestScanCopyUnescapedString<kParseInsituFlag, InsituStringStream>();
Milo Yipd258f592016-02-03 12:51:02 +0800132}
Milo Yipcefae772016-02-03 13:29:25 +0800133
Milo Yip6a6d9c72016-02-14 17:37:53 +0800134TEST(SIMD, SIMD_SUFFIX(ScanWriteUnescapedString)) {
135 for (size_t step = 0; step < 1024; step++) {
136 char s[2048 + 1];
137 char *p = s;
138 for (size_t i = 0; i < step; i++)
139 *p++= "ABCD"[i % 4];
140 char escape = "\0\n\\\""[step % 4];
141 *p++ = escape;
142 for (size_t i = 0; i < step; i++)
143 *p++= "ABCD"[i % 4];
144
145 StringBuffer sb;
146 Writer<StringBuffer> writer(sb);
147 writer.String(s, SizeType(step * 2 + 1));
148 const char* q = sb.GetString();
149 EXPECT_EQ('\"', *q++);
150 for (size_t i = 0; i < step; i++)
151 EXPECT_EQ("ABCD"[i % 4], *q++);
152 if (escape == '\0') {
153 EXPECT_EQ('\\', *q++);
154 EXPECT_EQ('u', *q++);
155 EXPECT_EQ('0', *q++);
156 EXPECT_EQ('0', *q++);
157 EXPECT_EQ('0', *q++);
158 EXPECT_EQ('0', *q++);
159 }
160 else if (escape == '\n') {
161 EXPECT_EQ('\\', *q++);
162 EXPECT_EQ('n', *q++);
163 }
164 else if (escape == '\\') {
165 EXPECT_EQ('\\', *q++);
166 EXPECT_EQ('\\', *q++);
167 }
168 else if (escape == '\"') {
169 EXPECT_EQ('\\', *q++);
170 EXPECT_EQ('\"', *q++);
171 }
172 for (size_t i = 0; i < step; i++)
173 EXPECT_EQ("ABCD"[i % 4], *q++);
174 EXPECT_EQ('\"', *q++);
175 EXPECT_EQ('\0', *q++);
176 }
177}
178
Milo Yipcefae772016-02-03 13:29:25 +0800179#ifdef __GNUC__
180RAPIDJSON_DIAG_POP
181#endif