2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-05-24 08:28:31 -04:00
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
import {
|
|
|
|
findIndex,
|
|
|
|
findLastIndex,
|
|
|
|
equal,
|
|
|
|
hasPrefix,
|
2020-05-20 10:32:28 -04:00
|
|
|
hasSuffix,
|
2020-03-22 14:49:09 -04:00
|
|
|
repeat,
|
2020-03-28 13:03:49 -04:00
|
|
|
concat,
|
2020-05-20 10:32:28 -04:00
|
|
|
contains,
|
2020-06-25 06:40:51 -04:00
|
|
|
copyBytes,
|
2020-03-22 14:49:09 -04:00
|
|
|
} from "./mod.ts";
|
|
|
|
import { assertEquals, assertThrows, assert } from "../testing/asserts.ts";
|
2020-04-01 15:23:39 -04:00
|
|
|
import { encode, decode } from "../encoding/utf8.ts";
|
2019-05-24 08:28:31 -04:00
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] findIndex1", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const i = findIndex(
|
|
|
|
new Uint8Array([1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
|
|
|
new Uint8Array([0, 1, 2])
|
|
|
|
);
|
|
|
|
assertEquals(i, 2);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] findIndex2", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const i = findIndex(new Uint8Array([0, 0, 1]), new Uint8Array([0, 1]));
|
|
|
|
assertEquals(i, 1);
|
|
|
|
});
|
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
Deno.test("[bytes] findIndex3", () => {
|
|
|
|
const i = findIndex(encode("Deno"), encode("D"));
|
|
|
|
assertEquals(i, 0);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] findLastIndex1", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const i = findLastIndex(
|
|
|
|
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
|
|
|
|
new Uint8Array([0, 1, 2])
|
|
|
|
);
|
|
|
|
assertEquals(i, 3);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] findLastIndex2", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const i = findLastIndex(new Uint8Array([0, 1, 1]), new Uint8Array([0, 1]));
|
|
|
|
assertEquals(i, 0);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] equal", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const v = equal(new Uint8Array([0, 1, 2, 3]), new Uint8Array([0, 1, 2, 3]));
|
|
|
|
assertEquals(v, true);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] hasPrefix", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
const v = hasPrefix(new Uint8Array([0, 1, 2]), new Uint8Array([0, 1]));
|
|
|
|
assertEquals(v, true);
|
|
|
|
});
|
|
|
|
|
2020-05-20 10:32:28 -04:00
|
|
|
Deno.test("[bytes] hasSuffix", () => {
|
|
|
|
const v = hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
|
|
|
|
assertEquals(v, true);
|
|
|
|
});
|
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
Deno.test("[bytes] repeat", () => {
|
2019-05-24 08:28:31 -04:00
|
|
|
// 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],
|
2020-03-28 13:03:49 -04:00
|
|
|
["abc ", "abc abc abc ", 3],
|
2019-05-24 08:28:31 -04:00
|
|
|
];
|
|
|
|
for (const [input, output, count, errMsg] of repeatTestCase) {
|
|
|
|
if (errMsg) {
|
|
|
|
assertThrows(
|
|
|
|
(): void => {
|
|
|
|
repeat(new TextEncoder().encode(input as string), count as number);
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
errMsg as string
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const newBytes = repeat(
|
|
|
|
new TextEncoder().encode(input as string),
|
|
|
|
count as number
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(new TextDecoder().decode(newBytes), output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-03-22 14:49:09 -04:00
|
|
|
|
|
|
|
Deno.test("[bytes] concat", () => {
|
|
|
|
const u1 = encode("Hello ");
|
|
|
|
const u2 = encode("World");
|
|
|
|
const joined = concat(u1, u2);
|
|
|
|
assertEquals(decode(joined), "Hello World");
|
|
|
|
assert(u1 !== joined);
|
|
|
|
assert(u2 !== joined);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[bytes] concat empty arrays", () => {
|
|
|
|
const u1 = new Uint8Array();
|
|
|
|
const u2 = new Uint8Array();
|
|
|
|
const joined = concat(u1, u2);
|
|
|
|
assertEquals(joined.byteLength, 0);
|
|
|
|
assert(u1 !== joined);
|
|
|
|
assert(u2 !== joined);
|
|
|
|
});
|
2020-05-20 10:32:28 -04:00
|
|
|
|
|
|
|
Deno.test("[bytes] contain", () => {
|
|
|
|
const source = encode("deno.land");
|
|
|
|
const pattern = encode("deno");
|
|
|
|
assert(contains(source, pattern));
|
|
|
|
|
|
|
|
assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
|
|
|
|
});
|
2020-06-25 06:40:51 -04:00
|
|
|
|
|
|
|
Deno.test("[io/tuil] copyBytes", function (): void {
|
|
|
|
const dst = new Uint8Array(4);
|
|
|
|
|
|
|
|
dst.fill(0);
|
|
|
|
let src = Uint8Array.of(1, 2);
|
|
|
|
let len = copyBytes(src, dst, 0);
|
|
|
|
assert(len === 2);
|
|
|
|
assertEquals(dst, Uint8Array.of(1, 2, 0, 0));
|
|
|
|
|
|
|
|
dst.fill(0);
|
|
|
|
src = Uint8Array.of(1, 2);
|
|
|
|
len = copyBytes(src, dst, 1);
|
|
|
|
assert(len === 2);
|
|
|
|
assertEquals(dst, Uint8Array.of(0, 1, 2, 0));
|
|
|
|
|
|
|
|
dst.fill(0);
|
|
|
|
src = Uint8Array.of(1, 2, 3, 4, 5);
|
|
|
|
len = copyBytes(src, dst);
|
|
|
|
assert(len === 4);
|
|
|
|
assertEquals(dst, Uint8Array.of(1, 2, 3, 4));
|
|
|
|
|
|
|
|
dst.fill(0);
|
|
|
|
src = Uint8Array.of(1, 2);
|
|
|
|
len = copyBytes(src, dst, 100);
|
|
|
|
assert(len === 0);
|
|
|
|
assertEquals(dst, Uint8Array.of(0, 0, 0, 0));
|
|
|
|
|
|
|
|
dst.fill(0);
|
|
|
|
src = Uint8Array.of(3, 4);
|
|
|
|
len = copyBytes(src, dst, -2);
|
|
|
|
assert(len === 2);
|
|
|
|
assertEquals(dst, Uint8Array.of(3, 4, 0, 0));
|
|
|
|
});
|