File: test_compress.cc

package info (click to toggle)
eckit 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,068 kB
  • sloc: cpp: 111,206; ansic: 2,826; yacc: 590; python: 418; lex: 361; sh: 162; makefile: 65
file content (141 lines) | stat: -rw-r--r-- 3,815 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cstring>

#include "eckit/io/Compress.h"
#include "eckit/io/MemoryHandle.h"
#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

static std::string test(const char* text) {
    char compressed[10240]   = {};
    char uncompressed[10240] = {};

    Compress compress(16);

    size_t s;
    size_t t;

    std::cout << "Text is [" << text << "]" << std::endl;

    {
        MemoryHandle in(text, strlen(text));
        MemoryHandle out(compressed, sizeof(compressed));

        s = compress.encode(in, out);
        std::cout << "Compress from " << strlen(text) << " to " << s << std::endl;
    }
    std::cout << "-----------------------" << std::endl;
    {
        MemoryHandle in(compressed, s);
        MemoryHandle out(uncompressed, sizeof(uncompressed));

        t = compress.decode(in, out);
        std::cout << "Uncompress from " << s << " to " << t << std::endl;
    }

    std::string u(uncompressed, uncompressed + t);

    std::cout << text << std::endl;
    std::cout << u << std::endl;

    for (size_t i = 0; i < std::min(u.size(), strlen(text)); ++i) {
        std::cout << ((text[i] == u[i]) ? ' ' : '|');
    }
    std::cout << std::endl;


    return u;
}

//----------------------------------------------------------------------------------------------------------------------

CASE("test_eckit_compress_1") {
    const char* pattern = "TOBEORNOTTOBEORTOBEORNOT#";

    std::string s = test(pattern);
    EXPECT(s == std::string(pattern));
}


CASE("test_eckit_compress_2") {
    const char* pattern = "ABCABCABCABCABCABC";

    std::string s = test(pattern);
    EXPECT(s == std::string(pattern));
}

CASE("test_eckit_compress_3") {
    const char* pattern = "";

    std::string s = test(pattern);
    EXPECT(s == std::string(pattern));
}


CASE("test_eckit_compress_4") {

    char original[102400];
    char compressed[102400 * 2];
    char uncompressed[102400];

    size_t s;
    size_t t;

    for (size_t i = 0; i < sizeof(original); ++i) {
        original[i] = i;
    }

    for (size_t bits = 16; bits >= 9; bits--) {
        std::cout << "Bits " << bits << std::endl;
        Compress compress(bits);

        {
            MemoryHandle in(original, sizeof(original));
            MemoryHandle out(compressed, sizeof(compressed));

            s = compress.encode(in, out);

            std::cout << "Compress from " << sizeof(original) << " to " << s << " "
                      << int(double(s) / double(sizeof(original)) * 100.0 + 0.5) << "%" << std::endl;
        }

        std::cout << "-----------------------" << std::endl;
        {
            MemoryHandle in(compressed, s);
            MemoryHandle out(uncompressed, sizeof(uncompressed));

            t = compress.decode(in, out);
            std::cout << "Uncompress from " << s << " to " << t << std::endl;
        }

        EXPECT(sizeof(original) == t);

        for (size_t i = 0; i < sizeof(original); ++i) {
            EXPECT(original[i] == uncompressed[i]);
        }
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}