~ubuntu-branches/ubuntu/natty/mimetic/natty

« back to all changes in this revision

Viewing changes to test/t.qp.h

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2006-06-16 13:16:07 UTC
  • Revision ID: james.westby@ubuntu.com-20060616131607-245mqjypkjuahq6b
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef _T_QP_H_
 
2
#define _T_QP_H_
 
3
#include <sstream>
 
4
#include <iterator>
 
5
#include <string>
 
6
#include "cutee.h"
 
7
#include <mimetic/codec/codec.h>
 
8
 
 
9
namespace mimetic 
 
10
{
 
11
 
 
12
class TEST_CLASS( test_qp )
 
13
{
 
14
    static const char* test[][3];
 
15
    static const char* test_decode_malformed[][2];
 
16
public:
 
17
    // calls qp.encode(char,out)
 
18
    void TEST_FUNCTION( testEncode )
 
19
    {
 
20
        int i = 0;
 
21
        while(test[i][0] != 0)
 
22
        {
 
23
            std::string src = test[i][0];
 
24
            std::string exp = test[i][1];
 
25
            std::string got;
 
26
            QP::Encoder qp;
 
27
            qp.maxlen(12);
 
28
            encode(src.begin(), src.end(), qp,std::back_inserter<std::string>(got));
 
29
            TEST_ASSERT_EQUALS_P( exp, got );
 
30
            i++;
 
31
        }
 
32
    }
 
33
    // calls qp.encode(InIt, InIt, ,out)
 
34
    void TEST_FUNCTION( testEncodeBlock )
 
35
    {
 
36
        int i = 0;
 
37
        while(test[i][0] != 0)
 
38
        {
 
39
            std::string src = test[i][0];
 
40
            std::string exp = test[i][1];
 
41
            std::string got;
 
42
            QP::Encoder qp;
 
43
            qp.maxlen(12);
 
44
            qp.process(src.begin(), src.end(), std::back_inserter<std::string>(got));
 
45
            TEST_ASSERT_EQUALS_P( exp, got);
 
46
            i++;
 
47
        }
 
48
    }
 
49
    void TEST_FUNCTION( testBinaryEncode )
 
50
    {
 
51
        int i = 0;
 
52
        while(test[i][0] != 0)
 
53
        {
 
54
            std::string src = test[i][0];
 
55
            std::string exp = (0 == test[i][2] ?test[i][1] : test[i][2]);
 
56
            std::string got;
 
57
            QP::Encoder qp(true);
 
58
            qp.maxlen(12);
 
59
            encode(src.begin(), src.end(),qp,std::back_inserter<std::string>(got));
 
60
            TEST_ASSERT_EQUALS_P( exp, got);
 
61
            i++;
 
62
        }
 
63
    }
 
64
    void TEST_FUNCTION( testBinaryEncodeBlock )
 
65
    {
 
66
        int i = 0;
 
67
        while(test[i][0] != 0)
 
68
        {
 
69
            std::string src = test[i][0];
 
70
            std::string exp = (0 == test[i][2] ?test[i][1] : test[i][2]);
 
71
            std::string got;
 
72
            QP::Encoder qp(true);
 
73
            qp.maxlen(12);
 
74
            qp.process(src.begin(), src.end(),std::back_inserter<std::string>(got));
 
75
            TEST_ASSERT_EQUALS_P( exp, got);
 
76
            i++;
 
77
        }
 
78
    }
 
79
    void TEST_FUNCTION( testDecode )
 
80
    {
 
81
        int i = 0;
 
82
        while(test[i][0] != 0)
 
83
        {
 
84
            std::string src = test[i][1];
 
85
            std::string exp = test[i][0];
 
86
            std::string got;
 
87
            QP::Decoder qp;
 
88
            decode(src.begin(), src.end(),qp,std::back_inserter<std::string>(got));
 
89
            TEST_ASSERT_EQUALS_P( exp, got);
 
90
            i++;
 
91
        }
 
92
    }
 
93
    void TEST_FUNCTION( testDecodeBlock )
 
94
    {
 
95
        int i = 0;
 
96
        while(test[i][0] != 0)
 
97
        {
 
98
            std::string src = test[i][1];
 
99
            std::string exp = test[i][0];
 
100
            std::string got;
 
101
            QP::Decoder qp;
 
102
            qp.process(src.begin(), src.end(),std::back_inserter<std::string>(got));
 
103
            TEST_ASSERT_EQUALS_P( exp, got);
 
104
            i++;
 
105
        }
 
106
    }
 
107
    void TEST_FUNCTION( testBinaryInputDecode )
 
108
    {
 
109
        int i = 0;
 
110
        while(test[i][2] != 0)
 
111
        {
 
112
            std::string src = test[i][2];
 
113
            std::string exp = test[i][0];
 
114
            std::string got;
 
115
            QP::Decoder qp;
 
116
            decode(src.begin(), src.end(),qp,std::back_inserter<std::string>(got));
 
117
            TEST_ASSERT_EQUALS_P( exp, got);
 
118
            i++;
 
119
        }
 
120
    }
 
121
    void TEST_FUNCTION( testBinaryInputDecodeBlock )
 
122
    {
 
123
        int i = 0;
 
124
        while(test[i][2] != 0)
 
125
        {
 
126
            std::string src = test[i][2];
 
127
            std::string exp = test[i][0];
 
128
            std::string got;
 
129
            QP::Decoder qp;
 
130
            qp.process(src.begin(), src.end(),std::back_inserter<std::string>(got));
 
131
            TEST_ASSERT_EQUALS_P( exp, got);
 
132
            i++;
 
133
        }
 
134
    }
 
135
    void TEST_FUNCTION( testMalformedInputDecode )
 
136
    {
 
137
        int i = 0;
 
138
        while(test_decode_malformed[i][0] != 0)
 
139
        {
 
140
            std::string src = test_decode_malformed[i][1];
 
141
            std::string exp = test_decode_malformed[i][0];
 
142
            std::string got;
 
143
            QP::Decoder qp;
 
144
            decode(src.begin(), src.end(),qp,std::back_inserter<std::string>(got));
 
145
            TEST_ASSERT_EQUALS_P( exp, got);
 
146
            i++;
 
147
        }
 
148
    }
 
149
    void TEST_FUNCTION( testMalformedInputDecodeBlock )
 
150
    {
 
151
        int i = 0;
 
152
        while(test_decode_malformed[i][0] != 0)
 
153
        {
 
154
            std::string src = test_decode_malformed[i][1];
 
155
            std::string exp = test_decode_malformed[i][0];
 
156
            std::string got;
 
157
            QP::Decoder qp;
 
158
            qp.process(src.begin(), src.end(),std::back_inserter<std::string>(got));
 
159
            TEST_ASSERT_EQUALS_P( exp, got);
 
160
            i++;
 
161
        }
 
162
    }
 
163
    // check correctness of static array QP::tb[]
 
164
    void TEST_FUNCTION( testTbValues )
 
165
    {
 
166
        short tb[256];
 
167
        for(int c = 0; c < 256; c++)
 
168
        {
 
169
            if (c != '=' && c > 32 && c < 127)
 
170
                tb[c] = QP::printable;
 
171
            else 
 
172
                tb[c] = QP::binary;
 
173
        }
 
174
        tb['\t'] = QP::tab; 
 
175
        tb[' '] = QP::sp;
 
176
        tb['='] = QP::unsafe;
 
177
        tb[QP::CR] = tb[QP::LF] = QP::newline;
 
178
        const char* unsafe = "!\"#$@[]\\^`{}|~";
 
179
        while(*unsafe != 0)
 
180
            tb[*unsafe++] = QP::unsafe;
 
181
        for(int i = 0; i < 256; i++)
 
182
        {
 
183
            TEST_ASSERT(tb[i] == QP::sTb[i]);
 
184
            //TEST_ASSERT("Correct struct: \n"+tbInitCode(tb), tb[i] == QP::sTb[i]);
 
185
        }
 
186
    }
 
187
private:
 
188
    std::string tbInitCode(short* tb)
 
189
    {
 
190
        std::ostringstream out;
 
191
        std::string comment, values;
 
192
        bool pcom = false;
 
193
        for(int i =0 ; i < 256; i++)
 
194
        {
 
195
            if(tb[i] == QP::printable)
 
196
            {
 
197
                comment += i;
 
198
                comment += ", ";
 
199
                pcom=true;
 
200
            } else {
 
201
                comment += "   ";
 
202
            }
 
203
            std::ostringstream oss;
 
204
            oss << tb[i] << ", ";
 
205
            values += oss.str();
 
206
            if((i+1) % 10 == 0)
 
207
            {
 
208
                if(pcom)
 
209
                    out << std::endl<< "// " << comment << std::endl;
 
210
                pcom = false;
 
211
                out << "   " << values << std::endl;
 
212
                comment = "";
 
213
                values = "";
 
214
            }
 
215
        }
 
216
        if(pcom)
 
217
             out << std::endl<< "// " << comment << std::endl;
 
218
        out << "   " << values << std::endl;
 
219
        out << std::endl;
 
220
        return out.str();
 
221
    }
 
222
 
 
223
};
 
224
 
 
225
}
 
226
 
 
227
#endif