DRY assertion functions in tests

This commit is contained in:
Saúl Ibarra Corretgé 2024-10-10 21:39:12 +02:00 committed by GitHub
parent e145244999
commit 364453b3d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 185 additions and 319 deletions

View file

@ -3965,8 +3965,6 @@ void js_std_add_helpers(JSContext *ctx, int argc, char **argv)
JS_SetPropertyStr(ctx, global_obj, "print", JS_SetPropertyStr(ctx, global_obj, "print",
JS_NewCFunction(ctx, js_print, "print", 1)); JS_NewCFunction(ctx, js_print, "print", 1));
JS_SetPropertyStr(ctx, global_obj, "__loadScript",
JS_NewCFunction(ctx, js_loadScript, "__loadScript", 1));
JS_FreeValue(ctx, global_obj); JS_FreeValue(ctx, global_obj);
} }

49
tests/assert.js Normal file
View file

@ -0,0 +1,49 @@
export function assert(actual, expected, message) {
if (arguments.length === 1)
expected = true;
if (typeof actual === typeof expected) {
if (actual === expected) {
if (actual !== 0 || (1 / actual) === (1 / expected))
return;
}
if (typeof actual === 'number') {
if (isNaN(actual) && isNaN(expected))
return;
}
if (typeof actual === 'object') {
if (actual !== null && expected !== null
&& actual.constructor === expected.constructor
&& actual.toString() === expected.toString())
return;
}
}
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
export function assertThrows(err, func)
{
var ex;
ex = false;
try {
func();
} catch(e) {
ex = true;
assert(e instanceof err);
}
assert(ex, true, "exception expected");
}
export function assertArrayEquals(a, b)
{
if (!Array.isArray(a) || !Array.isArray(b))
return assert(false);
assert(a.length, b.length);
a.forEach((value, idx) => {
assert(b[idx], value);
});
}

View file

@ -1,39 +1,5 @@
"use strict"; import { assert, assertThrows } from "./assert.js";
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
function assertThrows(err, func)
{
var ex;
ex = false;
try {
func();
} catch(e) {
ex = true;
assert(e instanceof err);
}
assert(ex, true, "exception expected");
}
// load more elaborate version of assert if available
try { __loadScript("test_assert.js"); } catch(e) {}
/*----------------*/
function bigint_pow(a, n) function bigint_pow(a, n)
{ {
@ -89,8 +55,8 @@ function test_bigint1()
a = bigint_pow(3n, 100n); a = bigint_pow(3n, 100n);
assert((a - 1n) != a); assert((a - 1n) != a);
assert(a == 515377520732011331036461129765621272702107522001n); assert(a, 515377520732011331036461129765621272702107522001n);
assert(a == 0x5a4653ca673768565b41f775d6947d55cf3813d1n); assert(a, 0x5a4653ca673768565b41f775d6947d55cf3813d1n);
r = 1n << 31n; r = 1n << 31n;
assert(r, 2147483648n, "1 << 31n === 2147483648n"); assert(r, 2147483648n, "1 << 31n === 2147483648n");

View file

@ -1,21 +1,5 @@
import * as bjson from "bjson"; import * as bjson from "bjson";
import { assert } from "./assert.js";
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
function toHex(a) function toHex(a)
{ {
@ -196,12 +180,12 @@ function bjson_test_symbol()
o = Symbol.for('foo'); o = Symbol.for('foo');
buf = bjson.write(o); buf = bjson.write(o);
r = bjson.read(buf, 0, buf.byteLength); r = bjson.read(buf, 0, buf.byteLength);
assert(o === r); assert(o, r);
o = Symbol.toStringTag; o = Symbol.toStringTag;
buf = bjson.write(o); buf = bjson.write(o);
r = bjson.read(buf, 0, buf.byteLength); r = bjson.read(buf, 0, buf.byteLength);
assert(o === r); assert(o, r);
} }
function bjson_test_all() function bjson_test_all()

View file

@ -1,4 +1,5 @@
import * as os from "os"; import * as os from "os";
import { assert, assertThrows } from "./assert.js";
// Keep this at the top; it tests source positions. // Keep this at the top; it tests source positions.
function test_exception_source_pos() function test_exception_source_pos()
@ -6,21 +7,21 @@ function test_exception_source_pos()
var e; var e;
try { try {
throw new Error(""); // line 9, column 19 throw new Error(""); // line 10, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
assert(e.stack.includes("test_builtin.js:9:19")); assert(e.stack.includes("test_builtin.js:10:19"));
} }
// Keep this at the top; it tests source positions. // Keep this at the top; it tests source positions.
function test_function_source_pos() // line 18, column 1 function test_function_source_pos() // line 19, column 1
{ {
function inner() {} // line 20, column 5 function inner() {} // line 21, column 5
var f = eval("function f() {} f"); var f = eval("function f() {} f");
assert(`${test_function_source_pos.lineNumber}:${test_function_source_pos.columnNumber}`, "18:1"); assert(`${test_function_source_pos.lineNumber}:${test_function_source_pos.columnNumber}`, "19:1");
assert(`${inner.lineNumber}:${inner.columnNumber}`, "20:5"); assert(`${inner.lineNumber}:${inner.columnNumber}`, "21:5");
assert(`${f.lineNumber}:${f.columnNumber}`, "1:1"); assert(`${f.lineNumber}:${f.columnNumber}`, "1:1");
} }
@ -35,19 +36,19 @@ function test_exception_prepare_stack()
}; };
try { try {
throw new Error(""); // line 38, column 19 throw new Error(""); // line 39, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
Error.prepareStackTrace = undefined; Error.prepareStackTrace = undefined;
assert(e.stack.length === 2); assert(e.stack.length, 2);
const f = e.stack[0]; const f = e.stack[0];
assert(f.getFunctionName() === 'test_exception_prepare_stack'); assert(f.getFunctionName(), 'test_exception_prepare_stack');
assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getFileName().endsWith('test_builtin.js'));
assert(f.getLineNumber() === 38); assert(f.getLineNumber(), 39);
assert(f.getColumnNumber() === 19); assert(f.getColumnNumber(), 19);
assert(!f.isNative()); assert(!f.isNative());
} }
@ -63,7 +64,7 @@ function test_exception_stack_size_limit()
}; };
try { try {
throw new Error(""); // line 66, column 19 throw new Error(""); // line 67, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
@ -71,61 +72,15 @@ function test_exception_stack_size_limit()
Error.stackTraceLimit = 10; Error.stackTraceLimit = 10;
Error.prepareStackTrace = undefined; Error.prepareStackTrace = undefined;
assert(e.stack.length === 1); assert(e.stack.length, 1);
const f = e.stack[0]; const f = e.stack[0];
assert(f.getFunctionName() === 'test_exception_stack_size_limit'); assert(f.getFunctionName(), 'test_exception_stack_size_limit');
assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getFileName().endsWith('test_builtin.js'));
assert(f.getLineNumber() === 66); assert(f.getLineNumber(), 67);
assert(f.getColumnNumber() === 19); assert(f.getColumnNumber(), 19);
assert(!f.isNative()); assert(!f.isNative());
} }
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (typeof actual === typeof expected) {
if (actual === expected) {
if (actual !== 0 || (1 / actual) === (1 / expected))
return;
}
if (typeof actual === 'number') {
if (isNaN(actual) && isNaN(expected))
return true;
}
if (typeof actual === 'object') {
if (actual !== null && expected !== null
&& actual.constructor === expected.constructor
&& actual.toString() === expected.toString())
return;
}
}
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
function assert_throws(expected_error, func)
{
var err = false;
try {
func();
} catch(e) {
err = true;
if (!(e instanceof expected_error)) {
throw Error("unexpected exception type");
}
}
if (!err) {
throw Error("expected exception");
}
}
// load more elaborate version of assert if available
try { __loadScript("test_assert.js"); } catch(e) {}
/*----------------*/
function my_func(a, b) function my_func(a, b)
{ {
return a + b; return a + b;
@ -155,7 +110,7 @@ function test_function()
r = (function () { return 1; }).apply(null, undefined); r = (function () { return 1; }).apply(null, undefined);
assert(r, 1); assert(r, 1);
assert_throws(TypeError, (function() { assertThrows(TypeError, (function() {
Reflect.apply((function () { return 1; }), null, undefined); Reflect.apply((function () { return 1; }), null, undefined);
})); }));
@ -221,7 +176,7 @@ function test()
assert(typeof a.y, "undefined", "extensible"); assert(typeof a.y, "undefined", "extensible");
assert(err, true, "extensible"); assert(err, true, "extensible");
assert_throws(TypeError, () => Object.setPrototypeOf(Object.prototype, {})); assertThrows(TypeError, () => Object.setPrototypeOf(Object.prototype, {}));
} }
function test_enum() function test_enum()
@ -1031,10 +986,10 @@ function test_cur_pc()
get: function() { throw Error("a[1]_get"); }, get: function() { throw Error("a[1]_get"); },
set: function(x) { throw Error("a[1]_set"); } set: function(x) { throw Error("a[1]_set"); }
}); });
assert_throws(Error, function() { return a[1]; }); assertThrows(Error, function() { return a[1]; });
assert_throws(Error, function() { a[1] = 1; }); assertThrows(Error, function() { a[1] = 1; });
assert_throws(Error, function() { return [...a]; }); assertThrows(Error, function() { return [...a]; });
assert_throws(Error, function() { return ({...b} = a); }); assertThrows(Error, function() { return ({...b} = a); });
var o = {}; var o = {};
Object.defineProperty(o, 'x', { Object.defineProperty(o, 'x', {
@ -1042,34 +997,34 @@ function test_cur_pc()
set: function(x) { throw Error("o.x_set"); } set: function(x) { throw Error("o.x_set"); }
}); });
o.valueOf = function() { throw Error("o.valueOf"); }; o.valueOf = function() { throw Error("o.valueOf"); };
assert_throws(Error, function() { return +o; }); assertThrows(Error, function() { return +o; });
assert_throws(Error, function() { return -o; }); assertThrows(Error, function() { return -o; });
assert_throws(Error, function() { return o+1; }); assertThrows(Error, function() { return o+1; });
assert_throws(Error, function() { return o-1; }); assertThrows(Error, function() { return o-1; });
assert_throws(Error, function() { return o*1; }); assertThrows(Error, function() { return o*1; });
assert_throws(Error, function() { return o/1; }); assertThrows(Error, function() { return o/1; });
assert_throws(Error, function() { return o%1; }); assertThrows(Error, function() { return o%1; });
assert_throws(Error, function() { return o**1; }); assertThrows(Error, function() { return o**1; });
assert_throws(Error, function() { return o<<1; }); assertThrows(Error, function() { return o<<1; });
assert_throws(Error, function() { return o>>1; }); assertThrows(Error, function() { return o>>1; });
assert_throws(Error, function() { return o>>>1; }); assertThrows(Error, function() { return o>>>1; });
assert_throws(Error, function() { return o&1; }); assertThrows(Error, function() { return o&1; });
assert_throws(Error, function() { return o|1; }); assertThrows(Error, function() { return o|1; });
assert_throws(Error, function() { return o^1; }); assertThrows(Error, function() { return o^1; });
assert_throws(Error, function() { return o<1; }); assertThrows(Error, function() { return o<1; });
assert_throws(Error, function() { return o==1; }); assertThrows(Error, function() { return o==1; });
assert_throws(Error, function() { return o++; }); assertThrows(Error, function() { return o++; });
assert_throws(Error, function() { return o--; }); assertThrows(Error, function() { return o--; });
assert_throws(Error, function() { return ++o; }); assertThrows(Error, function() { return ++o; });
assert_throws(Error, function() { return --o; }); assertThrows(Error, function() { return --o; });
assert_throws(Error, function() { return ~o; }); assertThrows(Error, function() { return ~o; });
Object.defineProperty(globalThis, 'xxx', { Object.defineProperty(globalThis, 'xxx', {
get: function() { throw Error("xxx_get"); }, get: function() { throw Error("xxx_get"); },
set: function(x) { throw Error("xxx_set"); } set: function(x) { throw Error("xxx_set"); }
}); });
assert_throws(Error, function() { return xxx; }); assertThrows(Error, function() { return xxx; });
assert_throws(Error, function() { xxx = 1; }); assertThrows(Error, function() { xxx = 1; });
} }
test(); test();

