1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestUnicodeStringAPI.cxx
5 
6 -------------------------------------------------------------------------
7   Copyright 2008 Sandia Corporation.
8   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9   the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13   All rights reserved.
14   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16      This software is distributed WITHOUT ANY WARRANTY; without even
17      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18      PURPOSE.  See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
22 #include <vtkUnicodeString.h>
23 
24 #include <vtksys/stl/iterator>
25 #include <vtksys/ios/iostream>
26 #include <vtksys/ios/sstream>
27 #include <vtksys/stl/stdexcept>
28 
29 #define test_expression(expression) \
30 { \
31   if(!(expression)) \
32     { \
33     vtksys_ios::ostringstream buffer; \
34     buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
35     throw std::runtime_error(buffer.str()); \
36     } \
37 }
38 
39 // Sample strings - nothing risque, I hope ...
40 static const std::string sample_utf8_ascii = "abcde123";
41 static const std::string sample_utf8_greek = "\xce\xb1\xce\xb2\xce\xb3"; // Greek lower-case alpha, beta, gamma.
42 static const std::string sample_utf8_thai = "\xe0\xb8\x81\xe0\xb8\x82\xe0\xb8\x83"; // Thai ko kai, kho khai, kho khuat.
43 static const std::string sample_utf8_linear_b = "\xf0\x90\x80\x80\xf0\x90\x80\x81\xf0\x90\x80\x82\xf0\x90\x80\x83\xf0\x90\x80\x84"; // Linear-B syllables a, e, i, o, u.
44 static const std::string sample_utf8_mixed = "a\xce\xb1\xe0\xb8\x81\xf0\x90\x80\x80"; // a, alpha, ko kai, syllable-a.
45 static const vtkTypeUInt16 sample_utf16[] =
46 {
47   0x0041,       // 'a'
48   0x0020,       // ' '
49   0xD800,       // high-half zone part
50   0xDC00,       // low-half zone part
51   0xD800,       // etc.
52   0xDC01,
53   0x0000
54 };
55 
56 
TestUnicodeStringAPI(int,char * [])57 int TestUnicodeStringAPI(int, char*[])
58 {
59   try
60     {
61     vtkUnicodeString a;
62     test_expression(a.empty());
63     test_expression(a.character_count() == 0);
64 
65     a = vtkUnicodeString::from_utf8(sample_utf8_ascii);
66     test_expression(!a.empty());
67     test_expression(a.character_count() == 8);
68     test_expression(a.utf8_str() == sample_utf8_ascii);
69     test_expression(a.at(1) == 0x00000062);
70     test_expression(a[1] == 0x00000062);
71 
72     a.clear();
73     test_expression(a.empty());
74 
75     a = vtkUnicodeString::from_utf8(sample_utf8_greek);
76     test_expression(a.character_count() == 3);
77     test_expression(a.utf8_str() == sample_utf8_greek);
78     test_expression(a.at(2) == 0x000003b3);
79     test_expression(a[2] == 0x000003b3);
80 
81     a = vtkUnicodeString::from_utf8(sample_utf8_thai);
82     test_expression(a.character_count() == 3);
83     test_expression(a.utf8_str() == sample_utf8_thai);
84     test_expression(a.at(1) == 0x00000e02);
85     test_expression(a[1] == 0x00000e02);
86 
87     a = vtkUnicodeString::from_utf8(sample_utf8_linear_b);
88     test_expression(a.character_count() == 5);
89     test_expression(a.utf8_str() == sample_utf8_linear_b);
90     test_expression(a.at(4) == 0x00010004);
91     test_expression(a[4] == 0x00010004);
92 
93     a = vtkUnicodeString::from_utf8(sample_utf8_mixed);
94     test_expression(a.character_count() == 4);
95     test_expression(a.utf8_str() == sample_utf8_mixed);
96     test_expression(a.at(2) == 0x00000e01);
97     test_expression(a[2] == 0x00000e01);
98 
99     vtkUnicodeString b = vtkUnicodeString::from_utf8(sample_utf8_ascii);
100     test_expression(b.utf8_str() == sample_utf8_ascii);
101     test_expression(a.utf8_str() == sample_utf8_mixed);
102     a.swap(b);
103     test_expression(a.utf8_str() == sample_utf8_ascii);
104     test_expression(b.utf8_str() == sample_utf8_mixed);
105 
106     a = vtkUnicodeString::from_utf16(sample_utf16);
107     test_expression(a.character_count() == 4);
108     test_expression(a[0] == 0x00000041);
109     test_expression(a[1] == 0x00000020);
110     test_expression(a[2] == 0x00010000);
111     test_expression(a[3] == 0x00010001);
112 
113     a = vtkUnicodeString::from_utf8("Hello, World!");
114     test_expression(a.substr(7) == vtkUnicodeString::from_utf8("World!"));
115     test_expression(a.substr(1, 4) == vtkUnicodeString::from_utf8("ello"));
116 
117     return 0;
118     }
119   catch(std::exception& e)
120     {
121     cerr << e.what() << endl;
122     return 1;
123     }
124 }
125 
126