Skip to content

pref: increase linked-list test coverage #3

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 3 commits into from
Jan 7, 2019
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ node_js:
- '10'
- '11'
- '8'
- '6'
script:
- npm run test:prod && npm run build
after_success:
Expand Down
61 changes: 53 additions & 8 deletions src/linked-list/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,20 @@ describe('LinkedList class', () => {
})

describe('helper functions', () => {
it('should stringify list correctly', () => {
it('Method toString()', () => {
expect(linkedList.toString()).toBe('')

linkedList.append(1)
expect(linkedList.toString()).toBe('1')

linkedList.append(2).append(3)
expect(linkedList.toString()).toBe('1,2,3')

const stringHandler = (value: any) => value;
expect(linkedList.toString(stringHandler)).toBe('1,2,3');
})

it('should toArray correctly', () => {
it('Method toArray()', () => {
expect(linkedList.toArray()).toEqual([])

linkedList.append(1)
Expand Down Expand Up @@ -147,6 +150,10 @@ describe('LinkedList class', () => {
expect(linkedList.delete).toBeInstanceOf(Function)
})

it("should return null if the list is empty()", () => {
expect(linkedList.delete(1)).toBe(null);
})

it('should delete nothing if not found value', () => {
linkedList
.append(1)
Expand Down Expand Up @@ -192,23 +199,31 @@ describe('LinkedList class', () => {

describe('Method deleteHead()', () => {
it('should delete head node and handle head & tail correctly', () => {
linkedList.append(1).deleteHead()

expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)

linkedList
.append(1)
.append(2)
.append(3)
.append(4)
.append(5)
linkedList.deleteHead()
expect((linkedList.deleteHead() as Node).value).toBe(1)
expect(linkedList.toString()).toBe('2,3,4,5')

expect((linkedList.head as Node).value).toBe(2)
expect((linkedList.tail as Node).value).toBe(5)
})

it("should set head & tail correctly when list have only one node", () => {
linkedList.append(1).deleteHead()

expect(linkedList.head).toBe(null)
expect(linkedList.tail).toBe(null)
})

it("should return null if the list is empty()", () => {
expect(linkedList.deleteHead()).toBe(null);
})


})

describe('Method deleteTail()', () => {
Expand All @@ -229,6 +244,13 @@ describe('LinkedList class', () => {
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(4)
})

it("should return null when list is empty", () => {
expect(linkedList.deleteTail()).toBe(null);
linkedList.append(1)
linkedList.append(2)
expect((linkedList.deleteHead() as Node).value).toBe(1);
})
})

describe('Method isEmpty()', () => {
Expand All @@ -250,6 +272,7 @@ describe('LinkedList class', () => {
})

describe('Method insert()', () => {

it('should insert value after specified value by default', () => {
linkedList
.append(1)
Expand Down Expand Up @@ -301,5 +324,27 @@ describe('LinkedList class', () => {
expect((linkedList.head as Node).value).toBe(1)
expect((linkedList.tail as Node).value).toBe(3)
})

it('should do nothing shen targetValue not in the list', () => {
linkedList
.append(1)
.append(2)
.append(3)
linkedList.insert(5, 4, 'before')

expect(linkedList.toString()).toBe('1,2,3')
})

it("should do nothing when list empty", () => {
linkedList.insert(5, 3)
expect(linkedList.toString()).toBe('')
expect(linkedList.head) .toBe(null)
expect(linkedList.tail).toBe(null)

linkedList.insert(5, 3, "before")
expect(linkedList.toString()).toBe('')
expect(linkedList.head) .toBe(null)
expect(linkedList.tail).toBe(null)
})
})
})
18 changes: 7 additions & 11 deletions src/linked-list/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Node } from './Node'

const stringifyDefault = (value: any) => {
return `${value}`
}

export class LinkedList {
public head: Node | null
public tail: Node | null
Expand Down Expand Up @@ -76,10 +80,6 @@ export class LinkedList {
targetValue: any,
direction: 'before' | 'after' = 'after'
): LinkedList | undefined {
if (!value || !targetValue) {
return
}

if (direction === 'after') {
let currentNode = this.head

Expand Down Expand Up @@ -172,11 +172,11 @@ export class LinkedList {

if (headNode !== null) {
deletedNode = headNode
if (headNode === this.tail) {
if (this.head === this.tail) {
this.head = null
this.tail = null
} else {
this.head = headNode.next ? headNode.next : null
this.head = headNode.next;
}
}

Expand Down Expand Up @@ -231,11 +231,7 @@ export class LinkedList {
return result
}

public toString(
handler: (value: any) => string = value => {
return `${value}`
}
): string {
public toString( handler: (value: any) => string = stringifyDefault ): string {
return this.toArray()
.map(value => {
return handler(value)
Expand Down