Skip to content

Commit 7492bb0

Browse files
author
Isaac Ramirez
committed
- implement Abstract GenericSort class
1 parent 028d666 commit 7492bb0

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const { StdOut } = require('../../libs')
2+
const { defaultComparator } = require('../../common')
3+
4+
/**
5+
* GenericSort
6+
* @classdesc Abstract Class for Sorting Algorithms.
7+
*/
8+
class GenericSort {
9+
/**
10+
* Returns whether `a` is less than `b`
11+
* based on a comparing function.
12+
* @param {*} a Comparable object `a`
13+
* @param {*} b Comparable object `b`
14+
* @param {function} comparator A comparing function that
15+
* returns -1 when `a` is less than `b`,
16+
* returns 1 when `a` is greater than `b` or
17+
* returns 0 when `a` is equal to `b`.
18+
*/
19+
static less (a, b, comparator = defaultComparator) {
20+
return comparator(a, b) < 0
21+
}
22+
23+
/**
24+
* Interchanges the values located in indexes `i` and `j`
25+
* for the given `array`.
26+
* @param {[*]} array Array of values.
27+
* @param {number} i Index 1
28+
* @param {number} j Index 2
29+
*/
30+
static exchange (array, i, j) {
31+
const temp = array[i]
32+
33+
array[i] = array[j]
34+
array[j] = temp
35+
}
36+
37+
/**
38+
* Returns `true` if the provided array is sorted,
39+
* `false` otherwise.
40+
* @param {[*]} array Array of values
41+
* @param {function} comparator A comparing function that
42+
* returns -1 when `a` is less than `b`,
43+
* returns 1 when `a` is greater than `b` or
44+
* returns 0 when `a` is equal to `b`.
45+
*/
46+
static isSorted (array, comparator = defaultComparator) {
47+
for (let i = 1; i < array.length; i++) {
48+
if (this.less(array[i], array[i - 1], comparator)) {
49+
return false
50+
}
51+
}
52+
53+
return true
54+
}
55+
56+
/**
57+
* A function that prints to the StdOut
58+
* the contents of the `array` provided.
59+
* @param {[*]} array Array of values.
60+
*/
61+
static show (array) {
62+
StdOut.println(array.join(' '))
63+
}
64+
65+
/**
66+
* Should sorts the `array` using the Sorting algorithm of your choice.
67+
* @abstract
68+
* @throws SyntaxError This function should be implemented by the client.
69+
*/
70+
static sort () {
71+
throw new SyntaxError('sort method not implemented')
72+
}
73+
}
74+
75+
module.exports = GenericSort
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const GenericSort = require('./generic-sort')
2+
const { newArrayOf } = require('../../utils')
3+
const { defaultComparator: comparator } = require('../../common')
4+
5+
describe('Unit Tests: GenericSort Abstract Class', () => {
6+
beforeEach(() => {
7+
this.orderedArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8+
this.reversedArray = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
9+
this.unorderedArray = [0, 9, 5, 2, 1, 8, 7, 6, 4, 3]
10+
this.allEqualArray = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
11+
})
12+
13+
describe('static less method', () => {
14+
it('should return true if a is less than b', () => {
15+
const a = 1
16+
const b = 2
17+
18+
expect(GenericSort.less(a, b, comparator)).toBe(true)
19+
})
20+
21+
it('should return false if a is greater than b', () => {
22+
const a = 2
23+
const b = 1
24+
25+
expect(GenericSort.less(a, b, comparator)).toBe(false)
26+
})
27+
28+
it('should return false if a is equal to b', () => {
29+
const a = 1
30+
const b = 1
31+
32+
expect(GenericSort.less(a, b, comparator)).toBe(false)
33+
})
34+
35+
it('should have a defaultComparator function', () => {
36+
expect(GenericSort.less(1, 10)).toBe(true)
37+
expect(GenericSort.less(1, 1)).toBe(false)
38+
expect(GenericSort.less(10, 1)).toBe(false)
39+
})
40+
})
41+
42+
describe('static exchange method', () => {
43+
it('should interchange the values of the two given indexes in the array', () => {
44+
const array = [5, 6, 7]
45+
const expectedArray = [7, 6, 5]
46+
47+
GenericSort.exchange(array, 0, 2)
48+
49+
expect(array).toEqual(expectedArray)
50+
})
51+
})
52+
53+
describe('static isSorted method', () => {
54+
it('should return true if an array is sorted', () => {
55+
expect(GenericSort.isSorted(this.orderedArray)).toBe(true)
56+
expect(GenericSort.isSorted(this.allEqualArray)).toBe(true)
57+
})
58+
59+
it('should return false if the array is not sorted', () => {
60+
expect(GenericSort.isSorted(this.unorderedArray)).toBe(false)
61+
expect(GenericSort.isSorted(this.reversedArray)).toBe(false)
62+
})
63+
64+
it('should accept a comparator function', () => {
65+
const orderedArray = ['A', 'B', 'C']
66+
const unorderedArray = ['Z', 'X', 'Y']
67+
68+
expect(GenericSort.isSorted(orderedArray, comparator)).toBe(true)
69+
expect(GenericSort.isSorted(unorderedArray, comparator)).toBe(false)
70+
})
71+
72+
it('should return true for a sorted big array', () => {
73+
const n = 1000000 // a million!
74+
const array = newArrayOf(n, i => i) // from 0 to 999999
75+
76+
expect(GenericSort.isSorted(array)).toBeTrue()
77+
})
78+
79+
it('should return false for a random big array', () => {
80+
const n = 1000000 // a million!
81+
const array = newArrayOf(n, i => i) // from 0 to 999999
82+
array[array.length - 1] = 0 // change last number to be 0
83+
84+
expect(GenericSort.isSorted(array)).toBeFalse()
85+
})
86+
})
87+
88+
describe('static show method', () => {
89+
it('should be a function', () => {
90+
// I could test the call to StdOut, but I don't want to.
91+
expect(GenericSort.show).toBeInstanceOf(Function)
92+
})
93+
})
94+
95+
describe('static sort method', () => {
96+
it('should throw because is not implemented', () => {
97+
expect(() => {
98+
GenericSort.sort()
99+
}).toThrowError('sort method not implemented')
100+
})
101+
})
102+
})

src/abstracts/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
ArrayIterator: require('./array-iterator/array-iterator'),
3+
GenericSort: require('./generic-sort/generic-sort'),
34
Iterator: require('./iterator/iterator'),
45
ReversedArrayIterator: require('./reversed-array-iterator/reversed-array-iterator')
56
}

0 commit comments

Comments
 (0)