⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_compress.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: test_compress.cpp,v $ * PRODUCTION Revision 1000.2  2004/06/01 19:42:40  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9 * PRODUCTION * =========================================================================== *//*  $Id: test_compress.cpp,v 1000.2 2004/06/01 19:42:40 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author:  Vladimir Ivanov * * File Description:  Test program for the Compression API * */#include <ncbi_pch.hpp>#include <corelib/ncbiapp.hpp>#include <corelib/ncbiargs.hpp>#include <corelib/ncbi_limits.hpp>#include <corelib/ncbifile.hpp>#include <util/compress/bzip2.hpp>#include <util/compress/zlib.hpp>#include <test/test_assert.h>  // This header must go lastUSING_NCBI_SCOPE;const size_t        kDataLen    = 100*1024;const size_t        kBufLen     = 102*1024;const int           kUnknownErr = kMax_Int;const unsigned int  kUnknown    = kMax_UInt;////////////////////////////////////////////////////////////////////////////////// The template class for compressor test//template<class TCompression,         class TCompressionFile,         class TStreamCompressor,         class TStreamDecompressor>class CTestCompressor{public:    // Run test for the template compressor usind data from "src_buf"    static void Run(const char* src_buf);    // Print out compress/decompress status    enum EPrintType {         eCompress,        eDecompress     };    static void PrintResult(EPrintType type, int last_errcode,                            unsigned int src_len,                            unsigned int dst_len,                            unsigned int out_len);};// Print OK message#define OK cout << "OK\n\n"// Init destination buffers#define INIT_BUFFERS  memset(dst_buf, 0, kBufLen); memset(cmp_buf, 0, kBufLen)template<class TCompression,    class TCompressionFile,    class TStreamCompressor,    class TStreamDecompressor>void CTestCompressor<TCompression, TCompressionFile,    TStreamCompressor, TStreamDecompressor>    ::Run(const char* src_buf){    char* dst_buf = new char[kBufLen];    char* cmp_buf = new char[kBufLen];    unsigned int dst_len, out_len;    bool result;    assert(dst_buf);    assert(cmp_buf);    //------------------------------------------------------------------------    // Compress/decomress buffer    //------------------------------------------------------------------------    {{        cout << "Testing default level compression...\n";        INIT_BUFFERS;        // Compress data        TCompression c(CCompression::eLevel_Medium);        result = c.CompressBuffer(src_buf, kDataLen, dst_buf, kBufLen,                                  &out_len);        PrintResult(eCompress, c.GetErrorCode(), kDataLen, kBufLen, out_len);        assert(result);        // Decompress data        dst_len = out_len;        result = c.DecompressBuffer(dst_buf, dst_len, cmp_buf, kBufLen,                                    &out_len);        PrintResult(eDecompress, c.GetErrorCode(), dst_len, kBufLen,out_len);        assert(result);        assert(out_len == kDataLen);        // Compare original and decompressed data        assert(memcmp(src_buf, cmp_buf, out_len) == 0);        OK;    }}    //------------------------------------------------------------------------    // Overflow test    //------------------------------------------------------------------------    {{        cout << "Output buffer overflow test...\n";        TCompression c;        dst_len = 100;        result = c.CompressBuffer(src_buf, kDataLen, dst_buf, dst_len,                                  &out_len);        PrintResult(eCompress, c.GetErrorCode(), kDataLen, dst_len, out_len);        assert(!result);        assert(out_len == dst_len);        OK;    }}    //------------------------------------------------------------------------    // File compress/decompress test    //------------------------------------------------------------------------    {{        cout << "File compress/decompress test...\n";        INIT_BUFFERS;        int n;        TCompressionFile zfile;        const string kFileName = "compressed.file";        // Compress data to file        assert(zfile.Open(kFileName, TCompressionFile::eMode_Write));         for (unsigned int i=0; i < kDataLen/1024; i++) {            n = zfile.Write(src_buf + i*1024, 1024);            assert(n == 1024);        }        assert(zfile.Close());         assert(CFile(kFileName).GetLength() > 0);                // Decompress data from file        assert(zfile.Open(kFileName, TCompressionFile::eMode_Read));         assert(zfile.Read(cmp_buf, kDataLen) == (int)kDataLen);        assert(zfile.Close());         // Compare original and decompressed data        assert(memcmp(src_buf, cmp_buf, kDataLen) == 0);        // Second test        INIT_BUFFERS;        {{            TCompressionFile zf(kFileName, TCompressionFile::eMode_Write,                                CCompression::eLevel_Best);            n = zf.Write(src_buf, kDataLen);            assert(n == (int)kDataLen);        }}        {{            TCompressionFile zf(kFileName, TCompressionFile::eMode_Read);            int nread = 0;            do {                n = zf.Read(cmp_buf + nread, 100);                assert(n >= 0);                nread += n;            } while ( n != 0 );            assert(nread == (int)kDataLen);        }}        // Compare original and decompressed data        assert(memcmp(src_buf, cmp_buf, kDataLen) == 0);        CFile(kFileName).Remove();        OK;    }}    //------------------------------------------------------------------------    // Compression input stream test    //------------------------------------------------------------------------    {{        cout << "Testing compression input stream...\n";        INIT_BUFFERS;        // Compression input stream test         CNcbiIstrstream is_str(src_buf, kDataLen);        CCompressionIStream ics_zip(is_str, new TStreamCompressor(),                                    CCompressionStream::fOwnProcessor);        // Read as much as possible        ics_zip.read(dst_buf, kBufLen);        dst_len = ics_zip.gcount();        assert(ics_zip.eof());        ics_zip.Finalize();        // Read the residue of the data after the compression finalization        if ( dst_len < kDataLen ) {            ics_zip.clear();            ics_zip.read(dst_buf + dst_len, kBufLen - dst_len);            dst_len += ics_zip.gcount();        }        PrintResult(eCompress, kUnknownErr, kDataLen, kUnknown, dst_len);        assert(ics_zip.GetProcessedSize() == kDataLen);        assert(ics_zip.GetOutputSize() == dst_len);        // Compress the data        TCompression c;        result = c.DecompressBuffer(dst_buf, dst_len, cmp_buf, kBufLen,                                    &out_len);        PrintResult(eDecompress, c.GetErrorCode(), dst_len, kBufLen, out_len);        assert(result);        // Compare original and uncompressed data        assert(out_len == kDataLen);        assert(memcmp(src_buf, cmp_buf, kDataLen) == 0);        OK;   }}    //------------------------------------------------------------------------    // Decompression input stream test    //------------------------------------------------------------------------    {{        cout << "Testing decompression input stream...\n";        INIT_BUFFERS;        // Compress the data        TCompression c;        result = c.CompressBuffer(src_buf, kDataLen, dst_buf, kBufLen,                                  &out_len);        PrintResult(eCompress, c.GetErrorCode(), kDataLen, kBufLen, out_len);        assert(result);        // Read decompressed data from stream        CNcbiIstrstream is_str(dst_buf, out_len);        size_t ids_zip_len;        {{            CCompressionIStream ids_zip(is_str, new TStreamDecompressor(),                                    CCompressionStream::fOwnReader);            ids_zip.read(cmp_buf, kDataLen);            ids_zip_len = ids_zip.gcount();            // For majority of decompressors we should have all unpacked data            // here, before the finalization.            assert(ids_zip.GetProcessedSize() == out_len);            assert(ids_zip.GetOutputSize() == kDataLen);            // Finalize decompression stream in the destructor        }}        // Get decompressed size        PrintResult(eDecompress, kUnknownErr, out_len, kBufLen, ids_zip_len);        // Compare original and uncompressed data        assert(ids_zip_len == kDataLen);        assert(memcmp(src_buf, cmp_buf, kDataLen) == 0);        OK;    }}    //------------------------------------------------------------------------    // Compression output stream test    //------------------------------------------------------------------------    {{        cout << "Testing compression output stream...\n";        INIT_BUFFERS;        // Write data to compressing stream        CNcbiOstrstream os_str;        {{            CCompressionOStream os_zip(os_str, new TStreamCompressor(),                                       CCompressionStream::fOwnWriter);            os_zip.write(src_buf, kDataLen);            // Finalize compression stream in the destructor        }}        // Get compressed size        const char* str = os_str.str();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -