mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:37:45 +00:00
LibJS: Reorganize tests into subfolders
This commit is contained in:
parent
21064a1883
commit
4c48c9d69d
213 changed files with 10 additions and 2 deletions
28
Libraries/LibJS/Tests/builtins/Array/Array.isArray.js
Normal file
28
Libraries/LibJS/Tests/builtins/Array/Array.isArray.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.isArray.length === 1);
|
||||
|
||||
assert(Array.isArray() === false);
|
||||
assert(Array.isArray("1") === false);
|
||||
assert(Array.isArray("foo") === false);
|
||||
assert(Array.isArray(1) === false);
|
||||
assert(Array.isArray(1, 2, 3) === false);
|
||||
assert(Array.isArray(undefined) === false);
|
||||
assert(Array.isArray(null) === false);
|
||||
assert(Array.isArray(Infinity) === false);
|
||||
assert(Array.isArray({}) === false);
|
||||
|
||||
assert(Array.isArray([]) === true);
|
||||
assert(Array.isArray([1]) === true);
|
||||
assert(Array.isArray([1, 2, 3]) === true);
|
||||
assert(Array.isArray(new Array()) === true);
|
||||
assert(Array.isArray(new Array(10)) === true);
|
||||
assert(Array.isArray(new Array("a", "b", "c")) === true);
|
||||
// FIXME: Array.prototype is supposed to be an array!
|
||||
// assert(Array.isArray(Array.prototype) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
48
Libraries/LibJS/Tests/builtins/Array/Array.js
Normal file
48
Libraries/LibJS/Tests/builtins/Array/Array.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.length === 1);
|
||||
assert(Array.name === "Array");
|
||||
assert(Array.prototype.length === 0);
|
||||
|
||||
assert(typeof Array() === "object");
|
||||
assert(typeof new Array() === "object");
|
||||
|
||||
var a;
|
||||
|
||||
a = new Array(5);
|
||||
assert(a.length === 5);
|
||||
|
||||
a = new Array("5");
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "5");
|
||||
|
||||
a = new Array(1, 2, 3);
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a = new Array([1, 2, 3]);
|
||||
assert(a.length === 1);
|
||||
assert(a[0][0] === 1);
|
||||
assert(a[0][1] === 2);
|
||||
assert(a[0][2] === 3);
|
||||
|
||||
a = new Array(1, 2, 3);
|
||||
Object.defineProperty(a, 3, { get() { return 10; } });
|
||||
assert(a.toString() === "1,2,3,10");
|
||||
|
||||
[-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
new Array(value);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Invalid array length"
|
||||
});
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
46
Libraries/LibJS/Tests/builtins/Array/Array.of.js
Normal file
46
Libraries/LibJS/Tests/builtins/Array/Array.of.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.of.length === 0);
|
||||
|
||||
assert(typeof Array.of() === "object");
|
||||
|
||||
var a;
|
||||
|
||||
a = Array.of(5);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === 5);
|
||||
|
||||
a = Array.of("5");
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "5");
|
||||
|
||||
a = Array.of(Infinity);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === Infinity);
|
||||
|
||||
a = Array.of(1, 2, 3);
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a = Array.of([1, 2, 3]);
|
||||
assert(a.length === 1);
|
||||
assert(a[0][0] === 1);
|
||||
assert(a[0][1] === 2);
|
||||
assert(a[0][2] === 3);
|
||||
|
||||
let t = [1, 2, 3];
|
||||
Object.defineProperty(t, 3, { get() { return 4; } });
|
||||
a = Array.of(...t);
|
||||
assert(a.length === 4);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === 4);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
[undefined, "foo", -42, 0].forEach(length => {
|
||||
const o = { length };
|
||||
|
||||
assert(Array.prototype.push.call(o, "foo") === 1);
|
||||
assert(o.length === 1);
|
||||
assert(o[0] === "foo");
|
||||
assert(Array.prototype.push.call(o, "bar", "baz") === 3);
|
||||
assert(o.length === 3);
|
||||
assert(o[0] === "foo");
|
||||
assert(o[1] === "bar");
|
||||
assert(o[2] === "baz");
|
||||
|
||||
assert(Array.prototype.pop.call(o) === "baz");
|
||||
assert(o.length === 2);
|
||||
assert(Array.prototype.pop.call(o) === "bar");
|
||||
assert(o.length === 1);
|
||||
assert(Array.prototype.pop.call(o) === "foo");
|
||||
assert(o.length === 0);
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
|
||||
o.length = length;
|
||||
assert(Array.prototype.pop.call(o) === undefined);
|
||||
assert(o.length === 0);
|
||||
});
|
||||
|
||||
{
|
||||
const o = { length: 3, 0: "hello", 2: "serenity" };
|
||||
const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends");
|
||||
assert(o.length === 3);
|
||||
assert(o[0] === "hello");
|
||||
assert(o[1] === "friends");
|
||||
assert(o[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === "hello");
|
||||
assert(removed[1] === undefined);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.join.call({}) === "");
|
||||
assert(Array.prototype.join.call({ length: "foo" }) === "");
|
||||
assert(Array.prototype.join.call({ length: 3 }) === ",,");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar");
|
||||
assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~");
|
||||
assert(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === "foo~bar~baz");
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.toString.call({}) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]");
|
||||
assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo");
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.indexOf.call({}) === -1);
|
||||
assert(Array.prototype.indexOf.call({ 0: undefined }) === -1);
|
||||
assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0);
|
||||
assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
|
||||
assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
|
||||
assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.lastIndexOf.call({}) === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4);
|
||||
assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2);
|
||||
}
|
||||
|
||||
{
|
||||
assert(Array.prototype.includes.call({}) === false);
|
||||
assert(Array.prototype.includes.call({ 0: undefined }) === false);
|
||||
assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true);
|
||||
assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false);
|
||||
assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true);
|
||||
}
|
||||
|
||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
||||
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.every.call(o, function (value) {
|
||||
visit(value);
|
||||
return true;
|
||||
});
|
||||
}, ["foo", "bar", "baz"]);
|
||||
}
|
||||
|
||||
["find", "findIndex"].forEach(name => {
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype[name].call(o, function (value) {
|
||||
visit(value);
|
||||
return false;
|
||||
});
|
||||
}, ["foo", "bar", undefined, "baz", undefined]);
|
||||
});
|
||||
|
||||
["filter", "forEach", "map", "some"].forEach(name => {
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype[name].call(o, function (value) {
|
||||
visit(value);
|
||||
return false;
|
||||
});
|
||||
}, ["foo", "bar", "baz"]);
|
||||
});
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.reduce.call(o, function (_, value) {
|
||||
visit(value);
|
||||
return false;
|
||||
}, "initial");
|
||||
}, ["foo", "bar", "baz"]);
|
||||
}
|
||||
|
||||
{
|
||||
assertVisitsAll(visit => {
|
||||
Array.prototype.reduceRight.call(o, function (_, value) {
|
||||
visit(value);
|
||||
return false;
|
||||
}, "initial");
|
||||
}, ["baz", "bar", "foo"]);
|
||||
}
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.concat.length === 1);
|
||||
|
||||
var array = ["hello", "friends"];
|
||||
|
||||
var array_concat = array.concat();
|
||||
assert(array_concat.length === array.length);
|
||||
|
||||
array_concat = array.concat(1)
|
||||
assert(array_concat.length === 3);
|
||||
assert(array_concat[2] === 1);
|
||||
|
||||
array_concat = array.concat([1, 2, 3])
|
||||
assert(array_concat.length === 5);
|
||||
assert(array_concat[2] === 1);
|
||||
assert(array_concat[3] === 2);
|
||||
assert(array_concat[4] === 3);
|
||||
|
||||
array_concat = array.concat(false, "serenity");
|
||||
assert(array_concat.length === 4);
|
||||
assert(array_concat[2] === false);
|
||||
assert(array_concat[3] === "serenity");
|
||||
|
||||
array_concat = array.concat({ name: "libjs" }, [1, [2, 3]]);
|
||||
assert(array_concat.length === 5);
|
||||
assert(array_concat[2].name === "libjs");
|
||||
assert(array_concat[3] === 1);
|
||||
assert(array_concat[4][0] === 2);
|
||||
assert(array_concat[4][1] === 3);
|
||||
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.every.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].every(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var arrayOne = ["serenity", { test: "serenity"} ];
|
||||
var arrayTwo = [true, false, 1, 2, 3, "3"];
|
||||
|
||||
assert(arrayOne.every(value => value === "hello") === false);
|
||||
assert(arrayOne.every(value => value === "serenity") === false);
|
||||
assert(arrayOne.every((value, index, arr) => index < 2) === true);
|
||||
assert(arrayOne.every(value => typeof(value) === "string") === false);
|
||||
assert(arrayOne.every(value => arrayOne.pop()) === true);
|
||||
|
||||
assert(arrayTwo.every((value, index, arr) => index > 0) === false);
|
||||
assert(arrayTwo.every((value, index, arr) => index >= 0) === true);
|
||||
assert(arrayTwo.every(value => typeof(value) !== "string") === false);
|
||||
assert(arrayTwo.every(value => typeof(value) === "number") === false);
|
||||
assert(arrayTwo.every(value => value > 0) === false);
|
||||
assert(arrayTwo.every(value => value >= 0 && value < 4) === true);
|
||||
assert(arrayTwo.every(value => arrayTwo.pop()) === true);
|
||||
|
||||
assert(["", "hello", "friends", "serenity"].every(value => value.length >= 0) === true);
|
||||
assert([].every(value => value === 1) === true);
|
||||
|
||||
arrayTwo = [true, false, 1, 2, 3, "3"];
|
||||
|
||||
// Every only goes up to the original length.
|
||||
assert(arrayTwo.every((value, index, arr) => {
|
||||
arr.push("serenity");
|
||||
return value < 4;
|
||||
}) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
24
Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js
Normal file
24
Libraries/LibJS/Tests/builtins/Array/Array.prototype.fill.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.fill.length === 1);
|
||||
|
||||
var array = [1, 2, 3, 4];
|
||||
assertArrayEquals(array.fill(0, 2, 4), [1, 2, 0, 0]);
|
||||
assertArrayEquals(array.fill(5, 1), [1, 5, 5, 5]);
|
||||
assertArrayEquals(array.fill(6), [6, 6, 6, 6]);
|
||||
|
||||
assertArrayEquals([1, 2, 3].fill(4), [4, 4, 4]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 1), [1, 4, 4]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 1, 2), [1, 4, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 3, 3), [1, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, -3, -2), [4, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, NaN, NaN), [1, 2, 3]);
|
||||
assertArrayEquals([1, 2, 3].fill(4, 3, 5), [1, 2, 3]);
|
||||
assertArrayEquals(Array(3).fill(4), [4, 4, 4]);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.filter.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].filter();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.filter() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].filter(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].filter(callback).length === 0);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1, 2, 3].filter(callback).length === 0);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
var evenNumbers = [0, 1, 2, 3, 4, 5, 6, 7].filter(x => x % 2 === 0);
|
||||
assert(evenNumbers.length === 4);
|
||||
assert(evenNumbers[0] === 0);
|
||||
assert(evenNumbers[1] === 2);
|
||||
assert(evenNumbers[2] === 4);
|
||||
assert(evenNumbers[3] === 6);
|
||||
|
||||
var fruits = ["Apple", "Banana", "Blueberry", "Grape", "Mango", "Orange", "Peach", "Pineapple", "Raspberry", "Watermelon"];
|
||||
const filterItems = (arr, query) => {
|
||||
return arr.filter(el => el.toLowerCase().indexOf(query.toLowerCase()) !== -1)
|
||||
};
|
||||
|
||||
var results;
|
||||
|
||||
results = filterItems(fruits, "Berry");
|
||||
assert(results.length === 2);
|
||||
assert(results[0] === "Blueberry");
|
||||
assert(results[1] === "Raspberry");
|
||||
|
||||
results = filterItems(fruits, "P");
|
||||
assert(results.length === 5);
|
||||
assert(results[0] === "Apple");
|
||||
assert(results[1] === "Grape");
|
||||
assert(results[2] === "Peach");
|
||||
assert(results[3] === "Pineapple");
|
||||
assert(results[4] === "Raspberry");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
49
Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js
Normal file
49
Libraries/LibJS/Tests/builtins/Array/Array.prototype.find.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.find.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].find(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.find(value => value === "hello") === "hello");
|
||||
assert(array.find((value, index, arr) => index === 1) === "friends");
|
||||
assert(array.find(value => value == "1") === 1);
|
||||
assert(array.find(value => value === 1) === 1);
|
||||
assert(array.find(value => typeof(value) !== "string") === 1);
|
||||
assert(array.find(value => typeof(value) === "boolean") === false);
|
||||
assert(array.find(value => value > 1) === 2);
|
||||
assert(array.find(value => value > 1 && value < 3) === 2);
|
||||
assert(array.find(value => value > 100) === undefined);
|
||||
assert([].find(value => value === 1) === undefined);
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
[].find(callback)
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
[1, 2, 3].find(callback);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].find(callback);
|
||||
assert(callbackCalled === 8);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].find(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
});
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.findIndex.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].findIndex(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false];
|
||||
|
||||
assert(array.findIndex(value => value === "hello") === 0);
|
||||
assert(array.findIndex((value, index, arr) => index === 1) === 1);
|
||||
assert(array.findIndex(value => value == "1") === 2);
|
||||
assert(array.findIndex(value => value === 1) === 2);
|
||||
assert(array.findIndex(value => typeof(value) !== "string") === 2);
|
||||
assert(array.findIndex(value => typeof(value) === "boolean") === 4);
|
||||
assert(array.findIndex(value => value > 1) === 3);
|
||||
assert(array.findIndex(value => value > 1 && value < 3) === 3);
|
||||
assert(array.findIndex(value => value > 100) === -1);
|
||||
assert([].findIndex(value => value === 1) === -1);
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
[].findIndex(callback)
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
[1, 2, 3].findIndex(callback);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].findIndex(callback);
|
||||
assert(callbackCalled === 8);
|
||||
|
||||
callbackCalled = 0;
|
||||
[1, , , "foo", , undefined, , ,].findIndex(value => {
|
||||
callbackCalled++;
|
||||
return value === undefined;
|
||||
});
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.forEach.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].forEach();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.forEach() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].forEach(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var a = [1, 2, 3];
|
||||
var o = {};
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].forEach(callback) === undefined);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert(a.forEach(callback) === undefined);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(value, index) {
|
||||
assert(value === a[index]);
|
||||
assert(index === a[index] - 1);
|
||||
});
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(_, _, array) {
|
||||
callbackCalled++;
|
||||
assert(a.length === array.length);
|
||||
a.push("test");
|
||||
});
|
||||
assert(callbackCalled === 3);
|
||||
assert(a.length === 6);
|
||||
|
||||
callbackCalled = 0;
|
||||
a.forEach(function(value, index) {
|
||||
callbackCalled++;
|
||||
this[index] = value;
|
||||
}, o);
|
||||
assert(callbackCalled === 6);
|
||||
assert(o[0] === 1);
|
||||
assert(o[1] === 2);
|
||||
assert(o[2] === 3);
|
||||
assert(o[3] === "test");
|
||||
assert(o[4] === "test");
|
||||
assert(o[5] === "test");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.includes.length === 1);
|
||||
|
||||
var array = ['hello', 'friends', 1, 2, false];
|
||||
|
||||
assert([].includes() === false);
|
||||
assert([undefined].includes() === true);
|
||||
assert(array.includes('hello') === true);
|
||||
assert(array.includes(1) === true);
|
||||
assert(array.includes(1, -3) === true);
|
||||
assert(array.includes('serenity') === false);
|
||||
assert(array.includes(false, -1) === true);
|
||||
assert(array.includes(2, -1) === false);
|
||||
assert(array.includes(2, -100) === true);
|
||||
assert(array.includes('friends', 100) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.indexOf.length === 1);
|
||||
|
||||
var array = ['hello', 'friends', 1, 2, false];
|
||||
|
||||
assert(array.indexOf('hello') === 0);
|
||||
assert(array.indexOf('friends') === 1);
|
||||
assert(array.indexOf(false) === 4);
|
||||
assert(array.indexOf(false, 2) === 4);
|
||||
assert(array.indexOf(false, -2) === 4);
|
||||
assert(array.indexOf(1) === 2);
|
||||
assert(array.indexOf(1, 1000) === -1);
|
||||
assert(array.indexOf(1, -1000) === 2);
|
||||
assert(array.indexOf('serenity') === -1);
|
||||
assert(array.indexOf(false, -1) === 4);
|
||||
assert(array.indexOf(2, -1) === -1);
|
||||
assert(array.indexOf(2, -2) === 3);
|
||||
assert([].indexOf('serenity') === -1);
|
||||
assert([].indexOf('serenity', 10) === -1);
|
||||
assert([].indexOf('serenity', -10) === -1);
|
||||
assert([].indexOf() === -1);
|
||||
assert([undefined].indexOf() === 0);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js
Normal file
19
Libraries/LibJS/Tests/builtins/Array/Array.prototype.join.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.join.length === 1);
|
||||
|
||||
assert(["hello", "friends"].join() === "hello,friends");
|
||||
assert(["hello", "friends"].join(" ") === "hello friends");
|
||||
assert(["hello", "friends", "foo"].join("~", "#") === "hello~friends~foo");
|
||||
assert([].join() === "");
|
||||
assert([null].join() === "");
|
||||
assert([undefined].join() === "");
|
||||
assert([undefined, null, ""].join() === ",,");
|
||||
assert([1, null, 2, undefined, 3].join() === "1,,2,,3");
|
||||
assert(Array(3).join() === ",,");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.lastIndexOf.length === 1);
|
||||
|
||||
var array = [1, 2, 3, 1, "hello"];
|
||||
|
||||
assert(array.lastIndexOf("hello") === 4);
|
||||
assert(array.lastIndexOf("hello", 1000) === 4);
|
||||
assert(array.lastIndexOf(1) === 3);
|
||||
assert(array.lastIndexOf(1, -1) === 3);
|
||||
assert(array.lastIndexOf(1, -2) === 3);
|
||||
assert(array.lastIndexOf(2) === 1);
|
||||
assert(array.lastIndexOf(2, -3) === 1);
|
||||
assert(array.lastIndexOf(2, -4) === 1);
|
||||
assert([].lastIndexOf('hello') === -1);
|
||||
assert([].lastIndexOf('hello', 10) === -1);
|
||||
assert([].lastIndexOf('hello', -10) === -1);
|
||||
assert([].lastIndexOf() === -1);
|
||||
assert([undefined].lastIndexOf() === 0);
|
||||
assert([undefined, undefined, undefined].lastIndexOf() === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
53
Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js
Normal file
53
Libraries/LibJS/Tests/builtins/Array/Array.prototype.map.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.map.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].map();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.map() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].map(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; };
|
||||
|
||||
assert([].map(callback).length === 0);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1, 2, 3].map(callback).length === 3);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, , , "foo", , undefined, , ,].map(callback).length === 8);
|
||||
assert(callbackCalled === 3);
|
||||
|
||||
var results = [undefined, null, true, "foo", 42, {}].map((value, index) => "" + index + " -> " + value);
|
||||
assert(results.length === 6);
|
||||
assert(results[0] === "0 -> undefined");
|
||||
assert(results[1] === "1 -> null");
|
||||
assert(results[2] === "2 -> true");
|
||||
assert(results[3] === "3 -> foo");
|
||||
assert(results[4] === "4 -> 42");
|
||||
assert(results[5] === "5 -> [object Object]");
|
||||
|
||||
var squaredNumbers = [0, 1, 2, 3, 4].map(x => x ** 2);
|
||||
assert(squaredNumbers.length === 5);
|
||||
assert(squaredNumbers[0] === 0);
|
||||
assert(squaredNumbers[1] === 1);
|
||||
assert(squaredNumbers[2] === 4);
|
||||
assert(squaredNumbers[3] === 9);
|
||||
assert(squaredNumbers[4] === 16);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
21
Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js
Normal file
21
Libraries/LibJS/Tests/builtins/Array/Array.prototype.pop.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
var value = a.pop();
|
||||
assert(value === 3);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
|
||||
var a = [];
|
||||
var value = a.pop();
|
||||
assert(value === undefined);
|
||||
assert(a.length === 0);
|
||||
|
||||
assert([,].pop() === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
30
Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js
Normal file
30
Libraries/LibJS/Tests/builtins/Array/Array.prototype.push.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.push.length === 1);
|
||||
|
||||
var a = ["hello"];
|
||||
var length = a.push();
|
||||
assert(length === 1);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "hello");
|
||||
|
||||
length = a.push("friends");
|
||||
assert(length === 2);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === "hello");
|
||||
assert(a[1] === "friends");
|
||||
|
||||
length = a.push(1, 2, 3);
|
||||
assert(length === 5);
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === "hello");
|
||||
assert(a[1] === "friends");
|
||||
assert(a[2] === 1);
|
||||
assert(a[3] === 2);
|
||||
assert(a[4] === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
104
Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js
Normal file
104
Libraries/LibJS/Tests/builtins/Array/Array.prototype.reduce.js
Normal file
|
@ -0,0 +1,104 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reduce.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[1].reduce();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Array.prototype.reduce() requires at least one argument"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[1].reduce(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].reduce((a, x) => x);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
[, ,].reduce((a, x) => x);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value"
|
||||
});
|
||||
|
||||
[1, 2].reduce(function () { assert(this === undefined) });
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => { callbackCalled++; return true };
|
||||
|
||||
assert([1].reduce(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([, 1].reduce(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([, , 1, 2, 3].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, , , 10, , 100, , ,].reduce(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
var constantlySad = () => ":^(";
|
||||
var result = [].reduce(constantlySad, ":^)");
|
||||
assert(result === ":^)");
|
||||
|
||||
result = [":^0"].reduce(constantlySad, ":^)");
|
||||
assert(result === ":^(");
|
||||
|
||||
result = [":^0"].reduce(constantlySad);
|
||||
assert(result === ":^0");
|
||||
|
||||
result = [5, 4, 3, 2, 1].reduce((accum, elem) => accum + elem);
|
||||
assert(result === 15);
|
||||
|
||||
result = [1, 2, 3, 4, 5, 6].reduce((accum, elem) => accum + elem, 100);
|
||||
assert(result === 121);
|
||||
|
||||
result = [6, 5, 4, 3, 2, 1].reduce((accum, elem) => { return accum + elem }, 100);
|
||||
assert(result === 121);
|
||||
|
||||
var indexes = [];
|
||||
result = ["foo", 1, true].reduce((a, v, i) => { indexes.push(i) });
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 2);
|
||||
assert(indexes[0] === 1);
|
||||
assert(indexes[1] === 2);
|
||||
|
||||
indexes = [];
|
||||
result = ["foo", 1, true].reduce((a, v, i) => { indexes.push(i) }, "foo");
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 3);
|
||||
assert(indexes[0] === 0);
|
||||
assert(indexes[1] === 1);
|
||||
assert(indexes[2] === 2);
|
||||
|
||||
var mutable = { prop: 0 };
|
||||
result = ["foo", 1, true].reduce((a, v) => { a.prop = v; return a; }, mutable);
|
||||
assert(result === mutable);
|
||||
assert(result.prop === true);
|
||||
|
||||
var a1 = [1, 2];
|
||||
var a2 = null;
|
||||
a1.reduce((a, v, i, t) => { a2 = t });
|
||||
assert(a1 === a2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reduceRight.length === 1);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[1].reduceRight();
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Array.prototype.reduceRight() requires at least one argument",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[1].reduceRight(undefined);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "undefined is not a function",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[].reduceRight((a, x) => x);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value",
|
||||
}
|
||||
);
|
||||
|
||||
assertThrowsError(
|
||||
() => {
|
||||
[, ,].reduceRight((a, x) => x);
|
||||
},
|
||||
{
|
||||
error: TypeError,
|
||||
message: "Reduce of empty array with no initial value",
|
||||
}
|
||||
);
|
||||
|
||||
[1, 2].reduceRight(function () {
|
||||
assert(this === undefined);
|
||||
});
|
||||
|
||||
var callbackCalled = 0;
|
||||
var callback = () => {
|
||||
callbackCalled++;
|
||||
return true;
|
||||
};
|
||||
|
||||
assert([1].reduceRight(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
assert([1].reduceRight(callback) === 1);
|
||||
assert(callbackCalled === 0);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([1, 2, 3, ,].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
callbackCalled = 0;
|
||||
assert([, , , 1, , , 10, , 100, , ,].reduceRight(callback) === true);
|
||||
assert(callbackCalled === 2);
|
||||
|
||||
var constantlySad = () => ":^(";
|
||||
var result = [].reduceRight(constantlySad, ":^)");
|
||||
assert(result === ":^)");
|
||||
|
||||
result = [":^0"].reduceRight(constantlySad, ":^)");
|
||||
assert(result === ":^(");
|
||||
|
||||
result = [":^0"].reduceRight(constantlySad);
|
||||
assert(result === ":^0");
|
||||
|
||||
result = [5, 4, 3, 2, 1].reduceRight((accum, elem) => "" + accum + elem);
|
||||
assert(result === "12345");
|
||||
|
||||
result = [1, 2, 3, 4, 5, 6].reduceRight((accum, elem) => {
|
||||
return "" + accum + elem;
|
||||
}, 100);
|
||||
assert(result === "100654321");
|
||||
|
||||
result = [6, 5, 4, 3, 2, 1].reduceRight((accum, elem) => {
|
||||
return "" + accum + elem;
|
||||
}, 100);
|
||||
assert(result === "100123456");
|
||||
|
||||
var indexes = [];
|
||||
result = ["foo", 1, true].reduceRight((a, v, i) => {
|
||||
indexes.push(i);
|
||||
});
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 2);
|
||||
assert(indexes[0] === 1);
|
||||
assert(indexes[1] === 0);
|
||||
|
||||
indexes = [];
|
||||
result = ["foo", 1, true].reduceRight((a, v, i) => {
|
||||
indexes.push(i);
|
||||
}, "foo");
|
||||
assert(result === undefined);
|
||||
assert(indexes.length === 3);
|
||||
assert(indexes[0] === 2);
|
||||
assert(indexes[1] === 1);
|
||||
assert(indexes[2] === 0);
|
||||
|
||||
var mutable = { prop: 0 };
|
||||
result = ["foo", 1, true].reduceRight((a, v) => {
|
||||
a.prop = v;
|
||||
return a;
|
||||
}, mutable);
|
||||
assert(result === mutable);
|
||||
assert(result.prop === "foo");
|
||||
|
||||
var a1 = [1, 2];
|
||||
var a2 = null;
|
||||
a1.reduceRight((a, v, i, t) => {
|
||||
a2 = t;
|
||||
});
|
||||
assert(a1 === a2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.reverse.length === 0);
|
||||
|
||||
var array = [1, 2, 3];
|
||||
|
||||
assert(array[0] === 1);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 3);
|
||||
|
||||
array.reverse();
|
||||
|
||||
assert(array[0] === 3);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 1);
|
||||
|
||||
var array_ref = array.reverse();
|
||||
|
||||
assert(array_ref[0] === 1);
|
||||
assert(array_ref[1] === 2);
|
||||
assert(array_ref[2] === 3);
|
||||
|
||||
assert(array[0] === 1);
|
||||
assert(array[1] === 2);
|
||||
assert(array[2] === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
var value = a.shift();
|
||||
assert(value === 1);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === 2);
|
||||
assert(a[1] === 3);
|
||||
|
||||
var a = [];
|
||||
var value = a.shift();
|
||||
assert(value === undefined);
|
||||
assert(a.length === 0);
|
||||
|
||||
assert([,].shift() === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.slice.length === 2);
|
||||
|
||||
var array = ["hello", "friends", "serenity", 1];
|
||||
|
||||
var array_slice = array.slice();
|
||||
assert(array_slice.length === array.length);
|
||||
assert(array_slice.length === 4);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
assert(array_slice[2] === "serenity");
|
||||
assert(array_slice[3] === 1);
|
||||
|
||||
array_slice = array.slice(1)
|
||||
assert(array_slice.length === 3);
|
||||
assert(array_slice[0] === "friends");
|
||||
assert(array_slice[1] === "serenity");
|
||||
assert(array_slice[2] === 1);
|
||||
|
||||
array_slice = array.slice(0, 2);
|
||||
assert(array_slice.length === 2);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
|
||||
array_slice = array.slice(-1);
|
||||
assert(array_slice.length === 1);
|
||||
assert(array_slice[0] === 1);
|
||||
|
||||
array_slice = array.slice(1, 1);
|
||||
assert(array_slice.length === 0);
|
||||
|
||||
array_slice = array.slice(1, -1);
|
||||
assert(array_slice.length === 2);
|
||||
assert(array_slice[0] === "friends");
|
||||
assert(array_slice[1] === "serenity");
|
||||
|
||||
array_slice = array.slice(2, -1);
|
||||
assert(array_slice.length === 1);
|
||||
assert(array_slice[0] === "serenity");
|
||||
|
||||
array_slice = array.slice(0, 100);
|
||||
assert(array_slice.length === 4);
|
||||
assert(array_slice[0] === "hello");
|
||||
assert(array_slice[1] === "friends");
|
||||
assert(array_slice[2] === "serenity");
|
||||
assert(array_slice[3] === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
34
Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js
Normal file
34
Libraries/LibJS/Tests/builtins/Array/Array.prototype.some.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.some.length === 1);
|
||||
|
||||
assertThrowsError(() => {
|
||||
[].some(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "undefined is not a function"
|
||||
});
|
||||
|
||||
var array = ["hello", "friends", 1, 2, false, -42, { name: "serenityos"}];
|
||||
|
||||
assert(array.some(value => value === "hello") === true);
|
||||
assert(array.some(value => value === "serenity") === false);
|
||||
assert(array.some((value, index, arr) => index === 1) === true);
|
||||
assert(array.some(value => value == "1") === true);
|
||||
assert(array.some(value => value === 1) === true);
|
||||
assert(array.some(value => value === 13) === false);
|
||||
assert(array.some(value => typeof(value) !== "string") === true);
|
||||
assert(array.some(value => typeof(value) === "boolean") === true);
|
||||
assert(array.some(value => value > 1) === true);
|
||||
assert(array.some(value => value > 1 && value < 3) === true);
|
||||
assert(array.some(value => value > 100) === false);
|
||||
assert(array.some(value => value < 0) === true);
|
||||
assert(array.some(value => array.pop()) === true);
|
||||
assert(["", "hello", "friends", "serenity"].some(value => value.length === 0) === true);
|
||||
assert([].some(value => value === 1) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.splice.length === 2);
|
||||
|
||||
var array = ["hello", "friends", "serenity", 1, 2];
|
||||
var removed = array.splice(3);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 2);
|
||||
assert(removed[0] === 1);
|
||||
assert(removed[1] === 2);
|
||||
|
||||
array = ["hello", "friends", "serenity", 1, 2];
|
||||
removed = array.splice(-2, 1);
|
||||
assert(array.length === 4);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(array[3] === 2);
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === 1);
|
||||
|
||||
array = ["serenity"];
|
||||
removed = array.splice(0, 0, "hello", "friends");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["goodbye", "friends", "serenity"];
|
||||
removed = array.splice(0, 1, "hello");
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "hello");
|
||||
assert(array[1] === "friends");
|
||||
assert(array[2] === "serenity");
|
||||
assert(removed.length === 1);
|
||||
assert(removed[0] === "goodbye");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice();
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
removed = array.splice(0, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(123, 123);
|
||||
assert(array.length === 3);
|
||||
assert(array[0] === "foo");
|
||||
assert(array[1] === "bar");
|
||||
assert(array[2] === "baz");
|
||||
assert(removed.length === 0);
|
||||
|
||||
array = ["foo", "bar", "baz"];
|
||||
removed = array.splice(-123, 123);
|
||||
assert(array.length === 0);
|
||||
assert(removed.length === 3);
|
||||
assert(removed[0] === "foo");
|
||||
assert(removed[1] === "bar");
|
||||
assert(removed[2] === "baz");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.toLocaleString.length === 0);
|
||||
|
||||
assert([].toLocaleString() === "");
|
||||
assert(["foo"].toLocaleString() === "foo");
|
||||
assert(["foo", "bar"].toLocaleString() === "foo,bar");
|
||||
assert(["foo", undefined, "bar", null, "baz"].toLocaleString() === "foo,,bar,,baz");
|
||||
|
||||
var toStringCalled = 0;
|
||||
var o = {
|
||||
toString: () => {
|
||||
toStringCalled++;
|
||||
return "o";
|
||||
}
|
||||
};
|
||||
assert([o, undefined, o, null, o].toLocaleString() === "o,,o,,o");
|
||||
assert(toStringCalled === 3);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
assert(a.toString() === '1,2,3');
|
||||
assert([].toString() === '');
|
||||
assert([5].toString() === '5');
|
||||
|
||||
assert("rgb(" + [10, 11, 12] + ")" === "rgb(10,11,12)");
|
||||
|
||||
assert([undefined, null].toString() === ",");
|
||||
|
||||
a = new Array(5);
|
||||
assert(a.toString() === ",,,,");
|
||||
a[2] = "foo";
|
||||
a[4] = "bar";
|
||||
assert(a.toString() === ",,foo,,bar");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Array.prototype.unshift.length === 1);
|
||||
|
||||
var a = ["hello"];
|
||||
var length = a.unshift();
|
||||
assert(length === 1);
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === "hello");
|
||||
|
||||
length = a.unshift("friends");
|
||||
assert(length === 2);
|
||||
assert(a.length === 2);
|
||||
assert(a[0] === "friends");
|
||||
assert(a[1] === "hello");
|
||||
|
||||
length = a.unshift(1, 2, 3);
|
||||
assert(length === 5);
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === "friends");
|
||||
assert(a[4] === "hello");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
63
Libraries/LibJS/Tests/builtins/Array/array-basic.js
Normal file
63
Libraries/LibJS/Tests/builtins/Array/array-basic.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
|
||||
assert(typeof a === "object");
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a[1] = 5;
|
||||
assert(a[1] === 5);
|
||||
assert(a.length === 3);
|
||||
|
||||
a.push(7);
|
||||
assert(a[3] === 7);
|
||||
assert(a.length === 4);
|
||||
|
||||
a = [,];
|
||||
assert(a.length === 1);
|
||||
assert(a.toString() === "");
|
||||
assert(a[0] === undefined);
|
||||
|
||||
a = [,,,,];
|
||||
assert(a.length === 4);
|
||||
assert(a.toString() === ",,,");
|
||||
assert(a[0] === undefined);
|
||||
assert(a[1] === undefined);
|
||||
assert(a[2] === undefined);
|
||||
assert(a[3] === undefined);
|
||||
|
||||
a = [1,,2,,,3,];
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,,2,,,3");
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === undefined);
|
||||
assert(a[2] === 2);
|
||||
assert(a[3] === undefined);
|
||||
assert(a[4] === undefined);
|
||||
assert(a[5] === 3);
|
||||
|
||||
a = [1,,2,,,3,];
|
||||
Object.defineProperty(a, 1, {
|
||||
get() {
|
||||
return this.secret_prop;
|
||||
},
|
||||
set(value) {
|
||||
this.secret_prop = value;
|
||||
},
|
||||
});
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,,2,,,3");
|
||||
assert(a.secret_prop === undefined);
|
||||
a[1] = 20;
|
||||
assert(a.length === 6);
|
||||
assert(a.toString() === "1,20,2,,,3");
|
||||
assert(a.secret_prop === 20);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
48
Libraries/LibJS/Tests/builtins/Array/array-length-setter.js
Normal file
48
Libraries/LibJS/Tests/builtins/Array/array-length-setter.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a = [1, 2, 3];
|
||||
|
||||
assert(a.length === 3);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
|
||||
a.length = 5;
|
||||
assert(a.length === 5);
|
||||
assert(a[0] === 1);
|
||||
assert(a[1] === 2);
|
||||
assert(a[2] === 3);
|
||||
assert(a[3] === undefined);
|
||||
assert(a[4] === undefined);
|
||||
|
||||
a.length = 1;
|
||||
assert(a.length === 1);
|
||||
assert(a[0] === 1);
|
||||
|
||||
a.length = 0;
|
||||
assert(a.length === 0);
|
||||
|
||||
a.length = "42";
|
||||
assert(a.length === 42);
|
||||
|
||||
a.length = [];
|
||||
assert(a.length === 0);
|
||||
|
||||
a.length = true;
|
||||
assert(a.length === 1);
|
||||
|
||||
[undefined, "foo", -1, Infinity, -Infinity, NaN].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
a.length = value;
|
||||
}, {
|
||||
error: RangeError,
|
||||
message: "Invalid array length"
|
||||
});
|
||||
assert(a.length === 1);
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var a, callbackCalled;
|
||||
|
||||
callbackCalled = 0;
|
||||
a = [1, 2, 3, 4, 5];
|
||||
a.find(() => {
|
||||
callbackCalled++;
|
||||
a.pop();
|
||||
});
|
||||
assert(callbackCalled === 5);
|
||||
|
||||
callbackCalled = 0;
|
||||
a = [1, 2, 3, 4, 5];
|
||||
a.findIndex(() => {
|
||||
callbackCalled++;
|
||||
a.pop();
|
||||
});
|
||||
assert(callbackCalled === 5);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
44
Libraries/LibJS/Tests/builtins/Array/array-spread.js
Normal file
44
Libraries/LibJS/Tests/builtins/Array/array-spread.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
function testArray(arr) {
|
||||
return arr.length === 4 &&
|
||||
arr[0] === 0 &&
|
||||
arr[1] === 1 &&
|
||||
arr[2] === 2 &&
|
||||
arr[3] === 3;
|
||||
}
|
||||
|
||||
try {
|
||||
let arr = [0, ...[1, 2], 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
let a = [1, 2];
|
||||
arr = [0, ...a, 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
let obj = { a: [1, 2] };
|
||||
arr = [0, ...obj.a, 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
arr = [...[], ...[...[0, 1, 2]], 3];
|
||||
assert(testArray(arr));
|
||||
|
||||
assertThrowsError(() => {
|
||||
[...1];
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "1 is not iterable",
|
||||
});
|
||||
|
||||
|
||||
assertThrowsError(() => {
|
||||
[...{}];
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "[object Object] is not iterable",
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
9
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js
Normal file
9
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.asIntN.length === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
9
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js
Normal file
9
Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.asUintN.length === 2);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
62
Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
62
Libraries/LibJS/Tests/builtins/BigInt/BigInt.js
Normal file
|
@ -0,0 +1,62 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.length === 1);
|
||||
assert(BigInt.name === "BigInt");
|
||||
|
||||
assert(BigInt(0) === 0n);
|
||||
assert(BigInt(1) === 1n);
|
||||
assert(BigInt(+1) === 1n);
|
||||
assert(BigInt(-1) === -1n);
|
||||
assert(BigInt("") === 0n);
|
||||
assert(BigInt("0") === 0n);
|
||||
assert(BigInt("1") === 1n);
|
||||
assert(BigInt("+1") === 1n);
|
||||
assert(BigInt("-1") === -1n);
|
||||
assert(BigInt("-1") === -1n);
|
||||
assert(BigInt([]) === 0n);
|
||||
assert(BigInt("42") === 42n);
|
||||
assert(BigInt(" \n 00100 \n ") === 100n);
|
||||
assert(BigInt(123n) === 123n);
|
||||
assert(BigInt("3323214327642987348732109829832143298746432437532197321") === 3323214327642987348732109829832143298746432437532197321n);
|
||||
|
||||
assertThrowsError(() => {
|
||||
new BigInt();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "BigInt is not a constructor"
|
||||
});
|
||||
|
||||
[null, undefined, Symbol()].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
BigInt(value);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: typeof value === "symbol"
|
||||
? "Cannot convert symbol to BigInt"
|
||||
: `Cannot convert ${value} to BigInt`
|
||||
});
|
||||
});
|
||||
|
||||
["foo", "123n", "1+1", {}, function () { }].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
BigInt(value);
|
||||
}, {
|
||||
error: SyntaxError,
|
||||
message: `Invalid value for BigInt: ${value}`
|
||||
});
|
||||
});
|
||||
|
||||
[1.23, Infinity, -Infinity, NaN].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
BigInt(value);
|
||||
}, {
|
||||
error: RangeError,
|
||||
message: "BigInt argument must be an integer"
|
||||
});
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.prototype.toLocaleString.length === 0);
|
||||
|
||||
assertThrowsError(() => {
|
||||
BigInt.prototype.toLocaleString.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a BigInt object"
|
||||
});
|
||||
|
||||
assert(BigInt(123).toLocaleString() === "123");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.prototype.toString.length === 0);
|
||||
|
||||
assertThrowsError(() => {
|
||||
BigInt.prototype.toString.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a BigInt object"
|
||||
});
|
||||
|
||||
assert(BigInt(123).toString() === "123");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(BigInt.prototype.valueOf.length === 0);
|
||||
|
||||
assertThrowsError(() => {
|
||||
BigInt.prototype.valueOf.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a BigInt object"
|
||||
});
|
||||
|
||||
assert(typeof BigInt(123).valueOf() === "bigint");
|
||||
// FIXME: Uncomment once we support Object() with argument
|
||||
// assert(typeof Object(123n).valueOf() === "bigint");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
68
Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js
Normal file
68
Libraries/LibJS/Tests/builtins/BigInt/bigint-basic.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var bigint = 123n;
|
||||
|
||||
assert(typeof bigint === "bigint");
|
||||
assert(-bigint === -123n);
|
||||
assert("" + bigint === "123")
|
||||
|
||||
assertThrowsError(() => {
|
||||
+bigint;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot convert BigInt to number"
|
||||
});
|
||||
|
||||
assert(12n + 34n === 46n);
|
||||
assert(12n - 34n === -22n);
|
||||
assert(8n * 12n === 96n);
|
||||
assert(123n / 10n === 12n);
|
||||
assert(2n ** 3n === 8n);
|
||||
assert(5n % 3n === 2n);
|
||||
assert(45977665298704210987n + 714320987142450987412098743217984576n / 4598741987421098765327980n * 987498743n === 199365500239020623962n);
|
||||
|
||||
assert((12n & 5n) === 4n);
|
||||
assert((1n | 2n) === 3n);
|
||||
assert((5n ^ 3n) === 6n);
|
||||
assert(~1n === -2n);
|
||||
|
||||
bigint = 1n;
|
||||
assert(bigint++ === 1n);
|
||||
assert(bigint === 2n);
|
||||
assert(bigint-- === 2n);
|
||||
assert(bigint === 1n);
|
||||
assert(++bigint === 2n);
|
||||
assert(bigint === 2n);
|
||||
assert(--bigint === 1n);
|
||||
assert(bigint === 1n);
|
||||
|
||||
|
||||
assert((1n == 1n) === true);
|
||||
assert((1n == 1) === true);
|
||||
assert((1 == 1n) === true);
|
||||
assert((1n == 1.23) === false);
|
||||
assert((1.23 == 1n) === false);
|
||||
|
||||
assert((1n != 1n) === false);
|
||||
assert((1n != 1) === false);
|
||||
assert((1 != 1n) === false);
|
||||
assert((1n != 1.23) === true);
|
||||
assert((1.23 != 1n) === true);
|
||||
|
||||
assert((1n === 1n) === true);
|
||||
assert((1n === 1) == false);
|
||||
assert((1 === 1n) === false);
|
||||
assert((1n === 1.23) === false);
|
||||
assert((1.23 === 1n) === false);
|
||||
|
||||
assert((1n !== 1n) === false);
|
||||
assert((1n !== 1) === true);
|
||||
assert((1 !== 1n) === true);
|
||||
assert((1n !== 1.23) === true);
|
||||
assert((1.23 !== 1n) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
[1, null, undefined].forEach(value => {
|
||||
assertThrowsError(() => {
|
||||
1n + value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use addition operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n - value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use subtraction operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n * value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use multiplication operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n / value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use division operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n % value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use modulo operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n ** value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use exponentiation operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n | value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use bitwise OR operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n & value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use bitwise AND operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n ^ value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use bitwise XOR operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n << value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use left-shift operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n >> value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use right-shift operator with BigInt and other type"
|
||||
});
|
||||
assertThrowsError(() => {
|
||||
1n >>> value;
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot use unsigned right-shift operator with BigInt"
|
||||
});
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
40
Libraries/LibJS/Tests/builtins/Boolean/Boolean.js
Normal file
40
Libraries/LibJS/Tests/builtins/Boolean/Boolean.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Boolean.length === 1);
|
||||
assert(Boolean.name === "Boolean");
|
||||
assert(Boolean.prototype.length === undefined);
|
||||
|
||||
assert(typeof new Boolean() === "object");
|
||||
assert(new Boolean().valueOf() === false);
|
||||
|
||||
var foo = new Boolean(true);
|
||||
var bar = new Boolean(true);
|
||||
|
||||
assert(foo !== bar);
|
||||
assert(foo.valueOf() === bar.valueOf());
|
||||
|
||||
assert(new Boolean(true).toString() === "true");
|
||||
assert(new Boolean(false).toString() === "false");
|
||||
|
||||
assert(typeof Boolean() === "boolean");
|
||||
assert(typeof Boolean(true) === "boolean");
|
||||
|
||||
assert(Boolean() === false);
|
||||
assert(Boolean(false) === false);
|
||||
assert(Boolean(null) === false);
|
||||
assert(Boolean(undefined) === false);
|
||||
assert(Boolean(NaN) === false);
|
||||
assert(Boolean("") === false);
|
||||
assert(Boolean(0.0) === false);
|
||||
assert(Boolean(-0.0) === false);
|
||||
assert(Boolean(true) === true);
|
||||
assert(Boolean("0") === true);
|
||||
assert(Boolean({}) === true);
|
||||
assert(Boolean([]) === true);
|
||||
assert(Boolean(1)) === true;
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
10
Libraries/LibJS/Tests/builtins/Boolean/Boolean.prototype.js
Normal file
10
Libraries/LibJS/Tests/builtins/Boolean/Boolean.prototype.js
Normal file
|
@ -0,0 +1,10 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(typeof Boolean.prototype === "object");
|
||||
assert(Boolean.prototype.valueOf() === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var foo = true;
|
||||
assert(foo.toString() === "true");
|
||||
assert(true.toString() === "true");
|
||||
|
||||
assert(Boolean.prototype.toString.call(true) === "true");
|
||||
assert(Boolean.prototype.toString.call(false) === "false");
|
||||
|
||||
assertThrowsError(() => {
|
||||
Boolean.prototype.toString.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Boolean object"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var foo = true;
|
||||
assert(foo.valueOf() === true);
|
||||
assert(true.valueOf() === true);
|
||||
|
||||
assert(Boolean.prototype.valueOf.call(true) === true);
|
||||
assert(Boolean.prototype.valueOf.call(false) === false);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Boolean.prototype.valueOf.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Boolean object"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
11
Libraries/LibJS/Tests/builtins/Date/Date.js
Normal file
11
Libraries/LibJS/Tests/builtins/Date/Date.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Date.length === 7);
|
||||
assert(Date.name === "Date");
|
||||
assert(Date.prototype.length === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
15
Libraries/LibJS/Tests/builtins/Date/Date.now.js
Normal file
15
Libraries/LibJS/Tests/builtins/Date/Date.now.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var last = 0;
|
||||
for (var i = 0; i < 100; ++i) {
|
||||
var now = Date.now();
|
||||
assert(!isNaN(now))
|
||||
assert(now > 1580000000000);
|
||||
assert(now >= last);
|
||||
last = now;
|
||||
}
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getDate()));
|
||||
assert(1 <= d.getDate() <= 31);
|
||||
assert(d.getDate() === d.getDate());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
11
Libraries/LibJS/Tests/builtins/Date/Date.prototype.getDay.js
Normal file
11
Libraries/LibJS/Tests/builtins/Date/Date.prototype.getDay.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getDay()));
|
||||
assert(0 <= d.getDay() <= 6);
|
||||
assert(d.getDay() === d.getDay());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getFullYear()));
|
||||
assert(d.getFullYear() >= 2020);
|
||||
assert(d.getFullYear() === d.getFullYear());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getHours()));
|
||||
assert(0 <= d.getHours() <= 23);
|
||||
assert(d.getHours() === d.getHours());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getMilliseconds()));
|
||||
assert(0 <= d.getMilliseconds() <= 999);
|
||||
assert(d.getMilliseconds() === d.getMilliseconds());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getMinutes()));
|
||||
assert(0 <= d.getMinutes() <= 59);
|
||||
assert(d.getMinutes() === d.getMinutes());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getMonth()));
|
||||
assert(0 <= d.getMonth() <= 11);
|
||||
assert(d.getMonth() === d.getMonth());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getSeconds()));
|
||||
assert(0 <= d.getSeconds() <= 59);
|
||||
assert(d.getSeconds() === d.getSeconds());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var d = new Date();
|
||||
assert(!isNaN(d.getTime()));
|
||||
assert(d.getTime() > 1580000000000);
|
||||
assert(d.getTime() === d.getTime());
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
33
Libraries/LibJS/Tests/builtins/Error/Error.js
Normal file
33
Libraries/LibJS/Tests/builtins/Error/Error.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Error.length === 1);
|
||||
assert(Error.name === "Error");
|
||||
assert(Error.prototype.length === undefined);
|
||||
|
||||
var e;
|
||||
|
||||
e = Error();
|
||||
assert(e.name === "Error");
|
||||
assert(e.message === "");
|
||||
|
||||
e = Error(undefined);
|
||||
assert(e.name === "Error");
|
||||
assert(e.message === "");
|
||||
|
||||
e = Error("test");
|
||||
assert(e.name === "Error");
|
||||
assert(e.message === "test");
|
||||
|
||||
e = Error(42);
|
||||
assert(e.name === "Error");
|
||||
assert(e.message === "42");
|
||||
|
||||
e = Error(null);
|
||||
assert(e.name === "Error");
|
||||
assert(e.message === "null");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
14
Libraries/LibJS/Tests/builtins/Error/Error.prototype.name.js
Normal file
14
Libraries/LibJS/Tests/builtins/Error/Error.prototype.name.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var changedInstance = new Error("");
|
||||
changedInstance.name = 'NewCustomError';
|
||||
assert(changedInstance.name === "NewCustomError");
|
||||
|
||||
var normalInstance = new Error("");
|
||||
assert(normalInstance.name === "Error");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Error().toString() === "Error");
|
||||
assert(Error(undefined).toString() === "Error");
|
||||
assert(Error(null).toString() === "Error: null");
|
||||
assert(Error("test").toString() === "Error: test");
|
||||
assert(Error(42).toString() === "Error: 42");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
42
Libraries/LibJS/Tests/builtins/Function/Function.js
Normal file
42
Libraries/LibJS/Tests/builtins/Function/Function.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Function.length === 1);
|
||||
assert(Function.name === "Function");
|
||||
assert(Function.prototype.length === 0);
|
||||
assert(Function.prototype.name === "");
|
||||
|
||||
assert(typeof Function() === "function");
|
||||
assert(typeof new Function() === "function");
|
||||
|
||||
assert(Function()() === undefined);
|
||||
assert(new Function()() === undefined);
|
||||
assert(Function("return 42")() === 42);
|
||||
assert(new Function("return 42")() === 42);
|
||||
assert(new Function("foo", "return foo")(42) === 42);
|
||||
assert(new Function("foo,bar", "return foo + bar")(1, 2) === 3);
|
||||
assert(new Function("foo", "bar", "return foo + bar")(1, 2) === 3);
|
||||
assert(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3) === 6);
|
||||
assert(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3) === 6);
|
||||
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true) === 42);
|
||||
assert(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false) === "bar");
|
||||
assert(new Function("return typeof Function()")() === "function");
|
||||
assert(new Function("x", "return function (y) { return x + y };")(1)(2) === 3);
|
||||
|
||||
assert(new Function().name === "anonymous");
|
||||
assert(new Function().toString() === "function anonymous() {\n ???\n}");
|
||||
|
||||
assertThrowsError(() => {
|
||||
new Function("[");
|
||||
}, {
|
||||
error: SyntaxError,
|
||||
// This might be confusing at first but keep in mind it's actually parsing
|
||||
// function anonymous() { [ }
|
||||
// This is in line with what other engines are reporting.
|
||||
message: "Unexpected token CurlyClose. Expected BracketClose (line: 1, column: 26)"
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function Foo(arg) {
|
||||
this.foo = arg;
|
||||
}
|
||||
function Bar(arg) {
|
||||
this.bar = arg;
|
||||
}
|
||||
function FooBar(arg) {
|
||||
Foo.apply(this, [arg]);
|
||||
Bar.apply(this, [arg]);
|
||||
}
|
||||
function FooBarBaz(arg) {
|
||||
Foo.apply(this, [arg]);
|
||||
Bar.apply(this, [arg]);
|
||||
this.baz = arg;
|
||||
}
|
||||
|
||||
assert(Function.prototype.apply.length === 2);
|
||||
|
||||
var foo = new Foo("test");
|
||||
assert(foo.foo === "test");
|
||||
assert(foo.bar === undefined);
|
||||
assert(foo.baz === undefined);
|
||||
|
||||
var bar = new Bar("test");
|
||||
assert(bar.foo === undefined);
|
||||
assert(bar.bar === "test");
|
||||
assert(bar.baz === undefined);
|
||||
|
||||
var foobar = new FooBar("test");
|
||||
assert(foobar.foo === "test");
|
||||
assert(foobar.bar === "test");
|
||||
assert(foobar.baz === undefined);
|
||||
|
||||
var foobarbaz = new FooBarBaz("test");
|
||||
assert(foobarbaz.foo === "test");
|
||||
assert(foobarbaz.bar === "test");
|
||||
assert(foobarbaz.baz === "test");
|
||||
|
||||
assert(Math.abs.apply(null, [-1]) === 1);
|
||||
|
||||
var add = (x, y) => x + y;
|
||||
assert(add.apply(null, [1, 2]) === 3);
|
||||
|
||||
var multiply = function (x, y) { return x * y; };
|
||||
assert(multiply.apply(null, [3, 4]) === 12);
|
||||
|
||||
assert((() => this).apply("foo") === globalThis);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Function.prototype.bind.length === 1);
|
||||
|
||||
var charAt = String.prototype.charAt.bind("bar");
|
||||
assert(charAt(0) + charAt(1) + charAt(2) === "bar");
|
||||
|
||||
function getB() {
|
||||
return this.toUpperCase().charAt(0);
|
||||
}
|
||||
assert(getB.bind("bar")() === "B");
|
||||
|
||||
// Bound functions should work with array functions
|
||||
var Make3 = Number.bind(null, 3);
|
||||
assert([55].map(Make3)[0] === 3);
|
||||
|
||||
var MakeTrue = Boolean.bind(null, true);
|
||||
assert([1, 2, 3].filter(MakeTrue).length === 3);
|
||||
|
||||
assert([1, 2, 3].reduce((function (acc, x) { return acc + x }).bind(null, 4, 5)) === 9);
|
||||
|
||||
assert([1, 2, 3].reduce((function (acc, x) { return acc + x + this; }).bind(3)) === 12);
|
||||
|
||||
function sum(a, b, c) {
|
||||
return a + b + c;
|
||||
}
|
||||
|
||||
// Arguments should be able to be bound to a function.
|
||||
var boundSum = sum.bind(null, 10, 5);
|
||||
assert(isNaN(boundSum()));
|
||||
|
||||
assert(boundSum(5) === 20);
|
||||
assert(boundSum(5, 6, 7) === 20);
|
||||
|
||||
// Arguments should be appended to a BoundFunction's bound arguments.
|
||||
assert(boundSum.bind(null, 5)() === 20);
|
||||
|
||||
// A BoundFunction's length property should be adjusted based on the number
|
||||
// of bound arguments.
|
||||
assert(sum.length === 3);
|
||||
assert(boundSum.length === 1);
|
||||
assert(boundSum.bind(null, 5).length === 0);
|
||||
assert(boundSum.bind(null, 5, 6, 7, 8).length === 0);
|
||||
|
||||
function identity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
// It should capture the global object if the |this| value is null or undefined.
|
||||
assert(identity.bind()() === globalThis);
|
||||
assert(identity.bind(null)() === globalThis);
|
||||
assert(identity.bind(undefined)() === globalThis);
|
||||
|
||||
function Foo() {
|
||||
assert(identity.bind()() === globalThis);
|
||||
assert(identity.bind(this)() === this);
|
||||
}
|
||||
new Foo();
|
||||
|
||||
// Primitive |this| values should be converted to objects.
|
||||
assert(identity.bind("foo")() instanceof String);
|
||||
assert(identity.bind(123)() instanceof Number);
|
||||
assert(identity.bind(true)() instanceof Boolean);
|
||||
|
||||
// It should retain |this| values passed to it.
|
||||
var obj = { foo: "bar" };
|
||||
|
||||
assert(identity.bind(obj)() === obj);
|
||||
|
||||
// The bound |this| can not be changed once set
|
||||
assert(identity.bind("foo").bind(123)() instanceof String);
|
||||
|
||||
// The bound |this| value should have no effect on a constructor.
|
||||
function Bar() {
|
||||
this.x = 3;
|
||||
this.y = 4;
|
||||
}
|
||||
Bar.prototype.baz = "baz";
|
||||
|
||||
var BoundBar = Bar.bind({ u: 5, v: 6 });
|
||||
|
||||
var bar = new BoundBar();
|
||||
assert(bar.x === 3);
|
||||
assert(bar.y === 4);
|
||||
assert(typeof bar.u === "undefined");
|
||||
assert(typeof bar.v === "undefined");
|
||||
// Objects constructed from BoundFunctions should retain the prototype of the original function.
|
||||
assert(bar.baz === "baz");
|
||||
// BoundFunctions should not have a prototype property.
|
||||
assert(typeof BoundBar.prototype === "undefined");
|
||||
|
||||
// Function.prototype.bind should not accept non-function values.
|
||||
assertThrowsError(() => {
|
||||
Function.prototype.bind.call("foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Not a Function object"
|
||||
});
|
||||
|
||||
// A constructor's arguments should be able to be bound.
|
||||
var Make5 = Number.bind(null, 5);
|
||||
assert(Make5() === 5);
|
||||
assert(new Make5().valueOf() === 5);
|
||||
|
||||
// Null or undefined should be passed through as a |this| value in strict mode.
|
||||
(() => {
|
||||
"use strict";
|
||||
function strictIdentity() {
|
||||
return this;
|
||||
}
|
||||
|
||||
assert(strictIdentity.bind()() === undefined);
|
||||
assert(strictIdentity.bind(null)() === null);
|
||||
assert(strictIdentity.bind(undefined)() === undefined);
|
||||
})();
|
||||
|
||||
// Arrow functions can not have their |this| value set.
|
||||
assert((() => this).bind("foo")() === globalThis)
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
function Foo(arg) {
|
||||
this.foo = arg;
|
||||
}
|
||||
function Bar(arg) {
|
||||
this.bar = arg;
|
||||
}
|
||||
function FooBar(arg) {
|
||||
Foo.call(this, arg);
|
||||
Bar.call(this, arg);
|
||||
}
|
||||
function FooBarBaz(arg) {
|
||||
Foo.call(this, arg);
|
||||
Bar.call(this, arg);
|
||||
this.baz = arg;
|
||||
}
|
||||
|
||||
assert(Function.prototype.call.length === 1);
|
||||
|
||||
var foo = new Foo("test");
|
||||
assert(foo.foo === "test");
|
||||
assert(foo.bar === undefined);
|
||||
assert(foo.baz === undefined);
|
||||
|
||||
var bar = new Bar("test");
|
||||
assert(bar.foo === undefined);
|
||||
assert(bar.bar === "test");
|
||||
assert(bar.baz === undefined);
|
||||
|
||||
var foobar = new FooBar("test");
|
||||
assert(foobar.foo === "test");
|
||||
assert(foobar.bar === "test");
|
||||
assert(foobar.baz === undefined);
|
||||
|
||||
var foobarbaz = new FooBarBaz("test");
|
||||
assert(foobarbaz.foo === "test");
|
||||
assert(foobarbaz.bar === "test");
|
||||
assert(foobarbaz.baz === "test");
|
||||
|
||||
assert(Math.abs.call(null, -1) === 1);
|
||||
|
||||
var add = (x, y) => x + y;
|
||||
assert(add.call(null, 1, 2) === 3);
|
||||
|
||||
var multiply = function (x, y) { return x * y; };
|
||||
assert(multiply.call(null, 3, 4) === 12);
|
||||
|
||||
assert((() => this).call("foo") === globalThis);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert((function() {}).toString() === "function () {\n ???\n}");
|
||||
assert((function(foo) {}).toString() === "function (foo) {\n ???\n}");
|
||||
assert((function(foo, bar, baz) {}).toString() === "function (foo, bar, baz) {\n ???\n}");
|
||||
assert((function(foo, bar, baz) {
|
||||
if (foo) {
|
||||
return baz;
|
||||
} else if (bar) {
|
||||
return foo;
|
||||
}
|
||||
return bar + 42;
|
||||
}).toString() === "function (foo, bar, baz) {\n ???\n}");
|
||||
assert(console.log.toString() === "function log() {\n [NativeFunction]\n}");
|
||||
assert(Function.toString() === "function Function() {\n [FunctionConstructor]\n}");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
27
Libraries/LibJS/Tests/builtins/Infinity/Infinity-basic.js
Normal file
27
Libraries/LibJS/Tests/builtins/Infinity/Infinity-basic.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Infinity + "" === "Infinity");
|
||||
assert(-Infinity + "" === "-Infinity");
|
||||
assert(Infinity === Infinity);
|
||||
assert(Infinity - 1 === Infinity);
|
||||
assert(Infinity + 1 === Infinity);
|
||||
assert(-Infinity === -Infinity);
|
||||
assert(-Infinity - 1 === -Infinity);
|
||||
assert(-Infinity + 1 === -Infinity);
|
||||
assert(1 / Infinity === 0);
|
||||
assert(1 / -Infinity === 0);
|
||||
assert(1 / 0 === Infinity);
|
||||
assert(-1 / 0 === -Infinity);
|
||||
assert(-100 < Infinity);
|
||||
assert(0 < Infinity);
|
||||
assert(100 < Infinity);
|
||||
assert(-Infinity < Infinity);
|
||||
assert(-100 > -Infinity);
|
||||
assert(0 > -Infinity);
|
||||
assert(100 > -Infinity);
|
||||
assert(Infinity > -Infinity);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
15
Libraries/LibJS/Tests/builtins/JSON/JSON.parse-reviver.js
Normal file
15
Libraries/LibJS/Tests/builtins/JSON/JSON.parse-reviver.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let string = `{"var1":10,"var2":"hello","var3":{"nested":5}}`;
|
||||
|
||||
let object = JSON.parse(string, (key, value) => typeof value === "number" ? value * 2 : value);
|
||||
assertDeepEquals(object, { var1: 20, var2: "hello", var3: { nested: 10 } });
|
||||
|
||||
object = JSON.parse(string, (key, value) => typeof value === "number" ? undefined : value);
|
||||
assertDeepEquals(object, { var2: "hello", var3: {} });
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
43
Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
Normal file
43
Libraries/LibJS/Tests/builtins/JSON/JSON.parse.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(JSON.parse.length === 2);
|
||||
|
||||
const properties = [
|
||||
["5", 5],
|
||||
["null", null],
|
||||
["true", true],
|
||||
["false", false],
|
||||
['"test"', "test"],
|
||||
['[1,2,"foo"]', [1, 2, "foo"]],
|
||||
['{"foo":1,"bar":"baz"}', { foo: 1, bar: "baz" }],
|
||||
];
|
||||
|
||||
properties.forEach(testCase => {
|
||||
assertDeepEquals(JSON.parse(testCase[0]), testCase[1]);
|
||||
});
|
||||
|
||||
let syntaxErrors = [
|
||||
undefined,
|
||||
NaN,
|
||||
-NaN,
|
||||
Infinity,
|
||||
-Infinity,
|
||||
'{ "foo" }',
|
||||
'{ foo: "bar" }',
|
||||
"[1,2,3,]",
|
||||
"[1,2,3, ]",
|
||||
'{ "foo": "bar",}',
|
||||
'{ "foo": "bar", }',
|
||||
];
|
||||
|
||||
syntaxErrors.forEach(error => assertThrowsError(() => {
|
||||
JSON.parse(error);
|
||||
}, {
|
||||
error: SyntaxError,
|
||||
}));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
36
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-order.js
Normal file
36
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-order.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(JSON.stringify.length === 3);
|
||||
|
||||
let o = {
|
||||
key1: "key1",
|
||||
key2: "key2",
|
||||
key3: "key3",
|
||||
};
|
||||
|
||||
Object.defineProperty(o, "defined", {
|
||||
enumerable: true,
|
||||
get() {
|
||||
o.prop = "prop";
|
||||
return "defined";
|
||||
},
|
||||
});
|
||||
|
||||
o.key4 = "key4";
|
||||
|
||||
o[2] = 2;
|
||||
o[0] = 0;
|
||||
o[1] = 1;
|
||||
|
||||
delete o.key1;
|
||||
delete o.key3;
|
||||
|
||||
o.key1 = "key1";
|
||||
|
||||
assert(JSON.stringify(o) === '{"0":0,"1":1,"2":2,"key2":"key2","defined":"defined","key4":"key4","key1":"key1"}');
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
18
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-proxy.js
Normal file
18
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-proxy.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let p = new Proxy([], {
|
||||
get(_, key) {
|
||||
if (key === "length")
|
||||
return 3;
|
||||
return Number(key);
|
||||
},
|
||||
});
|
||||
|
||||
assert(JSON.stringify(p) === "[0,1,2]");
|
||||
assert(JSON.stringify([[new Proxy(p, {})]]) === "[[[0,1,2]]]");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let o = {
|
||||
var1: "foo",
|
||||
var2: 42,
|
||||
arr: [1, 2, {
|
||||
nested: {
|
||||
hello: "world",
|
||||
},
|
||||
get x() { return 10; }
|
||||
}],
|
||||
obj: {
|
||||
subarr: [3],
|
||||
},
|
||||
};
|
||||
|
||||
let string = JSON.stringify(o, (key, value) => {
|
||||
if (key === "hello")
|
||||
return undefined;
|
||||
if (value === 10)
|
||||
return 20;
|
||||
if (key === "subarr")
|
||||
return [3, 4, 5];
|
||||
return value;
|
||||
});
|
||||
|
||||
assert(string === '{"var1":"foo","var2":42,"arr":[1,2,{"nested":{},"x":20}],"obj":{"subarr":[3,4,5]}}');
|
||||
|
||||
string = JSON.stringify(o, ["var1", "var1", "var2", "obj"]);
|
||||
assert(string == '{"var1":"foo","var2":42,"obj":{}}');
|
||||
|
||||
string = JSON.stringify(o, ["var1", "var1", "var2", "obj", "subarr"]);
|
||||
assert(string == '{"var1":"foo","var2":42,"obj":{"subarr":[3]}}');
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
51
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-space.js
Normal file
51
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify-space.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
let o = {
|
||||
foo: 1,
|
||||
bar: "baz",
|
||||
qux: {
|
||||
get x() { return 10; },
|
||||
y() { return 20; },
|
||||
arr: [1, 2, 3],
|
||||
}
|
||||
};
|
||||
|
||||
let string = JSON.stringify(o, null, 4);
|
||||
let expected =
|
||||
`{
|
||||
"foo": 1,
|
||||
"bar": "baz",
|
||||
"qux": {
|
||||
"x": 10,
|
||||
"arr": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
]
|
||||
}
|
||||
}`;
|
||||
|
||||
assert(string === expected);
|
||||
|
||||
string = JSON.stringify(o, null, "abcd");
|
||||
expected =
|
||||
`{
|
||||
abcd"foo": 1,
|
||||
abcd"bar": "baz",
|
||||
abcd"qux": {
|
||||
abcdabcd"x": 10,
|
||||
abcdabcd"arr": [
|
||||
abcdabcdabcd1,
|
||||
abcdabcdabcd2,
|
||||
abcdabcdabcd3
|
||||
abcdabcd]
|
||||
abcd}
|
||||
}`;
|
||||
|
||||
assert(string === expected);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
71
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js
Normal file
71
Libraries/LibJS/Tests/builtins/JSON/JSON.stringify.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(JSON.stringify.length === 3);
|
||||
|
||||
assertThrowsError(() => {
|
||||
JSON.stringify(5n);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot serialize BigInt value to JSON",
|
||||
});
|
||||
|
||||
const properties = [
|
||||
[5, "5"],
|
||||
[undefined, undefined],
|
||||
[null, "null"],
|
||||
[NaN, "null"],
|
||||
[-NaN, "null"],
|
||||
[Infinity, "null"],
|
||||
[-Infinity, "null"],
|
||||
[true, "true"],
|
||||
[false, "false"],
|
||||
["test", '"test"'],
|
||||
[new Number(5), "5"],
|
||||
[new Boolean(false), "false"],
|
||||
[new String("test"), '"test"'],
|
||||
[() => {}, undefined],
|
||||
[[1, 2, "foo"], '[1,2,"foo"]'],
|
||||
[{ foo: 1, bar: "baz", qux() {} }, '{"foo":1,"bar":"baz"}'],
|
||||
[
|
||||
{
|
||||
var1: 1,
|
||||
var2: 2,
|
||||
toJSON(key) {
|
||||
let o = this;
|
||||
o.var2 = 10;
|
||||
return o;
|
||||
}
|
||||
},
|
||||
'{"var1":1,"var2":10}',
|
||||
],
|
||||
];
|
||||
|
||||
properties.forEach(testCase => {
|
||||
assert(JSON.stringify(testCase[0]) === testCase[1]);
|
||||
});
|
||||
|
||||
let bad1 = {};
|
||||
bad1.foo = bad1;
|
||||
let bad2 = [];
|
||||
bad2[5] = [[[bad2]]];
|
||||
|
||||
let bad3a = { foo: "bar" };
|
||||
let bad3b = [1, 2, bad3a];
|
||||
bad3a.bad = bad3b;
|
||||
|
||||
[bad1, bad2, bad3a].forEach(bad => assertThrowsError(() => {
|
||||
JSON.stringify(bad);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot stringify circular object",
|
||||
}));
|
||||
|
||||
let o = { foo: "bar" };
|
||||
Object.defineProperty(o, "baz", { value: "qux", enumerable: false });
|
||||
assert(JSON.stringify(o) === '{"foo":"bar"}');
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
16
Libraries/LibJS/Tests/builtins/Math/Math-constants.js
Normal file
16
Libraries/LibJS/Tests/builtins/Math/Math-constants.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isClose(Math.E, 2.718281));
|
||||
assert(isClose(Math.LN2, 0.693147));
|
||||
assert(isClose(Math.LN10, 2.302585));
|
||||
assert(isClose(Math.LOG2E, 1.442695));
|
||||
assert(isClose(Math.LOG10E, 0.434294));
|
||||
assert(isClose(Math.PI, 3.1415926));
|
||||
assert(isClose(Math.SQRT1_2, 0.707106));
|
||||
assert(isClose(Math.SQRT2, 1.414213));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
16
Libraries/LibJS/Tests/builtins/Math/Math.abs.js
Normal file
16
Libraries/LibJS/Tests/builtins/Math/Math.abs.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.abs('-1') === 1);
|
||||
assert(Math.abs(-2) === 2);
|
||||
assert(Math.abs(null) === 0);
|
||||
assert(Math.abs('') === 0);
|
||||
assert(Math.abs([]) === 0);
|
||||
assert(Math.abs([2]) === 2);
|
||||
assert(isNaN(Math.abs([1, 2])));
|
||||
assert(isNaN(Math.abs({})));
|
||||
assert(isNaN(Math.abs('string')));
|
||||
assert(isNaN(Math.abs()));
|
||||
console.log("PASS");
|
||||
} catch {
|
||||
}
|
13
Libraries/LibJS/Tests/builtins/Math/Math.acosh.js
Normal file
13
Libraries/LibJS/Tests/builtins/Math/Math.acosh.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isNaN(Math.acosh(-1)));
|
||||
assert(isNaN(Math.acosh(0)));
|
||||
assert(isNaN(Math.acosh(0.5)));
|
||||
assert(isClose(Math.acosh(1), 0));
|
||||
// FIXME: assert(isClose(Math.acosh(2), 1.316957));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
11
Libraries/LibJS/Tests/builtins/Math/Math.asinh.js
Normal file
11
Libraries/LibJS/Tests/builtins/Math/Math.asinh.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isClose(Math.asinh(0), 0));
|
||||
|
||||
// FIXME: assert(isClose(Math.asinh(1), 0.881373));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
14
Libraries/LibJS/Tests/builtins/Math/Math.atanh.js
Normal file
14
Libraries/LibJS/Tests/builtins/Math/Math.atanh.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isNaN(Math.atanh(-2)));
|
||||
assert(Math.atanh(-1) === -Infinity);
|
||||
// FIXME: assert(Math.atanh(0) === 0);
|
||||
assert(isClose(Math.atanh(0.5), 0.549306));
|
||||
// FIXME: assert(Math.atanh(1) === Infinity);
|
||||
assert(isNaN(Math.atanh(2)));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
16
Libraries/LibJS/Tests/builtins/Math/Math.cbrt.js
Normal file
16
Libraries/LibJS/Tests/builtins/Math/Math.cbrt.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isNaN(Math.cbrt(NaN)));
|
||||
// FIXME: assert(Math.cbrt(-1) === -1);
|
||||
assert(Math.cbrt(-0) === -0);
|
||||
// FIXME: assert(Math.cbrt(-Infinity) === -Infinity);
|
||||
// FIXME: assert(Math.cbrt(1) === 1);
|
||||
// FIXME: assert(Math.cbrt(Infinity) === Infinity);
|
||||
assert(Math.cbrt(null) === 0);
|
||||
// FIXME: assert(isClose(Math.cbrt(2), 1.259921));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Math/Math.ceil.js
Normal file
19
Libraries/LibJS/Tests/builtins/Math/Math.ceil.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.ceil(0.95) === 1);
|
||||
assert(Math.ceil(4) === 4);
|
||||
assert(Math.ceil(7.004) == 8);
|
||||
assert(Math.ceil(-0.95) === -0);
|
||||
assert(Math.ceil(-4) === -4);
|
||||
assert(Math.ceil(-7.004) === -7);
|
||||
|
||||
assert(isNaN(Math.ceil()));
|
||||
assert(isNaN(Math.ceil(NaN)));
|
||||
|
||||
assert(Math.ceil.length === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
50
Libraries/LibJS/Tests/builtins/Math/Math.clz32.js
Normal file
50
Libraries/LibJS/Tests/builtins/Math/Math.clz32.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.clz32.length === 1);
|
||||
|
||||
assert(Math.clz32(0) === 32);
|
||||
assert(Math.clz32(1) === 31);
|
||||
assert(Math.clz32(2) === 30);
|
||||
assert(Math.clz32(3) === 30);
|
||||
assert(Math.clz32(4) === 29);
|
||||
assert(Math.clz32(5) === 29);
|
||||
assert(Math.clz32(-1) === 0);
|
||||
assert(Math.clz32(-10) === 0);
|
||||
assert(Math.clz32(-100) === 0);
|
||||
assert(Math.clz32(-1000) === 0);
|
||||
assert(Math.clz32(-0.123) === 32);
|
||||
assert(Math.clz32(0.123) === 32);
|
||||
assert(Math.clz32(1.23) === 31);
|
||||
assert(Math.clz32(12) === 28);
|
||||
assert(Math.clz32(123) === 25);
|
||||
assert(Math.clz32(1234) === 21);
|
||||
assert(Math.clz32(12345) === 18);
|
||||
assert(Math.clz32(123456) === 15);
|
||||
assert(Math.clz32(1234567) === 11);
|
||||
assert(Math.clz32(12345678) === 8);
|
||||
assert(Math.clz32(123456789) === 5);
|
||||
assert(Math.clz32(999999999) === 2);
|
||||
assert(Math.clz32(9999999999) === 1);
|
||||
assert(Math.clz32(99999999999) === 1);
|
||||
assert(Math.clz32(999999999999) === 0);
|
||||
assert(Math.clz32(9999999999999) === 1);
|
||||
assert(Math.clz32(99999999999999) === 3);
|
||||
assert(Math.clz32(999999999999999) === 0);
|
||||
|
||||
assert(Math.clz32() === 32);
|
||||
assert(Math.clz32(NaN) === 32);
|
||||
assert(Math.clz32(Infinity) === 32);
|
||||
assert(Math.clz32(-Infinity) === 32);
|
||||
assert(Math.clz32(false) === 32);
|
||||
assert(Math.clz32(true) === 31);
|
||||
assert(Math.clz32(null) === 32);
|
||||
assert(Math.clz32(undefined) === 32);
|
||||
assert(Math.clz32([]) === 32);
|
||||
assert(Math.clz32({}) === 32);
|
||||
assert(Math.clz32("foo") === 32);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
18
Libraries/LibJS/Tests/builtins/Math/Math.cos.js
Normal file
18
Libraries/LibJS/Tests/builtins/Math/Math.cos.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.cos(0) === 1);
|
||||
assert(Math.cos(null) === 1);
|
||||
assert(Math.cos('') === 1);
|
||||
assert(Math.cos([]) === 1);
|
||||
assert(Math.cos(Math.PI) === -1);
|
||||
assert(isNaN(Math.cos()));
|
||||
assert(isNaN(Math.cos(undefined)));
|
||||
assert(isNaN(Math.cos([1, 2, 3])));
|
||||
assert(isNaN(Math.cos({})));
|
||||
assert(isNaN(Math.cos("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Math/Math.exp.js
Normal file
19
Libraries/LibJS/Tests/builtins/Math/Math.exp.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.exp.length === 1);
|
||||
|
||||
assert(Math.exp(0) === 1);
|
||||
assert(isClose(Math.exp(-2), 0.135335));
|
||||
assert(isClose(Math.exp(-1), 0.367879));
|
||||
assert(isClose(Math.exp(1), 2.718281));
|
||||
assert(isClose(Math.exp(2), 7.389056));
|
||||
|
||||
assert(isNaN(Math.exp()));
|
||||
assert(isNaN(Math.exp(undefined)));
|
||||
assert(isNaN(Math.exp("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Math/Math.expm1.js
Normal file
19
Libraries/LibJS/Tests/builtins/Math/Math.expm1.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.expm1.length === 1);
|
||||
|
||||
assert(Math.expm1(0) === 0);
|
||||
assert(isClose(Math.expm1(-2), -0.864664));
|
||||
assert(isClose(Math.expm1(-1), -0.632120));
|
||||
assert(isClose(Math.expm1(1), 1.718281));
|
||||
assert(isClose(Math.expm1(2), 6.389056));
|
||||
|
||||
assert(isNaN(Math.expm1()));
|
||||
assert(isNaN(Math.expm1(undefined)));
|
||||
assert(isNaN(Math.expm1("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Math/Math.floor.js
Normal file
19
Libraries/LibJS/Tests/builtins/Math/Math.floor.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.floor(0.95) === 0);
|
||||
assert(Math.floor(4) === 4);
|
||||
assert(Math.floor(7.004) == 7);
|
||||
assert(Math.floor(-0.95) === -1);
|
||||
assert(Math.floor(-4) === -4);
|
||||
assert(Math.floor(-7.004) === -8);
|
||||
|
||||
assert(isNaN(Math.floor()));
|
||||
assert(isNaN(Math.floor(NaN)));
|
||||
|
||||
assert(Math.floor.length === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
13
Libraries/LibJS/Tests/builtins/Math/Math.log1p.js
Normal file
13
Libraries/LibJS/Tests/builtins/Math/Math.log1p.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(isNaN(Math.log1p(-2)));
|
||||
assert(Math.log1p(-1) === -Infinity);
|
||||
// FIXME: assert(Math.log1p(0) === 0);
|
||||
// FIXME: assert(isClose(Math.log1p(1), 0.693147));
|
||||
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
15
Libraries/LibJS/Tests/builtins/Math/Math.max.js
Normal file
15
Libraries/LibJS/Tests/builtins/Math/Math.max.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.max.length === 2);
|
||||
assert(Math.max() === -Infinity);
|
||||
assert(Math.max(1) === 1);
|
||||
assert(Math.max(2, 1) === 2);
|
||||
assert(Math.max(1, 2, 3) === 3);
|
||||
assert(isNaN(Math.max(NaN)));
|
||||
assert(isNaN(Math.max("String", 1)));
|
||||
|
||||
console.log("PASS");
|
||||
} catch {
|
||||
console.log("FAIL");
|
||||
}
|
14
Libraries/LibJS/Tests/builtins/Math/Math.min.js
vendored
Normal file
14
Libraries/LibJS/Tests/builtins/Math/Math.min.js
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.min.length === 2);
|
||||
assert(Math.min(1) === 1);
|
||||
assert(Math.min(2, 1) === 1);
|
||||
assert(Math.min(1, 2, 3) === 1);
|
||||
assert(isNaN(Math.min(NaN)));
|
||||
assert(isNaN(Math.min("String", 1)));
|
||||
|
||||
console.log("PASS");
|
||||
} catch {
|
||||
console.log("FAIL");
|
||||
}
|
29
Libraries/LibJS/Tests/builtins/Math/Math.pow.js
Normal file
29
Libraries/LibJS/Tests/builtins/Math/Math.pow.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.pow(2, 0) === 1);
|
||||
assert(Math.pow(2, 1) === 2);
|
||||
assert(Math.pow(2, 2) === 4);
|
||||
assert(Math.pow(2, 3) === 8);
|
||||
assert(Math.pow(2, -3) === 0.125);
|
||||
assert(Math.pow(3, 2) === 9);
|
||||
assert(Math.pow(0, 0) === 1);
|
||||
assert(Math.pow(2, Math.pow(3, 2)) === 512);
|
||||
assert(Math.pow(Math.pow(2, 3), 2) === 64);
|
||||
assert(Math.pow("2", "3") === 8);
|
||||
assert(Math.pow("", []) === 1);
|
||||
assert(Math.pow([], null) === 1);
|
||||
assert(Math.pow(null, null) === 1);
|
||||
assert(Math.pow(undefined, null) === 1);
|
||||
assert(isNaN(Math.pow(NaN, 2)));
|
||||
assert(isNaN(Math.pow(2, NaN)));
|
||||
assert(isNaN(Math.pow(undefined, 2)));
|
||||
assert(isNaN(Math.pow(2, undefined)));
|
||||
assert(isNaN(Math.pow(null, undefined)));
|
||||
assert(isNaN(Math.pow(2, "foo")));
|
||||
assert(isNaN(Math.pow("foo", 2)));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
42
Libraries/LibJS/Tests/builtins/Math/Math.sign.js
Normal file
42
Libraries/LibJS/Tests/builtins/Math/Math.sign.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
load("test-common.js");
|
||||
|
||||
function isPositiveZero(value) {
|
||||
return value === 0 && 1 / value === Infinity;
|
||||
}
|
||||
|
||||
function isNegativeZero(value) {
|
||||
return value === 0 && 1 / value === -Infinity;
|
||||
}
|
||||
|
||||
try {
|
||||
assert(Math.sign.length === 1);
|
||||
|
||||
assert(Math.sign(0.0001) === 1);
|
||||
assert(Math.sign(1) === 1);
|
||||
assert(Math.sign(42) === 1);
|
||||
assert(Math.sign(Infinity) === 1);
|
||||
assert(isPositiveZero(Math.sign(0)));
|
||||
assert(isPositiveZero(Math.sign(null)));
|
||||
assert(isPositiveZero(Math.sign('')));
|
||||
assert(isPositiveZero(Math.sign([])));
|
||||
|
||||
assert(Math.sign(-0.0001) === -1);
|
||||
assert(Math.sign(-1) === -1);
|
||||
assert(Math.sign(-42) === -1);
|
||||
assert(Math.sign(-Infinity) === -1);
|
||||
assert(isNegativeZero(Math.sign(-0)));
|
||||
assert(isNegativeZero(Math.sign(-null)));
|
||||
assert(isNegativeZero(Math.sign(-'')));
|
||||
assert(isNegativeZero(Math.sign(-[])));
|
||||
|
||||
assert(isNaN(Math.sign()));
|
||||
assert(isNaN(Math.sign(undefined)));
|
||||
assert(isNaN(Math.sign([1, 2, 3])));
|
||||
assert(isNaN(Math.sign({})));
|
||||
assert(isNaN(Math.sign(NaN)));
|
||||
assert(isNaN(Math.sign("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
19
Libraries/LibJS/Tests/builtins/Math/Math.sin.js
Normal file
19
Libraries/LibJS/Tests/builtins/Math/Math.sin.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.sin(0) === 0);
|
||||
assert(Math.sin(null) === 0);
|
||||
assert(Math.sin('') === 0);
|
||||
assert(Math.sin([]) === 0);
|
||||
assert(Math.sin(Math.PI * 3 / 2) === -1);
|
||||
assert(Math.sin(Math.PI / 2) === 1);
|
||||
assert(isNaN(Math.sin()));
|
||||
assert(isNaN(Math.sin(undefined)));
|
||||
assert(isNaN(Math.sin([1, 2, 3])));
|
||||
assert(isNaN(Math.sin({})));
|
||||
assert(isNaN(Math.sin("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
7
Libraries/LibJS/Tests/builtins/Math/Math.sqrt.js
Normal file
7
Libraries/LibJS/Tests/builtins/Math/Math.sqrt.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.sqrt(9) === 3);
|
||||
console.log("PASS");
|
||||
} catch {
|
||||
}
|
18
Libraries/LibJS/Tests/builtins/Math/Math.tan.js
Normal file
18
Libraries/LibJS/Tests/builtins/Math/Math.tan.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Math.tan(0) === 0);
|
||||
assert(Math.tan(null) === 0);
|
||||
assert(Math.tan('') === 0);
|
||||
assert(Math.tan([]) === 0);
|
||||
assert(Math.ceil(Math.tan(Math.PI / 4)) === 1);
|
||||
assert(isNaN(Math.tan()));
|
||||
assert(isNaN(Math.tan(undefined)));
|
||||
assert(isNaN(Math.tan([1, 2, 3])));
|
||||
assert(isNaN(Math.tan({})));
|
||||
assert(isNaN(Math.tan("foo")));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
18
Libraries/LibJS/Tests/builtins/Math/Math.trunc.js
Normal file
18
Libraries/LibJS/Tests/builtins/Math/Math.trunc.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
load("test-common.js")
|
||||
|
||||
try {
|
||||
assert(Math.trunc(13.37) === 13);
|
||||
assert(Math.trunc(42.84) === 42);
|
||||
assert(Math.trunc(0.123) === 0);
|
||||
assert(Math.trunc(-0.123) === -0);
|
||||
|
||||
assert(isNaN(Math.trunc(NaN)));
|
||||
assert(isNaN(Math.trunc('foo')));
|
||||
assert(isNaN(Math.trunc()));
|
||||
|
||||
assert(Math.trunc.length === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
21
Libraries/LibJS/Tests/builtins/NaN/NaN-basic.js
Normal file
21
Libraries/LibJS/Tests/builtins/NaN/NaN-basic.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
var nan = undefined + 1;
|
||||
assert(nan + "" == "NaN");
|
||||
assert(NaN + "" == "NaN");
|
||||
assert(nan !== nan);
|
||||
assert(NaN !== NaN);
|
||||
assert(isNaN(nan) === true);
|
||||
assert(isNaN(NaN) === true);
|
||||
assert(isNaN(0) === false);
|
||||
assert(isNaN(undefined) === true);
|
||||
assert(isNaN(null) === false);
|
||||
assert(isNaN(Infinity) === false);
|
||||
assert(!!NaN === false);
|
||||
assert(!!nan === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
17
Libraries/LibJS/Tests/builtins/Number/Number-constants.js
Normal file
17
Libraries/LibJS/Tests/builtins/Number/Number-constants.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.EPSILON === 2 ** -52);
|
||||
assert(Number.EPSILON > 0);
|
||||
assert(Number.MAX_SAFE_INTEGER === 2 ** 53 - 1);
|
||||
assert(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);
|
||||
assert(Number.MIN_SAFE_INTEGER === -(2 ** 53 - 1));
|
||||
assert(Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2);
|
||||
assert(Number.POSITIVE_INFINITY === Infinity);
|
||||
assert(Number.NEGATIVE_INFINITY === -Infinity);
|
||||
assert(isNaN(Number.NaN));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
30
Libraries/LibJS/Tests/builtins/Number/Number.isFinite.js
Normal file
30
Libraries/LibJS/Tests/builtins/Number/Number.isFinite.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.isFinite !== isFinite);
|
||||
assert(Number.isFinite.length === 1);
|
||||
|
||||
assert(Number.isFinite(0) === true);
|
||||
assert(Number.isFinite(1.23) === true);
|
||||
assert(Number.isFinite(42) === true);
|
||||
|
||||
assert(Number.isFinite("") === false);
|
||||
assert(Number.isFinite("0") === false);
|
||||
assert(Number.isFinite("42") === false);
|
||||
assert(Number.isFinite(true) === false);
|
||||
assert(Number.isFinite(false) === false);
|
||||
assert(Number.isFinite(null) === false);
|
||||
assert(Number.isFinite([]) === false);
|
||||
assert(Number.isFinite() === false);
|
||||
assert(Number.isFinite(NaN) === false);
|
||||
assert(Number.isFinite(undefined) === false);
|
||||
assert(Number.isFinite(Infinity) === false);
|
||||
assert(Number.isFinite(-Infinity) === false);
|
||||
assert(Number.isFinite("foo") === false);
|
||||
assert(Number.isFinite({}) === false);
|
||||
assert(Number.isFinite([1, 2, 3]) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
38
Libraries/LibJS/Tests/builtins/Number/Number.isInteger.js
Normal file
38
Libraries/LibJS/Tests/builtins/Number/Number.isInteger.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.isInteger.length === 1);
|
||||
|
||||
assert(Number.isInteger(0) === true);
|
||||
assert(Number.isInteger(42) === true);
|
||||
assert(Number.isInteger(-10000) === true);
|
||||
assert(Number.isInteger(5) === true);
|
||||
assert(Number.isInteger(5.0) === true);
|
||||
assert(Number.isInteger(5 + 1/10000000000000000) === true);
|
||||
// FIXME: values outside of i32's range should still return true
|
||||
// assert(Number.isInteger(+2147483647 + 1) === true);
|
||||
// assert(Number.isInteger(-2147483648 - 1) === true);
|
||||
// assert(Number.isInteger(99999999999999999999999999999999999) === true);
|
||||
|
||||
assert(Number.isInteger(5 + 1/1000000000000000) === false);
|
||||
assert(Number.isInteger(1.23) === false);
|
||||
assert(Number.isInteger("") === false);
|
||||
assert(Number.isInteger("0") === false);
|
||||
assert(Number.isInteger("42") === false);
|
||||
assert(Number.isInteger(true) === false);
|
||||
assert(Number.isInteger(false) === false);
|
||||
assert(Number.isInteger(null) === false);
|
||||
assert(Number.isInteger([]) === false);
|
||||
assert(Number.isInteger(Infinity) === false);
|
||||
assert(Number.isInteger(-Infinity) === false);
|
||||
assert(Number.isInteger(NaN) === false);
|
||||
assert(Number.isInteger() === false);
|
||||
assert(Number.isInteger(undefined) === false);
|
||||
assert(Number.isInteger("foo") === false);
|
||||
assert(Number.isInteger({}) === false);
|
||||
assert(Number.isInteger([1, 2, 3]) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
31
Libraries/LibJS/Tests/builtins/Number/Number.isNaN.js
Normal file
31
Libraries/LibJS/Tests/builtins/Number/Number.isNaN.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.isNaN !== isNaN);
|
||||
assert(Number.isNaN.length === 1);
|
||||
|
||||
assert(Number.isNaN(0) === false);
|
||||
assert(Number.isNaN(42) === false);
|
||||
assert(Number.isNaN("") === false);
|
||||
assert(Number.isNaN("0") === false);
|
||||
assert(Number.isNaN("42") === false);
|
||||
assert(Number.isNaN(true) === false);
|
||||
assert(Number.isNaN(false) === false);
|
||||
assert(Number.isNaN(null) === false);
|
||||
assert(Number.isNaN([]) === false);
|
||||
assert(Number.isNaN(Infinity) === false);
|
||||
assert(Number.isNaN(-Infinity) === false);
|
||||
assert(Number.isNaN() === false);
|
||||
assert(Number.isNaN(undefined) === false);
|
||||
assert(Number.isNaN("foo") === false);
|
||||
assert(Number.isNaN({}) === false);
|
||||
assert(Number.isNaN([1, 2, 3]) === false);
|
||||
|
||||
assert(Number.isNaN(NaN) === true);
|
||||
assert(Number.isNaN(Number.NaN) === true);
|
||||
assert(Number.isNaN(0 / 0) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.isSafeInteger.length === 1);
|
||||
|
||||
assert(Number.isSafeInteger(0) === true);
|
||||
assert(Number.isSafeInteger(1) === true);
|
||||
assert(Number.isSafeInteger(2.0) === true);
|
||||
assert(Number.isSafeInteger(42) === true);
|
||||
assert(Number.isSafeInteger(Number.MAX_SAFE_INTEGER) === true);
|
||||
assert(Number.isSafeInteger(Number.MIN_SAFE_INTEGER) === true);
|
||||
|
||||
assert(Number.isSafeInteger() === false);
|
||||
assert(Number.isSafeInteger("1") === false);
|
||||
assert(Number.isSafeInteger(2.1) === false);
|
||||
assert(Number.isSafeInteger(42.42) === false);
|
||||
assert(Number.isSafeInteger("") === false);
|
||||
assert(Number.isSafeInteger([]) === false);
|
||||
assert(Number.isSafeInteger(null) === false);
|
||||
assert(Number.isSafeInteger(undefined) === false);
|
||||
assert(Number.isSafeInteger(NaN) === false);
|
||||
assert(Number.isSafeInteger(Infinity) === false);
|
||||
assert(Number.isSafeInteger(-Infinity) === false);
|
||||
assert(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1) === false);
|
||||
assert(Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
39
Libraries/LibJS/Tests/builtins/Number/Number.js
Normal file
39
Libraries/LibJS/Tests/builtins/Number/Number.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Number.length === 1);
|
||||
assert(Number.name === "Number");
|
||||
assert(Number.prototype.length === undefined);
|
||||
|
||||
assert(typeof Number() === "number");
|
||||
assert(typeof new Number() === "object");
|
||||
|
||||
assert(Number() === 0);
|
||||
assert(new Number().valueOf() === 0);
|
||||
assert(Number("42") === 42);
|
||||
assert(new Number("42").valueOf() === 42);
|
||||
assert(Number(null) === 0);
|
||||
assert(new Number(null).valueOf() === 0);
|
||||
assert(Number(true) === 1);
|
||||
assert(new Number(true).valueOf() === 1);
|
||||
assert(Number("Infinity") === Infinity);
|
||||
assert(new Number("Infinity").valueOf() === Infinity);
|
||||
assert(Number("+Infinity") === Infinity);
|
||||
assert(new Number("+Infinity").valueOf() === Infinity);
|
||||
assert(Number("-Infinity") === -Infinity);
|
||||
assert(new Number("-Infinity").valueOf() === -Infinity);
|
||||
assert(isNaN(Number(undefined)));
|
||||
assert(isNaN(new Number(undefined).valueOf()));
|
||||
assert(isNaN(Number({})));
|
||||
assert(isNaN(new Number({}).valueOf()));
|
||||
assert(isNaN(Number({a: 1})));
|
||||
assert(isNaN(new Number({a: 1}).valueOf()));
|
||||
assert(isNaN(Number([1, 2, 3])));
|
||||
assert(isNaN(new Number([1, 2, 3]).valueOf()));
|
||||
assert(isNaN(Number("foo")));
|
||||
assert(isNaN(new Number("foo").valueOf()));
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e.message);
|
||||
}
|
11
Libraries/LibJS/Tests/builtins/Number/Number.parseFloat.js
Normal file
11
Libraries/LibJS/Tests/builtins/Number/Number.parseFloat.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
// Ensuring it's the same function as the global
|
||||
// parseFloat() is enough as that already has tests :^)
|
||||
assert(Number.parseFloat === parseFloat);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue