ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

142 lines
3.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //#define DEBUG_FILE_BUFFER
  9. #include "http_file_buffer.h"
  10. #include "test.h"
  11. #include <iostream>
  12. #include <istream>
  13. #include <ostream>
  14. #include <iomanip>
  15. char cfpos(int c)
  16. {
  17. return '0' + c % 10;
  18. }
  19. void test_read(std::istream &is,int &oldpos,int size)
  20. {
  21. //static_cast<cppcms::http::impl::file_buffer *>(is.rdbuf())->status("Before");
  22. for(int i=oldpos;i<size;i++) {
  23. int r = is.get();
  24. if(r!=cfpos(i))
  25. printf("pos=%d %d [%c] %c\n",i,r,r,cfpos(i));
  26. TEST(r==cfpos(i));
  27. }
  28. TEST(is.get() == EOF);
  29. is.clear();
  30. oldpos = size;
  31. int limit = 100;
  32. if(limit > size * 2)
  33. limit = size * 2;
  34. for(int i=0;i<limit;i++) {
  35. int pos = rand() % size;
  36. int chunk = rand() % size + 1;
  37. if(chunk+pos > size) {
  38. chunk = size - pos;
  39. }
  40. switch(i%4) {
  41. case 0: is.seekg(pos); break;
  42. case 1: is.seekg(pos,std::ios_base::beg); break;
  43. case 2: is.seekg(pos - size,std::ios_base::end); break;
  44. case 3: is.seekg(pos - oldpos,std::ios_base::cur); break;
  45. }
  46. TEST(int(is.tellg()) == pos);
  47. for(int j=0;j<chunk;j++) {
  48. TEST(is.get() == cfpos(pos + j));
  49. }
  50. oldpos = pos + chunk;
  51. TEST(int(is.tellg()) == oldpos);
  52. if(oldpos == size) {
  53. TEST(is.get()==-1);
  54. is.clear();
  55. TEST(int(is.tellg()) == oldpos);
  56. }
  57. if(rand() % 5==0 && oldpos > 0) {
  58. int back = rand() % oldpos + 1;
  59. for(int i=0;i<back;i++) {
  60. TEST(is.unget());
  61. }
  62. oldpos -= back;
  63. }
  64. TEST(int(is.tellg()) == oldpos);
  65. }
  66. }
  67. void test(int const size,int msize)
  68. {
  69. std::cout << "Testing for file size = " << std::setw(10) << size << " in memory size " << std::setw(8) << msize << std::endl;
  70. int limit = 100;
  71. if(limit > size * 2)
  72. limit = size * 2;
  73. for(int i=0;i<limit;i++) {
  74. cppcms::http::impl::file_buffer fb(msize);
  75. fb.temp_dir(".");
  76. try {
  77. std::istream in(&fb);
  78. std::ostream out(&fb);
  79. int rpos = 0;
  80. for(int csize = 0;csize < size;) {
  81. int chunk = rand() % size + 1;
  82. if(chunk + csize > size)
  83. chunk = size - csize;
  84. for(int i=0;i<chunk;i++) {
  85. out << cfpos(csize + i);
  86. TEST(out);
  87. }
  88. csize += chunk;
  89. test_read(in,rpos,csize);
  90. }
  91. out<<std::flush;
  92. if(fb.size() != size)
  93. printf("%d %d\n",int(fb.size()),size);
  94. TEST(fb.size() == size);
  95. TEST(int(out.tellp())==size);
  96. TEST(fb.in_memory() == (size <= msize));
  97. }
  98. catch(...) {
  99. fb.close();
  100. if(!fb.name().empty())
  101. booster::nowide::remove(fb.name().c_str());
  102. throw;
  103. }
  104. fb.close();
  105. TEST(fb.name().empty() == fb.in_memory());
  106. if(!fb.in_memory()) {
  107. fflush(stdout);
  108. FILE *f=booster::nowide::fopen(fb.name().c_str(),"rb");
  109. TEST(f);
  110. for(int i=0;i<size;i++) {
  111. TEST(fgetc(f)==cfpos(i));
  112. }
  113. TEST(fgetc(f)==EOF);
  114. fclose(f);
  115. booster::nowide::remove(fb.name().c_str());
  116. }
  117. }
  118. }
  119. int main()
  120. {
  121. try {
  122. for(int size = 1; size <= 10000; size*= 10) {
  123. int inmem[] = { 0, 16, 1024, 20000, -1 };
  124. for(int m=0;inmem[m] != -1;m++) {
  125. test(size,inmem[m]);
  126. }
  127. }
  128. }
  129. catch(std::exception const &e) {
  130. std::cerr << "FAIL: " << e.what() << std::endl;
  131. return 1;
  132. }
  133. std::cout << "Ok" << std::endl;
  134. return 0;
  135. }