View file

@ -1,3 +1,5 @@
// This test cannot use imports because it needs to run in non-strict mode.
function assert(actual, expected, message) { function assert(actual, expected, message) {
if (arguments.length == 1) if (arguments.length == 1)
expected = true; expected = true;
@ -15,9 +17,6 @@ function assert(actual, expected, message) {
(message ? " (" + message + ")" : "")); (message ? " (" + message + ")" : ""));
} }
// load more elaborate version of assert if available
try { __loadScript("test_assert.js"); } catch(e) {}
/*----------------*/ /*----------------*/
var log_str = ""; var log_str = "";
@ -135,9 +134,9 @@ function test_arrow_function()
assert(a.length, 2); assert(a.length, 2);
assert(a[0] === 1 && a[1] === 2); assert(a[0] === 1 && a[1] === 2);
assert(f2.call("this_val") === "this_val"); assert(f2.call("this_val"), "this_val");
assert(f3.call("this_val") === "this_val"); assert(f3.call("this_val"), "this_val");
assert(new f4() === f4); assert(new f4(), f4);
var o1 = { f() { return this; } }; var o1 = { f() { return this; } };
var o2 = { f() { var o2 = { f() {
@ -145,7 +144,7 @@ function test_arrow_function()
} }; } };
o2.__proto__ = o1; o2.__proto__ = o1;
assert(o2.f() === o2); assert(o2.f(), o2);
} }
function test_with() function test_with()
@ -153,20 +152,20 @@ function test_with()
var o1 = { x: "o1", y: "o1" }; var o1 = { x: "o1", y: "o1" };
var x = "local"; var x = "local";
eval('var z="var_obj";'); eval('var z="var_obj";');
assert(z === "var_obj"); assert(z, "var_obj");
with (o1) { with (o1) {
assert(x === "o1"); assert(x, "o1");
assert(eval("x") === "o1"); assert(eval("x"), "o1");
var f = function () { var f = function () {
o2 = { x: "o2" }; o2 = { x: "o2" };
with (o2) { with (o2) {
assert(x === "o2"); assert(x, "o2");
assert(y === "o1"); assert(y, "o1");
assert(z === "var_obj"); assert(z, "var_obj");
assert(eval("x") === "o2"); assert(eval("x"), "o2");
assert(eval("y") === "o1"); assert(eval("y"), "o1");
assert(eval("z") === "var_obj"); assert(eval("z"), "var_obj");
assert(eval('eval("x")') === "o2"); assert(eval('eval("x")'), "o2");
} }
}; };
f(); f();
@ -182,7 +181,7 @@ function test_eval_closure()
eval("tab.push(function g1() { return i; })"); eval("tab.push(function g1() { return i; })");
} }
for(let i = 0; i < 3; i++) { for(let i = 0; i < 3; i++) {
assert(tab[i]() === i); assert(tab[i](), i);
} }
tab = []; tab = [];
@ -193,7 +192,7 @@ function test_eval_closure()
f(); f();
} }
for(let i = 0; i < 3; i++) { for(let i = 0; i < 3; i++) {
assert(tab[i]() === i); assert(tab[i](), i);
} }
} }

