-
Notifications
You must be signed in to change notification settings - Fork 2
/
IdGenerator.test.ts
53 lines (46 loc) · 1.59 KB
/
IdGenerator.test.ts
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
import { expect } from 'chai';
import 'mocha';
import {IdGenerator} from './IdGenerator';
describe("Direct result propagation", ()=> {
it('should return ascending id sequence', ()=> {
const gen = new IdGenerator();
let previous = undefined;
for(let i = 0; i < 512; ++i)
{
let value = gen.current();
if (i > 0) {
expect(value > previous!).to.eq(true, 'id must increase!');
}
previous = value;
gen.next();
}
});
it('increments first digit after 36 values', ()=>{
const gen = new IdGenerator();
for(let i = 0; i < 36; ++i)
{
let value = gen.current();
gen.next();
expect(value.substr(0, value.length - 1).replace(/^0+/, '')).to.eq('', 'Only zero before 36');
}
let value = gen.current();
expect(value.replace(/0+/g, '0')).to.eq('010', 'Only zero before 36');
});
it('renumber', ()=>{
const gen = new IdGenerator();
const keys = [ '00000009', '00000003', '00000001', '00000007', '00000008' ];
const byuuid:{[id:string]:string} = {};
for(const k of keys) {
byuuid[k] = k;
}
gen.renumber(keys, byuuid);
expect(byuuid).to.deep.equal({
"00000002": "00000009",
"00000003": "00000003",
"00000001": "00000001",
"00000004": "00000007",
"00000005": "00000008",
});
expect(keys).to.deep.equal(['00000002', '00000003', '00000001', '00000004', '00000005' ]);
});
});