1 /* $Id: mimestream-test.cc,v 1.2 2003/09/27 21:31:04 atterer Exp $ -*- C++ -*-
3 |_) /| Copyright (C) 2000-2002 | richard@
4 | \/¯| Richard Atterer | atterer.net
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2. See
8 the file COPYING for details.
10 Convert binary data to/from ASCII using Base64 encoding
22 #include <mimestream.hh>
23 //______________________________________________________________________
25 int main(int argc, char* argv[]) {
26 if (argc == 2) Logger::scanOptions(argv[1], argv[0]);
28 const char* correct = "V29yYmxlLCB3b2ZmbGUsIN_f3w";
30 char str[] = "Worble, woffle, ßßß";
32 for (char* s = str; *s != 0; ++s) m << *s;
34 Assert(m.result() == correct);
37 for (char* s = str; *s != 0; ++s) m << (*s);
38 m << flush; Assert(m.result() == correct);
41 m.write(str, sizeof(str) - 1); m << flush << flush;
42 Assert(m.result() == correct);
44 m.write((unsigned char*)str, sizeof(str) - 1); m.flush();
45 Assert(m.result() == correct);
47 m.write((signed char*)str, sizeof(str) - 1); m.flush();
48 Assert(m.result() == correct);
50 m.write((void*)str, sizeof(str) - 1); m.flush();
51 Assert(m.result() == correct);
55 Assert(m.result() == correct);
57 m << (unsigned char*)str; m.flush();
58 Assert(m.result() == correct);
60 m << (signed char*)str; m.flush();
61 Assert(m.result() == correct);
63 m << (void*)str; m.flush();
64 Assert(m.result() == correct);
65 //____________________
70 uint32 i = 0xf154ba1f;
72 Assert(m.result() == correct);
77 Assert(m.result() == correct);
80 m << char(0x1f) << (unsigned char)(0xba) << 0xfff54 << '\xf1';
82 Assert(m.result() == correct);
83 //____________________
85 /* Try many of the 16.7 million ways of a "3 byte" <=> "4 ASCII chars"
88 for (unsigned k = 0; k < 256; ++k) x[k * 3 + 2] = byte(k);
92 for (unsigned i = 0; i < 256; i += 57) {
93 for (unsigned k = 0; k < 256; ++k) x[k * 3 + 0] = i;
95 for (; j < 256; j += 43) {
96 for (unsigned k = 0; k < 256; ++k) x[k * 3 + 1] = j;
98 toAscii.write(x, 3*256).flush();
99 Assert(toAscii.result().length() == 4*256);
101 toBin.put(toAscii.result().data(), 4*256);
102 vector<byte>& r = toBin.result();
103 Assert(r.size() == 3*256);
104 for (unsigned k = 0; k < 256; ++k) {
105 if (r[k*3] != x[k*3] || r[k*3+1] != x[k*3+1] || r[k*3+2] != x[k*3+2]) {
107 cout << "x=["<<int(x[k*3])<<','<<int(x[k*3+1])<<','<<int(x[k*3+2])<<"] "
108 "r=["<<int(r[k*3])<<','<<int(r[k*3+1])<<','<<int(r[k*3+2])<<"] "
109 << toAscii.result() << endl;
113 toBin.result().clear();
114 toAscii.result().erase();
117 //cout << ' ' << 255-i << " \r" << flush;