platformtest.cpp 3.4 KB

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