From 338e4964cfb194ea889dcf15b0099093d982b709 Mon Sep 17 00:00:00 2001 From: Neelesh Roy Date: Sat, 5 May 2018 12:39:20 +0530 Subject: [PATCH 1/4] TESTBED: setRandomData() --- src/utils/ArrayTestBed.js | 30 ++++++++++++++++++++++++ test/ArrayTestBed.test.js | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/utils/ArrayTestBed.js create mode 100644 test/ArrayTestBed.test.js diff --git a/src/utils/ArrayTestBed.js b/src/utils/ArrayTestBed.js new file mode 100644 index 0000000..b04ccf1 --- /dev/null +++ b/src/utils/ArrayTestBed.js @@ -0,0 +1,30 @@ +export class ArrayTestBed { + constructor(numElements) { + this.dataStore = []; + this.pos = 0; + this.numElements = numElements; + } + + setRandomData() { + for (let i = 0; i < this.numElements; i++) { + this.dataStore[i] = Math.floor(Math.random() * + Math.pow(10, this.numElements.toString().length - 1) + 1); + } + } + + setRandomUniqueData() { + let i = 0; + while (i < this.numElements) { + const number = Math.floor(Math.random() * + Math.pow(10, this.numElements.toString().length) - 1) + 1; + if (this.dataStore.indexOf(number) > -1) { + this.dataStore[i] = number; + ++i; + } + } + } + + getNumbers() { + return this.dataStore; + } +} diff --git a/test/ArrayTestBed.test.js b/test/ArrayTestBed.test.js new file mode 100644 index 0000000..82cf620 --- /dev/null +++ b/test/ArrayTestBed.test.js @@ -0,0 +1,49 @@ +/** + * sorting-js + * + * Copyright © 2018 Neelesh Roy. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE.txt file in the root directory of this source tree. + */ + +import { expect } from 'chai'; +import { ArrayTestBed } from '../src/utils/ArrayTestBed'; + +describe('ArrayTestBed', () => { + + describe('ArrayTestBed - class and properties', () => { + + it('Should have the properties', () => { + const test = new ArrayTestBed(100); + + expect(test.dataStore).to.exist; + expect(test.pos).to.exist; + expect(test.numElements).to.exist; + + expect(test.dataStore).to.eql([]); + expect(test.pos).to.eql(0); + expect(test.numElements).to.eql(100); + }); + + }); + + describe('ArrayTestBed - setRandomData()', () => { + + it('Should not make any change in datastore', () => { + const test = new ArrayTestBed(0); + test.setRandomData(); + + expect(test.dataStore).to.eql([]); + }); + + it('Should set random data in datastore', () => { + const test = new ArrayTestBed(1000); + test.setRandomData(); + + expect(test.dataStore.length).to.eql(1000); + }); + + }); + +}); From a6639fc031673c00700f44993edf127e46926b5c Mon Sep 17 00:00:00 2001 From: Neelesh Roy Date: Sat, 5 May 2018 13:03:28 +0530 Subject: [PATCH 2/4] TESTBED: included unique numbers feature --- src/utils/ArrayTestBed.js | 28 ++++++++++++++-------------- test/ArrayTestBed.test.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/utils/ArrayTestBed.js b/src/utils/ArrayTestBed.js index b04ccf1..d9abaa8 100644 --- a/src/utils/ArrayTestBed.js +++ b/src/utils/ArrayTestBed.js @@ -5,26 +5,26 @@ export class ArrayTestBed { this.numElements = numElements; } - setRandomData() { + setData(random = true) { for (let i = 0; i < this.numElements; i++) { - this.dataStore[i] = Math.floor(Math.random() * - Math.pow(10, this.numElements.toString().length - 1) + 1); + let randomNumber = this.getRandomNumber(); + if (random) { + while(this.dataStore.indexOf(randomNumber) > -1) { + randomNumber = this.getRandomNumber(); + } + this.dataStore[i] = randomNumber; + } else { + this.dataStore[i] = randomNumber; + } } } - setRandomUniqueData() { - let i = 0; - while (i < this.numElements) { - const number = Math.floor(Math.random() * - Math.pow(10, this.numElements.toString().length) - 1) + 1; - if (this.dataStore.indexOf(number) > -1) { - this.dataStore[i] = number; - ++i; - } - } + getRandomNumber() { + return Math.floor(Math.random() * + Math.pow(10, this.numElements.toString().length - 1) + 1); } - getNumbers() { + getData() { return this.dataStore; } } diff --git a/test/ArrayTestBed.test.js b/test/ArrayTestBed.test.js index 82cf620..a3103e8 100644 --- a/test/ArrayTestBed.test.js +++ b/test/ArrayTestBed.test.js @@ -32,16 +32,42 @@ describe('ArrayTestBed', () => { it('Should not make any change in datastore', () => { const test = new ArrayTestBed(0); - test.setRandomData(); + test.setData(); expect(test.dataStore).to.eql([]); }); + it('Should set random unique data in datastore', () => { + const test = new ArrayTestBed(10); + test.setData(); + + expect(test.dataStore.length).to.eql(10); + }); + it('Should set random data in datastore', () => { - const test = new ArrayTestBed(1000); - test.setRandomData(); + const test = new ArrayTestBed(10); + test.setData(false); + console.log(test.dataStore); + expect(test.dataStore.length).to.eql(10); + }); + + }); + + describe('ArrayTestBed - getNumbers()', () => { + + it('Should return and empty array', () => { + const test = new ArrayTestBed(0); + const out = test.getData(); + + expect(out).to.eql([]); + }); - expect(test.dataStore.length).to.eql(1000); + it('Should get the random data from the datastore', () => { + const test = new ArrayTestBed(10); + test.setData(); + const out = test.getData(); + + expect(out.length).to.eql(10); }); }); From cb5c7b033888cf2ef6766c450ab837c1bc132dfc Mon Sep 17 00:00:00 2001 From: Neelesh Roy Date: Sat, 5 May 2018 15:11:39 +0530 Subject: [PATCH 3/4] TESTBED: complete --- src/utils/ArrayTestBed.js | 27 ++++++++++++++++++- test/ArrayTestBed.test.js | 57 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/utils/ArrayTestBed.js b/src/utils/ArrayTestBed.js index d9abaa8..42aae2c 100644 --- a/src/utils/ArrayTestBed.js +++ b/src/utils/ArrayTestBed.js @@ -9,7 +9,7 @@ export class ArrayTestBed { for (let i = 0; i < this.numElements; i++) { let randomNumber = this.getRandomNumber(); if (random) { - while(this.dataStore.indexOf(randomNumber) > -1) { + while (this.dataStore.indexOf(randomNumber) > -1) { randomNumber = this.getRandomNumber(); } this.dataStore[i] = randomNumber; @@ -27,4 +27,29 @@ export class ArrayTestBed { getData() { return this.dataStore; } + + insert(element) { + this.dataStore[this.pos++] = element; + } + + clear() { + this.dataStore = []; + this.pos = 0; + } + + toString() { + let returnStr = ''; + this.dataStore.forEach((value) => { + returnStr += `${value},`; + }); + return returnStr; + } + + swap(arr, index1, index2) { + const arrTemp = arr; + const temp = arrTemp[index1]; + arrTemp[index1] = arrTemp[index2]; + arrTemp[index2] = temp; + return arrTemp; + } } diff --git a/test/ArrayTestBed.test.js b/test/ArrayTestBed.test.js index a3103e8..aa66788 100644 --- a/test/ArrayTestBed.test.js +++ b/test/ArrayTestBed.test.js @@ -47,7 +47,7 @@ describe('ArrayTestBed', () => { it('Should set random data in datastore', () => { const test = new ArrayTestBed(10); test.setData(false); - console.log(test.dataStore); + expect(test.dataStore.length).to.eql(10); }); @@ -66,10 +66,63 @@ describe('ArrayTestBed', () => { const test = new ArrayTestBed(10); test.setData(); const out = test.getData(); - + expect(out.length).to.eql(10); }); }); + describe('ArrayTestBed - clear()', () => { + + it('Should clear the datastore', () => { + const test = new ArrayTestBed(10); + test.setData(); + expect(test.dataStore.length).to.eql(10); + test.clear(); + expect(test.dataStore).to.eql([]); + expect(test.pos).to.eql(0); + }); + + }); + + describe('ArrayTestBed - toString()', () => { + + it('Should return the elements of datastore in string format', () => { + const test = new ArrayTestBed(); + test.dataStore = [1, 2, 3]; + const out = test.toString(); + + expect(out).to.eql('1,2,3,'); + }); + + }); + + describe('ArrayTestBed - swap()', () => { + + it('Should swap the array elements', () => { + const test = [1, 2, 3, 4]; + const arrClass = new ArrayTestBed(); + + const out = arrClass.swap(test, 1, 2); + + expect(out).to.eql([1, 3, 2, 4]); + }); + + }); + + describe('ArrayTestBed - insert()', () => { + + it('Should insert the elements inside the array', () => { + const test = new ArrayTestBed(); + + test.insert(1); + test.insert(2); + test.insert(3); + test.insert(4); + + expect(test.dataStore).to.eql([1, 2, 3, 4]); + }); + + }); + }); From b1899dd7e2dc128149d25a494ff03c683d3889d6 Mon Sep 17 00:00:00 2001 From: Neelesh Roy Date: Sat, 5 May 2018 15:18:54 +0530 Subject: [PATCH 4/4] Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 363a372..ca9b096 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # [sorting-js](https://github.com/neeleshroy/sorting-js) [![Build Status](https://travis-ci.org/NeeleshRoy/sorting-js.svg?branch=develop)](https://travis-ci.org/NeeleshRoy/sorting-js) -[![Coverage Status](https://img.shields.io/coveralls/neeleshroy/sorting-js.svg?style=flat-square)](https://coveralls.io/neeleshroy/sorting-js) +[![Coverage Status](https://coveralls.io/repos/github/NeeleshRoy/sorting-js/badge.svg)](https://coveralls.io/github/NeeleshRoy/sorting-js) [![Dependency Status](http://img.shields.io/david/neeleshroy/sorting-js.svg?style=flat-square)](https://david-dm.org/neeleshroy/sorting-js) > Sorting algorithms implemented in JS