blob: 5e369f3bfb23ec1c6ca21652b48a2aa581245341 [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"
31
miloyipee505262015-04-15 18:34:18 +080032using namespace rapidjson_simd;
miloyipa32d8b72015-04-15 18:18:49 +080033
34#ifdef RAPIDJSON_SSE2
35#define SIMD_SUFFIX(name) name##_SSE2
36#elif defined(RAPIDJSON_SSE42)
37#define SIMD_SUFFIX(name) name##_SSE42
38#else
39#define SIMD_SUFFIX(name) name
40#endif
41
Milo Yip0571a212015-04-15 22:36:00 +080042template <typename StreamType>
43void TestSkipWhitespace() {
Milo Yip74c8dcf2015-12-18 18:34:04 +080044 for (size_t step = 1; step < 32; step++) {
Milo Yip4d3c64a2015-04-15 21:07:30 +080045 char buffer[1025];
46 for (size_t i = 0; i < 1024; i++)
47 buffer[i] = " \t\r\n"[i % 4];
48 for (size_t i = 0; i < 1024; i += step)
49 buffer[i] = 'X';
50 buffer[1024] = '\0';
miloyipa32d8b72015-04-15 18:18:49 +080051
Milo Yip0571a212015-04-15 22:36:00 +080052 StreamType s(buffer);
Milo Yip4d3c64a2015-04-15 21:07:30 +080053 size_t i = 0;
54 for (;;) {
55 SkipWhitespace(s);
56 if (s.Peek() == '\0')
57 break;
58 EXPECT_EQ(i, s.Tell());
59 EXPECT_EQ('X', s.Take());
60 i += step;
61 }
miloyipa32d8b72015-04-15 18:18:49 +080062 }
63}
Milo Yip0571a212015-04-15 22:36:00 +080064
65TEST(SIMD, SIMD_SUFFIX(SkipWhitespace)) {
66 TestSkipWhitespace<StringStream>();
67 TestSkipWhitespace<InsituStringStream>();
68}
Milo Yipd258f592016-02-03 12:51:02 +080069
70struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnescapedStringHandler> {
Milo Yip021d7462016-02-03 13:10:55 +080071 bool String(const char* str, size_t length, bool) {
Milo Yipd258f592016-02-03 12:51:02 +080072 memcpy(buffer, str, length + 1);
73 return true;
74 }
75 char buffer[1024 + 5];
76};
77
78template <typename StreamType>
79void TestScanCopyUnescapedString() {
80 for (size_t step = 0; step < 1024; step++) {
81 char json[1024 + 5];
82 char *p = json;
83 *p ++= '\"';
84 for (size_t i = 0; i < step; i++)
85 *p++= "ABCD"[i % 4];
86 *p++ = '\\';
87 *p++ = '\\';
88 *p++ = '\"';
89 *p++ = '\0';
90
91 StreamType s(json);
92 Reader reader;
93 ScanCopyUnescapedStringHandler h;
94 reader.Parse(s, h);
95 EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0);
96 EXPECT_EQ('\\', h.buffer[step]); // escaped
97 EXPECT_EQ('\0', h.buffer[step + 1]);
98 }
99}
100
101TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
102 TestScanCopyUnescapedString<StringStream>();
103 TestScanCopyUnescapedString<InsituStringStream>();
104}