l.cpp
Upload User: fxdzsc
Upload Date: 2021-02-18
Package Size: 1k
Code Size: 2k
Development Platform:

C/C++

  1. #include <codecogs/io/compression/lzw.h>
  2. #include <iostream>
  3.  
  4. // the number of characters to generate in the sample array
  5. #define N 10000
  6.  
  7. using namespace IO::Compression;
  8.  
  9. int main()
  10. {
  11.   // initialize random seed
  12.   srand(time(0));
  13.  
  14.   // generate an array of N random letters
  15.   std::vector<unsigned char> sample;
  16.   for (int i = 0; i < N; ++i)
  17.     sample.push_back('A' + rand() % ('Z' - 'A' + 1));
  18.  
  19.   // compress the sample array
  20.   std::vector<unsigned char> compressed = LZW::compress(sample);
  21.  
  22.   // decompress the compressed array
  23.   std::vector<unsigned char> uncompressed = LZW::decompress(compressed);
  24.  
  25.   // compare the sizes of the compressed and uncompressed arrays
  26.   std::cout << "      Size of the sample array: " << N << std::endl;
  27.   std::cout << "  Size of the compressed array: " << compressed.size() << std::endl;
  28.   std::cout << "Size of the uncompressed array: " << uncompressed.size() << std::endl;
  29.  
  30.   std::cout << std::endl;
  31.  
  32.   // test if the sample and the uncompressed arrays are identical
  33.   // this proves that the LZW compression algorithm does not affect the initial data
  34.   bool identical = (N == uncompressed.size());
  35.   for (size_t i = 0; identical && i < uncompressed.size(); ++i)
  36.     if (sample[i] != uncompressed[i])
  37.       identical = false;
  38.  
  39.   if (identical)
  40.     std::cout << "The sample and uncompressed arrays are identical." << std::endl;
  41.   else
  42.     std::cout << "Error! The sample and uncompressed arrays are NOT identical." << std::endl;
  43.  
  44.   return 0;
  45. }