View file

@ -1,3 +1,5 @@
// This test cannot use imports because it needs to run in non-strict mode.
function assert(actual, expected, message) { function assert(actual, expected, message) {
if (arguments.length == 1) if (arguments.length == 1)
expected = true; expected = true;
@ -43,9 +45,6 @@ function assert_throws(expected_error, func, message)
} }
} }
// load more elaborate version of assert if available
try { __loadScript("test_assert.js"); } catch(e) {}
/*----------------*/ /*----------------*/
function test_op1() function test_op1()
@ -119,20 +118,20 @@ function test_op1()
function test_cvt() function test_cvt()
{ {
assert((NaN | 0) === 0); assert((NaN | 0), 0);
assert((Infinity | 0) === 0); assert((Infinity | 0), 0);
assert(((-Infinity) | 0) === 0); assert(((-Infinity) | 0), 0);
assert(("12345" | 0) === 12345); assert(("12345" | 0), 12345);
assert(("0x12345" | 0) === 0x12345); assert(("0x12345" | 0), 0x12345);
assert(((4294967296 * 3 - 4) | 0) === -4); assert(((4294967296 * 3 - 4) | 0), -4);
assert(("12345" >>> 0) === 12345); assert(("12345" >>> 0), 12345);
assert(("0x12345" >>> 0) === 0x12345); assert(("0x12345" >>> 0), 0x12345);
assert((NaN >>> 0) === 0); assert((NaN >>> 0), 0);
assert((Infinity >>> 0) === 0); assert((Infinity >>> 0), 0);
assert(((-Infinity) >>> 0) === 0); assert(((-Infinity) >>> 0), 0);
assert(((4294967296 * 3 - 4) >>> 0) === (4294967296 - 4)); assert(((4294967296 * 3 - 4) >>> 0), (4294967296 - 4));
assert((19686109595169230000).toString() === "19686109595169230000"); assert((19686109595169230000).toString(), "19686109595169230000");
} }
function test_eq() function test_eq()
@ -317,36 +316,36 @@ function test_class()
} }
} }
assert(C.F() === -1); assert(C.F(), -1);
assert(Object.getOwnPropertyDescriptor(C.prototype, "y").get.name === "get y"); assert(Object.getOwnPropertyDescriptor(C.prototype, "y").get.name === "get y");
o = new C(); o = new C();
assert(o.f() === 1); assert(o.f(), 1);
assert(o.x === 10); assert(o.x, 10);
assert(D.F() === -1); assert(D.F(), -1);
assert(D.G() === -2); assert(D.G(), -2);
assert(D.H() === -1); assert(D.H(), -1);
o = new D(); o = new D();
assert(o.f() === 1); assert(o.f(), 1);
assert(o.g() === 2); assert(o.g(), 2);
assert(o.x === 10); assert(o.x, 10);
assert(o.z === 20); assert(o.z, 20);
assert(o.h() === 1); assert(o.h(), 1);
/* test class name scope */ /* test class name scope */
var E1 = class E { static F() { return E; } }; var E1 = class E { static F() { return E; } };
assert(E1 === E1.F()); assert(E1, E1.F());
class S { class S {
static x = 42; static x = 42;
static y = S.x; static y = S.x;
static z = this.x; static z = this.x;
} }
assert(S.x === 42); assert(S.x, 42);
assert(S.y === 42); assert(S.y, 42);
assert(S.z === 42); assert(S.z, 42);
class P { class P {
get; get;
@ -357,10 +356,10 @@ function test_class()
async = () => "789"; async = () => "789";
static() { return 42; } static() { return 42; }
} }
assert(new P().get() === "123"); assert(new P().get(), "123");
assert(new P().set() === "456"); assert(new P().set(), "456");
assert(new P().async() === "789"); assert(new P().async(), "789");
assert(new P().static() === 42); assert(new P().static(), 42);
}; };
function test_template() function test_template()
@ -390,7 +389,7 @@ function test_object_literal()
var x = 0, get = 1, set = 2; async = 3; var x = 0, get = 1, set = 2; async = 3;
a = { get: 2, set: 3, async: 4, get a(){ return this.get} }; a = { get: 2, set: 3, async: 4, get a(){ return this.get} };
assert(JSON.stringify(a), '{"get":2,"set":3,"async":4,"a":2}'); assert(JSON.stringify(a), '{"get":2,"set":3,"async":4,"a":2}');
assert(a.a === 2); assert(a.a, 2);
a = { x, get, set, async }; a = { x, get, set, async };
assert(JSON.stringify(a), '{"x":0,"get":1,"set":2,"async":3}'); assert(JSON.stringify(a), '{"x":0,"get":1,"set":2,"async":3}');
@ -400,10 +399,10 @@ function test_regexp_skip()
{ {
var a, b; var a, b;
[a, b = /abc\(/] = [1]; [a, b = /abc\(/] = [1];
assert(a === 1); assert(a, 1);
[a, b =/abc\(/] = [2]; [a, b =/abc\(/] = [2];
assert(a === 2); assert(a, 2);
} }
function test_labels() function test_labels()

View file

@ -1,3 +1,5 @@
// This test cannot use imports because it needs to run in non-strict mode.
function assert(actual, expected, message) { function assert(actual, expected, message) {
if (arguments.length == 1) if (arguments.length == 1)
expected = true; expected = true;
@ -15,9 +17,6 @@ function assert(actual, expected, message) {
(message ? " (" + message + ")" : "")); (message ? " (" + message + ")" : ""));
} }
// load more elaborate version of assert if available
try { __loadScript("test_assert.js"); } catch(e) {}
/*----------------*/ /*----------------*/
function test_while() function test_while()
@ -29,7 +28,7 @@ function test_while()
c++; c++;
i++; i++;
} }
assert(c === 3); assert(c, 3);
} }
function test_while_break() function test_while_break()

