This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
PN-Counter.test.js
275 lines (247 loc) · 8.36 KB
/
PN-Counter.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import assert from 'assert'
import { GCounter, PNCounter } from '../src/index.js'
describe('PN-Counter', () => {
describe('Instance', () => {
describe('constructor', () => {
it('creates a counter', () => {
const counter = new PNCounter('A')
assert(counter, null)
assert.notEqual(counter.id, null)
assert.notEqual(counter.p, null)
assert.notEqual(counter.n, null)
})
it('creates two GCounters', () => {
const gcounters = new PNCounter()
assert.notEqual(gcounters.p, null)
assert.notEqual(gcounters.n, null)
assert.equal(gcounters.p instanceof GCounter, true)
assert.equal(gcounters.n instanceof GCounter, true)
})
})
describe('value', () => {
it('returns the count', () => {
const counter = new PNCounter('A')
assert.equal(counter.value, 0)
})
it('returns the count after increment', () => {
const counter = new PNCounter('A')
counter.increment(5)
assert.equal(counter.value, 5)
})
it('returns the count after increment and decrement', () => {
const counter = new PNCounter('A')
counter.increment(5)
counter.decrement(3)
assert.equal(counter.value, 2)
})
})
describe('increment', () => {
it('increments the count by 1', () => {
const counter = new PNCounter('A')
counter.increment()
assert.equal(counter.value, 1)
})
it('increments the count by 2', () => {
const counter = new PNCounter('A')
counter.increment()
counter.increment()
assert.equal(counter.value, 2)
})
it('increments the count by 3', () => {
const counter = new PNCounter('A')
counter.increment(3)
assert.equal(counter.value, 3)
})
it('increments the count by 42', () => {
const counter = new PNCounter('A')
counter.increment()
counter.increment(42)
assert.equal(counter.value, 43)
})
it('can\'t increment the counter by negative number', () => {
const counter = new PNCounter('A')
counter.increment(-1)
assert.equal(counter.value, 0)
})
})
describe('decrement', () => {
it('decrements the count by 1', () => {
const counter = new PNCounter('A')
counter.decrement()
assert.equal(counter.value, -1)
})
it('increments the count by 2 then decrements by 1', () => {
const counter = new PNCounter('A')
counter.increment()
counter.increment()
counter.decrement()
assert.equal(counter.value, 1)
})
it('increments the count by 3 then decrement by 2', () => {
const counter = new PNCounter('A')
counter.increment(3)
counter.decrement(2)
assert.equal(counter.value, 1)
})
it('increment the count by 100 then decrement by 58', () => {
const counter = new PNCounter('A')
counter.increment()
counter.increment(99)
counter.decrement(58)
assert.equal(counter.value, 42)
})
it('can\'t decrement by negative number', () => {
const counter = new PNCounter('A')
counter.decrement(-1)
assert.equal(counter.value, 0)
})
})
describe('merge', () => {
it('merges two counters with same id', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('A')
counter1.increment()
counter2.increment()
counter1.merge(counter2)
assert.equal(counter1.value, 1)
})
it('merges two counters with same values', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
counter1.increment()
counter2.increment()
counter1.merge(counter2)
counter2.merge(counter1)
assert.equal(counter1.value, 2)
assert.equal(counter2.value, 2)
})
it('merges four different counters', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
const counter3 = new PNCounter('C')
const counter4 = new PNCounter('D')
counter1.increment()
counter2.increment(5)
counter3.increment(5)
counter3.decrement(3)
counter4.increment()
counter4.increment()
counter4.decrement()
counter1.merge(counter2)
counter1.merge(counter3)
counter1.merge(counter4)
assert.equal(counter1.value, 9)
})
it('doesn\'t overwrite its own value on merge', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
counter1.increment(3)
counter2.increment()
counter1.merge(counter2)
counter2.merge(counter1)
counter1.decrement()
counter1.merge(counter2)
assert.equal(counter1.value, 3)
})
it('doesn\'t overwrite others\' values on merge', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
counter1.increment()
counter2.increment()
counter1.merge(counter2)
counter2.merge(counter1)
counter1.increment(2)
counter2.increment()
counter2.decrement(2)
counter1.merge(counter2)
assert.equal(counter1.value, 3)
})
})
describe('toJSON', () => {
it('returns the counter as JSON object', () => {
const counter = new PNCounter('A')
assert.equal(counter.toJSON().id, 'A')
assert.equal(counter.toJSON().p.A, 0)
assert.equal(counter.toJSON().n.A, 0)
})
it('returns a JSON object after a merge', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
counter1.increment()
counter2.increment()
counter1.decrement()
counter2.decrement()
counter1.merge(counter2)
counter2.merge(counter1)
assert.equal(Object.keys(counter1.toJSON().p).length, 2)
assert.equal(Object.keys(counter1.toJSON().n).length, 2)
assert.equal(counter1.toJSON().p.A, 1)
assert.equal(counter1.toJSON().p.B, 1)
assert.equal(counter2.toJSON().p.A, 1)
assert.equal(counter2.toJSON().p.B, 1)
assert.equal(counter1.toJSON().n.A, 1)
assert.equal(counter1.toJSON().n.B, 1)
assert.equal(counter2.toJSON().n.A, 1)
assert.equal(counter2.toJSON().n.B, 1)
})
})
describe('isEqual', () => {
it('returns true for equal counters', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('A')
counter1.increment()
counter2.increment()
counter1.decrement()
counter2.decrement()
assert.equal(counter1.isEqual(counter2), true)
})
it('returns false for unequal counters - different id', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('B')
assert.equal(counter1.isEqual(counter2), false)
})
it('returns false for unequal counters - same id, different counts', () => {
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('A')
counter1.increment()
counter2.increment()
counter2.decrement()
assert.equal(counter1.isEqual(counter2), false)
})
})
})
describe('PNCounter.from', () => {
it('creates a new counter from JSON object', () => {
const counter1 = new PNCounter('A')
counter1.increment()
counter1.decrement()
const gCounter1 = new GCounter('A')
gCounter1.increment()
const gCounter2 = new GCounter('A')
gCounter2.increment()
const input = {
id: 'A',
p: gCounter1,
n: gCounter2
}
const counter2 = PNCounter.from(input)
assert.equal(PNCounter.isEqual(counter1, counter2), true)
assert.equal(counter2.id, 'A')
assert.equal(counter2.value, 0)
})
})
describe('PNCounter.isEqual', () => {
it('returns true if two PNCounters are equal', () => {
const values = ['A', 'B', 'C']
const counter1 = new PNCounter('A')
const counter2 = new PNCounter('A')
const counter3 = new PNCounter('B')
counter1.increment(2)
counter1.decrement()
counter2.increment(2)
counter2.decrement()
assert.equal(PNCounter.isEqual(counter1, counter2), true)
assert.equal(PNCounter.isEqual(counter1, counter3), false)
})
})
})