Skip to content

Commit 70a75d2

Browse files
LiviaMedeirosaduh95
authored andcommitted
assert: support Float16Array in loose deep equality checks
PR-URL: #57881 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent d42935b commit 70a75d2

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/internal/util/comparisons.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
isBooleanObject,
4444
isBigIntObject,
4545
isSymbolObject,
46+
isFloat16Array,
4647
isFloat32Array,
4748
isFloat64Array,
4849
isKeyObject,
@@ -245,7 +246,8 @@ function innerDeepEqual(val1, val2, mode, memos) {
245246
if (!isPartialArrayBufferView(val1, val2)) {
246247
return false;
247248
}
248-
} else if (mode === kLoose && (isFloat32Array(val1) || isFloat64Array(val1))) {
249+
} else if (mode === kLoose &&
250+
(isFloat32Array(val1) || isFloat64Array(val1) || isFloat16Array(val1))) {
249251
if (!areSimilarFloatArrays(val1, val2)) {
250252
return false;
251253
}

test/parallel/test-assert-partial-deep-equal.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// Flags: --js-float16array
2+
// TODO(LiviaMedeiros): once `Float16Array` is unflagged in v8, remove the line above
13
'use strict';
24

35
const common = require('../common');
46
const vm = require('node:vm');
57
const assert = require('node:assert');
68
const { describe, it } = require('node:test');
79

10+
// TODO(LiviaMedeiros): once linter recognizes `Float16Array`, remove next line
11+
const { Float16Array } = globalThis;
12+
813
const x = ['x'];
914

1015
function createCircularObject() {
@@ -484,6 +489,11 @@ describe('Object Comparison Tests', () => {
484489
actual: { dataView: new Uint8Array(3) },
485490
expected: { dataView: new DataView(new ArrayBuffer(3)) },
486491
},
492+
{
493+
description: 'throws when comparing Float16Array([+0.0]) with Float16Array([-0.0])',
494+
actual: new Float16Array([+0.0]),
495+
expected: new Float16Array([-0.0]),
496+
},
487497
{
488498
description: 'throws when comparing Float32Array([+0.0]) with Float32Array([-0.0])',
489499
actual: new Float32Array([+0.0]),

test/parallel/test-assert-typedarray-deepequal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
// Flags: --js-float16array
2+
// TODO(LiviaMedeiros): once `Float16Array` is unflagged in v8, remove the line above
13
'use strict';
24

35
require('../common');
46
const assert = require('assert');
57
const { test, suite } = require('node:test');
68

9+
// TODO(LiviaMedeiros): once linter recognizes `Float16Array`, remove next line
10+
const { Float16Array } = globalThis;
11+
712
function makeBlock(f) {
813
const args = Array.prototype.slice.call(arguments, 1);
914
return function() {
@@ -20,6 +25,7 @@ suite('equalArrayPairs', () => {
2025
[new Int8Array(1e5), new Int8Array(1e5)],
2126
[new Int16Array(1e5), new Int16Array(1e5)],
2227
[new Int32Array(1e5), new Int32Array(1e5)],
28+
[new Float16Array(1e5), new Float16Array(1e5)],
2329
[new Float32Array(1e5), new Float32Array(1e5)],
2430
[new Float64Array(1e5), new Float64Array(1e5)],
2531
[new Float32Array([+0.0]), new Float32Array([+0.0])],
@@ -41,6 +47,7 @@ suite('equalArrayPairs', () => {
4147

4248
suite('looseEqualArrayPairs', () => {
4349
const looseEqualArrayPairs = [
50+
[new Float16Array([+0.0]), new Float16Array([-0.0])],
4451
[new Float32Array([+0.0]), new Float32Array([-0.0])],
4552
[new Float64Array([+0.0]), new Float64Array([-0.0])],
4653
];
@@ -71,6 +78,8 @@ suite('notEqualArrayPairs', () => {
7178
[new Int16Array([0]), new Uint16Array([256])],
7279
[new Int16Array([-256]), new Uint16Array([0xff00])], // same bits
7380
[new Int32Array([-256]), new Uint32Array([0xffffff00])], // ditto
81+
[new Float16Array([0.1]), new Float16Array([0.0])],
82+
[new Float16Array([0.1]), new Float16Array([0.1, 0.2])],
7483
[new Float32Array([0.1]), new Float32Array([0.0])],
7584
[new Float32Array([0.1]), new Float32Array([0.1, 0.2])],
7685
[new Float64Array([0.1]), new Float64Array([0.0])],

0 commit comments

Comments
 (0)