summaryrefslogtreecommitdiff
path: root/net/ioutil_test.ts
diff options
context:
space:
mode:
authorYusuke Sakurai <kerokerokerop@gmail.com>2019-01-07 04:26:18 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-01-06 14:26:18 -0500
commit7907bfc4c91f5287237d87571d1933db4ae7a4fa (patch)
tree6c14062ed9e08bb7543053b760dacd043acd874d /net/ioutil_test.ts
parentc164e696d7f924fe785421058d834934b7014429 (diff)
Add web socket module (denoland/deno_std#84)
Original: https://github.com/denoland/deno_std/commit/2606e295c77fb9d5796d527ed15f2dab3de1a696
Diffstat (limited to 'net/ioutil_test.ts')
-rw-r--r--net/ioutil_test.ts62
1 files changed, 62 insertions, 0 deletions
diff --git a/net/ioutil_test.ts b/net/ioutil_test.ts
new file mode 100644
index 000000000..422901e4a
--- /dev/null
+++ b/net/ioutil_test.ts
@@ -0,0 +1,62 @@
+import { Reader, ReadResult } from "deno";
+import { assertEqual, test } from "../testing/mod.ts";
+import { readInt, readLong, readShort, sliceLongToBytes } from "./ioutil.ts";
+import { BufReader } from "./bufio.ts";
+
+class BinaryReader implements Reader {
+ index = 0;
+
+ constructor(private bytes: Uint8Array = new Uint8Array(0)) {}
+
+ async read(p: Uint8Array): Promise<ReadResult> {
+ p.set(this.bytes.subarray(this.index, p.byteLength));
+ this.index += p.byteLength;
+ return { nread: p.byteLength, eof: false };
+ }
+}
+
+test(async function testReadShort() {
+ const r = new BinaryReader(new Uint8Array([0x12, 0x34]));
+ const short = await readShort(new BufReader(r));
+ assertEqual(short, 0x1234);
+});
+
+test(async function testReadInt() {
+ const r = new BinaryReader(new Uint8Array([0x12, 0x34, 0x56, 0x78]));
+ const int = await readInt(new BufReader(r));
+ assertEqual(int, 0x12345678);
+});
+
+test(async function testReadLong() {
+ const r = new BinaryReader(
+ new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x12, 0x34, 0x56, 0x78])
+ );
+ const long = await readLong(new BufReader(r));
+ assertEqual(long, 0x1234567812345678);
+});
+
+test(async function testReadLong2() {
+ const r = new BinaryReader(
+ new Uint8Array([0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78])
+ );
+ const long = await readLong(new BufReader(r));
+ assertEqual(long, 0x12345678);
+});
+
+test(async function testSliceLongToBytes() {
+ const arr = sliceLongToBytes(0x1234567890abcdef);
+ const actual = readLong(new BufReader(new BinaryReader(new Uint8Array(arr))));
+ const expected = readLong(
+ new BufReader(
+ new BinaryReader(
+ new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef])
+ )
+ )
+ );
+ assertEqual(actual, expected);
+});
+
+test(async function testSliceLongToBytes2() {
+ const arr = sliceLongToBytes(0x12345678);
+ assertEqual(arr, [0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78]);
+});