-
Notifications
You must be signed in to change notification settings - Fork 9
/
unit.cpp
233 lines (159 loc) · 7 KB
/
unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// unit.cpp - Lxroot's C++ unit tests.
// Copyright (c) 2021 Parke Bostrom, parke.nexus at gmail.com
// Distributed under GPLv3 (see end of file) WITHOUT ANY WARRANTY.
// version 20220822
#define LXROOT_MAIN_SKIP 1
#include "lxroot.cpp"
struct Unit { // ----------------------------------------- struct Unit
void t_basename ( Str input, Str expect, cint line ) { // -----------
Str actual = input.basename();
if ( actual == expect ) { return; }
printe ( "\n" "basename_test failed line %d\n", line );
printe ( " input %d '%s'", input.n, input.s );
printe ( " expect %d '%s'", expect.n, expect.s );
printe ( " actual %d '%s'", actual.n, actual.s );
abort(); }
void t_Str () { // ------------------------------------- Unit t_Str
printe ("");
Str null = nullptr; assert ( null.n == 0 );
; assert ( null.s == nullptr );
Str empty = ""; assert ( empty.n == 0 );
; assert ( empty.s not_eq nullptr );
Str a = "a"; assert ( a.n == 2 );
Str a1 = { "a", 1 }; assert ( a1.n == 1 );
Str a2 = { "a", 2 }; assert ( a2.n == 2 );
Str trace = "--trace"; assert ( trace.starts_with ("--") );
; assert ( trace.starts_with ("-") );
; assert ( trace == "--trace" );
return; }
void t_Cat () { // ------------------------------------------- t_Cat
Str a = "a";
Str b = "b";
Str c = "c";
Str d = "d";
Cat2 c2 = a + b;
assert ( c2.a == "a" );
assert ( c2.b == "b" );
assert ( (&c2.a)[0] == "a" );
assert ( (&c2.a)[1] == "b" );
Cat3 c3 = a + b + c;
assert ( c3.a == "a" );
assert ( c3.b == "b" );
assert ( c3.c == "c" );
assert ( (&c3.a)[0] == "a" );
assert ( (&c3.a)[1] == "b" );
assert ( (&c3.a)[2] == "c" );
Cat3 c3a = "a" + b + "c";
assert ( c3a.a == "a" );
assert ( c3a.b == "b" );
assert ( c3a.c == "c" );
assert ( (&c3a.a)[0] == "a" );
assert ( (&c3a.a)[1] == "b" );
assert ( (&c3a.a)[2] == "c" );
assert ( oStr ( a + b ) == "ab" );
assert ( oStr ( a + "b" ) == "ab" );
assert ( oStr ( "a" + b ) == "ab" );
assert ( oStr ( a + b + c ) == "abc" );
assert ( oStr ( a + "b" + "c" ) == "abc" );
assert ( oStr ( "a" + b + c ) == "abc" );
assert ( oStr ( a + b + c + d ) == "abcd" );
assert ( oStr ( a + "b" + "c" + "d" ) == "abcd" );
assert ( oStr ( "a" + b + c + d ) == "abcd" );
return; }
void t_basename_all () { // ------------------- Unit t_basename_all
t_basename ( "", "", __LINE__ );
t_basename ( "/", "/", __LINE__ );
t_basename ( "//", "/", __LINE__ );
t_basename ( "///", "/", __LINE__ );
t_basename ( "a", "a", __LINE__ );
t_basename ( "/b", "b", __LINE__ );
t_basename ( "c/", "c", __LINE__ );
t_basename ( "/d/", "d", __LINE__ );
t_basename ( "e/f", "f", __LINE__ );
t_basename ( "/g/h", "h", __LINE__ );
t_basename ( "abc", "abc", __LINE__ );
t_basename ( "/def", "def", __LINE__ );
t_basename ( "ghi/", "ghi", __LINE__ );
t_basename ( "/jkl/", "jkl", __LINE__ );
t_basename ( "mno/pqr", "pqr", __LINE__ );
t_basename ( "/stu/vwx", "vwx", __LINE__ );
t_basename ( "./xyz", "xyz", __LINE__ );
return; }
void t_oStr () { // ----------------------------------- Unit t_oStr
Str a = "a"; // printe ( "a '%s'", a.s );
Str a1 = {"a", 1};
Str a2 = {"a", 2}; // printe ( "a '%s'", a.s );
assert ( a == "a" );
assert ( a1 == "a" );
assert ( a2 == "a" );
assert ( a == a1 );
assert ( a == a2 );
Str b = "b"; // printe ( "b '%s'", b.s );
assert ( a not_eq b );
oStr ab = a + b; // printe ( "ab '%s'", ab.s );
assert ( ab == "ab" ); //
assert ( ab == oStr(a+b) ); //
Str c = "c"; //
Str d = "d"; //
oStr cd = c + "=" + d; //
assert ( cd == "c=d" ); //
oStr ef = cd + "ef" + "gh"; //
assert ( ef == "c=defgh" ); //
assert ( cd == "c=d" ); //
return; }
static void t_Argv () { // ---------------------------- Unit t_Argv
str p[] = { "a=1", "b=2", "c=3", nullptr };
Argv a(p);
assert ( a[0] == p[0] );
assert ( a[1] == p[1] );
assert ( a[2] == p[2] );
assert ( a[3] == p[3] );
assert ( a[0] == a.p[0] );
assert ( a[1] == a.p[1] );
assert ( a[2] == a.p[2] );
assert ( a[3] == a.p[3] );
assert ( a.concat() == "a=1 b=2 c=3" );
assert ( a.env_get("a") == p[0] );
assert ( a.env_get("b") == p[1] );
assert ( a.env_get("c") == p[2] );
return; }
void t_st_to_ms () { // --------------------------- Unit t_st_to_ms
const auto st_to_ms = Syscall :: st_to_ms;
assert( st_to_ms ( ST_RDONLY ) == MS_RDONLY );
assert( st_to_ms ( ST_NOSUID ) == MS_NOSUID );
assert( st_to_ms ( ST_NODEV ) == MS_NODEV );
assert( st_to_ms ( ST_NOEXEC ) == MS_NOEXEC );
assert( st_to_ms ( ST_SYNCHRONOUS ) == MS_SYNCHRONOUS );
assert( st_to_ms ( ST_MANDLOCK ) == MS_MANDLOCK );
assert( st_to_ms ( ST_NOATIME ) == MS_NOATIME );
assert( st_to_ms ( ST_NODIRATIME ) == MS_NODIRATIME );
assert( st_to_ms ( ST_RELATIME ) == MS_RELATIME );
return; }
void main () { // ------------------------------------- Unit main
t_Str();
t_Cat();
t_basename_all();
t_oStr();
t_Argv();
t_st_to_ms();
printf ( "unit.cpp done all tests passed\n" ); }
}; // end struct Unit ------------------------------ end struct Unit
int main () { // -------------------------------------------- :: main
Unit() .main();
return 0; }
// unit.cpp - Lxroot's C++ unit tests.
//
// Copyright (c) 2021 Parke Bostrom, parke.nexus at gmail.com
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See version
// 3 of the GNU General Public License for more details.
//
// You should have received a copy of version 3 of the GNU General
// Public License along with this program. If not, see
// <https://www.gnu.org/licenses/>.