1
+ import BitArray , { bit } from "./bit-array" ;
2
+
3
+ test ( 'Passing less than 8 bit to the constructor' , ( ) => {
4
+ const arr1 = [ 1 ] ;
5
+ const arr2 = [ 1 , 0 ] ;
6
+ const arr3 = [ 1 , 0 , 1 ] ;
7
+ const arr4 = [ 1 , 0 , 1 , 0 ] ;
8
+ const arr5 = [ 1 , 0 , 1 , 0 , 1 ] ;
9
+ const arr6 = [ 1 , 0 , 1 , 0 , 1 , 0 ] ;
10
+ const arr7 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 ] ;
11
+
12
+ const bArr1 = new BitArray ( arr1 as Array < bit > ) ;
13
+ const bArr2 = new BitArray ( arr2 as Array < bit > ) ;
14
+ const bArr3 = new BitArray ( arr3 as Array < bit > ) ;
15
+ const bArr4 = new BitArray ( arr4 as Array < bit > ) ;
16
+ const bArr5 = new BitArray ( arr5 as Array < bit > ) ;
17
+ const bArr6 = new BitArray ( arr6 as Array < bit > ) ;
18
+ const bArr7 = new BitArray ( arr7 as Array < bit > ) ;
19
+
20
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( arr1 ) ;
21
+ expect ( bArr2 . atIndexRange ( 0 ) ) . toEqual ( arr2 ) ;
22
+ expect ( bArr3 . atIndexRange ( 0 ) ) . toEqual ( arr3 ) ;
23
+ expect ( bArr4 . atIndexRange ( 0 ) ) . toEqual ( arr4 ) ;
24
+ expect ( bArr5 . atIndexRange ( 0 ) ) . toEqual ( arr5 ) ;
25
+ expect ( bArr6 . atIndexRange ( 0 ) ) . toEqual ( arr6 ) ;
26
+ expect ( bArr7 . atIndexRange ( 0 ) ) . toEqual ( arr7 ) ;
27
+ } ) ;
28
+
29
+ test ( 'Passing more than 8 bit to the constructor' , ( ) => {
30
+ const arr8 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 ] ;
31
+ const arr9 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 ] ;
32
+
33
+ const bArr8 = new BitArray ( arr8 as Array < bit > ) ;
34
+ const bArr9 = new BitArray ( arr9 as Array < bit > ) ;
35
+
36
+ expect ( bArr8 . atIndexRange ( 0 ) ) . toEqual ( arr8 ) ;
37
+ expect ( bArr9 . atIndexRange ( 0 ) ) . toEqual ( arr9 ) ;
38
+ } ) ;
39
+
40
+ test ( 'Can get a specific index correctly' , ( ) => {
41
+ const bArr1 = new BitArray ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
42
+ const bArr2 = new BitArray ( [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ) ;
43
+
44
+ for ( let i = 0 ; i < bArr1 . size ; i ++ ) {
45
+ expect ( bArr1 . atIndex ( i ) ) . toBe ( 0 ) ;
46
+ expect ( bArr2 . atIndex ( i ) ) . toBe ( 1 ) ;
47
+ }
48
+ } ) ;
49
+
50
+ test ( 'Can\'t get an invalid index' , ( ) => {
51
+ const bArr1 = new BitArray ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
52
+
53
+ expect ( ( ) => bArr1 . atIndex ( - 1 ) ) . toThrowError ( ) ;
54
+ expect ( ( ) => bArr1 . atIndex ( bArr1 . size + 1 ) ) . toThrowError ( ) ;
55
+ } ) ;
56
+
57
+ test ( 'Can\'t get an invalid index range' , ( ) => {
58
+ const bArr1 = new BitArray ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ;
59
+
60
+ expect ( ( ) => bArr1 . atIndexRange ( - 1 ) ) . toThrowError ( ) ;
61
+ expect ( ( ) => bArr1 . atIndexRange ( 0 , bArr1 . size + 1 ) ) . toThrowError ( ) ;
62
+ } ) ;
63
+
64
+ test ( 'Can get a specific index range correctly' , ( ) => {
65
+ const arr1 = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
66
+ const arr2 = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ;
67
+
68
+ const bArr1 = new BitArray ( arr1 as Array < bit > ) ;
69
+ const bArr2 = new BitArray ( arr2 as Array < bit > ) ;
70
+
71
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( arr1 ) ;
72
+ expect ( bArr2 . atIndexRange ( 0 ) ) . toEqual ( arr2 ) ;
73
+
74
+ expect ( bArr1 . atIndexRange ( 1 ) ) . toEqual ( arr1 . slice ( 1 ) ) ;
75
+ expect ( bArr2 . atIndexRange ( 1 ) ) . toEqual ( arr2 . slice ( 1 ) ) ;
76
+
77
+ expect ( bArr1 . atIndexRange ( 0 , 2 ) ) . toEqual ( arr1 . slice ( 0 , 2 ) ) ;
78
+ expect ( bArr2 . atIndexRange ( 0 , 2 ) ) . toEqual ( arr2 . slice ( 0 , 2 ) ) ;
79
+
80
+ expect ( bArr1 . atIndexRange ( 1 , 5 ) ) . toEqual ( arr1 . slice ( 1 , 6 ) ) ;
81
+ expect ( bArr2 . atIndexRange ( 1 , 5 ) ) . toEqual ( arr2 . slice ( 1 , 6 ) ) ;
82
+ } ) ;
83
+
84
+ test ( 'Can push 0 values onto BitArray' , ( ) => {
85
+ const bArr1 = new BitArray ( [ ] ) ;
86
+
87
+ for ( let i = 0 ; i < 100 ; i ++ ) {
88
+ bArr1 . push ( 1 ) ;
89
+ expect ( bArr1 . atIndex ( i ) ) . toBe ( 1 ) ;
90
+ }
91
+ } ) ;
92
+
93
+ test ( 'Can push 1 values onto BitArray' , ( ) => {
94
+ const bArr1 = new BitArray ( [ ] ) ;
95
+
96
+ for ( let i = 0 ; i < 100 ; i ++ ) {
97
+ bArr1 . push ( 0 ) ;
98
+ expect ( bArr1 . atIndex ( i ) ) . toBe ( 0 ) ;
99
+ }
100
+ bArr1 [ 0 ] = 1 ;
101
+ } ) ;
102
+
103
+ test ( 'Can set values' , ( ) => {
104
+ const bArr1 = new BitArray ( [ 1 , 0 ] ) ;
105
+
106
+ bArr1 . set ( 0 , 1 ) ;
107
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( [ 1 , 0 ] ) ;
108
+ bArr1 . set ( 0 , 0 ) ;
109
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( [ 0 , 0 ] ) ;
110
+ bArr1 . set ( 1 , 0 ) ;
111
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( [ 0 , 0 ] ) ;
112
+ bArr1 . set ( 1 , 1 ) ;
113
+ expect ( bArr1 . atIndexRange ( 0 ) ) . toEqual ( [ 0 , 1 ] ) ;
114
+ } ) ;
115
+
116
+ test ( 'Can\'t set values for invalid index' , ( ) => {
117
+ const bArr1 = new BitArray ( [ 1 , 0 ] ) ;
118
+
119
+ expect ( ( ) => bArr1 . set ( - 1 , 0 ) ) . toThrowError ( ) ;
120
+ expect ( ( ) => bArr1 . set ( bArr1 . size + 1 , 0 ) ) . toThrowError ( ) ;
121
+ } ) ;
122
+
123
+ test ( 'Size is valid' , ( ) => {
124
+ const bArr1 = new BitArray ( [ 1 , 0 ] ) ;
125
+ expect ( bArr1 . size ) . toBe ( 2 ) ;
126
+
127
+ bArr1 . pushAll ( [ 1 , 1 ] ) ;
128
+ expect ( bArr1 . size ) . toBe ( 4 ) ;
129
+
130
+ bArr1 . push ( 1 ) ;
131
+ expect ( bArr1 . size ) . toBe ( 5 ) ;
132
+ } ) ;
0 commit comments