]> git.lyx.org Git - features.git/blob - src/support/tests/check_trivstring.cpp
f2c44e44ad38fd6fc66ad301d679a40dcf45a38f
[features.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 }
72
73 void test_trivdocstring()
74 {
75         docstring const input[] = {
76                 from_ascii(""),
77                 from_ascii("a"),
78                 from_ascii("42"),
79                 from_ascii("max"), // max. string with sso on 64 bit
80                 from_ascii("something which does not fit into sso")
81         };
82         size_t const n = sizeof(input) / sizeof(input[0]);
83         for (size_t i = 0; i < n; ++i) {
84                 // construction from std::string
85                 trivdocstring const a(input[i]);
86                 // construction from trivstring
87                 trivdocstring const b(a);
88                 // assignment from trivstring
89                 trivdocstring const c = a;
90                 // assignment from std::string
91                 trivdocstring d = input[i];
92                 // assignment from trivstring
93                 docstring const e = a;
94                 // assignment from trivstring via C string
95                 docstring const f = a.c_str();
96                 if (a.empty())
97                         cout << "empty ";
98                 else
99                         cout << "not empty ";
100                 cout << a.length() << endl;
101                 cout << to_ascii(a) << endl;
102                 cout << to_ascii(b) << endl;
103                 cout << to_ascii(c) << endl;
104                 cout << to_ascii(d) << endl;
105                 cout << to_ascii(e) << endl;
106                 cout << to_ascii(f) << endl;
107                 // swap
108                 trivdocstring g(from_ascii("swap"));
109                 cout << to_ascii(g) << endl;
110                 d.swap(g);
111                 cout << to_ascii(d) << endl;
112                 cout << to_ascii(g) << endl;
113         }
114         // comparison
115         trivdocstring const a;
116         trivdocstring const b(from_ascii("a"));
117         trivdocstring const c(from_ascii("b"));
118         trivdocstring const d(from_ascii("42"));
119         cout << (a == a) << ' ' << (a < a) << endl; // equal strings
120         cout << (a == b) << ' ' << (a < b) << endl; // different strings, same length
121         cout << (b == a) << ' ' << (b < a) << endl; // different strings, same length
122         cout << (a == c) << ' ' << (a < c) << endl; // different strings, different length
123         cout << (c == a) << ' ' << (c < a) << endl; // different strings, different length
124         // per character initialization works also if char_type != wchar
125         char_type const e[1] = {'\0'};
126         char_type const f[2] = {'b', '\0'};
127         char_type const g[3] = {'4', '2', '\0'};
128         cout << (a == e) << ' ' << (e == a) << endl; // empty strings
129         cout << (c == a) << ' ' << (a == c) << endl; // equal strings
130         cout << (a == f) << ' ' << (f == a) << endl; // different strings, same length
131         cout << (a == g) << ' ' << (g == a) << endl; // different strings, different length
132 }
133
134 int main()
135 {
136         test_trivstring();
137         test_trivdocstring();
138 }