Skip to content

Commit

Permalink
Enable bytes tests and add bytesRepeat (denoland#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed May 24, 2019
1 parent 7a722ce commit bd46d60
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
34 changes: 34 additions & 0 deletions bytes/bytes.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}
43 changes: 41 additions & 2 deletions bytes/bytes_test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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 changes: 2 additions & 0 deletions bytes/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import "./bytes_test.ts";
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit bd46d60

Please sign in to comment.