Skip to content

Commit 4ab0e0e

Browse files
axetroyry
authored andcommitted
Enable bytes tests and add bytesRepeat (denoland/std#446)
Original: denoland/std@bd46d60
1 parent 0803912 commit 4ab0e0e

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

bytes/bytes.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
import { copyBytes } from "../io/util.ts";
23

34
/** Find first index of binary pattern from a. If not found, then return -1 **/
45
export function bytesFindIndex(a: Uint8Array, pat: Uint8Array): number {
@@ -60,3 +61,36 @@ export function bytesHasPrefix(a: Uint8Array, prefix: Uint8Array): boolean {
6061
}
6162
return true;
6263
}
64+
65+
/**
66+
* Repeat bytes. returns a new byte slice consisting of `count` copies of `b`.
67+
* @param b The origin bytes
68+
* @param count The count you want to repeat.
69+
*/
70+
export function bytesRepeat(b: Uint8Array, count: number): Uint8Array {
71+
if (count === 0) {
72+
return new Uint8Array();
73+
}
74+
75+
if (count < 0) {
76+
throw new Error("bytes: negative repeat count");
77+
} else if ((b.length * count) / count !== b.length) {
78+
throw new Error("bytes: repeat count causes overflow");
79+
}
80+
81+
const int = Math.floor(count);
82+
83+
if (int !== count) {
84+
throw new Error("bytes: repeat count must be an integer");
85+
}
86+
87+
const nb = new Uint8Array(b.length * count);
88+
89+
let bp = copyBytes(nb, b);
90+
91+
for (; bp < nb.length; bp *= 2) {
92+
copyBytes(nb, nb.slice(0, bp), bp);
93+
}
94+
95+
return nb;
96+
}

bytes/bytes_test.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
13
import {
24
bytesFindIndex,
35
bytesFindLastIndex,
46
bytesEqual,
5-
bytesHasPrefix
7+
bytesHasPrefix,
8+
bytesRepeat
69
} from "./bytes.ts";
710
import { test } from "../testing/mod.ts";
8-
import { assertEquals } from "../testing/asserts.ts";
11+
import { assertEquals, assertThrows } from "../testing/asserts.ts";
912

1013
test(function bytesBytesFindIndex1(): void {
1114
const i = bytesFindIndex(
@@ -48,3 +51,39 @@ test(function bytesBytesHasPrefix(): void {
4851
const v = bytesHasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
4952
assertEquals(v, true);
5053
});
54+
55+
test(function bytesBytesRepeat(): void {
56+
// input / output / count / error message
57+
const repeatTestCase = [
58+
["", "", 0],
59+
["", "", 1],
60+
["", "", 1.1, "bytes: repeat count must be an integer"],
61+
["", "", 2],
62+
["", "", 0],
63+
["-", "", 0],
64+
["-", "-", -1, "bytes: negative repeat count"],
65+
["-", "----------", 10],
66+
["abc ", "abc abc abc ", 3]
67+
];
68+
for (const [input, output, count, errMsg] of repeatTestCase) {
69+
if (errMsg) {
70+
assertThrows(
71+
(): void => {
72+
bytesRepeat(
73+
new TextEncoder().encode(input as string),
74+
count as number
75+
);
76+
},
77+
Error,
78+
errMsg as string
79+
);
80+
} else {
81+
const newBytes = bytesRepeat(
82+
new TextEncoder().encode(input as string),
83+
count as number
84+
);
85+
86+
assertEquals(new TextDecoder().decode(newBytes), output);
87+
}
88+
}
89+
});

bytes/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2+
import "./bytes_test.ts";

test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env deno run -A
22
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
33
import "./archive/tar_test.ts";
4+
import "./bytes/test.ts";
45
import "./colors/test.ts";
56
import "./datetime/test.ts";
67
import "./encoding/test.ts";

0 commit comments

Comments
 (0)