platformtest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.
  4. //
  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
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  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.
  14. #include "perftest.h"
  15. // This file is for giving the performance characteristics of the platform (compiler/OS/CPU).
  16. #if TEST_PLATFORM
  17. #include <cmath>
  18. #include <fcntl.h>
  19. // Windows
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #endif
  23. // UNIX
  24. #if defined(unix) || defined(__unix__) || defined(__unix)
  25. #include <unistd.h>
  26. #ifdef _POSIX_MAPPED_FILES
  27. #include <sys/mman.h>
  28. #endif
  29. #endif
  30. class Platform : public PerfTest {
  31. public:
  32. virtual void SetUp() {
  33. PerfTest::SetUp();
  34. // temp buffer for testing
  35. temp_ = (char *)malloc(length_ + 1);
  36. memcpy(temp_, json_, length_);
  37. checkSum_ = CheckSum();
  38. }
  39. char CheckSum() {
  40. char c = 0;
  41. for (size_t i = 0; i < length_; ++i)
  42. c += temp_[i];
  43. return c;
  44. }
  45. virtual void TearDown() {
  46. PerfTest::TearDown();
  47. free(temp_);
  48. }
  49. protected:
  50. char *temp_;
  51. char checkSum_;
  52. };
  53. TEST_F(Platform, CheckSum) {
  54. for (int i = 0; i < kTrialCount; i++)
  55. EXPECT_EQ(checkSum_, CheckSum());
  56. }
  57. TEST_F(Platform, strlen) {
  58. for (int i = 0; i < kTrialCount; i++) {
  59. size_t l = strlen(json_);
  60. EXPECT_EQ(length_, l);
  61. }
  62. }
  63. TEST_F(Platform, memcmp) {
  64. for (int i = 0; i < kTrialCount; i++) {
  65. EXPECT_EQ(0, memcmp(temp_, json_, length_));
  66. }
  67. }
  68. TEST_F(Platform, pow) {
  69. double sum = 0;
  70. for (int i = 0; i < kTrialCount * kTrialCount; i++)
  71. sum += pow(10.0, i & 255);
  72. EXPECT_GT(sum, 0.0);
  73. }
  74. TEST_F(Platform, Whitespace_strlen) {
  75. for (int i = 0; i < kTrialCount; i++) {
  76. size_t l = strlen(whitespace_);
  77. EXPECT_GT(l, whitespace_length_);
  78. }
  79. }
  80. TEST_F(Platform, Whitespace_strspn) {
  81. for (int i = 0; i < kTrialCount; i++) {
  82. size_t l = strspn(whitespace_, " \n\r\t");
  83. EXPECT_EQ(whitespace_length_, l);
  84. }
  85. }
  86. TEST_F(Platform, fread) {
  87. for (int i = 0; i < kTrialCount; i++) {
  88. FILE *fp = fopen(filename_, "rb");
  89. ASSERT_EQ(length_, fread(temp_, 1, length_, fp));
  90. EXPECT_EQ(checkSum_, CheckSum());
  91. fclose(fp);
  92. }
  93. }
  94. #ifdef _MSC_VER
  95. TEST_F(Platform, read) {
  96. for (int i = 0; i < kTrialCount; i++) {
  97. int fd = _open(filename_, _O_BINARY | _O_RDONLY);
  98. ASSERT_NE(-1, fd);
  99. ASSERT_EQ(length_, _read(fd, temp_, length_));
  100. EXPECT_EQ(checkSum_, CheckSum());
  101. _close(fd);
  102. }
  103. }
  104. #else
  105. TEST_F(Platform, read) {
  106. for (int i = 0; i < kTrialCount; i++) {
  107. int fd = open(filename_, O_RDONLY);
  108. ASSERT_NE(-1, fd);
  109. ASSERT_EQ(length_, read(fd, temp_, length_));
  110. EXPECT_EQ(checkSum_, CheckSum());
  111. close(fd);
  112. }
  113. }
  114. #endif
  115. #ifdef _WIN32
  116. TEST_F(Platform, MapViewOfFile) {
  117. for (int i = 0; i < kTrialCount; i++) {
  118. HANDLE file = CreateFile(filename_, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  119. ASSERT_NE(INVALID_HANDLE_VALUE, file);
  120. HANDLE mapObject = CreateFileMapping(file, NULL, PAGE_READONLY, 0, length_, NULL);
  121. ASSERT_NE(INVALID_HANDLE_VALUE, mapObject);
  122. void *p = MapViewOfFile(mapObject, FILE_MAP_READ, 0, 0, length_);
  123. ASSERT_TRUE(p != NULL);
  124. EXPECT_EQ(checkSum_, CheckSum());
  125. ASSERT_TRUE(UnmapViewOfFile(p) == TRUE);
  126. ASSERT_TRUE(CloseHandle(mapObject) == TRUE);
  127. ASSERT_TRUE(CloseHandle(file) == TRUE);
  128. }
  129. }
  130. #endif
  131. #ifdef _POSIX_MAPPED_FILES
  132. TEST_F(Platform, mmap) {
  133. for (int i = 0; i < kTrialCount; i++) {
  134. int fd = open(filename_, O_RDONLY);
  135. ASSERT_NE(-1, fd);
  136. void *p = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0);
  137. ASSERT_TRUE(p != NULL);
  138. EXPECT_EQ(checkSum_, CheckSum());
  139. munmap(p, length_);
  140. close(fd);
  141. }
  142. }
  143. #endif
  144. #endif // TEST_PLATFORM