0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Enable bytes tests and add bytesRepeat (denoland/deno_std#446)

Original: bd46d60ded
This commit is contained in:
Axetroy 2019-05-24 19:44:13 +08:00 committed by Ryan Dahl
parent 0803912c7f
commit 4ab0e0e918
4 changed files with 78 additions and 2 deletions

View file

@ -1,4 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { copyBytes } from "../io/util.ts";
/** Find first index of binary pattern from a. If not found, then return -1 **/
export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
@ -60,3 +61,36 @@ export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean {
}
return true;
}
/**
* Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
* @param b The origin bytes
* @param count The count you want to repeat.
*/
export function bytesRepeat(b: Uint8Array, count: number): Uint8Array {
if (count === 0) {
return new Uint8Array();
}
if (count < 0) {
throw new Error("bytes: negative repeat count");
} else if ((b.length * count) / count !== b.length) {
throw new Error("bytes: repeat count causes overflow");
}
const int = Math.floor(count);
if (int !== count) {
throw new Error("bytes: repeat count must be an integer");
}
const nb = new Uint8Array(b.length * count);
let bp = copyBytes(nb, b);
for (; bp < nb.length; bp *= 2) {
copyBytes(nb, nb.slice(0, bp), bp);
}
return nb;
}

View file

@ -1,11 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import {
bytesFindIndex,
bytesFindLastIndex,
bytesEqual,
bytesHasPrefix
bytesHasPrefix,
bytesRepeat
} from "./bytes.ts";
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { assertEquals, assertThrows } from "../testing/asserts.ts";
test(function bytesBytesFindIndex1(): void {
const i = bytesFindIndex(
@ -48,3 +51,39 @@ test(function bytesBytesHasPrefix(): void {
const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
assertEquals(v, true);
});
test(function bytesBytesRepeat(): void {
// input / output / count / error message
const repeatTestCase = [
["", "", 0],
["", "", 1],
["", "", 1.1, "bytes: repeat count must be an integer"],
["", "", 2],
["", "", 0],
["-", "", 0],
["-", "-", -1, "bytes: negative repeat count"],
["-", "----------", 10],
["abc ", "abc abc abc ", 3]
];
for (const [input, output, count, errMsg] of repeatTestCase) {
if (errMsg) {
assertThrows(
(): void => {
bytesRepeat(
new TextEncoder().encode(input as string),
count as number
);
},
Error,
errMsg as string
);
} else {
const newBytes = bytesRepeat(
new TextEncoder().encode(input as string),
count as number
);
assertEquals(new TextDecoder().decode(newBytes), output);
}
}
});

2
bytes/test.ts Normal file
View file

@ -0,0 +1,2 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import "./bytes_test.ts";

View file

@ -1,6 +1,7 @@
#!/usr/bin/env deno run -A
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import "./archive/tar_test.ts";
import "./bytes/test.ts";
import "./colors/test.ts";
import "./datetime/test.ts";
import "./encoding/test.ts";