simdtest.cpp 3.33 KB
Newer Older
1 2 3
// Tencent is pleased to support the open source community by making RapidJSON available.
// 
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
//
5 6
// Licensed under the MIT License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
7
//
8
// http://opensource.org/licenses/MIT
9
//
10 11 12 13
// Unless required by applicable law or agreed to in writing, software distributed 
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, either express or implied. See the License for the 
// specific language governing permissions and limitations under the License.
14 15 16 17 18 19 20 21 22 23 24 25

// Since Travis CI installs old Valgrind 3.7.0, which fails with some SSE4.2
// The unit tests prefix with SIMD should be skipped by Valgrind test

// __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler.
// We use -march=native with gmake to enable -msse2 and -msse4.2, if supported.
#if defined(__SSE4_2__)
#  define RAPIDJSON_SSE42
#elif defined(__SSE2__)
#  define RAPIDJSON_SSE2
#endif

26 27
#define RAPIDJSON_NAMESPACE rapidjson_simd

28 29 30 31
#include "unittest.h"

#include "rapidjson/reader.h"

Milo Yip's avatar
Milo Yip committed
32 33 34 35 36
#ifdef __GNUC__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
#endif

37
using namespace rapidjson_simd;
38 39 40 41 42 43 44 45 46

#ifdef RAPIDJSON_SSE2
#define SIMD_SUFFIX(name) name##_SSE2
#elif defined(RAPIDJSON_SSE42)
#define SIMD_SUFFIX(name) name##_SSE42
#else
#define SIMD_SUFFIX(name) name
#endif

47 48
template <typename StreamType>
void TestSkipWhitespace() {
Milo Yip's avatar
Milo Yip committed
49
    for (size_t step = 1; step < 32; step++) {
50 51 52 53 54 55 56
        char buffer[1025];
        for (size_t i = 0; i < 1024; i++)
            buffer[i] = " \t\r\n"[i % 4];
        for (size_t i = 0; i < 1024; i += step)
            buffer[i] = 'X';
        buffer[1024] = '\0';

57
        StreamType s(buffer);
58 59 60 61 62 63 64 65 66
        size_t i = 0;
        for (;;) {
            SkipWhitespace(s);
            if (s.Peek() == '\0')
                break;
            EXPECT_EQ(i, s.Tell());
            EXPECT_EQ('X', s.Take());
            i += step;
        }
67 68
    }
}
69 70 71 72 73

TEST(SIMD, SIMD_SUFFIX(SkipWhitespace)) {
    TestSkipWhitespace<StringStream>();
    TestSkipWhitespace<InsituStringStream>();
}
74 75

struct ScanCopyUnescapedStringHandler : BaseReaderHandler<UTF8<>, ScanCopyUnescapedStringHandler> {
Milo Yip's avatar
Milo Yip committed
76
    bool String(const char* str, size_t length, bool) {
77 78 79 80 81 82
        memcpy(buffer, str, length + 1);
        return true;
    }
    char buffer[1024 + 5];
};

83
template <unsigned parseFlags, typename StreamType>
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
void TestScanCopyUnescapedString() {
    for (size_t step = 0; step < 1024; step++) {
        char json[1024 + 5];
        char *p = json;
        *p ++= '\"';
        for (size_t i = 0; i < step; i++)
            *p++= "ABCD"[i % 4];
        *p++ = '\\';
        *p++ = '\\';
        *p++ = '\"';
        *p++ = '\0';

        StreamType s(json);
        Reader reader;
        ScanCopyUnescapedStringHandler h;
99
        reader.Parse<parseFlags>(s, h);
100 101 102 103 104 105 106
        EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0);
        EXPECT_EQ('\\', h.buffer[step]);    // escaped
        EXPECT_EQ('\0', h.buffer[step + 1]);
    }
}

TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) {
107 108
    TestScanCopyUnescapedString<kParseDefaultFlags, StringStream>();
    TestScanCopyUnescapedString<kParseInsituFlag, InsituStringStream>();
109
}
Milo Yip's avatar
Milo Yip committed
110 111 112 113

#ifdef __GNUC__
RAPIDJSON_DIAG_POP
#endif