mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 12:48:10 +00:00
LibJS: Add parseFloat()
This commit is contained in:
parent
cc42d75209
commit
6f6b089aa0
3 changed files with 87 additions and 0 deletions
71
Libraries/LibJS/Tests/parseFloat.js
Normal file
71
Libraries/LibJS/Tests/parseFloat.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
const NUMBER_TEST_CASES = [
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[.23, 0.23],
|
||||
[1.23, 1.23],
|
||||
[0.0123E+2, 1.23],
|
||||
[1.23e4, 12300],
|
||||
[Infinity, Infinity]
|
||||
];
|
||||
|
||||
NUMBER_TEST_CASES.forEach(test => {
|
||||
const value = test[0];
|
||||
const result = test[1];
|
||||
assert(parseFloat(value) === result);
|
||||
assert(parseFloat(+value) === result);
|
||||
assert(parseFloat(-value) === -result);
|
||||
});
|
||||
|
||||
const STRING_TEST_CASES = [
|
||||
["0", 0],
|
||||
["1", 1],
|
||||
[".23", 0.23],
|
||||
["1.23", 1.23],
|
||||
["0.0123E+2", 1.23],
|
||||
["1.23e4", 12300],
|
||||
["Infinity", Infinity]
|
||||
];
|
||||
|
||||
STRING_TEST_CASES.forEach(test => {
|
||||
const value = test[0];
|
||||
const result = test[1];
|
||||
assert(parseFloat(value) === result);
|
||||
assert(parseFloat(`+${value}`) === result);
|
||||
assert(parseFloat(`-${value}`) === -result);
|
||||
assert(parseFloat(`${value}foo`) === result);
|
||||
assert(parseFloat(`+${value}foo`) === result);
|
||||
assert(parseFloat(`-${value}foo`) === -result);
|
||||
assert(parseFloat(` \n \t ${value} \v foo `) === result);
|
||||
assert(parseFloat(` \r -${value} \f \n\n foo `) === -result);
|
||||
assert(parseFloat({ toString: () => value }) === result);
|
||||
});
|
||||
|
||||
const NAN_TEST_CASES = [
|
||||
"",
|
||||
[],
|
||||
[],
|
||||
true,
|
||||
false,
|
||||
null,
|
||||
undefined,
|
||||
NaN,
|
||||
"foo123",
|
||||
"foo+123",
|
||||
"fooInfinity",
|
||||
"foo+Infinity"
|
||||
];
|
||||
|
||||
assert(isNaN(parseFloat()));
|
||||
assert(isNaN(parseFloat("", 123, Infinity)));
|
||||
|
||||
NAN_TEST_CASES.forEach(value => {
|
||||
assert(isNaN(parseFloat(value)));
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue