1 // Tencent is pleased to support the open source community by making RapidJSON available.
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
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
8 // http://opensource.org/licenses/MIT
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.
17 // This file is for giving the performance characteristics of the platform (compiler/OS/CPU).
30 #if defined(unix) || defined(__unix__) || defined(__unix)
32 #ifdef _POSIX_MAPPED_FILES
37 class Platform : public PerfTest {
39 virtual void SetUp() {
42 // temp buffer for testing
43 temp_ = (char *)malloc(length_ + 1);
44 memcpy(temp_, json_, length_);
45 checkSum_ = CheckSum();
50 for (size_t i = 0; i < length_; ++i)
55 virtual void TearDown() {
65 TEST_F(Platform, CheckSum) {
66 for (int i = 0; i < kTrialCount; i++)
67 EXPECT_EQ(checkSum_, CheckSum());
70 TEST_F(Platform, strlen) {
71 for (int i = 0; i < kTrialCount; i++) {
72 size_t l = strlen(json_);
73 EXPECT_EQ(length_, l);
77 TEST_F(Platform, memcmp) {
78 for (int i = 0; i < kTrialCount; i++) {
79 EXPECT_EQ(0, memcmp(temp_, json_, length_));
83 TEST_F(Platform, pow) {
85 for (int i = 0; i < kTrialCount * kTrialCount; i++)
86 sum += pow(10.0, i & 255);
90 TEST_F(Platform, Whitespace_strlen) {
91 for (int i = 0; i < kTrialCount; i++) {
92 size_t l = strlen(whitespace_);
93 EXPECT_GT(l, whitespace_length_);
97 TEST_F(Platform, Whitespace_strspn) {
98 for (int i = 0; i < kTrialCount; i++) {
99 size_t l = strspn(whitespace_, " \n\r\t");
100 EXPECT_EQ(whitespace_length_, l);
104 TEST_F(Platform, fread) {
105 for (int i = 0; i < kTrialCount; i++) {
106 FILE *fp = fopen(filename_, "rb");
107 ASSERT_EQ(length_, fread(temp_, 1, length_, fp));
108 EXPECT_EQ(checkSum_, CheckSum());
114 TEST_F(Platform, read) {
115 for (int i = 0; i < kTrialCount; i++) {
116 int fd = _open(filename_, _O_BINARY | _O_RDONLY);
118 ASSERT_EQ(length_, _read(fd, temp_, length_));
119 EXPECT_EQ(checkSum_, CheckSum());
124 TEST_F(Platform, read) {
125 for (int i = 0; i < kTrialCount; i++) {
126 int fd = open(filename_, O_RDONLY);
128 ASSERT_EQ(length_, read(fd, temp_, length_));
129 EXPECT_EQ(checkSum_, CheckSum());
136 TEST_F(Platform, MapViewOfFile) {
137 for (int i = 0; i < kTrialCount; i++) {
138 HANDLE file = CreateFile(filename_, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
139 ASSERT_NE(INVALID_HANDLE_VALUE, file);
140 HANDLE mapObject = CreateFileMapping(file, NULL, PAGE_READONLY, 0, length_, NULL);
141 ASSERT_NE(INVALID_HANDLE_VALUE, mapObject);
142 void *p = MapViewOfFile(mapObject, FILE_MAP_READ, 0, 0, length_);
143 ASSERT_TRUE(p != NULL);
144 EXPECT_EQ(checkSum_, CheckSum());
145 ASSERT_TRUE(UnmapViewOfFile(p) == TRUE);
146 ASSERT_TRUE(CloseHandle(mapObject) == TRUE);
147 ASSERT_TRUE(CloseHandle(file) == TRUE);
152 #ifdef _POSIX_MAPPED_FILES
153 TEST_F(Platform, mmap) {
154 for (int i = 0; i < kTrialCount; i++) {
155 int fd = open(filename_, O_RDONLY);
157 void *p = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0);
158 ASSERT_TRUE(p != NULL);
159 EXPECT_EQ(checkSum_, CheckSum());
166 #endif // TEST_PLATFORM