Line data Source code
1 : // Copyright (c) 2011-2023 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/as2js
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // as2js
20 : //
21 : #include <as2js/stream.h>
22 :
23 :
24 : // self
25 : //
26 : #include "catch_main.h"
27 :
28 :
29 : // libutf8
30 : //
31 : #include <libutf8/iterator.h>
32 : #include <libutf8/libutf8.h>
33 :
34 :
35 : // C++
36 : //
37 : #include <iomanip>
38 : #include <fstream>
39 : #include <sstream>
40 :
41 :
42 : // last include
43 : //
44 : #include <snapdev/poison.h>
45 :
46 :
47 :
48 :
49 :
50 : namespace
51 : {
52 :
53 3 : void generate_string(std::string & str)
54 : {
55 3 : char32_t c(U'\0');
56 3 : int ctrl(rand() % 7);
57 3 : int const max_chars(rand() % 25 + 5);
58 51 : for(int j(0); j < max_chars; ++j)
59 : {
60 : do
61 : {
62 77 : c = rand() & 0x1FFFFF;
63 77 : if(ctrl == 0)
64 : {
65 23 : ctrl = rand() % 7;
66 23 : c &= 0x1F;
67 : }
68 : else
69 : {
70 54 : --ctrl;
71 : }
72 : }
73 : while(c >= 0x110000
74 48 : || (c >= 0xD800 && c <= 0xDFFF)
75 48 : || ((c & 0xFFFE) == 0xFFFE)
76 125 : || c == '\0');
77 :
78 : //std::cerr << "-- adding char [" << static_cast<int>(c) << "]\n";
79 48 : str += libutf8::to_u8string(c);
80 : }
81 3 : }
82 :
83 :
84 :
85 : }
86 : // no name namespace
87 :
88 :
89 :
90 :
91 4 : CATCH_TEST_CASE("input_stream", "[stream][input]")
92 : {
93 4 : CATCH_START_SECTION("input_stream: load from input string stream")
94 : {
95 1 : std::string str;
96 1 : generate_string(str);
97 :
98 1 : as2js::input_stream<std::stringstream>::pointer_t in(std::make_shared<as2js::input_stream<std::stringstream>>());
99 1 : *in << str;
100 :
101 1 : libutf8::utf8_iterator it(str);
102 12 : for(;; ++it)
103 : {
104 13 : std::int32_t c(in->read_char());
105 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
106 13 : if(c == EOF)
107 : {
108 1 : CATCH_REQUIRE(it == str.end());
109 1 : break;
110 : }
111 12 : CATCH_REQUIRE(it != str.end());
112 12 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
113 12 : }
114 1 : }
115 4 : CATCH_END_SECTION()
116 :
117 4 : CATCH_START_SECTION("input_stream: load from input file stream")
118 : {
119 1 : std::stringstream ss;
120 1 : ss << SNAP_CATCH2_NAMESPACE::g_tmp_dir() << "/stream_input.txt";
121 1 : std::string const filename(ss.str());
122 :
123 1 : std::string str;
124 1 : generate_string(str);
125 :
126 : // create the file content
127 : {
128 1 : std::ofstream out;
129 1 : out.open(filename);
130 1 : CATCH_REQUIRE(out.is_open());
131 1 : out << str;
132 1 : }
133 :
134 1 : as2js::input_stream<std::ifstream>::pointer_t in(std::make_shared<as2js::input_stream<std::ifstream>>());
135 1 : in->open(filename);
136 1 : CATCH_REQUIRE(in->is_open());
137 :
138 1 : libutf8::utf8_iterator it(str);
139 24 : for(;; ++it)
140 : {
141 25 : std::int32_t c(in->read_char());
142 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
143 25 : if(c == EOF)
144 : {
145 1 : CATCH_REQUIRE(it == str.end());
146 1 : break;
147 : }
148 24 : CATCH_REQUIRE(it != str.end());
149 24 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
150 24 : }
151 1 : }
152 4 : CATCH_END_SECTION()
153 :
154 4 : CATCH_START_SECTION("input_stream: load from input file stream")
155 : {
156 : // 1. create a file with some test data in it
157 : //
158 1 : std::stringstream ss;
159 1 : ss << SNAP_CATCH2_NAMESPACE::g_tmp_dir() << "/strandard_input.txt";
160 1 : std::string const filename(ss.str());
161 :
162 1 : std::string str;
163 1 : generate_string(str);
164 :
165 : {
166 1 : std::ofstream out;
167 1 : out.open(filename);
168 1 : CATCH_REQUIRE(out.is_open());
169 1 : out << str;
170 1 : }
171 :
172 : // 2. put that in std::cin
173 : //
174 1 : CATCH_REQUIRE(freopen(filename.c_str(), "r", stdin) != nullptr);
175 :
176 1 : as2js::cin_stream::pointer_t in(std::make_shared<as2js::cin_stream>());
177 :
178 : // The filename for the standard input is set to "-" by default
179 : //
180 1 : CATCH_REQUIRE(in->get_position().get_filename() == "-");
181 1 : CATCH_REQUIRE(in->get_position().get_line() == 1);
182 :
183 1 : libutf8::utf8_iterator it(str);
184 12 : for(;; ++it)
185 : {
186 13 : std::int32_t const c(in->read_char());
187 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
188 13 : if(c == EOF)
189 : {
190 1 : CATCH_REQUIRE(it == str.end());
191 1 : break;
192 : }
193 12 : CATCH_REQUIRE(it != str.end());
194 12 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
195 12 : }
196 1 : }
197 4 : CATCH_END_SECTION()
198 :
199 4 : CATCH_START_SECTION("input_stream: end stream with a UTF-8 character")
200 : {
201 1 : std::stringstream ss;
202 :
203 1 : ss << "end with UTF-8 character 0x2022 = \xe2\x80\xa2";
204 :
205 1 : std::string const str(ss.str());
206 :
207 1 : as2js::input_stream<std::stringstream>::pointer_t in(std::make_shared<as2js::input_stream<std::stringstream>>());
208 1 : *in << str;
209 :
210 1 : libutf8::utf8_iterator it(str);
211 35 : for(;; ++it)
212 : {
213 36 : std::int32_t c(in->read_char());
214 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
215 36 : if(c == EOF)
216 : {
217 1 : CATCH_REQUIRE(it == str.end());
218 1 : break;
219 : }
220 35 : CATCH_REQUIRE(it != str.end());
221 35 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
222 35 : }
223 1 : }
224 4 : CATCH_END_SECTION()
225 4 : }
226 :
227 :
228 2 : CATCH_TEST_CASE("input_stream_invalid_utf8", "[stream][input][invalid]")
229 : {
230 2 : CATCH_START_SECTION("input_stream_invalid_utf8: invalid UTF-8")
231 : {
232 65 : for(int i(0x80); i < 0xC0; ++i)
233 : {
234 64 : std::stringstream sa, sb;
235 : sa << "This is okay -- "
236 64 : << static_cast<char>(i); // bad character
237 64 : sb << "This is okay -- ";
238 64 : int const max(rand() % 8);
239 264 : for(int j(0); j < max; ++j)
240 : {
241 : // more bad bytes to exercise the loop
242 : //
243 200 : sa << static_cast<char>(rand() % 0x40 + 0x80);
244 : }
245 64 : sa << " -- end here\n";
246 64 : sb << " -- end here\n";
247 :
248 64 : as2js::input_stream<std::stringstream>::pointer_t in(std::make_shared<as2js::input_stream<std::stringstream>>());
249 64 : *in << sa.str();
250 :
251 64 : std::string str(sb.str());
252 64 : libutf8::utf8_iterator it(str);
253 1856 : for(;; ++it)
254 : {
255 1920 : std::int32_t c(in->read_char());
256 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
257 1920 : if(c == EOF)
258 : {
259 64 : CATCH_REQUIRE(it == str.end());
260 64 : break;
261 : }
262 1856 : CATCH_REQUIRE(it != str.end());
263 1856 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
264 1856 : }
265 64 : }
266 : }
267 2 : CATCH_END_SECTION()
268 :
269 2 : CATCH_START_SECTION("input_stream_short_utf8: invalid UTF-8 length")
270 : {
271 65 : for(int i(0x80); i < 0xC0; ++i)
272 : {
273 64 : std::stringstream sa, sb;
274 :
275 : sa << "This is okay -- "
276 : << static_cast<char>(0xE0) // good intro
277 64 : << static_cast<char>(i); // good follower
278 : // ... missing 2nd code ...
279 64 : sb << "This is okay -- ";
280 :
281 64 : sa << " -- missing second code -- ";
282 64 : sb << " -- missing second code -- ";
283 :
284 64 : as2js::input_stream<std::stringstream>::pointer_t in(std::make_shared<as2js::input_stream<std::stringstream>>());
285 64 : *in << sa.str();
286 :
287 64 : std::string str(sb.str());
288 64 : libutf8::utf8_iterator it(str);
289 2752 : for(;; ++it)
290 : {
291 2816 : std::int32_t c(in->read_char());
292 : //std::cerr << "-- read char [" << static_cast<int>(c) << "]\n";
293 2816 : if(c == EOF)
294 : {
295 64 : CATCH_REQUIRE(it == str.end());
296 64 : break;
297 : }
298 2752 : CATCH_REQUIRE(it != str.end());
299 2752 : CATCH_REQUIRE(static_cast<char32_t>(c) == *it);
300 2752 : }
301 64 : }
302 : }
303 2 : CATCH_END_SECTION()
304 2 : }
305 :
306 :
307 :
308 :
309 : //void As2JsStreamUnitTests::test_filter_iso88591()
310 : //{
311 : // {
312 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterISO88591);
313 : // for(int c(1); c < 256; ++c)
314 : // {
315 : // filter->putc(c);
316 : // CPPUNIT_ASSERT(filter->getc() == c);
317 : // }
318 : // // check EOF and make sure it remains that way
319 : // for(int c(0); c < 256; ++c)
320 : // {
321 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
322 : // }
323 : // }
324 : // {
325 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterISO88591);
326 : // for(int c(1); c < 256; ++c)
327 : // {
328 : // filter->putc(c);
329 : // }
330 : // for(int c(1); c < 256; ++c)
331 : // {
332 : // CPPUNIT_ASSERT(filter->getc() == c);
333 : // }
334 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
335 : //
336 : // // then try with random data
337 : // int buf[256];
338 : // for(int c(0); c < 256; ++c)
339 : // {
340 : // CPPUNIT_ASSERT(static_cast<size_t>(c) < sizeof(buf) / sizeof(buf[0]));
341 : // do
342 : // {
343 : // buf[c] = rand() & 0xFF;
344 : // }
345 : // while(buf[c] == 0);
346 : // filter->putc(buf[c]);
347 : // }
348 : // for(int c(0); c < 256; ++c)
349 : // {
350 : // CPPUNIT_ASSERT(filter->getc() == buf[c]);
351 : // }
352 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
353 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
354 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
355 : // }
356 : //}
357 : //
358 : //
359 : //void As2JsStreamUnitTests::test_filter_utf8()
360 : //{
361 : // {
362 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterUTF8);
363 : //
364 : // // The Stream reimplements its own UTF-8 conversion so we want to
365 : // // test all the characters here...
366 : // for(as2js::as_char_t wc(1); wc < 0x1FFFFF; ++wc)
367 : // {
368 : // if((wc & 0xFFFF) == 0)
369 : // {
370 : // std::cout << "." << std::flush;
371 : // }
372 : //
373 : // bool err(wc >= 0xD800 && wc <= 0xDFFF || wc > 0x10FFFF);
374 : //
375 : // // 1 to 7 character strings
376 : // char buf[10];
377 : // wctombs(buf, wc);
378 : //
379 : // for(size_t idx(0); buf[idx] != '\0'; ++idx)
380 : // {
381 : // filter->putc(buf[idx]);
382 : // if(buf[idx + 1] == '\0')
383 : // {
384 : // if(err)
385 : // {
386 : // // invalid characters must return an error
387 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
388 : // }
389 : // else //if(wc != as2js::String::STRING_BOM)
390 : // {
391 : // as2js::as_char_t get_wc(filter->getc());
392 : ////std::cerr << "got " << get_wc << ", expected " << wc << "\n";
393 : // CPPUNIT_ASSERT(get_wc == wc);
394 : // }
395 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
396 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
397 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
398 : // }
399 : // else
400 : // {
401 : // // NAC remains any number of times until we add
402 : // // enough bytes to the input
403 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
404 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
405 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
406 : // }
407 : // }
408 : // }
409 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
410 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
411 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
412 : // }
413 : //
414 : // // now check sending many characters with putc() and reading them back later
415 : // {
416 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterUTF8);
417 : //
418 : // as2js::String result;
419 : // for(int c(0); c < 256; ++c)
420 : // {
421 : // // 5 to 9 character strings
422 : // int32_t wc;
423 : // do
424 : // {
425 : // // generate a random Unicode character
426 : // wc = ((rand() << 16) ^ rand()) & 0x1FFFFF;
427 : // }
428 : // while((wc >= 0xD800 && wc <= 0xDFFF) || wc >= 0x110000);
429 : // char buf[10];
430 : // wctombs(buf, wc);
431 : // for(int idx(0); buf[idx] != '\0'; ++idx)
432 : // {
433 : // filter->putc(buf[idx]);
434 : // }
435 : // //if(wc != as2js::String::STRING_BOM)
436 : // {
437 : // result += wc;
438 : // }
439 : // }
440 : //
441 : // for(size_t idx(0); idx < result.length(); ++idx)
442 : // {
443 : // as2js::as_char_t get_wc(filter->getc());
444 : ////std::cerr << get_wc << " vs. " << result[idx];
445 : // CPPUNIT_ASSERT(get_wc == result[idx]);
446 : // }
447 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
448 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
449 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
450 : // }
451 : //
452 : // // bytes F8 to FF generate errors immedately
453 : // {
454 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterUTF8);
455 : //
456 : // for(int idx(0xF8); idx < 0x100; ++idx)
457 : // {
458 : // filter->putc(idx);
459 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
460 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
461 : // }
462 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
463 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
464 : // }
465 : //
466 : // // invalid continue bytes test
467 : ////std::cerr << "\n";
468 : // {
469 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterUTF8);
470 : //
471 : // for(int idx(0xC0); idx < 0xF8; ++idx)
472 : // {
473 : ////std::cerr << std::hex << "testing " << idx;
474 : // filter->putc(idx);
475 : // as2js::as_char_t bad, extra1, extra2, extra3(0);
476 : // do
477 : // {
478 : // bad = rand() & 0xFF;
479 : // }
480 : // while(bad >= 0x80 && bad <= 0xBF); // skip valid bytes
481 : ////std::cerr << std::hex << " + " << bad;
482 : // filter->putc(bad);
483 : // if(idx >= 0xE0)
484 : // {
485 : // do
486 : // {
487 : // extra1 = rand() & 0x7F;
488 : // }
489 : // while(extra1 == 0);
490 : // filter->putc(extra1);
491 : ////std::cerr << std::hex << " + " << extra1;
492 : // }
493 : // else
494 : // {
495 : // extra1 = 0;
496 : // }
497 : // if(idx >= 0xF0)
498 : // {
499 : // do
500 : // {
501 : // extra2 = rand() & 0x7F;
502 : // }
503 : // while(extra2 == 0);
504 : // filter->putc(extra2);
505 : ////std::cerr << std::hex << " + " << extra2;
506 : // }
507 : // else
508 : // {
509 : // extra2 = 0;
510 : // }
511 : ////std::cerr << "\n";
512 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
513 : // // the bad byte is still there, check it...
514 : // if(bad < 0x80)
515 : // {
516 : // // load a normal ISO-8859-1 character
517 : // CPPUNIT_ASSERT(filter->getc() == bad);
518 : // if(extra1 != 0)
519 : // {
520 : // CPPUNIT_ASSERT(filter->getc() == extra1);
521 : // }
522 : // if(extra2 != 0)
523 : // {
524 : // CPPUNIT_ASSERT(filter->getc() == extra2);
525 : // }
526 : // }
527 : // else if(bad >= 0xC0 && bad < 0xE0)
528 : // {
529 : // // do something to reset the state
530 : // if(extra1 == 0)
531 : // {
532 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
533 : //
534 : // extra1 = rand() & 0x7F;
535 : // filter->putc(extra1);
536 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
537 : // CPPUNIT_ASSERT(filter->getc() == extra1);
538 : // }
539 : // else
540 : // {
541 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
542 : // CPPUNIT_ASSERT(filter->getc() == extra1);
543 : // if(extra2 != 0)
544 : // {
545 : // CPPUNIT_ASSERT(filter->getc() == extra2);
546 : // }
547 : // }
548 : // }
549 : // else if(bad >= 0xE0 && bad < 0xF0)
550 : // {
551 : // if(extra1 == 0)
552 : // {
553 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
554 : // extra1 = rand() & 0x7F;
555 : // filter->putc(extra1);
556 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
557 : // extra2 = rand() & 0x7F;
558 : // filter->putc(extra2);
559 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
560 : // CPPUNIT_ASSERT(filter->getc() == extra1);
561 : // CPPUNIT_ASSERT(filter->getc() == extra2);
562 : // }
563 : // else if(extra2 == 0)
564 : // {
565 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
566 : // extra2 = rand() & 0x7F;
567 : // filter->putc(extra2);
568 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
569 : // CPPUNIT_ASSERT(filter->getc() == extra1);
570 : // CPPUNIT_ASSERT(filter->getc() == extra2);
571 : // }
572 : // else
573 : // {
574 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
575 : // CPPUNIT_ASSERT(filter->getc() == extra1);
576 : // CPPUNIT_ASSERT(filter->getc() == extra2);
577 : // }
578 : // }
579 : // else if(bad >= 0xF0 && bad < 0xF8)
580 : // {
581 : // if(extra1 == 0)
582 : // {
583 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
584 : // extra1 = rand() & 0x7F;
585 : // filter->putc(extra1);
586 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
587 : // extra2 = rand() & 0x7F;
588 : // filter->putc(extra2);
589 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
590 : // extra3 = rand() & 0x7F;
591 : // filter->putc(extra3);
592 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
593 : // CPPUNIT_ASSERT(filter->getc() == extra1);
594 : // CPPUNIT_ASSERT(filter->getc() == extra2);
595 : // CPPUNIT_ASSERT(filter->getc() == extra3);
596 : // }
597 : // else if(extra2 == 0)
598 : // {
599 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
600 : // extra2 = rand() & 0x7F;
601 : // filter->putc(extra2);
602 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
603 : // extra3 = rand() & 0x7F;
604 : // filter->putc(extra3);
605 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
606 : // CPPUNIT_ASSERT(filter->getc() == extra1);
607 : // CPPUNIT_ASSERT(filter->getc() == extra2);
608 : // CPPUNIT_ASSERT(filter->getc() == extra3);
609 : // }
610 : // else
611 : // {
612 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
613 : // extra3 = rand() & 0x7F;
614 : // filter->putc(extra3);
615 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
616 : // CPPUNIT_ASSERT(filter->getc() == extra1);
617 : // CPPUNIT_ASSERT(filter->getc() == extra2);
618 : // CPPUNIT_ASSERT(filter->getc() == extra3);
619 : // }
620 : // }
621 : // else
622 : // {
623 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_ERR);
624 : // if(extra1 != 0)
625 : // {
626 : // CPPUNIT_ASSERT(filter->getc() == extra1);
627 : // }
628 : // if(extra2 != 0)
629 : // {
630 : // CPPUNIT_ASSERT(filter->getc() == extra2);
631 : // }
632 : // }
633 : // // make sure the buffer is empty
634 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
635 : // }
636 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
637 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
638 : // }
639 : //}
640 : //
641 : //
642 : //void As2JsStreamUnitTests::test_filter_utf16()
643 : //{
644 : // {
645 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF16BE);
646 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF16LE);
647 : //
648 : // // The Stream reimplements its own UTF-16 conversion so we want to
649 : // // test all the characters here... Also we have a BE and an LE
650 : // // version so we check both at the same time; just valid characters
651 : // for(as2js::as_char_t wc(1); wc < 0x110000; ++wc)
652 : // {
653 : // if((wc & 0xFFFF) == 0)
654 : // {
655 : // std::cout << "." << std::flush;
656 : // }
657 : //
658 : // if(wc >= 0xD800 && wc <= 0xDFFF)
659 : // {
660 : // continue;
661 : // }
662 : //
663 : // // putc() accepts bytes only, so we need to break down all those
664 : // // character into bytes as expected by the respective filter
665 : // if(wc > 0xFFFF)
666 : // {
667 : // // in this case we need to send 2x uint16_t values
668 : // uint16_t lead((((wc - 0x10000) >> 10) & 0x03FF) | 0xD800);
669 : // uint16_t trail(((wc - 0x10000) & 0x03FF) | 0xDC00);
670 : //
671 : // filter_be->putc(lead >> 8);
672 : // filter_be->putc(lead & 255);
673 : // filter_be->putc(trail >> 8);
674 : // filter_be->putc(trail & 255);
675 : // CPPUNIT_ASSERT(filter_be->getc() == wc);
676 : //
677 : // // little endian swaps bytes, not the lead & trail
678 : // filter_le->putc(lead & 255);
679 : // filter_le->putc(lead >> 8);
680 : // filter_le->putc(trail & 255);
681 : // filter_le->putc(trail >> 8);
682 : // CPPUNIT_ASSERT(filter_le->getc() == wc);
683 : // }
684 : // //else if(wc == as2js::String::STRING_BOM)
685 : // //{
686 : // // // the BOM is never returned
687 : // // filter_be->putc(wc >> 8);
688 : // // filter_be->putc(wc & 255);
689 : // // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
690 : //
691 : // // filter_le->putc(wc & 255);
692 : // // filter_le->putc(wc >> 8);
693 : // // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
694 : // //}
695 : // else
696 : // {
697 : // filter_be->putc(wc >> 8);
698 : // filter_be->putc(wc & 255);
699 : // as2js::as_char_t get_wc(filter_be->getc());
700 : ////std::cerr << "wc " << wc << " got " << get_wc << "\n";
701 : // CPPUNIT_ASSERT(get_wc == wc);
702 : //
703 : // filter_le->putc(wc & 255);
704 : // filter_le->putc(wc >> 8);
705 : // CPPUNIT_ASSERT(filter_le->getc() == wc);
706 : // }
707 : // }
708 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
709 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
710 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
711 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
712 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
713 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
714 : // }
715 : //
716 : // // do it again, this time try all the NAC
717 : // {
718 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF16BE);
719 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF16LE);
720 : //
721 : // // The Stream reimplements its own UTF-16 conversion so we want to
722 : // // test all the characters here... Also we have a BE and an LE
723 : // // version so we check both at the same time; just valid characters
724 : // for(as2js::as_char_t wc(1); wc < 0x110000; ++wc)
725 : // {
726 : // if((wc & 0xFFFF) == 0)
727 : // {
728 : // std::cout << "." << std::flush;
729 : // }
730 : //
731 : // if(wc >= 0xD800 && wc <= 0xDFFF)
732 : // {
733 : // continue;
734 : // }
735 : //
736 : // // putc() accepts bytes only, so we need to break down all those
737 : // // character into bytes as expected by the respective filter
738 : // if(wc > 0xFFFF)
739 : // {
740 : // // in this case we need to send 2x uint16_t values
741 : // uint16_t lead((((wc - 0x10000) >> 10) & 0x03FF) | 0xD800);
742 : // uint16_t trail(((wc - 0x10000) & 0x03FF) | 0xDC00);
743 : //
744 : // filter_be->putc(lead >> 8);
745 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
746 : // filter_be->putc(lead & 255);
747 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
748 : // filter_be->putc(trail >> 8);
749 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
750 : // filter_be->putc(trail & 255);
751 : // CPPUNIT_ASSERT(filter_be->getc() == wc);
752 : //
753 : // // little endian swaps bytes, not the lead & trail
754 : // filter_le->putc(lead & 255);
755 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
756 : // filter_le->putc(lead >> 8);
757 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
758 : // filter_le->putc(trail & 255);
759 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
760 : // filter_le->putc(trail >> 8);
761 : // CPPUNIT_ASSERT(filter_le->getc() == wc);
762 : // }
763 : // //else if(wc == as2js::String::STRING_BOM)
764 : // //{
765 : // // // the BOM is never returned
766 : // // filter_be->putc(wc >> 8);
767 : // // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
768 : // // filter_be->putc(wc & 255);
769 : // // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
770 : //
771 : // // filter_le->putc(wc & 255);
772 : // // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
773 : // // filter_le->putc(wc >> 8);
774 : // // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
775 : // //}
776 : // else
777 : // {
778 : // filter_be->putc(wc >> 8);
779 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
780 : // filter_be->putc(wc & 255);
781 : // as2js::as_char_t get_wc(filter_be->getc());
782 : ////std::cerr << "wc " << wc << " got " << get_wc << "\n";
783 : // CPPUNIT_ASSERT(get_wc == wc);
784 : //
785 : // filter_le->putc(wc & 255);
786 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
787 : // filter_le->putc(wc >> 8);
788 : // CPPUNIT_ASSERT(filter_le->getc() == wc);
789 : // }
790 : // }
791 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
792 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
793 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
794 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
795 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
796 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
797 : // }
798 : //
799 : // // invalid surrogates
800 : // // (1) trail surrogate without a lead
801 : // std::cout << "." << std::flush;
802 : // {
803 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF16BE);
804 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF16LE);
805 : //
806 : // // The Stream reimplements its own UTF-16 conversion so we want to
807 : // // test all the characters here... Also we have a BE and an LE
808 : // // version so we check both at the same time; just valid characters
809 : // for(as2js::as_char_t wc(0xDC00); wc < 0xE000; ++wc)
810 : // {
811 : // filter_be->putc(wc >> 8);
812 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
813 : // filter_be->putc(wc & 255);
814 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_ERR);
815 : //
816 : // filter_le->putc(wc & 255);
817 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
818 : // filter_le->putc(wc >> 8);
819 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_ERR);
820 : // }
821 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
822 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
823 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
824 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
825 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
826 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
827 : // }
828 : //
829 : // // invalid surrogates
830 : // // (2) lead surrogate without a trail
831 : // std::cout << "." << std::flush;
832 : // {
833 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF16BE);
834 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF16LE);
835 : //
836 : // // The Stream reimplements its own UTF-16 conversion so we want to
837 : // // test all the characters here... Also we have a BE and an LE
838 : // // version so we check both at the same time; just valid characters
839 : // for(as2js::as_char_t wc(0xD800); wc < 0xDC00; ++wc)
840 : // {
841 : // // get another character which is not a surrogate
842 : // as2js::as_char_t extra1;
843 : // do
844 : // {
845 : // extra1 = rand() & 0x0FFFF;
846 : // }
847 : // while(extra1 >= 0xD800 && extra1 <= 0xDFFF);
848 : //
849 : // filter_be->putc(wc >> 8);
850 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
851 : // filter_be->putc(wc & 255);
852 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
853 : // filter_be->putc(extra1 >> 8);
854 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
855 : // filter_be->putc(extra1 & 255);
856 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_ERR);
857 : // as2js::as_char_t get_wc(filter_be->getc());
858 : ////std::cerr << "got " << get_wc << ", expected " << extra1 << "\n";
859 : // //if(extra1 == as2js::String::STRING_BOM)
860 : // //{
861 : // // CPPUNIT_ASSERT(get_wc == as2js::Input::INPUT_EOF);
862 : // //}
863 : // //else
864 : // {
865 : // CPPUNIT_ASSERT(get_wc == extra1);
866 : // }
867 : //
868 : // filter_le->putc(wc & 255);
869 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
870 : // filter_le->putc(wc >> 8);
871 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
872 : // filter_le->putc(extra1 & 255);
873 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
874 : // filter_le->putc(extra1 >> 8);
875 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_ERR);
876 : // //if(extra1 == as2js::String::STRING_BOM)
877 : // //{
878 : // // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
879 : // //}
880 : // //else
881 : // {
882 : // CPPUNIT_ASSERT(filter_le->getc() == extra1);
883 : // }
884 : // }
885 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
886 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
887 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
888 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
889 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
890 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
891 : // }
892 : //
893 : //}
894 : //
895 : //
896 : //void As2JsStreamUnitTests::test_filter_utf32()
897 : //{
898 : // {
899 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF32BE);
900 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF32LE);
901 : //
902 : // // The Stream reimplements its own UTF-16 conversion so we want to
903 : // // test all the characters here... Also we have a BE and an LE
904 : // // version so we check both at the same time; just valid characters
905 : // for(as2js::as_char_t wc(1); wc < 0x1FFFFF; ++wc)
906 : // {
907 : // if((wc & 0xFFFF) == 0)
908 : // {
909 : // std::cout << "." << std::flush;
910 : // }
911 : //
912 : // bool const err((wc >= 0xD800 && wc <= 0xDFFF) || wc > 0x10FFFF);
913 : //
914 : // // putc() accepts bytes only, so we need to break down all those
915 : // // character into bytes as expected by the respective filter
916 : //
917 : // // in this case we need to send 2x uint16_t values
918 : //
919 : // filter_be->putc((wc >> 24) & 255);
920 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
921 : // filter_be->putc((wc >> 16) & 255);
922 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
923 : // filter_be->putc((wc >> 8) & 255);
924 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_NAC);
925 : // filter_be->putc((wc >> 0) & 255);
926 : // //if(wc == as2js::String::STRING_BOM)
927 : // //{
928 : // // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
929 : // //}
930 : // //else
931 : // {
932 : // CPPUNIT_ASSERT(filter_be->getc() == (err ? as2js::Input::INPUT_ERR : wc));
933 : // }
934 : //
935 : // // little endian swaps bytes, not the lead & trail
936 : // filter_le->putc((wc >> 0) & 255);
937 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
938 : // filter_le->putc((wc >> 8) & 255);
939 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
940 : // filter_le->putc((wc >> 16) & 255);
941 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_NAC);
942 : // filter_le->putc((wc >> 24) & 255);
943 : // //if(wc == as2js::String::STRING_BOM)
944 : // //{
945 : // // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
946 : // //}
947 : // //else
948 : // {
949 : // CPPUNIT_ASSERT(filter_le->getc() == (err ? as2js::Input::INPUT_ERR : wc));
950 : // }
951 : // }
952 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
953 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
954 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
955 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
956 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
957 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
958 : // }
959 : //
960 : // {
961 : // as2js::DecodingFilter *filter_be(new as2js::DecodingFilterUTF32BE);
962 : // as2js::DecodingFilter *filter_le(new as2js::DecodingFilterUTF32LE);
963 : //
964 : // // The Stream reimplements its own UTF-16 conversion so we want to
965 : // // test all the characters here... Also we have a BE and an LE
966 : // // version so we check both at the same time; just valid characters
967 : // std::cout << "-" << std::flush;
968 : // std::vector<as2js::as_char_t> result;
969 : // for(as2js::as_char_t idx(0); idx < 256; ++idx)
970 : // {
971 : // as2js::as_char_t wc(((rand() << 16) ^ rand()) & 0x1FFFFF);
972 : //
973 : // //if(wc != as2js::String::STRING_BOM)
974 : // {
975 : // result.push_back(wc);
976 : // }
977 : //
978 : // // putc() accepts bytes only, so we need to break down all those
979 : // // character into bytes as expected by the respective filter
980 : //
981 : // // in this case we need to send 2x uint16_t values
982 : //
983 : // filter_be->putc((wc >> 24) & 255);
984 : // filter_be->putc((wc >> 16) & 255);
985 : // filter_be->putc((wc >> 8) & 255);
986 : // filter_be->putc((wc >> 0) & 255);
987 : //
988 : // filter_le->putc((wc >> 0) & 255);
989 : // filter_le->putc((wc >> 8) & 255);
990 : // filter_le->putc((wc >> 16) & 255);
991 : // filter_le->putc((wc >> 24) & 255);
992 : // }
993 : // std::cout << "+" << std::flush;
994 : // for(size_t idx(0); idx < result.size(); ++idx)
995 : // {
996 : // as2js::as_char_t wc(result[idx]);
997 : //
998 : // bool const err((wc >= 0xD800 && wc <= 0xDFFF) || wc > 0x10FFFF);
999 : //
1000 : // CPPUNIT_ASSERT(filter_be->getc() == (err ? as2js::Input::INPUT_ERR : wc));
1001 : // CPPUNIT_ASSERT(filter_le->getc() == (err ? as2js::Input::INPUT_ERR : wc));
1002 : // }
1003 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
1004 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
1005 : // CPPUNIT_ASSERT(filter_be->getc() == as2js::Input::INPUT_EOF);
1006 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
1007 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
1008 : // CPPUNIT_ASSERT(filter_le->getc() == as2js::Input::INPUT_EOF);
1009 : // }
1010 : //}
1011 : //
1012 : //
1013 : //void As2JsStreamUnitTests::test_filter_detect()
1014 : //{
1015 : // // test UTF32BE
1016 : // {
1017 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1018 : //
1019 : // // BOM + 0x10203
1020 : // filter->putc(0);
1021 : // filter->putc(0);
1022 : // filter->putc(0xFE);
1023 : // filter->putc(0xFF);
1024 : // filter->putc(0);
1025 : // filter->putc(1);
1026 : // filter->putc(2);
1027 : // filter->putc(3);
1028 : //
1029 : // as2js::as_char_t wc(filter->getc());
1030 : // CPPUNIT_ASSERT(wc == 0x10203);
1031 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1032 : // }
1033 : //
1034 : // // test UTF32LE
1035 : // {
1036 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1037 : //
1038 : // // BOM + 0x10203
1039 : // filter->putc(0xFF);
1040 : // filter->putc(0xFE);
1041 : // filter->putc(0);
1042 : // filter->putc(0);
1043 : // filter->putc(3);
1044 : // filter->putc(2);
1045 : // filter->putc(1);
1046 : // filter->putc(0);
1047 : //
1048 : // as2js::as_char_t wc(filter->getc());
1049 : // CPPUNIT_ASSERT(wc == 0x10203);
1050 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1051 : // }
1052 : //
1053 : // // test UTF16BE
1054 : // {
1055 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1056 : //
1057 : // // BOM + 0x102
1058 : // filter->putc(0xFE);
1059 : // filter->putc(0xFF);
1060 : // filter->putc(1);
1061 : // filter->putc(2);
1062 : //
1063 : // as2js::as_char_t wc(filter->getc());
1064 : // CPPUNIT_ASSERT(wc == 0x102);
1065 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1066 : // }
1067 : //
1068 : // // test UTF16LE
1069 : // {
1070 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1071 : //
1072 : // // BOM + 0x102
1073 : // filter->putc(0xFF);
1074 : // filter->putc(0xFE);
1075 : // filter->putc(2);
1076 : // filter->putc(1);
1077 : //
1078 : // as2js::as_char_t wc(filter->getc());
1079 : // CPPUNIT_ASSERT(wc == 0x102);
1080 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1081 : // }
1082 : //
1083 : // // test UTF8 with BOM
1084 : // {
1085 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1086 : //
1087 : // // BOM + 0x10203
1088 : // as2js::String wstr;
1089 : // wstr += 0x0000FEFF; // BOM
1090 : // wstr += 0x00010203; // 0x10203
1091 : // std::string utf8(wstr.to_utf8());
1092 : // for(size_t idx(0); idx < utf8.size(); ++idx)
1093 : // {
1094 : // filter->putc(utf8[idx]);
1095 : // }
1096 : //
1097 : // as2js::as_char_t wc(filter->getc());
1098 : // CPPUNIT_ASSERT(wc == 0x10203);
1099 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1100 : // }
1101 : //
1102 : // // test UTF8 without BOM
1103 : // {
1104 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1105 : //
1106 : // // 0x10203 and 0x30201
1107 : // as2js::String wstr;
1108 : // wstr += 0x00010203;
1109 : // wstr += 0x00030201;
1110 : // std::string utf8(wstr.to_utf8());
1111 : // for(size_t idx(0); idx < utf8.size(); ++idx)
1112 : // {
1113 : // filter->putc(utf8[idx]);
1114 : // }
1115 : //
1116 : // as2js::as_char_t wc(filter->getc());
1117 : // CPPUNIT_ASSERT(wc == 0x10203);
1118 : // wc = filter->getc();
1119 : // CPPUNIT_ASSERT(wc == 0x30201);
1120 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1121 : // }
1122 : //
1123 : // // test ISO-8859-1 (fallback)
1124 : // {
1125 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1126 : //
1127 : // // invalid UTF-8 on the first 4 bytes
1128 : // filter->putc(0xFF);
1129 : // filter->putc(0x01);
1130 : // filter->putc(0x02);
1131 : // filter->putc(0x03);
1132 : //
1133 : // CPPUNIT_ASSERT(filter->getc() == 0xFF);
1134 : // CPPUNIT_ASSERT(filter->getc() == 0x01);
1135 : // CPPUNIT_ASSERT(filter->getc() == 0x02);
1136 : // CPPUNIT_ASSERT(filter->getc() == 0x03);
1137 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1138 : // }
1139 : //
1140 : // // test UTF32BE with NAC tests
1141 : // {
1142 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1143 : //
1144 : // // BOM + 0x10203
1145 : // filter->putc(0);
1146 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1147 : // filter->putc(0);
1148 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1149 : // filter->putc(0xFE);
1150 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1151 : // filter->putc(0xFF);
1152 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1153 : // filter->putc(0);
1154 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1155 : // filter->putc(1);
1156 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1157 : // filter->putc(2);
1158 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1159 : // filter->putc(3);
1160 : // CPPUNIT_ASSERT(filter->getc() == 0x10203);
1161 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1162 : // }
1163 : //
1164 : // // test UTF32LE
1165 : // {
1166 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1167 : //
1168 : // // BOM + 0x10203
1169 : // filter->putc(0xFF);
1170 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1171 : // filter->putc(0xFE);
1172 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1173 : // filter->putc(0);
1174 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1175 : // filter->putc(0);
1176 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1177 : // filter->putc(3);
1178 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1179 : // filter->putc(2);
1180 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1181 : // filter->putc(1);
1182 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1183 : // filter->putc(0);
1184 : // CPPUNIT_ASSERT(filter->getc() == 0x10203);
1185 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1186 : // }
1187 : //
1188 : // // test UTF16BE
1189 : // {
1190 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1191 : //
1192 : // // BOM + 0x102
1193 : // filter->putc(0xFE);
1194 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1195 : // filter->putc(0xFF);
1196 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1197 : // filter->putc(1);
1198 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1199 : // filter->putc(2);
1200 : // CPPUNIT_ASSERT(filter->getc() == 0x102);
1201 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1202 : // }
1203 : //
1204 : // // test UTF16LE
1205 : // {
1206 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1207 : //
1208 : // // BOM + 0x102
1209 : // filter->putc(0xFF);
1210 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1211 : // filter->putc(0xFE);
1212 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1213 : // filter->putc(2);
1214 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1215 : // filter->putc(1);
1216 : // CPPUNIT_ASSERT(filter->getc() == 0x102);
1217 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1218 : // }
1219 : //
1220 : // // test UTF8 with BOM
1221 : // {
1222 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1223 : //
1224 : // // BOM + 0x10203
1225 : // as2js::String wstr;
1226 : // wstr += 0x0000FEFF; // BOM
1227 : // wstr += 0x00010203; // 0x10203
1228 : // std::string utf8(wstr.to_utf8());
1229 : // for(size_t idx(0); idx < utf8.size(); ++idx)
1230 : // {
1231 : // filter->putc(utf8[idx]);
1232 : // switch(idx)
1233 : // {
1234 : // case 0:
1235 : // case 1:
1236 : // case 2:
1237 : // case 3: // at 3 bytes the BOM is available but not detected yet...
1238 : // case 4: // at 4 bytes we got the BOM + 1 byte of the next character so we get a NAC again...
1239 : // case 5:
1240 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1241 : // break;
1242 : //
1243 : // case 6:
1244 : // CPPUNIT_ASSERT(filter->getc() == 0x10203);
1245 : // break;
1246 : //
1247 : // default:
1248 : // CPPUNIT_ASSERT(false);
1249 : // break;
1250 : //
1251 : // }
1252 : // }
1253 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_EOF);
1254 : // }
1255 : //
1256 : // // test UTF8 without BOM
1257 : // {
1258 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1259 : //
1260 : // // 0x10203 and 0x30201
1261 : // as2js::String wstr;
1262 : // wstr += 0x00010203;
1263 : // wstr += 0x00030201;
1264 : // std::string utf8(wstr.to_utf8());
1265 : // for(size_t idx(0); idx < utf8.size(); ++idx)
1266 : // {
1267 : // filter->putc(utf8[idx]);
1268 : // switch(idx)
1269 : // {
1270 : // case 0:
1271 : // case 1:
1272 : // case 2:
1273 : // case 4:
1274 : // case 5:
1275 : // case 6:
1276 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1277 : // break;
1278 : //
1279 : // case 3:
1280 : // CPPUNIT_ASSERT(filter->getc() == 0x10203);
1281 : // break;
1282 : //
1283 : // case 7:
1284 : // CPPUNIT_ASSERT(filter->getc() == 0x30201);
1285 : // break;
1286 : //
1287 : // default:
1288 : // CPPUNIT_ASSERT(false);
1289 : // break;
1290 : //
1291 : // }
1292 : // }
1293 : // }
1294 : //
1295 : // // test ISO-8859-1 (fallback)
1296 : // {
1297 : // as2js::DecodingFilter::pointer_t filter(new as2js::DecodingFilterDetect);
1298 : //
1299 : // // invalid UTF-8 on the first 4 bytes
1300 : // filter->putc(0xFF);
1301 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1302 : // filter->putc(0x01);
1303 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1304 : // filter->putc(0x02);
1305 : // CPPUNIT_ASSERT(filter->getc() == as2js::Input::INPUT_NAC);
1306 : // filter->putc(0x03);
1307 : //
1308 : // CPPUNIT_ASSERT(filter->getc() == 0xFF);
1309 : // CPPUNIT_ASSERT(filter->getc() == 0x01);
1310 : // CPPUNIT_ASSERT(filter->getc() == 0x02);
1311 : // CPPUNIT_ASSERT(filter->getc() == 0x03);
1312 : // }
1313 : //}
1314 : //
1315 : //
1316 : //void As2JsStreamUnitTests::test_string_input()
1317 : //{
1318 : // {
1319 : // as2js::String input_data("This is\nthe\ninput data\n");
1320 : // as2js::Input::pointer_t str_input(new as2js::StringInput(input_data));
1321 : //
1322 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1323 : // for(size_t idx(0); idx < input_data.length(); ++idx)
1324 : // {
1325 : // as2js::Input::char_t c(str_input->getc());
1326 : // CPPUNIT_ASSERT(c == input_data[idx]);
1327 : // // the input does not know anything about the position
1328 : // // so it does not change a bit
1329 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1330 : // }
1331 : // }
1332 : //
1333 : // {
1334 : // as2js::String input_data("Here we have another string\n");
1335 : // as2js::Input::pointer_t str_input(new as2js::StringInput(input_data));
1336 : //
1337 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1338 : // for(size_t idx(0); idx < input_data.length(); ++idx)
1339 : // {
1340 : // as2js::Input::char_t c(str_input->getc());
1341 : // CPPUNIT_ASSERT(c == input_data[idx]);
1342 : //
1343 : // // unget and re-get
1344 : // str_input->ungetc(c);
1345 : // as2js::Input::char_t bad;
1346 : // do
1347 : // {
1348 : // bad = (rand() << 16) ^ rand();
1349 : // }
1350 : // while(bad > 0 && bad < 0x110000);
1351 : // str_input->ungetc(bad); // this will be ignored
1352 : // str_input->ungetc(0); // and this too (0 is rather unlikely otherwise
1353 : // CPPUNIT_ASSERT(str_input->getc() == input_data[idx]);
1354 : //
1355 : // // the input does not know anything about the position
1356 : // // so it does not change a bit
1357 : // CPPUNIT_ASSERT(static_cast<as2js::Input const *>(str_input.get())->get_position().get_line() == 1);
1358 : // }
1359 : // }
1360 : //
1361 : // {
1362 : // as2js::String input_data("This is\nthe\ninput data\n");
1363 : // as2js::Input::pointer_t str_input(new as2js::StringInput(input_data));
1364 : //
1365 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1366 : // int line(1);
1367 : // for(size_t idx(0); idx < input_data.length(); ++idx)
1368 : // {
1369 : // as2js::Input::char_t c(str_input->getc());
1370 : // CPPUNIT_ASSERT(c == input_data[idx]);
1371 : // if(c == '\n')
1372 : // {
1373 : // ++line;
1374 : // str_input->get_position().new_line();
1375 : // }
1376 : // // we handle the '\n' so the line no. increases in this one
1377 : // CPPUNIT_ASSERT(static_cast<as2js::Input const *>(str_input.get())->get_position().get_line() == line);
1378 : // }
1379 : // }
1380 : //}
1381 : //
1382 : //
1383 : //void As2JsStreamUnitTests::test_stdin()
1384 : //{
1385 : // {
1386 : // // 1. create a file with some test in it
1387 : // char filename[256];
1388 : // strncpy(filename, "/tmp/testXXXXXX.js", sizeof(filename));
1389 : // int fd(mkstemps(filename, 3));
1390 : // char const *input_data("This is\nthe\ninput data\nfor std::cin\n");
1391 : // size_t const len(strlen(input_data));
1392 : // if(write(fd, input_data, len) != static_cast<ssize_t>(len))
1393 : // {
1394 : // CPPUNIT_ASSERT(!"write failed");
1395 : // return;
1396 : // }
1397 : // close(fd);
1398 : //
1399 : // // 2. put that in std::cin
1400 : // if(freopen(filename, "r", stdin) == nullptr)
1401 : // {
1402 : // std::cerr << "error: failed to set stdin to a file" << std::endl;
1403 : // CPPUNIT_ASSERT(!"freopen(..., stdin) failed");
1404 : // return;
1405 : // }
1406 : //
1407 : // as2js::Input::pointer_t str_input(new as2js::StandardInput);
1408 : //
1409 : // // The filename for the StandardInput is set to "-" by default
1410 : // CPPUNIT_ASSERT(str_input->get_position().get_filename() == "-");
1411 : //
1412 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1413 : // as2js::String input_data_str(input_data);
1414 : // for(size_t idx(0); idx < input_data_str.length(); ++idx)
1415 : // {
1416 : // as2js::Input::char_t c(str_input->getc());
1417 : // CPPUNIT_ASSERT(c == input_data_str[idx]);
1418 : // // the input does not know anything about the position
1419 : // // so it does not change a bit
1420 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1421 : // }
1422 : //
1423 : // unlink(filename);
1424 : // }
1425 : //}
1426 : //
1427 : //
1428 : //void As2JsStreamUnitTests::test_file()
1429 : //{
1430 : // {
1431 : // // 1. create a file with some test in it
1432 : // char filename[256];
1433 : // strncpy(filename, "/tmp/testXXXXXX.js", sizeof(filename));
1434 : // int fd(mkstemps(filename, 3));
1435 : // char const *input_data("This is\nthe\ninput data\nfor the file\n");
1436 : // size_t const len(strlen(input_data));
1437 : // if(write(fd, input_data, len) != static_cast<ssize_t>(len))
1438 : // {
1439 : // CPPUNIT_ASSERT(!"write failed");
1440 : // return;
1441 : // }
1442 : // close(fd);
1443 : //
1444 : // // 2. put that in a file
1445 : // as2js::FileInput::pointer_t str_input(new as2js::FileInput);
1446 : //
1447 : // // test a filename that does not exist
1448 : // CPPUNIT_ASSERT(!str_input->open("I'm pretty sure that this will not work although a funky programmer may end up creating such a file..."));
1449 : // // filename not modified if open fails
1450 : // CPPUNIT_ASSERT(str_input->get_position().get_filename() == "");
1451 : //
1452 : // // expect this open to work
1453 : // CPPUNIT_ASSERT(str_input->open(filename));
1454 : //
1455 : // // The filename for the StandardInput is set to "-" by default
1456 : // CPPUNIT_ASSERT(str_input->get_position().get_filename() == filename);
1457 : //
1458 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1459 : // as2js::String input_data_str(input_data);
1460 : // for(size_t idx(0); idx < input_data_str.length(); ++idx)
1461 : // {
1462 : // as2js::Input::char_t c(str_input->getc());
1463 : // CPPUNIT_ASSERT(c == input_data_str[idx]);
1464 : // // the input does not know anything about the position
1465 : // // so it does not change a bit
1466 : // CPPUNIT_ASSERT(str_input->get_position().get_line() == 1);
1467 : // }
1468 : //
1469 : // // if already open, we get a throw
1470 : // CPPUNIT_ASSERT_THROW(str_input->open("This is yet another filename..."), as2js::exception_file_already_open);
1471 : // CPPUNIT_ASSERT(str_input->get_position().get_filename() == filename);
1472 : //
1473 : // unlink(filename);
1474 : // }
1475 : //}
1476 : //
1477 : //
1478 : //void As2JsStreamUnitTests::test_bad_impl()
1479 : //{
1480 : // {
1481 : // class BadImpl : public as2js::Input
1482 : // {
1483 : // public:
1484 : // // no overloading of either virtual function is a problem!
1485 : // };
1486 : //
1487 : // as2js::Input::pointer_t str_input(new BadImpl);
1488 : // CPPUNIT_ASSERT_THROW(str_input->getc(), as2js::exception_internal_error);
1489 : // }
1490 : //}
1491 : //
1492 : //
1493 : //void As2JsStreamUnitTests::test_stdout()
1494 : //{
1495 : // {
1496 : // // 1. create an empty file
1497 : // char filename[256];
1498 : // strncpy(filename, "/tmp/testXXXXXX.js", sizeof(filename));
1499 : // int fd(mkostemps(filename, 3, O_WRONLY));
1500 : //
1501 : // // 2. put that in std::cout
1502 : // if(freopen(filename, "a", stdout) == nullptr)
1503 : // {
1504 : // CPPUNIT_ASSERT(!"freopen() of stdout failed");
1505 : // return;
1506 : // }
1507 : //
1508 : // // 3. generate some data in the file
1509 : // as2js::String str("This is some test to send to stdout\n");
1510 : // bool assert0(false);
1511 : // bool assert1(false);
1512 : // bool assert2(false);
1513 : // {
1514 : // as2js::Output::pointer_t output(new as2js::StandardOutput);
1515 : //
1516 : // // at the start the position is expected to be 1
1517 : //
1518 : // // The filename for the StandardOutput is set to "-" by default
1519 : // assert0 = output->get_position().get_filename() == "-";
1520 : //
1521 : // assert1 = output->get_position().get_line() == 1;
1522 : //
1523 : // output->write(str);
1524 : //
1525 : // // the write() has no effect over the position
1526 : // assert2 = static_cast<as2js::Output const *>(output.get())->get_position().get_line() == 1;
1527 : // }
1528 : // // now StandardOutput is closed, verify the contents of the file
1529 : //
1530 : // // 4. reassign the output
1531 : // //
1532 : // // This freopen() works under Linux, on other systems, you may
1533 : // // have to fiddle with the code; see:
1534 : // // https://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdo
1535 : // if(freopen("/dev/tty", "a", stdout) == nullptr)
1536 : // {
1537 : // CPPUNIT_ASSERT(!"freopen() to restore stdout failed");
1538 : // return;
1539 : // }
1540 : //
1541 : // CPPUNIT_ASSERT(assert0 && assert1 && assert2); // these are here because stdout is now restored
1542 : //
1543 : // lseek(fd, 0, SEEK_SET);
1544 : //
1545 : // char buf[256];
1546 : // ssize_t l(read(fd, buf, sizeof(buf)));
1547 : // CPPUNIT_ASSERT(l == str.utf8_length());
1548 : // CPPUNIT_ASSERT(static_cast<size_t>(l) < sizeof(buf)); // too large, we cannot continue
1549 : // buf[l] = '\0';
1550 : // CPPUNIT_ASSERT(str == buf);
1551 : //
1552 : // close(fd);
1553 : // unlink(filename);
1554 : // }
1555 : //}
1556 : //
1557 : //
1558 : //void As2JsStreamUnitTests::test_stdout_destructive()
1559 : //{
1560 : // if(as2js_test::g_run_destructive)
1561 : // {
1562 : // // 1. create an empty file
1563 : // char filename[256];
1564 : // strncpy(filename, "/tmp/testXXXXXX.js", sizeof(filename));
1565 : // int fd(mkostemps(filename, 3, O_WRONLY));
1566 : // close(fd);
1567 : //
1568 : // // 2. put that in std::cout
1569 : // if(freopen(filename, "a", stdout) == nullptr)
1570 : // {
1571 : // CPPUNIT_ASSERT(!"freopen() of stdout failed");
1572 : // return;
1573 : // }
1574 : // setbuf(stdout, nullptr); // with a buffer the write would not fail!
1575 : //
1576 : // // 3. generate some data in the file
1577 : // as2js::String str("This is some test to send to stdout\n");
1578 : // {
1579 : // as2js::Output::pointer_t output(new as2js::StandardOutput);
1580 : //
1581 : // // close stdout before writing to it so we get an error
1582 : // close(fileno(stdout));
1583 : //
1584 : // CPPUNIT_ASSERT_THROW(output->write(str), as2js::exception_exit);
1585 : // }
1586 : // // now StandardOutput is closed, verify the contents of the file
1587 : //
1588 : // // 4. reassign the output
1589 : // //
1590 : // // This freopen() fails with error 22 (EINVAL).
1591 : // // have to fiddle with the code; see:
1592 : // // https://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdo
1593 : // if(freopen("/dev/tty", "a+", stdout) == nullptr)
1594 : // {
1595 : // CPPUNIT_ASSERT(!"freopen() of stdout failed");
1596 : // return;
1597 : // }
1598 : //
1599 : // unlink(filename);
1600 : // }
1601 : // else
1602 : // {
1603 : // std::cout << " --- test_stdout_destructive() not run, use --destructive on the command line to not bypass this test --- ";
1604 : // }
1605 : //}
1606 : //
1607 : //
1608 : //void As2JsStreamUnitTests::test_output()
1609 : //{
1610 : // {
1611 : // // 1. create an empty file
1612 : // char const *filename("/tmp/test123456.js");
1613 : //
1614 : // // 2. generate some data in the file
1615 : // as2js::String str("This is\nsome test\nto send\nto \"filename\".\n");
1616 : // {
1617 : // as2js::FileOutput::pointer_t output(new as2js::FileOutput);
1618 : //
1619 : // CPPUNIT_ASSERT(!output->open("/first/we/want/to/test/with/an/invalid/filename!"));
1620 : //
1621 : // CPPUNIT_ASSERT(output->open(filename));
1622 : //
1623 : // CPPUNIT_ASSERT_THROW(output->open("another one"), as2js::exception_file_already_open);
1624 : //
1625 : // // at the start the position is expected to be 1
1626 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1627 : //
1628 : // // The filename for the FileOutput is set to the file filename as passed to open()
1629 : // CPPUNIT_ASSERT(output->get_position().get_filename() == filename);
1630 : //
1631 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1632 : //
1633 : // output->write(str);
1634 : //
1635 : // // the write() has no effect over the position
1636 : // CPPUNIT_ASSERT(static_cast<as2js::Output const *>(output.get())->get_position().get_line() == 1);
1637 : // }
1638 : // // now FileOutput is closed, verify the contents of the file
1639 : //
1640 : // int fd(open(filename, O_RDONLY));
1641 : //
1642 : // char buf[256];
1643 : // ssize_t l(read(fd, buf, sizeof(buf)));
1644 : // CPPUNIT_ASSERT(l == str.utf8_length());
1645 : // CPPUNIT_ASSERT(static_cast<size_t>(l) < sizeof(buf)); // too large, we cannot continue
1646 : // buf[l] = '\0';
1647 : // CPPUNIT_ASSERT(str == buf);
1648 : //
1649 : // close(fd);
1650 : // unlink(filename);
1651 : // }
1652 : //
1653 : // {
1654 : // // 1. create an empty file
1655 : // char const *filename("/tmp/test789012.js");
1656 : //
1657 : // // 2. determine the current fd
1658 : // // (see use of fd_to_close further below)
1659 : // char const *find_fd("/tmp/test345678.js");
1660 : // int fd_to_close(open(find_fd, O_RDWR|O_CREAT, 0600));
1661 : // CPPUNIT_ASSERT(fd_to_close >= 0);
1662 : // close(fd_to_close);
1663 : // unlink(find_fd);
1664 : //
1665 : // // 3. generate some data in the file
1666 : // as2js::String str("This is\nsome test\nto send\nto \"filename\".\n");
1667 : // while(str.length() < 64 * 1024) // we should get the size from a file created with fopen()... ?
1668 : // {
1669 : // str += "This string is too short to make sure we get a flush and a write error...";
1670 : // }
1671 : // {
1672 : // as2js::FileOutput::pointer_t output(new as2js::FileOutput);
1673 : //
1674 : // CPPUNIT_ASSERT(!output->open("/first/we/want/to/test/with/an/invalid/filename!"));
1675 : //
1676 : // CPPUNIT_ASSERT(output->open(filename));
1677 : //
1678 : // CPPUNIT_ASSERT_THROW(output->open("another one"), as2js::exception_file_already_open);
1679 : //
1680 : // // at the start the position is expected to be 1
1681 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1682 : //
1683 : // // The filename for the StandardOutput is set to "-" by default
1684 : // CPPUNIT_ASSERT(output->get_position().get_filename() == filename);
1685 : //
1686 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1687 : //
1688 : // // close so we can generate an error...
1689 : // close(fd_to_close);
1690 : //
1691 : // CPPUNIT_ASSERT_THROW(output->write(str), as2js::exception_exit);
1692 : //
1693 : // // the write() has no effect over the position
1694 : // CPPUNIT_ASSERT(static_cast<as2js::Output const *>(output.get())->get_position().get_line() == 1);
1695 : // }
1696 : // // now FileOutput is closed, verify the contents of the file
1697 : //
1698 : // int fd(open(filename, O_RDONLY));
1699 : //
1700 : // char buf[256];
1701 : // ssize_t l(read(fd, buf, sizeof(buf)));
1702 : // CPPUNIT_ASSERT(l == 0); // must be empty since it was closed before the write
1703 : //
1704 : // close(fd);
1705 : // unlink(filename);
1706 : // }
1707 : //}
1708 : //
1709 : //
1710 : //void As2JsStreamUnitTests::test_string_output()
1711 : //{
1712 : // {
1713 : // as2js::String str("This is\nsome test\nto send\nto \"filename\".\n");
1714 : //
1715 : // as2js::StringOutput::pointer_t output(new as2js::StringOutput);
1716 : //
1717 : // // at the start the position is expected to be 1
1718 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1719 : //
1720 : // // The filename for the StringOutput is always ""
1721 : // CPPUNIT_ASSERT(output->get_position().get_filename() == "");
1722 : //
1723 : // CPPUNIT_ASSERT(output->get_position().get_line() == 1);
1724 : //
1725 : // output->write(str);
1726 : //
1727 : // // the write() has no effect over the position
1728 : // CPPUNIT_ASSERT(static_cast<as2js::Output const *>(output.get())->get_position().get_line() == 1);
1729 : //
1730 : // CPPUNIT_ASSERT(output->get_string() == str);
1731 : //
1732 : // output->write(str);
1733 : // CPPUNIT_ASSERT(output->get_string() == str + str);
1734 : // }
1735 : //}
1736 :
1737 :
1738 : // vim: ts=4 sw=4 et
|