1
1
import PriorityQueue from '../PriorityQueue' ;
2
2
3
+ const JOB1 = { type : 'job1' } ;
4
+ const JOB2 = { type : 'job2' } ;
5
+ const JOB3 = { type : 'job3' } ;
6
+ const JOB4 = { type : 'job4' } ;
7
+ const JOB5 = { type : 'job5' } ;
8
+
3
9
describe ( 'PriorityQueue' , ( ) => {
4
10
it ( 'should create default priority queue' , ( ) => {
5
11
const priorityQueue = new PriorityQueue ( ) ;
@@ -10,94 +16,118 @@ describe('PriorityQueue', () => {
10
16
it ( 'should insert items to the queue and respect priorities' , ( ) => {
11
17
const priorityQueue = new PriorityQueue ( ) ;
12
18
13
- priorityQueue . add ( 10 , 1 ) ;
14
- expect ( priorityQueue . peek ( ) ) . toBe ( 10 ) ;
19
+ priorityQueue . add ( JOB1 , 1 ) ;
20
+ expect ( priorityQueue . peek ( ) ) . toBe ( JOB1 ) ;
15
21
16
- priorityQueue . add ( 5 , 2 ) ;
17
- expect ( priorityQueue . peek ( ) ) . toBe ( 10 ) ;
22
+ priorityQueue . add ( JOB2 , 2 ) ;
23
+ expect ( priorityQueue . peek ( ) ) . toBe ( JOB1 ) ;
18
24
19
- priorityQueue . add ( 100 , 0 ) ;
20
- expect ( priorityQueue . peek ( ) ) . toBe ( 100 ) ;
25
+ priorityQueue . add ( JOB3 , 0 ) ;
26
+ expect ( priorityQueue . peek ( ) ) . toBe ( JOB3 ) ;
21
27
} ) ;
22
28
23
29
it ( 'should poll from queue with respect to priorities' , ( ) => {
24
30
const priorityQueue = new PriorityQueue ( ) ;
25
31
26
- priorityQueue . add ( 10 , 1 ) ;
27
- priorityQueue . add ( 5 , 2 ) ;
28
- priorityQueue . add ( 100 , 0 ) ;
29
- priorityQueue . add ( 200 , 0 ) ;
32
+ priorityQueue . add ( JOB1 , 1 ) ;
33
+ priorityQueue . add ( JOB2 , 2 ) ;
34
+ priorityQueue . add ( JOB3 , 0 ) ;
35
+ priorityQueue . add ( JOB4 , 0 ) ;
30
36
31
- expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
32
- expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
33
- expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
34
- expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
37
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB3 ) ;
38
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB4 ) ;
39
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB1 ) ;
40
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB2 ) ;
35
41
} ) ;
36
42
37
43
it ( 'should be possible to change priority of internal nodes' , ( ) => {
38
44
const priorityQueue = new PriorityQueue ( ) ;
39
45
40
- priorityQueue . add ( 10 , 1 ) ;
41
- priorityQueue . add ( 5 , 2 ) ;
42
- priorityQueue . add ( 100 , 0 ) ;
43
- priorityQueue . add ( 200 , 0 ) ;
46
+ priorityQueue . add ( JOB1 , 1 ) ;
47
+ priorityQueue . add ( JOB2 , 2 ) ;
48
+ priorityQueue . add ( JOB3 , 0 ) ;
49
+ priorityQueue . add ( JOB4 , 0 ) ;
44
50
45
- priorityQueue . changePriority ( 100 , 10 ) ;
46
- priorityQueue . changePriority ( 10 , 20 ) ;
51
+ priorityQueue . changePriority ( JOB4 , 10 ) ;
52
+ priorityQueue . changePriority ( JOB1 , 20 ) ;
47
53
48
- expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
49
- expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
50
- expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
51
- expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
54
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB3 ) ;
55
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB2 ) ;
56
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB4 ) ;
57
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB1 ) ;
52
58
} ) ;
53
59
54
60
it ( 'should be possible to change priority of head node' , ( ) => {
55
61
const priorityQueue = new PriorityQueue ( ) ;
56
62
57
- priorityQueue . add ( 10 , 1 ) ;
58
- priorityQueue . add ( 5 , 2 ) ;
59
- priorityQueue . add ( 100 , 0 ) ;
60
- priorityQueue . add ( 200 , 0 ) ;
63
+ priorityQueue . add ( JOB1 , 1 ) ;
64
+ priorityQueue . add ( JOB2 , 2 ) ;
65
+ priorityQueue . add ( JOB3 , 0 ) ;
66
+ priorityQueue . add ( JOB4 , 0 ) ;
61
67
62
- priorityQueue . changePriority ( 200 , 10 ) ;
63
- priorityQueue . changePriority ( 10 , 20 ) ;
68
+ priorityQueue . changePriority ( JOB3 , 10 ) ;
69
+ priorityQueue . changePriority ( JOB1 , 20 ) ;
64
70
65
- expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
66
- expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
67
- expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
68
- expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
71
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB4 ) ;
72
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB2 ) ;
73
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB3 ) ;
74
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB1 ) ;
69
75
} ) ;
70
76
71
77
it ( 'should be possible to change priority along with node addition' , ( ) => {
72
78
const priorityQueue = new PriorityQueue ( ) ;
73
79
74
- priorityQueue . add ( 10 , 1 ) ;
75
- priorityQueue . add ( 5 , 2 ) ;
76
- priorityQueue . add ( 100 , 0 ) ;
77
- priorityQueue . add ( 200 , 0 ) ;
80
+ priorityQueue . add ( JOB1 , 1 ) ;
81
+ priorityQueue . add ( JOB2 , 2 ) ;
82
+ priorityQueue . add ( JOB3 , 0 ) ;
83
+ priorityQueue . add ( JOB4 , 0 ) ;
78
84
79
- priorityQueue . changePriority ( 200 , 10 ) ;
80
- priorityQueue . changePriority ( 10 , 20 ) ;
85
+ priorityQueue . changePriority ( JOB4 , 10 ) ;
86
+ priorityQueue . changePriority ( JOB1 , 20 ) ;
81
87
82
- priorityQueue . add ( 15 , 15 ) ;
88
+ priorityQueue . add ( JOB5 , 15 ) ;
83
89
84
- expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
85
- expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
86
- expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
87
- expect ( priorityQueue . poll ( ) ) . toBe ( 15 ) ;
88
- expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
90
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB3 ) ;
91
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB2 ) ;
92
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB4 ) ;
93
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB5 ) ;
94
+ expect ( priorityQueue . poll ( ) ) . toBe ( JOB1 ) ;
89
95
} ) ;
90
96
91
97
it ( 'should be possible to search in priority queue by value' , ( ) => {
92
98
const priorityQueue = new PriorityQueue ( ) ;
93
99
94
- priorityQueue . add ( 10 , 1 ) ;
95
- priorityQueue . add ( 5 , 2 ) ;
96
- priorityQueue . add ( 100 , 0 ) ;
97
- priorityQueue . add ( 200 , 0 ) ;
98
- priorityQueue . add ( 15 , 15 ) ;
100
+ priorityQueue . add ( JOB1 , 1 ) ;
101
+ priorityQueue . add ( JOB2 , 2 ) ;
102
+ priorityQueue . add ( JOB3 , 0 ) ;
103
+ priorityQueue . add ( JOB4 , 0 ) ;
104
+ priorityQueue . add ( JOB5 , 15 ) ;
105
+
106
+ const job6 = { type : 'job6' } ;
107
+
108
+ expect ( priorityQueue . hasValue ( job6 ) ) . toBe ( false ) ;
109
+ expect ( priorityQueue . hasValue ( JOB5 ) ) . toBe ( true ) ;
110
+ } ) ;
111
+
112
+ it ( 'should accept a custom compareValue function' , ( ) => {
113
+ const compareByType = ( a , b ) => {
114
+ if ( a . type === b . type ) {
115
+ return 0 ;
116
+ }
117
+
118
+ return a . type < b . type ? - 1 : 1 ;
119
+ } ;
120
+
121
+ const priorityQueue = new PriorityQueue ( compareByType ) ;
122
+
123
+ priorityQueue . add ( JOB1 , 1 ) ;
124
+ priorityQueue . add ( JOB2 , 2 ) ;
125
+ priorityQueue . add ( JOB3 , 0 ) ;
126
+
127
+ const existingJobType = { type : 'job1' } ;
128
+ const newJobType = { type : 'job4' } ;
99
129
100
- expect ( priorityQueue . hasValue ( 70 ) ) . toBe ( false ) ;
101
- expect ( priorityQueue . hasValue ( 15 ) ) . toBe ( true ) ;
130
+ expect ( priorityQueue . hasValue ( existingJobType ) ) . toBe ( true ) ;
131
+ expect ( priorityQueue . hasValue ( newJobType ) ) . toBe ( false ) ;
102
132
} ) ;
103
133
} ) ;
0 commit comments