summaryrefslogtreecommitdiff
path: root/src/test/cpp/helpers/transcodertestcase.cpp
blob: 9711317ce9ebfd1d6f10d4f7fb348270c45be69f (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log4cxx/helpers/transcoder.h>
#include "../insertwide.h"
#include "../logunit.h"


using namespace log4cxx;
using namespace log4cxx::helpers;


LOGUNIT_CLASS(TranscoderTestCase)
{
        LOGUNIT_TEST_SUITE(TranscoderTestCase);
                LOGUNIT_TEST(decode1);
#if LOG4CXX_WCHAR_T_API
                LOGUNIT_TEST(decode2);
#endif
                LOGUNIT_TEST(decode3);
#if LOG4CXX_WCHAR_T_API
                LOGUNIT_TEST(decode4);
#endif
                LOGUNIT_TEST(decode7);
                LOGUNIT_TEST(decode8);
#if LOG4CXX_WCHAR_T_API
                LOGUNIT_TEST(encode1);
#endif
                LOGUNIT_TEST(encode2);
#if LOG4CXX_WCHAR_T_API
                LOGUNIT_TEST(encode3);
#endif
                LOGUNIT_TEST(encode4);
#if LOG4CXX_WCHAR_T_API
                LOGUNIT_TEST(encode5);
#endif
                LOGUNIT_TEST(encode6);
                LOGUNIT_TEST(testDecodeUTF8_1);
                LOGUNIT_TEST(testDecodeUTF8_2);
                LOGUNIT_TEST(testDecodeUTF8_3);
                LOGUNIT_TEST(testDecodeUTF8_4);
#if LOG4CXX_UNICHAR_API
                LOGUNIT_TEST(udecode2);
                LOGUNIT_TEST(udecode4);
                LOGUNIT_TEST(uencode1);
                LOGUNIT_TEST(uencode3);
                LOGUNIT_TEST(uencode5);
#endif
                
        LOGUNIT_TEST_SUITE_END();


public:
        void decode1() {
          const char* greeting = "Hello, World";
          LogString decoded(LOG4CXX_STR("foo\n"));
          Transcoder::decode(greeting, decoded);
          LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\nHello, World"), decoded);
        }

#if LOG4CXX_WCHAR_T_API
        void decode2() {
          const wchar_t* greeting = L"Hello, World";
          LogString decoded(LOG4CXX_STR("foo\n"));
          Transcoder::decode(greeting, decoded);
          LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\nHello, World"), decoded);
        }
#endif

        void decode3() {
           const char* nothing = "";
           LogString decoded(LOG4CXX_STR("foo\n"));
           Transcoder::decode(nothing, decoded);
           LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\n"), decoded);
        }

#if LOG4CXX_WCHAR_T_API
        void decode4() {
            const wchar_t* nothing = L"";
            LogString decoded(LOG4CXX_STR("foo\n"));
            Transcoder::decode(nothing, decoded);
            LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\n"), decoded);
        }
#endif


        enum { BUFSIZE = 255 };

        void decode7() {
            //
            //   normal characters striding over a buffer boundary
            //
            std::string longMsg(BUFSIZE - 2, 'A');
            longMsg.append("Hello");
            LogString decoded;
            Transcoder::decode(longMsg, decoded);
            LOGUNIT_ASSERT_EQUAL((size_t) BUFSIZE + 3, decoded.length());
            LOGUNIT_ASSERT_EQUAL(LogString(BUFSIZE -2, LOG4CXX_STR('A')),
                  decoded.substr(0, BUFSIZE - 2));
            LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("Hello"),
                  decoded.substr(BUFSIZE -2 ));
        }

        void decode8() {
            std::string msg("Hello, World.");
            LogString actual;
            Transcoder::decode(msg, actual);
            LogString expected(LOG4CXX_STR("Hello, World."));
            LOGUNIT_ASSERT_EQUAL(expected, actual);
        }


#if LOG4CXX_WCHAR_T_API
        void encode1() {
          const LogString greeting(LOG4CXX_STR("Hello, World"));
          std::wstring encoded;
          Transcoder::encode(greeting, encoded);
          LOGUNIT_ASSERT_EQUAL((std::wstring) L"Hello, World", encoded);
        }
#endif

        void encode2() {
          const LogString greeting(LOG4CXX_STR("Hello, World"));
          std::string encoded;
          Transcoder::encode(greeting, encoded);
          LOGUNIT_ASSERT_EQUAL((std::string) "Hello, World", encoded);
        }

#if LOG4CXX_WCHAR_T_API
        void encode3() {
          LogString greeting(BUFSIZE - 3, LOG4CXX_STR('A'));
          greeting.append(LOG4CXX_STR("Hello"));
          std::wstring encoded;
          Transcoder::encode(greeting, encoded);
          std::wstring manyAs(BUFSIZE - 3, L'A');
          LOGUNIT_ASSERT_EQUAL(manyAs, encoded.substr(0, BUFSIZE - 3));
          LOGUNIT_ASSERT_EQUAL(std::wstring(L"Hello"), encoded.substr(BUFSIZE - 3));
        }
#endif

        void encode4() {
          LogString greeting(BUFSIZE - 3, LOG4CXX_STR('A'));
          greeting.append(LOG4CXX_STR("Hello"));
          std::string encoded;
          Transcoder::encode(greeting, encoded);
          std::string manyAs(BUFSIZE - 3, 'A');
          LOGUNIT_ASSERT_EQUAL(manyAs, encoded.substr(0, BUFSIZE - 3));
          LOGUNIT_ASSERT_EQUAL(std::string("Hello"), encoded.substr(BUFSIZE - 3));
        }

#if LOG4CXX_WCHAR_T_API
        void encode5() {
          //   arbitrary, hopefully meaningless, characters from
          //     Latin, Arabic, Armenian, Bengali, CJK and Cyrillic
          const wchar_t greeting[] = { L'A', 0x0605, 0x0530, 0x984, 0x40E3, 0x400, 0 };
          //
          //  decode to LogString (UTF-16 or UTF-8)
          //
          LogString decoded;
          Transcoder::decode(greeting, decoded);
          //
          //  decode to wstring
          //
          std::wstring encoded;
          Transcoder::encode(decoded, encoded);
          //
          //   should be lossless
          //
          LOGUNIT_ASSERT_EQUAL((std::wstring) greeting, encoded);
        }
#endif

        void encode6() {
#if LOG4CXX_LOGCHAR_IS_WCHAR || LOG4CXX_LOGCHAR_IS_UNICHAR
          //   arbitrary, hopefully meaningless, characters from
          //     Latin, Arabic, Armenian, Bengali, CJK and Cyrillic
          const logchar greeting[] = { L'A', 0x0605, 0x0530, 0x984, 0x40E3, 0x400, 0 };
#endif

#if LOG4CXX_LOGCHAR_IS_UTF8
          const char greeting[] = { 'A',
                                    (char) 0xD8, (char) 0x85,
                                    (char) 0xD4, (char) 0xB0,
                                    (char) 0xE0, (char) 0xCC, (char) 0x84,
                                    (char) 0xE8, (char) 0x87, (char) 0x83,
                                    (char) 0xD0, (char) 0x80,
                                    0 };
#endif

          //
          //  decode to LogString (UTF-16 or UTF-8)
          //
          LogString decoded;
          Transcoder::decode(greeting, decoded);
          //
          //  decode to wstring
          //
          std::string encoded;
          //
          //   likely 'A\u0605\u0530\u0984\u40E3\u0400'
          //
          Transcoder::encode(decoded, encoded);
        }

    void testDecodeUTF8_1() {
        std::string src("a");
        LogString out;
        Transcoder::decodeUTF8(src, out);
        LOGUNIT_ASSERT_EQUAL(LogString(LOG4CXX_STR("a")), out);
    }

    void testDecodeUTF8_2() {
        std::string src(1, 0x80);
        LogString out;
        Transcoder::decodeUTF8(src, out);
        LOGUNIT_ASSERT_EQUAL(LogString(1, Transcoder::LOSSCHAR), out);
    }

    void testDecodeUTF8_3() {
        std::string src("\xC2");
        LogString out;
        Transcoder::decodeUTF8(src, out);
        LOGUNIT_ASSERT_EQUAL(LogString(1, Transcoder::LOSSCHAR), out);
    }

    void testDecodeUTF8_4() {
        std::string src("\xC2\xA9");
        LogString out;
        Transcoder::decodeUTF8(src, out);
        LogString::const_iterator iter = out.begin();
        unsigned int sv = Transcoder::decode(out, iter);
        LOGUNIT_ASSERT_EQUAL((unsigned int) 0xA9, sv);
        LOGUNIT_ASSERT_EQUAL(true, iter == out.end());
    }


#if LOG4CXX_UNICHAR_API
        void udecode2() {
          const UniChar greeting[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', 0 };
          LogString decoded(LOG4CXX_STR("foo\n"));
          Transcoder::decode(greeting, decoded);
          LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\nHello, World"), decoded);
        }

        void udecode4() {
            const UniChar nothing[] = { 0 };
            LogString decoded(LOG4CXX_STR("foo\n"));
            Transcoder::decode(nothing, decoded);
            LOGUNIT_ASSERT_EQUAL((LogString) LOG4CXX_STR("foo\n"), decoded);
        }

        void uencode1() {
          const LogString greeting(LOG4CXX_STR("Hello, World"));
          std::basic_string<UniChar> encoded;
          Transcoder::encode(greeting, encoded);
          const UniChar expected[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', 0 };
          LOGUNIT_ASSERT_EQUAL(std::basic_string<UniChar>(expected), encoded);
        }

        void uencode3() {
          LogString greeting(BUFSIZE - 3, LOG4CXX_STR('A'));
          greeting.append(LOG4CXX_STR("Hello"));
          std::basic_string<UniChar> encoded;
          Transcoder::encode(greeting, encoded);
          std::basic_string<UniChar> manyAs(BUFSIZE - 3, 'A');
          LOGUNIT_ASSERT_EQUAL(manyAs, encoded.substr(0, BUFSIZE - 3));
          const UniChar hello[] = { 'H', 'e', 'l', 'l', 'o', 0 };
          LOGUNIT_ASSERT_EQUAL(std::basic_string<UniChar>(hello), encoded.substr(BUFSIZE - 3));
        }

        void uencode5() {
          //   arbitrary, hopefully meaningless, characters from
          //     Latin, Arabic, Armenian, Bengali, CJK and Cyrillic
          const UniChar greeting[] = { L'A', 0x0605, 0x0530, 0x984, 0x40E3, 0x400, 0 };
          //
          //  decode to LogString (UTF-16 or UTF-8)
          //
          LogString decoded;
          Transcoder::decode(greeting, decoded);
          //
          //  decode to basic_string<UniChar>
          //
          std::basic_string<UniChar> encoded;
          Transcoder::encode(decoded, encoded);
          //
          //   should be lossless
          //
          LOGUNIT_ASSERT_EQUAL(std::basic_string<UniChar>(greeting), encoded);
        }
#endif


};

LOGUNIT_TEST_SUITE_REGISTRATION(TranscoderTestCase);