View file

@ -1,57 +1,12 @@
function assert(actual, expected, message) { import { assert, assertArrayEquals, assertThrows } from "./assert.js";
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
function assert_throws(expected_error, func)
{
var err = false;
try {
func();
} catch(e) {
err = true;
if (!(e instanceof expected_error)) {
throw Error("unexpected exception type");
}
}
if (!err) {
throw Error("expected exception");
}
}
function assert_array_equals(a, b) {
if (!Array.isArray(a) || !Array.isArray(b))
return assert(false);
assert(a.length === b.length);
a.forEach((value, idx) => {
assert(b[idx] === value);
});
}
// load more elaborate version of assert if available
try { std.loadScript("test_assert.js"); } catch(e) {}
function test_types() { function test_types() {
assert_throws(TypeError, () => queueMicrotask(), "no argument"); assertThrows(TypeError, () => queueMicrotask(), "no argument");
assert_throws(TypeError, () => queueMicrotask(undefined), "undefined"); assertThrows(TypeError, () => queueMicrotask(undefined), "undefined");
assert_throws(TypeError, () => queueMicrotask(null), "null"); assertThrows(TypeError, () => queueMicrotask(null), "null");
assert_throws(TypeError, () => queueMicrotask(0), "0"); assertThrows(TypeError, () => queueMicrotask(0), "0");
assert_throws(TypeError, () => queueMicrotask({ handleEvent() { } }), "an event handler object"); assertThrows(TypeError, () => queueMicrotask({ handleEvent() { } }), "an event handler object");
assert_throws(TypeError, () => queueMicrotask("window.x = 5;"), "a string"); assertThrows(TypeError, () => queueMicrotask("window.x = 5;"), "a string");
} }
function test_async() { function test_async() {
@ -74,7 +29,7 @@ function test_async_order() {
queueMicrotask(() => happenings.push("b")); queueMicrotask(() => happenings.push("b"));
Promise.reject().catch(() => happenings.push("c")); Promise.reject().catch(() => happenings.push("c"));
queueMicrotask(() => { queueMicrotask(() => {
assert_array_equals(happenings, ["a", "b", "c"]); assertArrayEquals(happenings, ["a", "b", "c"]);
}); });
} }

View file

@ -1,30 +1,10 @@
import * as std from "std"; import * as std from "std";
import * as os from "os"; import * as os from "os";
import { assert } from "./assert.js";
const isWin = os.platform === 'win32'; const isWin = os.platform === 'win32';
const isCygwin = os.platform === 'cygwin'; const isCygwin = os.platform === 'cygwin';
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
// load more elaborate version of assert if available
try { std.loadScript("test_assert.js"); } catch(e) {}
/*----------------*/
function test_printf() function test_printf()
{ {
@ -47,19 +27,19 @@ function test_file1()
f.seek(0, std.SEEK_SET); f.seek(0, std.SEEK_SET);
str1 = f.readAsString(); str1 = f.readAsString();
assert(str1 === str); assert(str1, str);
f.seek(0, std.SEEK_END); f.seek(0, std.SEEK_END);
size = f.tell(); size = f.tell();
assert(size === str.length); assert(size, str.length);
f.seek(0, std.SEEK_SET); f.seek(0, std.SEEK_SET);
buf = new Uint8Array(size); buf = new Uint8Array(size);
ret = f.read(buf.buffer, 0, size); ret = f.read(buf.buffer, 0, size);
assert(ret === size); assert(ret, size);
for(i = 0; i < size; i++) for(i = 0; i < size; i++)
assert(buf[i] === str.charCodeAt(i)); assert(buf[i], str.charCodeAt(i));
f.close(); f.close();
} }
@ -74,9 +54,9 @@ function test_file2()
f.putByte(str.charCodeAt(i)); f.putByte(str.charCodeAt(i));
f.seek(0, std.SEEK_SET); f.seek(0, std.SEEK_SET);
for(i = 0; i < size; i++) { for(i = 0; i < size; i++) {
assert(str.charCodeAt(i) === f.getByte()); assert(str.charCodeAt(i), f.getByte());
} }
assert(f.getByte() === -1); assert(f.getByte(), -1);
f.close(); f.close();
} }
@ -97,11 +77,11 @@ function test_getline()
line = f.getline(); line = f.getline();
if (line === null) if (line === null)
break; break;
assert(line == lines[line_count]); assert(line, lines[line_count]);
line_count++; line_count++;
} }
assert(f.eof()); assert(f.eof());
assert(line_count === lines.length); assert(line_count, lines.length);
f.close(); f.close();
} }
@ -145,7 +125,7 @@ function test_os()
os.remove(fdir); os.remove(fdir);
err = os.mkdir(fdir, 0o755); err = os.mkdir(fdir, 0o755);
assert(err === 0); assert(err, 0);
fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC); fd = os.open(fpath, os.O_RDWR | os.O_CREAT | os.O_TRUNC);
assert(fd >= 0); assert(fd >= 0);
@ -153,22 +133,22 @@ function test_os()
buf = new Uint8Array(10); buf = new Uint8Array(10);
for(i = 0; i < buf.length; i++) for(i = 0; i < buf.length; i++)
buf[i] = i; buf[i] = i;
assert(os.write(fd, buf.buffer, 0, buf.length) === buf.length); assert(os.write(fd, buf.buffer, 0, buf.length), buf.length);
assert(os.seek(fd, 0, std.SEEK_SET) === 0); assert(os.seek(fd, 0, std.SEEK_SET), 0);
buf2 = new Uint8Array(buf.length); buf2 = new Uint8Array(buf.length);
assert(os.read(fd, buf2.buffer, 0, buf2.length) === buf2.length); assert(os.read(fd, buf2.buffer, 0, buf2.length), buf2.length);
for(i = 0; i < buf.length; i++) for(i = 0; i < buf.length; i++)
assert(buf[i] == buf2[i]); assert(buf[i] == buf2[i]);
if (typeof BigInt !== "undefined") { if (typeof BigInt !== "undefined") {
assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6)); assert(os.seek(fd, BigInt(6), std.SEEK_SET), BigInt(6));
assert(os.read(fd, buf2.buffer, 0, 1) === 1); assert(os.read(fd, buf2.buffer, 0, 1), 1);
assert(buf[6] == buf2[0]); assert(buf[6] == buf2[0]);
} }
assert(os.close(fd) === 0); assert(os.close(fd), 0);
[files, err] = os.readdir(fdir); [files, err] = os.readdir(fdir);
assert(err, 0); assert(err, 0);
@ -186,7 +166,7 @@ function test_os()
if (!isWin) { if (!isWin) {
err = os.symlink(fname, link_path); err = os.symlink(fname, link_path);
assert(err === 0); assert(err, 0);
[st, err] = os.lstat(link_path); [st, err] = os.lstat(link_path);
assert(err, 0); assert(err, 0);
@ -286,7 +266,7 @@ function test_timeout_order()
function a() { s += "a"; os.setTimeout(c, 300); } function a() { s += "a"; os.setTimeout(c, 300); }
function b() { s += "b"; } function b() { s += "b"; }
function c() { s += "c"; } function c() { s += "c"; }
function d() { assert(s === "abc"); } // not "acb" function d() { assert(s, "abc"); } // not "acb"
} }
function test_stdio_close() function test_stdio_close()

View file

@ -1,23 +1,5 @@
/* os.Worker API test */
import * as std from "std";
import * as os from "os"; import * as os from "os";
import { assert } from "./assert.js";
function assert(actual, expected, message) {
if (arguments.length == 1)
expected = true;
if (actual === expected)
return;
if (actual !== null && expected !== null
&& typeof actual == 'object' && typeof expected == 'object'
&& actual.toString() === expected.toString())
return;
throw Error("assertion failed: got |" + actual + "|" +
", expected |" + expected + "|" +
(message ? " (" + message + ")" : ""));
}
var worker; var worker;