Skip to content

Instantly share code, notes, and snippets.

@szuend
Created September 19, 2019 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szuend/05ae15b4e1329b264ab4c9a1cda09242 to your computer and use it in GitHub Desktop.
Save szuend/05ae15b4e1329b264ab4c9a1cda09242 to your computer and use it in GitHub Desktop.
Array#sort spec change: various test cases
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let 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();
log(array);
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();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let 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();
log(array);
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();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let 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();
log(array);
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();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let 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();
log(array);
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();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let 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();
log(array);
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();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { array.pop(); array.pop(); return this.foo; },
set(v) { this.foo = v; }
});
array.sort();
log(array);
array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { return this.foo; },
set(v) { array.pop(); array.pop(); this.foo = v; }
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { array[1] = 'foobar'; return this.foo; },
set(v) { this.foo = v; }
});
array.sort();
log(array);
array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { return this.foo; },
set(v) { array[1] = 'foobar'; this.foo = v; }
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
let array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { array[3] = 'foobar'; return this.foo; },
set(v) { this.foo = v; }
});
array.sort();
log(array);
array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { return this.foo; },
set(v) { array[3] = 'foobar'; this.foo = v; }
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '2', {
get() { print('get [2]'); return this.foo; },
set(v) { print(`set [2] with ${v}`); this.foo = v; }
});
Object.defineProperty(array, '3', {
get() { print('get [3]'); return this.bar; },
set(v) { print(`set [3] with ${v}`); this.bar = v; }
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
const object = {};
Object.defineProperty(object, '2', {
get() { print('get [2]'); return this.foo; },
set(v) { print(`set [2] with ${v}`); this.foo = v; }
});
Object.defineProperty(object, '3', {
get() { print('get [3]'); return this.bar; },
set(v) { print(`set [3] with ${v}`); this.bar = v; }
});
array.__proto__ = object;
Array.prototype.sort.call(array);
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '6', {
value: 'a',
configurable: false,
enumerable: true,
writable: true,
});
Object.defineProperty(array, '7', {
value: 'd',
configurable: false,
enumerable: true,
writable: true,
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
Object.defineProperty(Object.prototype, '2', {
value: 'foo',
configurable: false,
enumerable: true,
writable: true,
});
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
Object.defineProperty(array, '1', {
value: 'c',
configurable: true,
enumerable: true,
writable: false,
});
Object.defineProperty(array, '3', {
value: 'b',
configurable: true,
enumerable: true,
writable: false,
});
array.sort();
log(array);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
Object.defineProperty(Object.prototype, '2', {
value: 'foo',
configurable: true,
enumerable: true,
writable: false,
});
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
array.sort();
log(array);
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
const proxy = new Proxy({}, {
get: (target, name) => {
print("get ['" + name + "']");
return target[name];
},
set: (target, name, value) => {
print("set ['" + name + "'] = " + value);
target[name] = value;
return true;
},
has: (target, name) => {
print("has ['" + name + "']");
return name in target;
},
deleteProperty: (target, name) => {
print("delete ['" + name + "']");
return delete target[name];
}
});
array.__proto__ = proxy;
Array.prototype.sort.call(array);
const array = [undefined, 'c', /*hole*/, 'b', undefined, /*hole*/, 'a', 'd'];
const proxy = new Proxy(array, {
get: (target, name) => {
print("get ['" + name + "']");
return target[name];
},
set: (target, name, value) => {
print("set ['" + name + "'] = " + value);
target[name] = value;
return true;
},
has: (target, name) => {
print("has ['" + name + "']");
return name in target;
},
deleteProperty: (target, name) => {
print("delete ['" + name + "']");
return delete target[name];
}
});
Array.prototype.sort.call(proxy);
function log(array) {
let buf = '';
for (let index = 0; index < array.length; index++) {
if (array.hasOwnProperty(index)) {
buf += String(array[index]);
} else {
buf += 'hole';
}
if (index < array.length - 1) buf += ',';
}
print(buf);
}
const array = [];
Object.defineProperty(array, '0', {
get() { print('get [0]'); return this.foo; },
set(v) { print(`set [0] with ${v}`); this.foo = v; }
});
print("0-length array:");
array.sort();
log(array);
print("1-length array:");
array.push('bar');
array.sort();
log(array);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment