11import { SortedSet } from '..'
22
33describe ( 'SortedSet' , ( ) => {
4+ const numberCompare = ( a : number , b : number ) : number => a - b
5+
46 describe ( 'get' , ( ) => {
57 it ( 'Should return undefined for invalid indicies' , ( ) => {
6- const arr = new SortedSet < number > ( ( a , b ) => a - b )
8+ const arr = new SortedSet ( numberCompare )
79
810 arr . add ( 1 )
911 arr . add ( 5 )
@@ -17,7 +19,7 @@ describe('SortedSet', () => {
1719
1820 describe ( 'size' , ( ) => {
1921 it ( 'Should return the number of values in the tree' , ( ) => {
20- const arr = new SortedSet < number > ( ( a , b ) => a - b )
22+ const arr = new SortedSet ( numberCompare )
2123
2224 expect ( arr . size ) . toBe ( 0 )
2325
@@ -35,7 +37,7 @@ describe('SortedSet', () => {
3537
3638 describe ( 'iterator' , ( ) => {
3739 it ( 'Should iterate the right values and in order' , ( ) => {
38- const arr = new SortedSet < number > ( ( a , b ) => a - b )
40+ const arr = new SortedSet ( numberCompare )
3941 arr . add ( 2 )
4042 arr . add ( 3 )
4143 arr . add ( 1 )
@@ -50,7 +52,7 @@ describe('SortedSet', () => {
5052
5153 describe ( 'add' , ( ) => {
5254 it ( 'Should insert values in order' , ( ) => {
53- const arr = new SortedSet < number > ( ( a , b ) => a - b )
55+ const arr = new SortedSet ( numberCompare )
5456
5557 arr . add ( 1 )
5658 arr . add ( 5 )
@@ -65,7 +67,7 @@ describe('SortedSet', () => {
6567
6668 describe ( 'remove' , ( ) => {
6769 it ( 'Should remove a value from a tree.' , ( ) => {
68- const s = new SortedSet < number > ( ( a , b ) => a - b )
70+ const s = new SortedSet ( numberCompare )
6971 const vals = [ ]
7072 for ( let i = 0 ; i < 10 ; i ++ ) {
7173 s . add ( i )
@@ -78,4 +80,23 @@ describe('SortedSet', () => {
7880 expect ( [ ...s ] ) . toMatchObject ( vals )
7981 } )
8082 } )
83+
84+ describe ( 'from' , ( ) => {
85+ const iterable = [ 1 , 1 , 2 , 3 , 6 , - 100 ]
86+ const result = SortedSet . from ( iterable , numberCompare )
87+
88+ it ( 'Should contain the correct values' , ( ) => {
89+ for ( const value of iterable ) {
90+ expect ( result . has ( value ) ) . toBe ( true )
91+ }
92+ } )
93+
94+ it ( 'Should be the right size' , ( ) => {
95+ expect ( result . size ) . toBe ( new Set ( iterable ) . size )
96+ } )
97+
98+ it ( 'Should be in-order' , ( ) => {
99+ expect ( [ ...result ] ) . toMatchObject ( [ ...result ] . sort ( ) )
100+ } )
101+ } )
81102} )
0 commit comments