-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.ts
132 lines (121 loc) · 3.3 KB
/
index.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
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
import {
LatitudeRange,
LongitudeRange,
TimeStampRange,
_highOrLow,
_calculateBits,
encodeHash,
decodeHash,
HashInput,
Precision
} from './index'
const inputs: HashInput[] = [
// mayflower landing
{
latitude: 41.9584367,
longitude: -70.6619798,
timestamp: -11017900800
},
// Roanoke colony established
{
latitude: 35.8897,
longitude: -75.6615,
timestamp: -12149395200
},
// Siege of bastogne
{
latitude: 50.0,
longitude: 5.7214,
timestamp: -791596800
},
// Apollo 11 Launch
{
latitude: 28.5620,
longitude: -80.57721,
timestamp: -14601600
},
// Woodstock begins
{
latitude: 41.477525,
longitude: -74.36358,
timestamp: -12009600
}
];
describe('Test ranges', () => {
test('Latitude', () => {
expect(LatitudeRange.min).toEqual(-90);
expect(LatitudeRange.max).toEqual(90);
});
test('Longitude', () => {
expect(LongitudeRange.min).toEqual(-180);
expect(LongitudeRange.max).toEqual(180);
});
// test('Timestamp', () => {
// expect(LatitudeRange.min).toEqual(-90);
// expect(LatitudeRange.max).toEqual(90);
// });
});
describe('Test individual functions', () => {
test('highOrLow', () => {
expect(_highOrLow(0,10,3)).toEqual(0);
expect(_highOrLow(3,7,6)).toEqual(1);
})
test('calculateBits', () => {
expect(_calculateBits({min: 0, max: 10}, 4, 3)).toEqual('011')
expect(_calculateBits({min: -10, max: 10}, 4, 3)).toEqual('101')
});
test('decodeHash', () => {
// console.log(decodeHash('Iy07LzspJQAqFCkHJAka'));
// reverseHash()
})
});
describe('Test known hashes', () => {
// console.log(encodeHash(inputs[0], 15));
// console.log('chud');
});
describe('Precision', () => {
test('Exact', () => {
for ( let input of inputs) {
const hash = encodeHash(input, Precision.Exact);
const decoded = decodeHash(hash);
// ~1cm precision
expect(decoded.latitude).toBeCloseTo(input.latitude, 7);
expect(decoded.longitude).toBeCloseTo(input.longitude, 7);
// Nanosecond precision
expect(decoded.timestamp).toBeCloseTo(input.timestamp, 3);
}
});
test('High', () => {
for ( let input of inputs) {
const hash = encodeHash(input, Precision.High);
const decoded = decodeHash(hash);
// ~1m precision
expect(decoded.latitude).toBeCloseTo(input.latitude, 5);
expect(decoded.longitude).toBeCloseTo(input.longitude, 5);
// 100ms precision
expect(decoded.timestamp).toBeCloseTo(input.timestamp, 1);
}
});
test('Mid', () => {
for ( let input of inputs) {
const hash = encodeHash(input, Precision.Mid);
const decoded = decodeHash(hash);
// ~1m precision
expect(decoded.latitude).toBeCloseTo(input.latitude, 5);
expect(decoded.longitude).toBeCloseTo(input.longitude, 5);
// 1m precision
expect(decoded.timestampError).toBeLessThan(60);
}
});
test('Low', () => {
for ( let input of inputs) {
const hash = encodeHash(input, Precision.Low);
const decoded = decodeHash(hash);
// ~1km precision
expect(decoded.latitude).toBeCloseTo(input.latitude, 2);
expect(decoded.longitude).toBeCloseTo(input.longitude, 2);
// 1 month precision
expect(decoded.timestampError).toBeLessThan(60 * 60 * 24 * 30);
}
});
});