miloyip | 8d39282 | 2015-04-18 21:41:38 +0800 | [diff] [blame] | 1 | // 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. |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 4 | // |
miloyip | 8d39282 | 2015-04-18 21:41:38 +0800 | [diff] [blame] | 5 | // 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 |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 7 | // |
miloyip | 8d39282 | 2015-04-18 21:41:38 +0800 | [diff] [blame] | 8 | // http://opensource.org/licenses/MIT |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 9 | // |
miloyip | 8d39282 | 2015-04-18 21:41:38 +0800 | [diff] [blame] | 10 | // 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. |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 14 | |
| 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 | |
miloyip | ee50526 | 2015-04-15 18:34:18 +0800 | [diff] [blame] | 26 | #define RAPIDJSON_NAMESPACE rapidjson_simd |
| 27 | |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 28 | #include "unittest.h" |
| 29 | |
| 30 | #include "rapidjson/reader.h" |
| 31 | |
Milo Yip | cefae77 | 2016-02-03 13:29:25 +0800 | [diff] [blame] | 32 | #ifdef __GNUC__ |
| 33 | RAPIDJSON_DIAG_PUSH |
| 34 | RAPIDJSON_DIAG_OFF(effc++) |
| 35 | #endif |
| 36 | |
miloyip | ee50526 | 2015-04-15 18:34:18 +0800 | [diff] [blame] | 37 | using namespace rapidjson_simd; |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 38 | |
| 39 | #ifdef RAPIDJSON_SSE2 |
| 40 | #define SIMD_SUFFIX(name) name##_SSE2 |
| 41 | #elif defined(RAPIDJSON_SSE42) |
| 42 | #define SIMD_SUFFIX(name) name##_SSE42 |
| 43 | #else |
| 44 | #define SIMD_SUFFIX(name) name |
| 45 | #endif |
| 46 | |
Milo Yip | 0571a21 | 2015-04-15 22:36:00 +0800 | [diff] [blame] | 47 | template <typename StreamType> |
| 48 | void TestSkipWhitespace() { |
Milo Yip | 74c8dcf | 2015-12-18 18:34:04 +0800 | [diff] [blame] | 49 | for (size_t step = 1; step < 32; step++) { |
Milo Yip | 4d3c64a | 2015-04-15 21:07:30 +0800 | [diff] [blame] | 50 | char buffer[1025]; |
| 51 | for (size_t i = 0; i < 1024; i++) |
| 52 | buffer[i] = " \t\r\n"[i % 4]; |
| 53 | for (size_t i = 0; i < 1024; i += step) |
| 54 | buffer[i] = 'X'; |
| 55 | buffer[1024] = '\0'; |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 56 | |
Milo Yip | 0571a21 | 2015-04-15 22:36:00 +0800 | [diff] [blame] | 57 | StreamType s(buffer); |
Milo Yip | 4d3c64a | 2015-04-15 21:07:30 +0800 | [diff] [blame] | 58 | size_t i = 0; |
| 59 | for (;;) { |
| 60 | SkipWhitespace(s); |
| 61 | if (s.Peek() == '\0') |
| 62 | break; |
| 63 | EXPECT_EQ(i, s.Tell()); |
| 64 | EXPECT_EQ('X', s.Take()); |
| 65 | i += step; |
| 66 | } |
miloyip | a32d8b7 | 2015-04-15 18:18:49 +0800 | [diff] [blame] | 67 | } |
| 68 | } |
Milo Yip | 0571a21 | 2015-04-15 22:36:00 +0800 | [diff] [blame] | 69 | |
| 70 | TEST(SIMD, SIMD_SUFFIX(SkipWhitespace)) { |
| 71 | TestSkipWhitespace<StringStream>(); |
| 72 | TestSkipWhitespace<InsituStringStream>(); |
| 73 | } |
Milo Yip | d258f59 | 2016-02-03 12:51:02 +0800 | [diff] [blame] | 74 | |
| 75 | struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnescapedStringHandler> { |
Milo Yip | 021d746 | 2016-02-03 13:10:55 +0800 | [diff] [blame] | 76 | bool String(const char* str, size_t length, bool) { |
Milo Yip | d258f59 | 2016-02-03 12:51:02 +0800 | [diff] [blame] | 77 | memcpy(buffer, str, length + 1); |
| 78 | return true; |
| 79 | } |
| 80 | char buffer[1024 + 5]; |
| 81 | }; |
| 82 | |
Milo Yip | 7c72640 | 2016-02-03 13:48:39 +0800 | [diff] [blame^] | 83 | template <unsigned parseFlags, typename StreamType> |
Milo Yip | d258f59 | 2016-02-03 12:51:02 +0800 | [diff] [blame] | 84 | void TestScanCopyUnescapedString() { |
| 85 | for (size_t step = 0; step < 1024; step++) { |
| 86 | char json[1024 + 5]; |
| 87 | char *p = json; |
| 88 | *p ++= '\"'; |
| 89 | for (size_t i = 0; i < step; i++) |
| 90 | *p++= "ABCD"[i % 4]; |
| 91 | *p++ = '\\'; |
| 92 | *p++ = '\\'; |
| 93 | *p++ = '\"'; |
| 94 | *p++ = '\0'; |
| 95 | |
| 96 | StreamType s(json); |
| 97 | Reader reader; |
| 98 | ScanCopyUnescapedStringHandler h; |
Milo Yip | 7c72640 | 2016-02-03 13:48:39 +0800 | [diff] [blame^] | 99 | reader.Parse<parseFlags>(s, h); |
Milo Yip | d258f59 | 2016-02-03 12:51:02 +0800 | [diff] [blame] | 100 | EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0); |
| 101 | EXPECT_EQ('\\', h.buffer[step]); // escaped |
| 102 | EXPECT_EQ('\0', h.buffer[step + 1]); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) { |
Milo Yip | 7c72640 | 2016-02-03 13:48:39 +0800 | [diff] [blame^] | 107 | TestScanCopyUnescapedString<kParseDefaultFlags, StringStream>(); |
| 108 | TestScanCopyUnescapedString<kParseInsituFlag, InsituStringStream>(); |
Milo Yip | d258f59 | 2016-02-03 12:51:02 +0800 | [diff] [blame] | 109 | } |
Milo Yip | cefae77 | 2016-02-03 13:29:25 +0800 | [diff] [blame] | 110 | |
| 111 | #ifdef __GNUC__ |
| 112 | RAPIDJSON_DIAG_POP |
| 113 | #endif |