|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import { describe, it } from "jsr:@std/testing/bdd"; |
| 6 | +import { TokenIterator } from "./vlq.ts"; |
| 7 | +import { assertFalse, assertStrictEquals } from "jsr:@std/assert"; |
| 8 | + |
| 9 | +describe("TokenIterator", () => { |
| 10 | + describe("nextUnsignedVLQ", () => { |
| 11 | + it("returns zero when no more characters are available", () => { |
| 12 | + const iter = new TokenIterator(""); |
| 13 | + assertFalse(iter.hasNext()); |
| 14 | + |
| 15 | + assertStrictEquals(iter.nextUnsignedVLQ(), 0); |
| 16 | + }); |
| 17 | + |
| 18 | + it("treats unknown characters as 0", () => { |
| 19 | + const iter = new TokenIterator("h,C"); // 'h' has the continuation bit set. ',' is treated as 0 so we stop decoding. |
| 20 | + |
| 21 | + assertStrictEquals(iter.nextUnsignedVLQ(), 1); |
| 22 | + assertStrictEquals(iter.peek(), "C"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("treats unknown unicode characters as 0", () => { |
| 26 | + const iter = new TokenIterator("hæC"); // 'h' has the continuation bit set. 'æ' falls outside the array so has 'undefined as its digit value. |
| 27 | + |
| 28 | + assertStrictEquals(iter.nextUnsignedVLQ(), 1); |
| 29 | + assertStrictEquals(iter.peek(), "C"); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("nextSignedVLQ", () => { |
| 34 | + it("returns zero when no more characters are available", () => { |
| 35 | + const iter = new TokenIterator(""); |
| 36 | + assertFalse(iter.hasNext()); |
| 37 | + |
| 38 | + assertStrictEquals(iter.nextSignedVLQ(), 0); |
| 39 | + }); |
| 40 | + |
| 41 | + it("treats unknown characters as 0", () => { |
| 42 | + const iter = new TokenIterator("i,C"); // 'i' has the continuation bit set. ',' is treated as 0 so we stop decoding. |
| 43 | + |
| 44 | + assertStrictEquals(iter.nextSignedVLQ(), 1); |
| 45 | + assertStrictEquals(iter.peek(), "C"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("treats unknown unicode characters as 0", () => { |
| 49 | + const iter = new TokenIterator("iæC"); // 'i' has the continuation bit set. 'æ' falls outside the array so has 'undefined as its digit value. |
| 50 | + |
| 51 | + assertStrictEquals(iter.nextSignedVLQ(), 1); |
| 52 | + assertStrictEquals(iter.peek(), "C"); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("current", () => { |
| 57 | + it("returns the empty string when calling it without advancing the iterator", () => { |
| 58 | + const iter = new TokenIterator("CCCC"); |
| 59 | + |
| 60 | + assertStrictEquals(iter.currentChar(), ""); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments