blob: 6ded7402fbfdd6a65e92f08054968329b1b76198 [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
76struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnescapedStringHandler> {
Milo Yip021d7462016-02-03 13:10:55 +080077 bool String(const char* str, size_t length, bool) {
Milo Yipd258f592016-02-03 12:51:02 +080078 memcpy(buffer, str, length + 1);
79 return true;
80 }
81 char buffer[1024 + 5];
82};
83
Milo Yip7c726402016-02-03 13:48:39 +080084template <unsigned parseFlags, typename StreamType>
Milo Yipd258f592016-02-03 12:51:02 +080085void TestScanCopyUnescapedString() {
86 for (size_t step = 0; step < 1024; step++) {
87 char json[1024 + 5];
88 char *p = json;
89 *p ++= '\"';
90 for (size_t i = 0; i < step; i++)
91 *p++= "ABCD"[i % 4];
92 *p++ = '\\';
93 *p++ = '\\';
94 *p++ = '\"';
95 *p++ = '\0';
96
97 StreamType s(json);
98 Reader reader;
99 ScanCopyUnescapedStringHandler h;
Milo Yip7c726402016-02-03 13:48:39 +0800100 reader.Parse<parseFlags>(s, h);
Milo Yipd258f592016-02-03 12:51:02 +0800101 EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0);
102 EXPECT_EQ('\\', h.buffer[step]); // escaped
103 EXPECT_EQ('\0', h.buffer[step + 1]);
104 }
105}
106
107TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
Milo Yip7c726402016-02-03 13:48:39 +0800108 TestScanCopyUnescapedString<kParseDefaultFlags, StringStream>();
109 TestScanCopyUnescapedString<kParseInsituFlag, InsituStringStream>();
Milo Yipd258f592016-02-03 12:51:02 +0800110}
Milo Yipcefae772016-02-03 13:29:25 +0800111
Milo Yip6a6d9c72016-02-14 17:37:53 +0800112TEST(SIMD, SIMD_SUFFIX(ScanWriteUnescapedString)) {
113 for (size_t step = 0; step < 1024; step++) {
114 char s[2048 + 1];
115 char *p = s;
116 for (size_t i = 0; i < step; i++)
117 *p++= "ABCD"[i % 4];
118 char escape = "\0\n\\\""[step % 4];
119 *p++ = escape;
120 for (size_t i = 0; i < step; i++)
121 *p++= "ABCD"[i % 4];
122
123 StringBuffer sb;
124 Writer<StringBuffer> writer(sb);
125 writer.String(s, SizeType(step * 2 + 1));
126 const char* q = sb.GetString();
127 EXPECT_EQ('\"', *q++);
128 for (size_t i = 0; i < step; i++)
129 EXPECT_EQ("ABCD"[i % 4], *q++);
130 if (escape == '\0') {
131 EXPECT_EQ('\\', *q++);
132 EXPECT_EQ('u', *q++);
133 EXPECT_EQ('0', *q++);
134 EXPECT_EQ('0', *q++);
135 EXPECT_EQ('0', *q++);
136 EXPECT_EQ('0', *q++);
137 }
138 else if (escape == '\n') {
139 EXPECT_EQ('\\', *q++);
140 EXPECT_EQ('n', *q++);
141 }
142 else if (escape == '\\') {
143 EXPECT_EQ('\\', *q++);
144 EXPECT_EQ('\\', *q++);
145 }
146 else if (escape == '\"') {
147 EXPECT_EQ('\\', *q++);
148 EXPECT_EQ('\"', *q++);
149 }
150 for (size_t i = 0; i < step; i++)
151 EXPECT_EQ("ABCD"[i % 4], *q++);
152 EXPECT_EQ('\"', *q++);
153 EXPECT_EQ('\0', *q++);
154 }
155}
156
Milo Yipcefae772016-02-03 13:29:25 +0800157#ifdef __GNUC__
158RAPIDJSON_DIAG_POP
159#endif