Add tests for more precise Array#sort

https://github.com/tc39/ecma262/pull/1585
This commit is contained in:
Mathias Bynens 2021-01-22 13:06:47 +01:00 committed by Rick Waldron
parent a840b0d093
commit 82f634d7fc
11 changed files with 454 additions and 0 deletions

View File

@ -0,0 +1,27 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
Object.prototype[2] = 4;
const array = [undefined, 3, /*hole*/, 2, undefined, /*hole*/, 1];
array.sort();
assert.sameValue(array[0], 1);
assert.sameValue(array[1], 2);
assert.sameValue(array[2], 3);
assert.sameValue(array[3], 4);
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue('6' in array, false);
assert.sameValue(array.hasOwnProperty('6'), false);
assert.sameValue(array.length, 7);

View File

@ -0,0 +1,42 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const logs = [];
Object.defineProperty(Object.prototype, '2', {
get() {
logs.push('get');
return 4;
},
set(v) {
logs.push(`set with ${v}`);
}
});
const array = [undefined, 3, /*hole*/, 2, undefined, /*hole*/, 1];
array.sort();
assert.sameValue(logs[0], 'get');
assert.sameValue(logs[1], 'set with 3');
assert.sameValue(logs.length, 2);
assert.sameValue(array[0], 1);
assert.sameValue(array[1], 2);
assert.sameValue('2' in array, true);
assert.sameValue(array.hasOwnProperty('2'), false);
assert.sameValue(array[3], 4);
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue('6' in array, false);
assert.sameValue(array.hasOwnProperty('6'), false);
assert.sameValue(array.length, 7);

View File

@ -0,0 +1,52 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const logs = [];
Object.defineProperty(Object.prototype, '2', {
get() {
logs.push('get');
return 4;
},
set(v) {
logs.push(`set with ${v}`);
}
});
const array = [undefined, 3, /*hole*/, 2, undefined, /*hole*/, 1];
let count = 0;
try {
array.sort((a, b) => {
if (++count === 3) {
throw new Error('lolwat');
}
return b - a;
});
} catch (exception) {
logs.push(exception.message);
}
assert.sameValue(logs[0], 'get');
assert.sameValue(logs[1], 'lolwat');
assert.sameValue(logs.length, 2);
assert.sameValue(array[0], undefined);
assert.sameValue(array[1], 3);
assert.sameValue('2' in array, true);
assert.sameValue(array.hasOwnProperty('2'), false);
assert.sameValue(array[3], 2);
assert.sameValue(array[4], undefined);
assert.sameValue('5' in array, false);
assert.sameValue(array.hasOwnProperty('5'), false);
assert.sameValue(array[6], 1);
assert.sameValue(array.length, 7);

View File

@ -0,0 +1,49 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
array.push('foo');
array.push('bar');
return this.foo;
},
set(v) {
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'b');
assert.sameValue('2' in array, true);
assert.sameValue(array.hasOwnProperty('2'), true);
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array[8], 'foo');
assert.sameValue(array[9], 'bar');
assert.sameValue(array.length, 10);
assert.sameValue(array.foo, 'c');
assert.sameValue(array[2], 'c');
assert.sameValue(array[10], 'foo');
assert.sameValue(array[11], 'bar');
assert.sameValue(array.length, 12);
assert.sameValue(array.foo, 'c');

View File

@ -0,0 +1,42 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
return this.foo;
},
set(v) {
array.push('foo');
array.push('bar');
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'b');
assert.sameValue(array[2], 'c');
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array[8], 'foo');
assert.sameValue(array[9], 'bar');
assert.sameValue(array.length, 10);
assert.sameValue(array.foo, 'c');

View File

@ -0,0 +1,39 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
array.length = array.length - 2;
return this.foo;
},
set(v) {
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'b');
assert.sameValue(array[1], 'c');
assert.sameValue(array[3], undefined);
assert.sameValue(array[4], undefined);
assert.sameValue('5' in array, false);
assert.sameValue(array.hasOwnProperty('5'), false);
assert.sameValue(array.length, 6);
assert.sameValue(array.foo, undefined);
assert.sameValue(array[2], undefined);
assert.sameValue(array.length, 4);

View File

@ -0,0 +1,37 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
return this.foo;
},
set(v) {
array.length = array.length - 2;
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'b');
assert.sameValue(array[2], 'c');
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue(array.length, 7);
assert.sameValue(array.foo, 'c');

View File

@ -0,0 +1,43 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
delete array[1];
return this.foo;
},
set(v) {
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'b');
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array.length, 8);
assert.sameValue(array.foo, 'c');
assert.sameValue(array[2], 'c');
assert.sameValue('1' in array, false);
assert.sameValue(array.hasOwnProperty(1), false);
assert.sameValue(array.length, 8);

View File

@ -0,0 +1,40 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
return this.foo;
},
set(v) {
delete array[1];
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue('1' in array, false);
assert.sameValue(array.hasOwnProperty(1), false);
assert.sameValue(array[2], 'c');
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array.length, 8);
assert.sameValue(array.foo, 'c');

View File

@ -0,0 +1,44 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
delete array[3];
return this.foo;
},
set(v) {
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'c');
assert.sameValue(array[3], undefined);
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue('6' in array, false);
assert.sameValue(array.hasOwnProperty('6'), false);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array.length, 8);
assert.sameValue(array.foo, 'd');
assert.sameValue(array[2], 'd');
assert.sameValue('3' in array, false);
assert.sameValue(array.hasOwnProperty(3), false);
assert.sameValue(array.length, 8);

View File

@ -0,0 +1,39 @@
// Copyright (C) 2021 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.sort
description: >
Previously implementation-defined aspects of Array.prototype.sort.
info: |
Historically, many aspects of Array.prototype.sort remained
implementation-defined. https://github.com/tc39/ecma262/pull/1585
described some behaviors more precisely, reducing the amount of cases
that result in an implementation-defined sort order.
---*/
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() {
return this.foo;
},
set(v) {
delete array[3];
this.foo = v;
}
});
array.sort();
assert.sameValue(array[0], 'a');
assert.sameValue(array[1], 'b');
assert.sameValue(array[2], 'c');
assert.sameValue(array[3], 'd');
assert.sameValue(array[4], undefined);
assert.sameValue(array[5], undefined);
assert.sameValue(array[6], undefined);
assert.sameValue('7' in array, false);
assert.sameValue(array.hasOwnProperty('7'), false);
assert.sameValue(array.length, 8);
assert.sameValue(array.foo, 'c');