Skip to content

Reverse tdd #9

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lab-davidw/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/node_modules/*
**/vendor/*
**/*.min.js
25 changes: 25 additions & 0 deletions lab-davidw/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rules": {
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"no-console": "off",
"indent": [ "error", 2 ],
"semi": ["error", "always"]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"globals": {
"__API_URI__": false,
"__DEBUG__": false
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
65 changes: 65 additions & 0 deletions lab-davidw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Created by https://www.gitignore.io/api/osx,linux,node,vim

### OSX ###
.DS_Store
.AppleDouble
.LSOverride

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

### Vim ###
# swap
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# session
Session.vim
# temporary
.netrwhist
*~

# auto-generated tag files
tags
24 changes: 24 additions & 0 deletions lab-davidw/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# lab 10 Single Linked Lists - Sorting
----
## Goal
Write out methods for creating and sorting singly linked lists. Prove they work with tests.
- personal goal TDD

## Setup
- You will will need node packet manager.
- ``` npm init ```
- ``` npm i -D mocha chai ```

## Usage
- the only relevant command is mocha:
```npm run test ```

## Expected
- You will see test output. Three test cases, at least, for each method:
- append
- prepend
- reverse
- remove

### Attributions
I got stuck and took a look at Glen Pham's repository to compare my reverse and remove methods to another student's. His was very similar, but I did note some differences which could have been bugs, and changed my code to be more like his.
6 changes: 6 additions & 0 deletions lab-davidw/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function(value) {
this.val = value;
this.next = null;
};
27 changes: 27 additions & 0 deletions lab-davidw/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "lab-davidw",
"version": "0.0.1",
"description": "singly linked list exercise",
"main": "sll.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha",
"lint": "eslint ./.eslintrc"
},
"repository": {
"type": "git",
"url": "https://github.com/DavidGWheeler/lab-10-career-development/tree/scaffold/lab-davidw"
},
"keywords": [
"linked",
"lists"
],
"author": "DavidGWheeler",
"license": "MIT",
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^3.3.0"
}
}
76 changes: 76 additions & 0 deletions lab-davidw/sll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const Node = require('./node');

const SLL = module.exports = function() {
this.head = null;
};

// O(n)
SLL.prototype.prepend = function (val) {
let node = new Node(val);

if(!this.head) {
this.head = node;
return this;
}

node.next = this.head;
this.head = node;
return this;
};

//O(n)
SLL.prototype.append = function(val) {
let node = new Node(val);
let lastNode = null;

if(!this.head) {
this.head = node;
return this;
}

_setLastNode(this.head);
lastNode.next = node;
return this;

function _setLastNode(node) {
if(!node) return;
lastNode = node;
_setLastNode(node.next);
}
};

//O(n)
SLL.prototype.reverse = function() {
let current = this.head;
let previous = null;
let next;

while(current) {
next = current.next;
current.next = previous;
previous = current;
current = next;
}

this.head = previous;
};


SLL.prototype.remove = function(val) {
if(this.head.val === val) {
this.head = this.head.next;
return;
}

let previous = null;
let current = this.head;

while(current.val !== val) {
previous = current;
current = current.next;
}

previous.next = current.next;
};
145 changes: 145 additions & 0 deletions lab-davidw/test/sll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
'use strict';

const expect = require('chai').expect;
const SLL = require('../sll');


describe('#SLL method tests', function() {
describe('linked list creation', function() {
let sll = new SLL();
it('should create a new linked list object', done => {
expect(sll).to.be.an('object')
.that.has.property('head')
.that.equals(null);
done();
});

describe('#append method', function() {
it('should not contain the value before appending', done => {
expect(sll).to.have.property('head')
.that.equals(null);
done();
});

it('should add a new node at the end of the list', done => {
sll.append(2);
expect(sll.head).to.be.an('object')
.that.has.property('val')
.that.equals(2);
done();
});

it('should add another node', done => {
sll.append(3);
expect(sll.head.next).to.be.an('object')
.that.has.property('val')
.that.equals(3);
done();
});
});

describe('#prepend method', function() {
let sll = new SLL();
it('should not contain a value before prepending', done => {
expect(sll).to.have.property('head')
.that.equals(null);
done();
});

it('should add a new node to the beginning of the list', done => {
sll.prepend(2);
expect(sll.head).to.be.an('object')
.that.has.property('val')
.that.equals(2);
done();
});

it('should add another node', done => {
sll.prepend(2);
expect(sll.head.next).to.be.an('object')
.that.has.property('val')
.that.equals(2);
done();
});
});

describe('#reverse method', function() {
let revSll = new SLL().append(1);
revSll.append(2);
revSll.append(3);

it('should start with an order of 1, 2, 3', done => {
expect(revSll.head.next.next).to.be.an('object')
.that.has.property('next')
.that.equals(null);

expect(revSll.head.next.next).to.be.an('object')
.that.has.property('val')
.that.equals(3);

expect(revSll.head.next).to.be.an('object')
.that.has.property('val')
.that.equals(2);

expect(revSll.head).to.be.an('object')
.that.has.property('val')
.that.equals(1);
done();
});

it('should make new order become 3, 2, 1', function() {
revSll.reverse();
it('should now have an order of 3, 2, 1', done => {
expect(revSll.head.next.next).to.be.an('object')
.that.has.property('next')
.that.equals(null);

expect(revSll.head).to.be.an('object')
.that.has.property('val')
.that.equals(3);

expect(revSll.head.next).to.be.an('object')
.that.has.property('val')
.that.equals(2);

expect(revSll.head.next.next).to.be.an('object')
.that.has.property('val')
.that.equals(1);
done();
});
});
describe('#remove method', function() {
it('should start with values 1 and 2', done => {
let remSll = new SLL();
remSll.append(1);
remSll.append(2);
expect(remSll.head).to.be.an('object')
.that.has.property('val')
.that.equals(1);
done();
});
it('should have only a value of 2 after removing 1', done => {
let remSll = new SLL();
remSll.append(1);
remSll.append(2);
remSll.remove(1);
expect(remSll.head).to.be.an('object')
.that.has.property('val')
.that.equals(2);
done();
});
it('should have only a value of head and next = null after removing 1 and 2', done => {
let remSll = new SLL();
remSll.append(1);
remSll.append(2);
remSll.remove(1);
remSll.remove(2);
expect(remSll).to.be.an('object')
.that.has.property('head')
.that.equals(null);
done();
});
});
});
});
});