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 diff --git a/src/utils/ArrayTestBed.js b/src/utils/ArrayTestBed.js new file mode 100644 index 0000000..42aae2c --- /dev/null +++ b/src/utils/ArrayTestBed.js @@ -0,0 +1,55 @@ +export class ArrayTestBed { + constructor(numElements) { + this.dataStore = []; + this.pos = 0; + this.numElements = numElements; + } + + setData(random = true) { + for (let i = 0; i < this.numElements; i++) { + let randomNumber = this.getRandomNumber(); + if (random) { + while (this.dataStore.indexOf(randomNumber) > -1) { + randomNumber = this.getRandomNumber(); + } + this.dataStore[i] = randomNumber; + } else { + this.dataStore[i] = randomNumber; + } + } + } + + getRandomNumber() { + return Math.floor(Math.random() * + Math.pow(10, this.numElements.toString().length - 1) + 1); + } + + 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 new file mode 100644 index 0000000..aa66788 --- /dev/null +++ b/test/ArrayTestBed.test.js @@ -0,0 +1,128 @@ +/** + * 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.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(10); + test.setData(false); + + 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([]); + }); + + 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); + }); + + }); + + 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]); + }); + + }); + +});