Skip to content

assert: support Float16Array in loose deep equality checks #57881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const {
isBooleanObject,
isBigIntObject,
isSymbolObject,
isFloat16Array,
isFloat32Array,
isFloat64Array,
isKeyObject,
Expand Down Expand Up @@ -245,7 +246,8 @@ function objectComparisonStart(val1, val2, mode, memos) {
if (!isPartialArrayBufferView(val1, val2)) {
return false;
}
} else if (mode === kLoose && (isFloat32Array(val1) || isFloat64Array(val1))) {
} else if (mode === kLoose &&
(isFloat32Array(val1) || isFloat64Array(val1) || isFloat16Array(val1))) {
if (!areSimilarFloatArrays(val1, val2)) {
return false;
}
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-assert-partial-deep-equal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
// Flags: --js-float16array
// TODO(LiviaMedeiros): once `Float16Array` is unflagged in v8, remove the line above
'use strict';

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

// TODO(LiviaMedeiros): once linter recognizes `Float16Array`, remove next line
const { Float16Array } = globalThis;

const x = ['x'];

function createCircularObject() {
Expand Down Expand Up @@ -494,6 +499,11 @@ describe('Object Comparison Tests', () => {
actual: { dataView: new Uint8Array(3) },
expected: { dataView: new DataView(new ArrayBuffer(3)) },
},
{
description: 'throws when comparing Float16Array([+0.0]) with Float16Array([-0.0])',
actual: new Float16Array([+0.0]),
expected: new Float16Array([-0.0]),
},
{
description: 'throws when comparing Float32Array([+0.0]) with Float32Array([-0.0])',
actual: new Float32Array([+0.0]),
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-assert-typedarray-deepequal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Flags: --js-float16array
// TODO(LiviaMedeiros): once `Float16Array` is unflagged in v8, remove the line above
'use strict';

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

// TODO(LiviaMedeiros): once linter recognizes `Float16Array`, remove next line
const { Float16Array } = globalThis;

function makeBlock(f) {
const args = Array.prototype.slice.call(arguments, 1);
return function() {
Expand All @@ -20,6 +25,7 @@ suite('equalArrayPairs', () => {
[new Int8Array(1e5), new Int8Array(1e5)],
[new Int16Array(1e5), new Int16Array(1e5)],
[new Int32Array(1e5), new Int32Array(1e5)],
[new Float16Array(1e5), new Float16Array(1e5)],
[new Float32Array(1e5), new Float32Array(1e5)],
[new Float64Array(1e5), new Float64Array(1e5)],
[new Float32Array([+0.0]), new Float32Array([+0.0])],
Expand All @@ -41,6 +47,7 @@ suite('equalArrayPairs', () => {

suite('looseEqualArrayPairs', () => {
const looseEqualArrayPairs = [
[new Float16Array([+0.0]), new Float16Array([-0.0])],
[new Float32Array([+0.0]), new Float32Array([-0.0])],
[new Float64Array([+0.0]), new Float64Array([-0.0])],
];
Expand Down Expand Up @@ -71,6 +78,8 @@ suite('notEqualArrayPairs', () => {
[new Int16Array([0]), new Uint16Array([256])],
[new Int16Array([-256]), new Uint16Array([0xff00])], // same bits
[new Int32Array([-256]), new Uint32Array([0xffffff00])], // ditto
[new Float16Array([0.1]), new Float16Array([0.0])],
[new Float16Array([0.1]), new Float16Array([0.1, 0.2])],
[new Float32Array([0.1]), new Float32Array([0.0])],
[new Float32Array([0.1]), new Float32Array([0.1, 0.2])],
[new Float64Array([0.1]), new Float64Array([0.0])],
Expand Down
Loading