1
1
const Insertion = require ( './insertion-sort' )
2
2
const { newArrayOf } = require ( '../../utils' )
3
3
const { StdRandom } = require ( '../../libs' )
4
- const { defaultComparator : comparator } = require ( '../../common' )
5
4
6
5
describe ( 'Unit Tests: Insertion Sort Algorithm' , ( ) => {
7
6
beforeEach ( ( ) => {
@@ -11,97 +10,7 @@ describe('Unit Tests: Insertion Sort Algorithm', () => {
11
10
this . allEqualArray = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 ]
12
11
} )
13
12
14
- describe ( 'static less method' , ( ) => {
15
- it ( 'should return true if a is less than b' , ( ) => {
16
- const a = 1
17
- const b = 2
18
-
19
- expect ( Insertion . less ( a , b , comparator ) ) . toBe ( true )
20
- } )
21
-
22
- it ( 'should return false if a is greater than b' , ( ) => {
23
- const a = 2
24
- const b = 1
25
-
26
- expect ( Insertion . less ( a , b , comparator ) ) . toBe ( false )
27
- } )
28
-
29
- it ( 'should return false if a is equal to b' , ( ) => {
30
- const a = 1
31
- const b = 1
32
-
33
- expect ( Insertion . less ( a , b , comparator ) ) . toBe ( false )
34
- } )
35
-
36
- it ( 'should have a defaultComparator function' , ( ) => {
37
- expect ( Insertion . less ( 1 , 10 ) ) . toBe ( true )
38
- expect ( Insertion . less ( 1 , 1 ) ) . toBe ( false )
39
- expect ( Insertion . less ( 10 , 1 ) ) . toBe ( false )
40
- } )
41
- } )
42
-
43
- describe ( 'static exchange method' , ( ) => {
44
- it ( 'should interchange the values of the two given indexes in the array' , ( ) => {
45
- const array = [ 5 , 6 , 7 ]
46
- const expectedArray = [ 7 , 6 , 5 ]
47
-
48
- Insertion . exchange ( array , 0 , 2 )
49
-
50
- expect ( array ) . toEqual ( expectedArray )
51
- } )
52
- } )
53
-
54
- describe ( 'static isSorted method' , ( ) => {
55
- it ( 'should return true if an array is sorted' , ( ) => {
56
- expect ( Insertion . isSorted ( this . orderedArray ) ) . toBe ( true )
57
- expect ( Insertion . isSorted ( this . allEqualArray ) ) . toBe ( true )
58
- } )
59
-
60
- it ( 'should return false if the array is not sorted' , ( ) => {
61
- expect ( Insertion . isSorted ( this . unorderedArray ) ) . toBe ( false )
62
- expect ( Insertion . isSorted ( this . reversedArray ) ) . toBe ( false )
63
- } )
64
-
65
- it ( 'should accept a comparator function' , ( ) => {
66
- const orderedArray = [ 'A' , 'B' , 'C' ]
67
- const unorderedArray = [ 'Z' , 'X' , 'Y' ]
68
-
69
- expect ( Insertion . isSorted ( orderedArray , comparator ) ) . toBe ( true )
70
- expect ( Insertion . isSorted ( unorderedArray , comparator ) ) . toBe ( false )
71
- } )
72
-
73
- it ( 'should return true for a sorted big array' , ( ) => {
74
- const n = 1000000 // a million!
75
- const array = newArrayOf ( n , i => i ) // from 0 to 999999
76
-
77
- expect ( Insertion . isSorted ( array ) ) . toBeTrue ( )
78
- } )
79
-
80
- it ( 'should return false for a random big array' , ( ) => {
81
- const n = 1000000 // a million!
82
- const array = newArrayOf ( n , i => i ) // from 0 to 999999
83
- array [ array . length - 1 ] = 0 // change last number to be 0
84
-
85
- expect ( Insertion . isSorted ( array ) ) . toBeFalse ( )
86
- } )
87
- } )
88
-
89
- describe ( 'static show method' , ( ) => {
90
- it ( 'should be a function' , ( ) => {
91
- // I could test the call to StdOut, but I don't want to.
92
- expect ( Insertion . show ) . toBeInstanceOf ( Function )
93
- } )
94
- } )
95
-
96
13
describe ( 'static sort method' , ( ) => {
97
- it ( 'should sort a unordered array' , ( ) => {
98
- const expectedArray = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
99
-
100
- Insertion . sort ( this . unorderedArray )
101
-
102
- expect ( this . unorderedArray ) . toEqual ( expectedArray )
103
- } )
104
-
105
14
it ( 'should sort an ordered array' , ( ) => {
106
15
const expectedArray = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
107
16
@@ -118,6 +27,14 @@ describe('Unit Tests: Insertion Sort Algorithm', () => {
118
27
expect ( this . reversedArray ) . toEqual ( expectedArray )
119
28
} )
120
29
30
+ it ( 'should sort a unordered array' , ( ) => {
31
+ const expectedArray = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
32
+
33
+ Insertion . sort ( this . unorderedArray )
34
+
35
+ expect ( this . unorderedArray ) . toEqual ( expectedArray )
36
+ } )
37
+
121
38
it ( 'should sort an array with all the values to be equal' , ( ) => {
122
39
const expectedArray = [ 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 , 9 ]
123
40
0 commit comments