Skip to content

Commit

Permalink
feat: disallow non-instanced list concatenations
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Jul 5, 2021
1 parent ab69c44 commit 97a972e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 320 deletions.
12 changes: 11 additions & 1 deletion __tests__/CircularDoublyLinkedList.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'tape';

import { CircularDoublyLinkedList } from '../lib';
import { CircularDoublyLinkedList, CircularSinglyLinkedList } from '../lib';
import { Node } from '../lib/Atomics';

const subject = 'The circular doubly linked list';
Expand Down Expand Up @@ -313,6 +313,16 @@ test(`${subject} is not modified when invoking 'moveAfter', 'moveBefore' with a
t.end();
});

test(`${subject} throws an error if provided a list of a different type`, t => {
const l = init();
const l2 = new CircularSinglyLinkedList();

t.throws(() => l.pushBackList(l2));
t.throws(() => l.pushFrontList(l2));

t.end();
});

/* refHelpers */

function checkListSize (list, expectedSize) {
Expand Down
220 changes: 0 additions & 220 deletions __tests__/CircularRingList.test.js

This file was deleted.

12 changes: 11 additions & 1 deletion __tests__/CircularSinglyLinkedList.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'tape';

import { CircularSinglyLinkedList } from '../lib';
import { CircularSinglyLinkedList, CircularDoublyLinkedList } from '../lib';
import { ForwardNode } from '../lib/Atomics';

const subject = 'The circular singly linked list';
Expand Down Expand Up @@ -433,6 +433,16 @@ test(`${subject} maintains integrity when operating upon a single-node list`, t
t.end();
});

test(`${subject} throws an error if provided a list of a different type`, t => {
const l = init();
const l2 = new CircularDoublyLinkedList();

t.throws(() => l.pushBackList(l2));
t.throws(() => l.pushFrontList(l2));

t.end();
});

/* Helpers */

function checkListSize (list, expectedSize) {
Expand Down
6 changes: 4 additions & 2 deletions lib/CircularDoublyLinkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,12 @@ export class CircularDoublyLinkedList {
*
* The lists may be the same, but must not be null
* @param {CircularDoublyLinkedList} other
* @throws {TypeError} Throws if provided a list that is not an instance of `CircularDoublyLinkedList`
*/
pushBackList (other) {
if (!this.sentinel.next) {
Object.assign(this, new CircularDoublyLinkedList());
}
} else if (!(other instanceof CircularDoublyLinkedList)) throw new TypeError('other must be an instance of `CircularDoublyLinkedList`');

for (let i = other.size(), n = other.head(); i > 0; i--, n = other.next(n)) {
this.insertValue(n.value, this.sentinel.prev);
Expand All @@ -315,11 +316,12 @@ export class CircularDoublyLinkedList {
*
* The lists may be the same, but must not be null
* @param {CircularDoublyLinkedList} other
* @throws {TypeError} Throws if provided a list that is not an instance of `CircularDoublyLinkedList`
*/
pushFrontList (other) {
if (!this.sentinel.next) {
Object.assign(this, new CircularDoublyLinkedList());
}
} else if (!(other instanceof CircularDoublyLinkedList)) throw new TypeError('other must be an instance of `CircularDoublyLinkedList`');

for (let i = other.size(), n = other.tail(); i > 0; i--, n = other.prev(n)) {
this.insertValue(n.value, this.sentinel);
Expand Down
96 changes: 0 additions & 96 deletions lib/CircularRingList.js

This file was deleted.

Loading

0 comments on commit 97a972e

Please sign in to comment.