]> git.lyx.org Git - lyx.git/blob - src/support/tests/check_trivstring.cpp
Less expensive OP first as this might be called often.
[lyx.git] / src / support / tests / check_trivstring.cpp
1 #include <config.h>
2
3 #include "../trivstring.h"
4 #include "../docstring.h"
5
6 #include <iostream>
7
8
9 using namespace lyx;
10
11 using namespace std;
12
13 void test_trivstring()
14 {
15         string const input[] = {
16                 "",
17                 "a",
18                 "42",
19                 "max sso", // max. string with sso on 64 bit
20                 "something which does not fit into sso"
21         };
22         size_t const n = sizeof(input) / sizeof(input[0]);
23         for (size_t i = 0; i < n; ++i) {
24                 // construction from std::string
25                 trivstring const a(input[i]);
26                 // construction from trivstring
27                 trivstring const b(a);
28                 // assignment from trivstring
29                 trivstring const c = a;
30                 // assignment from std::string
31                 trivstring d = input[i];
32                 // assignment from trivstring
33                 string const e = a;
34                 // assignment from trivstring via C string
35                 string const f = a.c_str();
36                 if (a.empty())
37                         cout << "empty ";
38                 else
39                         cout << "not empty ";
40                 cout << a.length() << endl;
41                 cout << a << endl;
42                 cout << b << endl;
43                 cout << c << endl;
44                 cout << d << endl;
45                 cout << e << endl;
46                 cout << f << endl;
47                 // swap
48                 trivstring g("swap");
49                 cout << g << endl;
50                 d.swap(g);
51                 cout << d << endl;
52                 cout << g << endl;
53         }
54         // comparison
55         trivstring const a;
56         trivstring const b("a");
57         trivstring const c("b");
58         trivstring const d("42");
59         cout << (a == a) << ' ' << (a < a) << endl; // equal strings
60         cout << (a == b) << ' ' << (a < b) << endl; // different strings, same length
61         cout << (b == a) << ' ' << (b < a) << endl; // different strings, same length
62         cout << (a == c) << ' ' << (a < c) << endl; // different strings, different length
63         cout << (c == a) << ' ' << (c < a) << endl; // different strings, different length
64         char const * e = "";
65         char const * f = "b";
66         char const * g = "42";
67         cout << (a == e) << ' ' << (e == a) << endl; // empty strings
68         cout << (c == a) << ' ' << (a == c) << endl; // equal strings
69         cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length
70         cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length
71         // operator[]
72         cout << d[1] << d[0] << endl;
73         // substr()
74         cout << d.substr(1) << endl;    // default argument
75         cout << d.substr(1, 1) << endl; // maximum length
76         cout << d.substr(1, 2) << endl; // length larger than max
77         cout << d.substr(2) << endl;    // maximum pos
78 }
79
80 void test_trivdocstring()
81 {
82         docstring const input[] = {
83                 from_ascii(""),
84                 from_ascii("a"),
85                 from_ascii("42"),
86                 from_ascii("max"), // max. string with sso on 64 bit
87                 from_ascii("something which does not fit into sso")
88         };
89         size_t const n = sizeof(input) / sizeof(input[0]);
90         for (size_t i = 0; i < n; ++i) {
91                 // construction from std::string
92                 trivdocstring const a(input[i]);
93                 // construction from trivstring
94                 trivdocstring const b(a);
95                 // assignment from trivstring
96                 trivdocstring const c = a;
97                 // assignment from std::string
98                 trivdocstring d = input[i];
99                 // assignment from trivstring
100                 docstring const e = a;
101                 // assignment from trivstring via C string
102                 docstring const f = a.c_str();
103                 if (a.empty())
104                         cout << "empty ";
105                 else
106                         cout << "not empty ";
107                 cout << a.length() << endl;
108                 cout << to_ascii(a) << endl;
109                 cout << to_ascii(b) << endl;
110                 cout << to_ascii(c) << endl;
111                 cout << to_ascii(d) << endl;
112                 cout << to_ascii(e) << endl;
113                 cout << to_ascii(f) << endl;
114                 // swap
115                 trivdocstring g(from_ascii("swap"));
116                 cout << to_ascii(g) << endl;
117                 d.swap(g);
118                 cout << to_ascii(d) << endl;
119                 cout << to_ascii(g) << endl;
120         }
121         // comparison
122         trivdocstring const a;
123         trivdocstring const b(from_ascii("a"));
124         trivdocstring const c(from_ascii("b"));
125         trivdocstring const d(from_ascii("42"));
126         cout << (a == a) << ' ' << (a < a) << endl; // equal strings
127         cout << (a == b) << ' ' << (a < b) << endl; // different strings, same length
128         cout << (b == a) << ' ' << (b < a) << endl; // different strings, same length
129         cout << (a == c) << ' ' << (a < c) << endl; // different strings, different length
130         cout << (c == a) << ' ' << (c < a) << endl; // different strings, different length
131         // per character initialization works also if char_type != wchar
132         char_type const e[1] = {'\0'};
133         char_type const f[2] = {'b', '\0'};
134         char_type const g[3] = {'4', '2', '\0'};
135         cout << (a == e) << ' ' << (e == a) << endl; // empty strings
136         cout << (c == a) << ' ' << (a == c) << endl; // equal strings
137         cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length
138         cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length
139         // operator[]
140         cout << static_cast<char>(d[1]) << static_cast<char>(d[0]) << endl;
141         // substr()
142         cout << to_ascii(d.substr(1)) << endl;    // default argument
143         cout << to_ascii(d.substr(1, 1)) << endl; // maximum length
144         cout << to_ascii(d.substr(1, 2)) << endl; // length larger than max
145         cout << to_ascii(d.substr(2)) << endl;    // maximum pos
146 }
147
148 int main()
149 {
150         test_trivstring();
151         test_trivdocstring();
152 }