/** * Copyright (c) 2022 EdgerOS Team. * All rights reserved. * * Detailed license information can be found in the LICENSE file. * * Author : xueqiang * Date : 2024-08-07 18:32:55 * LastEditors : xueqiang * LastEditTime : 2024-08-14 14:15:23 */ import { Range } from '../' import { test } from '@edgeros/tapes' import assert from 'assert' test('Range - prefix - generates prefixes for an empty string', (t) => { t.plan(3) const range = Range.prefix(Buffer.from([])) t.deepEqual(range.start, Buffer.from([0])) t.deepEqual(range.end, Buffer.from([0])) t.pass('Range - prefix - generates prefixes for an empty string - success') t.end() }) test('Range - generates prefixes for a "normal" string', (t) => { t.plan(3) const range = Range.prefix(Buffer.from([1, 2])) t.deepEqual(range.start, Buffer.from([1, 2])) t.deepEqual(range.end, Buffer.from([1, 3])) t.pass('Range - generates prefixes for a "normal" string - success') t.end() }) test('Range - rolls on a high end-bit', (t) => { t.plan(3) const range = Range.prefix(Buffer.from([1, 255])) t.deepEqual(range.start, Buffer.from([1, 255])) t.deepEqual(range.end, Buffer.from([2])) t.pass('Range - rolls on a high end-bit - success') t.end() }) test('Range - aborts on all high bits', (t) => { t.plan(3) const range = Range.prefix(Buffer.from([255, 255])) t.deepEqual(range.start, Buffer.from([255, 255])) t.deepEqual(range.end, Buffer.from([0])) t.pass('Range - aborts on all high bits - success') t.end() }) test('comparisons - compares ranges', (t) => { t.plan(2) const prefix = [] for (let i = 0; i < 10; i += 1) { prefix.push(Buffer.from([i])) } const r = new Range(prefix[2], prefix[5]) assert.equal(r.compare(new Range(Buffer.alloc(0), Buffer.alloc(0))), -1) assert.equal(r.compare(new Range(prefix[2], prefix[5])), 0) assert.equal(r.compare(new Range(prefix[3], prefix[6])), 0) assert.equal(r.compare(new Range(prefix[0], prefix[4])), 0) assert.equal(r.compare(new Range(prefix[0], prefix[9])), 0) assert.equal(r.compare(new Range(prefix[3], prefix[4])), 0) assert.equal(r.compare(new Range(prefix[5], prefix[7])), -1) assert.equal(r.compare(new Range(prefix[0], prefix[1])), 1) t.pass('comparisons - compares ranges - ') assert.ok(!r.includes(prefix[1])) assert.ok(r.includes(prefix[2])) assert.ok(!r.includes(prefix[5])) t.pass('comparisons - checks if a key is included - ') t.end() }) test('Range - from', (t) => { t.plan(4) const range = Range.from({ start: 'p', prefix: 'p', }) t.equal(range.end.toString(), 'q', 'Range - - from - rangableIsPrefix') const range1 = Range.from('p') t.equal(range1.start.toString(), 'p', 'Range - - from - string') const range2 = Range.from(range1) t.equal(range2, range1, 'Range - - from - string') const range3 = Range.from({ start: 'start', end: 'end', }) t.ok((range3.start.toString() === 'start' && range3.end.toString() === 'end'), 'Range - - from - string') t.end() })