Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/test/node/variableLengthQuantity.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { describe, expect, it } from 'vitest';
7
import { readVariableLengthQuantity, writeVariableLengthQuantity } from '../../common/variableLengthQuantity';
8
9
describe('variableLengthQuantity', () => {
10
it('is sane', () => {
11
const numbers = [
12
-100000,
13
-100,
14
-1,
15
0,
16
1,
17
100,
18
100000,
19
];
20
21
for (const n of numbers) {
22
const b = writeVariableLengthQuantity(n);
23
const { value, consumed } = readVariableLengthQuantity(b, 0);
24
expect(value).toBe(n);
25
expect(consumed).toBe(b.buffer.length);
26
}
27
});
28
29
it('is fuzzy', () => {
30
for (let i = 0; i < 1000; i++) {
31
const x = Math.round((Math.random() * 2 ** 31) * (Math.random() < 0.5 ? -1 : 1));
32
33
const b = writeVariableLengthQuantity(x);
34
const { value, consumed } = readVariableLengthQuantity(b, 0);
35
expect(value).toBe(x);
36
expect(consumed).toBe(b.buffer.length);
37
}
38
});
39
});
40
41