1
+ import { describe , it , expect } from "bun:test" ;
2
+ import { insertionSort , insertionSortCallback } from "./insertion-sort" ;
3
+
4
+ describe ( 'Insertion Sort Functions' , ( ) => {
5
+ const unsortedArray = [ 99 , 44 , 6 , 2 , 1 , 5 , 63 , 87 , 283 , 4 , 0 ] ;
6
+ const sortedArrayAsc = [ 0 , 1 , 2 , 4 , 5 , 6 , 44 , 63 , 87 , 99 , 283 ] ;
7
+ const sortedArrayDesc = [ ...sortedArrayAsc ] . reverse ( ) ;
8
+
9
+ describe ( 'insertionSort' , ( ) => {
10
+ it ( 'should sort the array in ascending order' , ( ) => {
11
+ const result = insertionSort ( [ ...unsortedArray ] ) ; // Spread to avoid mutation
12
+ expect ( result ) . toEqual ( sortedArrayAsc ) ;
13
+ } ) ;
14
+
15
+ it ( 'should handle an already sorted array' , ( ) => {
16
+ const result = insertionSort ( [ ...sortedArrayAsc ] ) ;
17
+ expect ( result ) . toEqual ( sortedArrayAsc ) ;
18
+ } ) ;
19
+
20
+ it ( 'should handle an empty array' , ( ) => {
21
+ const result = insertionSort ( [ ] ) ;
22
+ expect ( result ) . toEqual ( [ ] ) ;
23
+ } ) ;
24
+
25
+ it ( 'should handle an array with one element' , ( ) => {
26
+ const result = insertionSort ( [ 42 ] ) ;
27
+ expect ( result ) . toEqual ( [ 42 ] ) ;
28
+ } ) ;
29
+
30
+ it ( 'should handle an array with duplicate elements' , ( ) => {
31
+ const result = insertionSort ( [ 3 , 1 , 2 , 3 , 3 , 0 ] ) ;
32
+ expect ( result ) . toEqual ( [ 0 , 1 , 2 , 3 , 3 , 3 ] ) ;
33
+ } ) ;
34
+ } ) ;
35
+
36
+ describe ( 'insertionSortCallback' , ( ) => {
37
+ it ( 'should sort the array in ascending order using a callback' , ( ) => {
38
+ const result = insertionSortCallback ( [ ...unsortedArray ] , ( a , b ) => a - b ) ;
39
+ expect ( result ) . toEqual ( sortedArrayAsc ) ;
40
+ } ) ;
41
+
42
+ it ( 'should sort the array in descending order using a callback' , ( ) => {
43
+ const result = insertionSortCallback ( [ ...unsortedArray ] , ( a , b ) => b - a ) ;
44
+ expect ( result ) . toEqual ( sortedArrayDesc ) ;
45
+ } ) ;
46
+
47
+ it ( 'should handle an already sorted array in ascending order' , ( ) => {
48
+ const result = insertionSortCallback ( [ ...sortedArrayAsc ] , ( a , b ) => a - b ) ;
49
+ expect ( result ) . toEqual ( sortedArrayAsc ) ;
50
+ } ) ;
51
+
52
+ it ( 'should handle an already sorted array in descending order' , ( ) => {
53
+ const result = insertionSortCallback ( [ ...sortedArrayDesc ] , ( a , b ) => b - a ) ;
54
+ expect ( result ) . toEqual ( sortedArrayDesc ) ;
55
+ } ) ;
56
+
57
+ it ( 'should handle an empty array' , ( ) => {
58
+ const result = insertionSortCallback ( [ ] , ( a , b ) => a - b ) ;
59
+ expect ( result ) . toEqual ( [ ] ) ;
60
+ } ) ;
61
+
62
+ it ( 'should handle an array with one element' , ( ) => {
63
+ const result = insertionSortCallback ( [ 42 ] , ( a , b ) => a - b ) ;
64
+ expect ( result ) . toEqual ( [ 42 ] ) ;
65
+ } ) ;
66
+
67
+ it ( 'should handle an array with duplicate elements' , ( ) => {
68
+ const result = insertionSortCallback ( [ 3 , 1 , 2 , 3 , 3 , 0 ] , ( a , b ) => a - b ) ;
69
+ expect ( result ) . toEqual ( [ 0 , 1 , 2 , 3 , 3 , 3 ] ) ;
70
+ } ) ;
71
+ } ) ;
72
+ } ) ;
0 commit comments