testlz77.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <stdlib.h>
  2. #include <sys/time.h>
  3. #include <string>
  4. #include <iostream>
  5. struct bm {
  6. struct timeval b;
  7. std::string msg;
  8. bm(const std::string& s) : msg(s) {
  9. gettimeofday(&b, NULL);
  10. }
  11. ~bm() {
  12. struct timeval e;
  13. gettimeofday(&e, NULL);
  14. size_t a = (e.tv_sec*1e6 + e.tv_usec);
  15. size_t q = (b.tv_sec*1e6 + b.tv_usec);
  16. std::cout << msg << ": " << ((double)a-(double)q)/1e6 << std::endl;
  17. }
  18. };
  19. struct bm_s {
  20. struct timeval b;
  21. double& _s;
  22. bm_s(double& s) : _s(s) {
  23. gettimeofday(&b, NULL);
  24. }
  25. ~bm_s() {
  26. struct timeval e;
  27. gettimeofday(&e, NULL);
  28. size_t a = (e.tv_sec*1e6 + e.tv_usec);
  29. size_t q = (b.tv_sec*1e6 + b.tv_usec);
  30. _s += ((double)a-(double)q)/1e6;
  31. }
  32. };
  33. #include "lz77.h"
  34. #include <fstream>
  35. int main(int argc, char** argv) {
  36. std::string inp(argv[3]);
  37. if (inp == "-f") {
  38. bm _x1("reading");
  39. std::ifstream in(argv[4], std::ios::in | std::ios::binary);
  40. if (!in)
  41. return 1;
  42. std::string data;
  43. in.seekg(0, std::ios::end);
  44. data.resize(in.tellg());
  45. in.seekg(0, std::ios::beg);
  46. in.read(&data[0], data.size());
  47. in.close();
  48. inp.swap(data);
  49. } else if (inp == "-d") {
  50. std::ifstream in(argv[4], std::ios::in | std::ios::binary);
  51. if (!in)
  52. return 1;
  53. std::string data;
  54. in.seekg(0, std::ios::end);
  55. data.resize(in.tellg());
  56. in.seekg(0, std::ios::beg);
  57. in.read(&data[0], data.size());
  58. in.close();
  59. inp.swap(data);
  60. lz77::decompress_t decompress;
  61. std::string extra;
  62. for (unsigned char cc : inp) {
  63. decompress.feed(std::string(1, cc), extra);
  64. }
  65. std::ifstream in2(argv[5], std::ios::in | std::ios::binary);
  66. if (!in2)
  67. return 1;
  68. data.clear();
  69. in2.seekg(0, std::ios::end);
  70. data.resize(in2.tellg());
  71. in2.seekg(0, std::ios::beg);
  72. in2.read(&data[0], data.size());
  73. in2.close();
  74. inp.swap(data);
  75. for (unsigned char cc : inp) {
  76. decompress.feed(std::string(1, cc), extra);
  77. }
  78. std::cout << std::string(decompress.outb, decompress.out) << std::endl;
  79. return 0;
  80. }
  81. size_t a = ::atoi(argv[1]);
  82. size_t b = ::atoi(argv[2]);
  83. std::string out;
  84. {
  85. bm _x2("compressing");
  86. out = lz77::compress(inp, a, b);
  87. }
  88. std::cout << "c." << out.size() << " uc." << inp.size() << std::endl;
  89. lz77::decompress_t decompress;
  90. {
  91. bm _x2("decompressing");
  92. std::string extra;
  93. /*
  94. size_t nb = 0;
  95. size_t ne = std::max(out.size(), (size_t)256);
  96. bool done = decompress.start(std::string(out.begin() + nb, out.begin() + ne), extra);
  97. while (!done) {
  98. size_t tmp = ne;
  99. ne = std::max(out.size(), ne + 256);
  100. nb = tmp;
  101. done = decompress.more(std::string(out.begin() + nb, out.begin() + ne), extra);
  102. }
  103. */
  104. if (!decompress.feed(out, extra) || extra.size() > 0) {
  105. std::cout << "Sanity error: failed to decompress whole buffer." << std::endl;
  106. return 1;
  107. }
  108. }
  109. if (decompress.result() != inp) {
  110. std::cout << "Compression-decompression equivalence test failed!" << std::endl;
  111. if (decompress.result().size() < 1024)
  112. std::cout << " " << decompress.result() << std::endl;
  113. return 1;
  114. }
  115. return 0;
  116. }