[79a0317] | 1 | import * as assert from 'assert';
|
---|
| 2 | import test, { describe } from 'node:test';
|
---|
| 3 | import parse from '../parse.js';
|
---|
| 4 | import stringify from '../stringify.js';
|
---|
| 5 | import uuidv4 from '../v4.js';
|
---|
| 6 | function splitmix32(a) {
|
---|
| 7 | return function () {
|
---|
| 8 | a |= 0;
|
---|
| 9 | a = (a + 0x9e3779b9) | 0;
|
---|
| 10 | let t = a ^ (a >>> 16);
|
---|
| 11 | t = Math.imul(t, 0x21f0aaad);
|
---|
| 12 | t = t ^ (t >>> 15);
|
---|
| 13 | t = Math.imul(t, 0x735a2d97);
|
---|
| 14 | return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
|
---|
| 15 | };
|
---|
| 16 | }
|
---|
| 17 | const rand = splitmix32(0x12345678);
|
---|
| 18 | function rng(bytes = new Uint8Array(16)) {
|
---|
| 19 | for (let i = 0; i < 16; i++) {
|
---|
| 20 | bytes[i] = rand() * 256;
|
---|
| 21 | }
|
---|
| 22 | return bytes;
|
---|
| 23 | }
|
---|
| 24 | describe('parse', () => {
|
---|
| 25 | test('String -> bytes parsing', () => {
|
---|
| 26 | assert.deepStrictEqual(parse('0f5abcd1-c194-47f3-905b-2df7263a084b'), Uint8Array.from([
|
---|
| 27 | 0x0f, 0x5a, 0xbc, 0xd1, 0xc1, 0x94, 0x47, 0xf3, 0x90, 0x5b, 0x2d, 0xf7, 0x26, 0x3a, 0x08,
|
---|
| 28 | 0x4b,
|
---|
| 29 | ]));
|
---|
| 30 | });
|
---|
| 31 | test('String -> bytes -> string symmetry for assorted uuids', () => {
|
---|
| 32 | for (let i = 0; i < 1000; i++) {
|
---|
| 33 | const uuid = uuidv4({ rng });
|
---|
| 34 | assert.equal(stringify(parse(uuid)), uuid);
|
---|
| 35 | }
|
---|
| 36 | });
|
---|
| 37 | test('Case neutrality', () => {
|
---|
| 38 | assert.deepStrictEqual(parse('0f5abcd1-c194-47f3-905b-2df7263a084b'), parse('0f5abcd1-c194-47f3-905b-2df7263a084b'.toUpperCase()));
|
---|
| 39 | });
|
---|
| 40 | test('Null UUID case', () => {
|
---|
| 41 | assert.deepStrictEqual(parse('00000000-0000-0000-0000-000000000000'), Uint8Array.from(new Array(16).fill(0)));
|
---|
| 42 | });
|
---|
| 43 | test('UUID validation', () => {
|
---|
| 44 | assert.throws(() => parse());
|
---|
| 45 | assert.throws(() => parse('invalid uuid'));
|
---|
| 46 | assert.throws(() => parse('zyxwvuts-rqpo-nmlk-jihg-fedcba000000'));
|
---|
| 47 | });
|
---|
| 48 | });
|
---|