Debian Codegraph @

quickjs/tests/test_std.js

raw symbols SCIP
1 #! (shebang test)
2 import * as std from "std";
3 import * as os from "os";
4
5 function assert(actual, expected, message) {
6 if (arguments.length == 1)
7 expected = true;
8
9 if (actual === expected)
10 return;
11
12 if (actual !== null && expected !== null
13 && typeof actual == 'object' && typeof expected == 'object'
14 && actual.toString() === expected.toString())
15 return;
16
17 throw Error("assertion failed: got |" + actual + "|" +
18 ", expected |" + expected + "|" +
19 (message ? " (" + message + ")" : ""));
20 }
21
22 // load more elaborate version of assert if available
23 try { std.loadScript("test_assert.js"); } catch(e) {}
24
25 /*----------------*/
26
27 function test_printf()
28 {
29 assert(std.sprintf("a=%d s=%s", 123, "abc"), "a=123 s=abc");
30 assert(std.sprintf("%010d", 123), "0000000123");
31 assert(std.sprintf("%x", -2), "fffffffe");
32 assert(std.sprintf("%lx", -2), "fffffffffffffffe");
33 assert(std.sprintf("%10.1f", 2.1), " 2.1");
34 assert(std.sprintf("%*.*f", 10, 2, -2.13), " -2.13");
35 assert(std.sprintf("%#lx", 0x7fffffffffffffffn), "0x7fffffffffffffff");
36 }
37
38 function test_file1()
39 {
40 var f, len, str, size, buf, ret, i, str1;
41
42 f = std.tmpfile();
43 str = "hello world\n";
44 f.puts(str);
45
46 f.seek(0, std.SEEK_SET);
47 str1 = f.readAsString();
48 assert(str1 === str);
49
50 f.seek(0, std.SEEK_END);
51 size = f.tell();
52 assert(size === str.length);
53
54 f.seek(0, std.SEEK_SET);
55
56 buf = new Uint8Array(size);
57 ret = f.read(buf.buffer, 0, size);
58 assert(ret === size);
59 for(i = 0; i < size; i++)
60 assert(buf[i] === str.charCodeAt(i));
61
62 f.close();
63 }
64
65 function test_file2()
66 {
67 var f, str, i, size;
68 f = std.tmpfile();
69 str = "hello world\n";
70 size = str.length;
71 for(i = 0; i < size; i++)
72 f.putByte(str.charCodeAt(i));
73 f.seek(0, std.SEEK_SET);
74 for(i = 0; i < size; i++) {
75 assert(str.charCodeAt(i) === f.getByte());
76 }
77 assert(f.getByte() === -1);
78 f.close();
79 }
80
81 function test_getline()
82 {
83 var f, line, line_count, lines, i;
84
85 lines = ["hello world", "line 1", "line 2" ];
86 f = std.tmpfile();
87 for(i = 0; i < lines.length; i++) {
88 f.puts(lines[i], "\n");
89 }
90
91 f.seek(0, std.SEEK_SET);
92 assert(!f.eof());
93 line_count = 0;
94 for(;;) {
95 line = f.getline();
96 if (line === null)
97 break;
98 assert(line == lines[line_count]);
99 line_count++;
100 }
101 assert(f.eof());
102 assert(line_count === lines.length);
103
104 f.close();
105 }
106
107 function test_popen()
108 {
109 var str, f, fname = "tmp_file.txt";
110 var content = "hello world";
111
112 f = std.open(fname, "w");
113 f.puts(content);
114 f.close();
115
116 /* test loadFile */
117 assert(std.loadFile(fname), content);
118
119 /* execute the 'cat' shell command */
120 f = std.popen("cat " + fname, "r");
121 str = f.readAsString();
122 f.close();
123
124 assert(str, content);
125
126 os.remove(fname);
127 }
128
129 function test_ext_json()
130 {
131 var expected, input, obj;
132 expected = '{"x":false,"y":true,"z2":null,"a":[1,8,160],"s":"str"}';
133 input = `{ "x":false, /*comments are allowed */
134 "y":true, // also a comment
135 z2:null, // unquoted property names
136 "a":[+1,0o10,0xa0,], // plus prefix, octal, hexadecimal
137 "s":"str",} // trailing comma in objects and arrays
138 `;
139 obj = std.parseExtJSON(input);
140 assert(JSON.stringify(obj), expected);
141 }
142
143 function test_os()
144 {
145 var fd, fpath, fname, fdir, buf, buf2, i, files, err, fdate, st, link_path;
146
147 assert(os.isatty(0));
148
149 fdir = "test_tmp_dir";
150 fname = "tmp_file.txt";
151 fpath = fdir + "/" + fname;
152 link_path = fdir + "/test_link";
153
154 os.remove(link_path);
155 os.remove(fpath);
156 os.remove(fdir);
157
158 err = os.mkdir(fdir, 0o755);
159 assert(err === 0);
160
161 fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC);
162 assert(fd >= 0);
163
164 buf = new Uint8Array(10);
165 for(i = 0; i < buf.length; i++)
166 buf[i] = i;
167 assert(os.write(fd, buf.buffer, 0, buf.length) === buf.length);
168
169 assert(os.seek(fd, 0, std.SEEK_SET) === 0);
170 buf2 = new Uint8Array(buf.length);
171 assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length);
172
173 for(i = 0; i < buf.length; i++)
174 assert(buf[i] == buf2[i]);
175
176 if (typeof BigInt !== "undefined") {
177 assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6));
178 assert(os.read(fd, buf2.buffer, 0, 1) === 1);
179 assert(buf[6] == buf2[0]);
180 }
181
182 assert(os.close(fd) === 0);
183
184 [files, err] = os.readdir(fdir);
185 assert(err, 0);
186 assert(files.indexOf(fname) >= 0);
187
188 fdate = 10000;
189
190 err = os.utimes(fpath, fdate, fdate);
191 assert(err, 0);
192
193 [st, err] = os.stat(fpath);
194 assert(err, 0);
195 assert(st.mode & os.S_IFMT, os.S_IFREG);
196 assert(st.mtime, fdate);
197
198 err = os.symlink(fname, link_path);
199 assert(err === 0);
200
201 [st, err] = os.lstat(link_path);
202 assert(err, 0);
203 assert(st.mode & os.S_IFMT, os.S_IFLNK);
204
205 [buf, err] = os.readlink(link_path);
206 assert(err, 0);
207 assert(buf, fname);
208
209 assert(os.remove(link_path) === 0);
210
211 [buf, err] = os.getcwd();
212 assert(err, 0);
213
214 [buf2, err] = os.realpath(".");
215 assert(err, 0);
216
217 assert(buf, buf2);
218
219 assert(os.remove(fpath) === 0);
220
221 fd = os.open(fpath, os.O_RDONLY);
222 assert(fd < 0);
223
224 assert(os.remove(fdir) === 0);
225 }
226
227 function test_os_exec()
228 {
229 var ret, fds, pid, f, status;
230
231 ret = os.exec(["true"]);
232 assert(ret, 0);
233
234 ret = os.exec(["/bin/sh", "-c", "exit 1"], { usePath: false });
235 assert(ret, 1);
236
237 fds = os.pipe();
238 pid = os.exec(["sh", "-c", "echo $FOO"], {
239 stdout: fds[1],
240 block: false,
241 env: { FOO: "hello" },
242 } );
243 assert(pid >= 0);
244 os.close(fds[1]); /* close the write end (as it is only in the child) */
245 f = std.fdopen(fds[0], "r");
246 assert(f.getline(), "hello");
247 assert(f.getline(), null);
248 f.close();
249 [ret, status] = os.waitpid(pid, 0);
250 assert(ret, pid);
251 assert(status & 0x7f, 0); /* exited */
252 assert(status >> 8, 0); /* exit code */
253
254 pid = os.exec(["cat"], { block: false } );
255 assert(pid >= 0);
256 os.kill(pid, os.SIGQUIT);
257 [ret, status] = os.waitpid(pid, 0);
258 assert(ret, pid);
259 assert(status & 0x7f, os.SIGQUIT);
260 }
261
262 function test_timer()
263 {
264 var th, i;
265
266 /* just test that a timer can be inserted and removed */
267 th = [];
268 for(i = 0; i < 3; i++)
269 th[i] = os.setTimeout(function () { }, 1000);
270 for(i = 0; i < 3; i++)
271 os.clearTimeout(th[i]);
272 }
273
274 /* test closure variable handling when freeing asynchronous
275 function */
276 function test_async_gc()
277 {
278 (async function run () {
279 let obj = {}
280
281 let done = () => {
282 obj
283 std.gc();
284 }
285
286 Promise.resolve().then(done)
287
288 const p = new Promise(() => {})
289
290 await p
291 })();
292 }
293
294 test_printf();
295 test_file1();
296 test_file2();
297 test_getline();
298 test_popen();
299 test_os();
300 test_os_exec();
301 test_timer();
302 test_ext_json();
303 test_async_gc();
304