Build system improvements
[ustl.git] / bvt / bvt16.cc
blob4ed4d81636601f9d9f561b28f339c9787bf88ce8
1 // This file is part of the ustl library, an STL implementation.
2 //
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
7 #include "stdtest.h"
9 static void Widen (const string& str, vector<wchar_t>& result)
11 result.clear();
12 result.resize (str.length());
13 copy (str.utf8_begin(), str.utf8_end(), result.begin());
16 static void DumpWchars (const vector<wchar_t>& v)
18 foreach (vector<wchar_t>::const_iterator, i, v)
19 cout.format (" %u", uint32_t(*i));
22 void TestUTF8 (void)
24 cout << "Generating Unicode characters ";
25 vector<wchar_t> srcChars;
26 srcChars.resize (0xFFFF);
27 iota (srcChars.begin(), srcChars.end(), 0);
28 cout.format ("%zu - %zu\n", size_t(srcChars[0]), size_t(srcChars.back()));
30 cout << "Encoding to utf8.\n";
31 string encoded;
32 encoded.reserve (srcChars.size() * 4);
33 copy (srcChars, utf8out (back_inserter(encoded)));
34 static const char c_ProperEncoding[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
35 if (encoded.compare (encoded.begin(), encoded.begin() + VectorSize(c_ProperEncoding), VectorRange(c_ProperEncoding))) {
36 cout << "Encoding failed: ";
37 for (string::const_iterator i = encoded.begin(); i != encoded.begin() + VectorSize(c_ProperEncoding); ++ i)
38 cout << uint32_t(*i);
39 cout << endl;
42 cout << "Decoding back.\n";
43 vector<wchar_t> decChars;
44 Widen (encoded, decChars);
46 cout.format ("Comparing.\nsrc = %zu chars, encoded = %zu chars, decoded = %zu\n", srcChars.size(), encoded.size(), decChars.size());
47 size_t nDiffs = 0;
48 for (uoff_t i = 0; i < min (srcChars.size(), decChars.size()); ++ i) {
49 if (srcChars[i] != decChars[i]) {
50 cout.format ("%u != %u\n", uint32_t(srcChars[i]), uint32_t(decChars[i]));
51 ++ nDiffs;
54 cout.format ("%zu differences between src and decoded.\n", nDiffs);
56 cout << "Testing wide character string::insert\n";
57 string ws ("1234567890", 10);
59 ws.insert (ws.find('1'), wchar_t(1234));
60 static const wchar_t c_WChars[2] = { 3456, 4567 };
61 ws.insert (ws.find('3'), VectorRange(c_WChars), 2);
62 ws.insert (ws.find('3'), wchar_t(2345));
63 ws.insert (ws.size(), wchar_t(5678));
64 cout.format ("Values[%zu]:", ws.length());
65 for (string::utf8_iterator j = ws.utf8_begin(); j < ws.utf8_end(); ++ j)
66 cout.format (" %u", uint32_t(*j));
67 cout << endl;
69 cout << "Character offsets:";
70 for (string::utf8_iterator k = ws.utf8_begin(); k < ws.utf8_end(); ++ k)
71 cout.format (" %zu", distance (ws.begin(), k.base()));
72 cout << endl;
74 cout.format ("Erasing character %zu: ", ws.length() - 1);
75 ws.erase (ws.wiat(ws.length() - 1), ws.end());
76 Widen (ws, decChars);
77 DumpWchars (decChars);
78 cout << endl;
80 cout << "Erasing 2 characters after '2': ";
81 ws.erase (ws.find('2')+1, Utf8Bytes(VectorRange(c_WChars)));
82 Widen (ws, decChars);
83 DumpWchars (decChars);
84 cout << endl;
87 StdBvtMain (TestUTF8)