@@ -2,45 +2,57 @@ const expect = require('chai').expect;
22const findMagicIndex = require ( './magic-index' ) ;
33
44describe ( 'findMagicIndex' , function ( ) {
5- it ( 'all sorted' , function ( ) {
6- const array = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ;
7- expect ( findMagicIndex ( array ) ) . to . equal ( 0 ) ;
8- } ) ;
9-
10- it ( 'no magic index' , function ( ) {
11- const array = [ 10 , 100 , 240 ] ;
12- expect ( findMagicIndex ( array ) ) . to . equal ( - 1 ) ;
13- } ) ;
14-
15- it ( 'values are not distinct' , function ( ) {
16- // index 0 1 2 3 4
17- const array = [ 1 , 2 , 3 , 3 , 5 ] ;
18- expect ( findMagicIndex ( array ) ) . to . equal ( 3 ) ;
19- } ) ;
20-
21- it ( 'has negative values' , function ( ) {
22- // index 0 1 2 3 4 5 6 7 8 9 10
23- const array = [ - 40 , - 20 , - 1 , 1 , 2 , 3 , 5 , 7 , 9 , 12 , 13 ] ;
24- expect ( findMagicIndex ( array ) ) . to . equal ( 7 ) ;
25- } ) ;
26-
27- it ( 'should work with 1 element' , function ( ) {
28- const array = [ 0 ] ;
29- expect ( findMagicIndex ( array ) ) . to . equal ( 0 ) ;
30- } ) ;
31-
32- it ( 'should work with 1 element that is not a magic index' , function ( ) {
33- const array = [ 10 ] ;
34- expect ( findMagicIndex ( array ) ) . to . equal ( - 1 ) ;
35- } ) ;
36-
37- it ( 'should work with 2 elements' , function ( ) {
38- const array = [ 1 , 1 ] ;
39- expect ( findMagicIndex ( array ) ) . to . equal ( 1 ) ;
40- } ) ;
41-
42-
43- it ( 'should handle null cases' , function ( ) {
44- expect ( findMagicIndex ( ) ) . to . equal ( - 1 ) ;
5+ describe ( 'when elements are distinct' , function ( ) {
6+ it ( 'no magic index' , function ( ) {
7+ const array = [ 10 , 100 , 240 ] ;
8+ expect ( findMagicIndex ( array ) ) . to . equal ( - 1 ) ;
9+ } ) ;
10+
11+ it ( 'has negative values' , function ( ) {
12+ // index 0 1 2 3 4 5 6 7 8 9 10
13+ const array = [ - 40 , - 20 , - 1 , 1 , 2 , 3 , 5 , 7 , 9 , 12 , 13 ] ;
14+ expect ( findMagicIndex ( array ) ) . to . equal ( 7 ) ;
15+ } ) ;
16+
17+ it ( 'should work with 1 element' , function ( ) {
18+ const array = [ 0 ] ;
19+ expect ( findMagicIndex ( array ) ) . to . equal ( 0 ) ;
20+ } ) ;
21+
22+ it ( 'should work with 1 element that is not a magic index' , function ( ) {
23+ const array = [ 10 ] ;
24+ expect ( findMagicIndex ( array ) ) . to . equal ( - 1 ) ;
25+ } ) ;
26+
27+ it ( 'should handle undefined cases' , function ( ) {
28+ expect ( findMagicIndex ( ) ) . to . equal ( - 1 ) ;
29+ } ) ;
30+
31+ it ( 'should handle null cases' , function ( ) {
32+ expect ( findMagicIndex ( null ) ) . to . equal ( - 1 ) ;
33+ } ) ;
34+
35+ it ( 'should handle true cases' , function ( ) {
36+ expect ( findMagicIndex ( true ) ) . to . equal ( - 1 ) ;
37+ } ) ;
38+ } ) ;
39+
40+ describe ( 'when some elements are repeated' , function ( ) {
41+ it ( 'should work with 2 elements' , function ( ) {
42+ const array = [ 1 , 1 ] ;
43+ expect ( findMagicIndex ( array ) ) . to . equal ( 1 ) ;
44+ } ) ;
45+
46+ it ( 'values are NOT distinct to the right' , function ( ) {
47+ // index 0 1 2 3 4
48+ const array = [ 1 , 2 , 3 , 3 , 5 ] ;
49+ expect ( findMagicIndex ( array ) ) . to . equal ( 3 ) ;
50+ } ) ;
51+
52+ it ( 'repeated values on the left' , function ( ) {
53+ // index 0 1 2 3 4 5 6 7 8 9 10
54+ const array = [ - 40 , - 20 , 2 , 2 , 2 , 3 , 5 , 8 , 9 , 12 , 13 ] ;
55+ expect ( findMagicIndex ( array ) ) . to . equal ( 2 ) ;
56+ } ) ;
4557 } ) ;
4658} ) ;
0 commit comments