|
11 | 11 | */ |
12 | 12 | import { BLOCK_MAXSIZE } from "rt/common"; |
13 | 13 |
|
| 14 | +// Helper function to quickly create a Buffer from an array. |
| 15 | +//@ts-ignore |
| 16 | +function create<T>(values: valueof<T>[]): T { |
| 17 | + let result = instantiate<T>(values.length); |
| 18 | + //@ts-ignore |
| 19 | + for (let i = 0; i < values.length; i++) result[i] = values[i]; |
| 20 | + return result; |
| 21 | +} |
| 22 | + |
14 | 23 | describe("buffer", () => { |
15 | 24 | test("#constructor", () => { |
16 | 25 | expect<Buffer>(new Buffer(0)).toBeTruthy(); |
@@ -58,50 +67,150 @@ describe("buffer", () => { |
58 | 67 | expect<bool>(Buffer.isBuffer<Buffer | null>(null)).toBeFalsy(); |
59 | 68 | }); |
60 | 69 |
|
| 70 | + test("#readInt8", () => { |
| 71 | + let buff = create<Buffer>([0x5,0x0,0x0,0x0,0xFF]); |
| 72 | + expect<i8>(buff.readInt8()).toBe(5); |
| 73 | + // Testing offset, and casting between u8 and i8. |
| 74 | + expect<i8>(buff.readInt8(4)).toBe(-1); |
| 75 | + // TODO: |
| 76 | + // expectFn(() => { |
| 77 | + // let newBuff = new Buffer(1); |
| 78 | + // newBuff.readInt8(5); |
| 79 | + // }).toThrow(); |
| 80 | + }); |
| 81 | + |
61 | 82 | test("#readUInt8", () => { |
62 | | - let buff = new Buffer(10); |
63 | | - buff[0] = -2; |
64 | | - buff[9] = 47; |
| 83 | + let buff = create<Buffer>([0xFE,0x0,0x0,0x0,0x2F]); |
65 | 84 | // Testing casting between u8 and i8. |
66 | | - expect<u8>(buff.readUInt8(0)).toBe(254); |
67 | 85 | expect<u8>(buff.readUInt8()).toBe(254); |
68 | 86 | // Testing offset |
69 | | - expect<u8>(buff.readUInt8(9)).toBe(47); |
| 87 | + expect<u8>(buff.readUInt8(4)).toBe(47); |
70 | 88 | // TODO: |
71 | 89 | // expectFn(() => { |
72 | 90 | // let newBuff = new Buffer(1); |
73 | 91 | // newBuff.readUInt8(5); |
74 | 92 | // }).toThrow(); |
75 | 93 | }); |
76 | 94 |
|
| 95 | + test("#writeInt8", () => { |
| 96 | + let buff = new Buffer(5); |
| 97 | + expect<i32>(buff.writeInt8(9)).toBe(1); |
| 98 | + expect<i32>(buff.writeInt8(-3,4)).toBe(5); |
| 99 | + let result = create<Buffer>([0x09, 0x0, 0x0, 0x0, 0xFD]); |
| 100 | + expect<Buffer>(buff).toStrictEqual(result); |
| 101 | + // TODO: |
| 102 | + // expectFn(() => { |
| 103 | + // let newBuff = new Buffer(1); |
| 104 | + // newBuff.writeInt8(5,10); |
| 105 | + // }).toThrow(); |
| 106 | + }); |
| 107 | + |
77 | 108 | test("#writeUInt8", () => { |
78 | 109 | let buff = new Buffer(5); |
79 | 110 | expect<i32>(buff.writeUInt8(4)).toBe(1); |
80 | 111 | expect<i32>(buff.writeUInt8(252,4)).toBe(5); |
81 | | - expect<u8>(buff[0]).toBe(4); |
82 | | - expect<u8>(buff[4]).toBe(252); |
| 112 | + let result = create<Buffer>([0x04, 0x0, 0x0, 0x0, 0xFC]); |
| 113 | + expect<Buffer>(buff).toStrictEqual(result); |
| 114 | + // TODO: |
| 115 | + // expectFn(() => { |
| 116 | + // let newBuff = new Buffer(1); |
| 117 | + // newBuff.writeUInt8(5,10); |
| 118 | + // }).toThrow(); |
| 119 | + }); |
| 120 | + |
| 121 | + test("#readInt16LE", () => { |
| 122 | + let buff = create<Buffer>([0x0,0x05,0x0]); |
| 123 | + expect<i16>(buff.readInt16LE()).toBe(1280); |
| 124 | + expect<i16>(buff.readInt16LE(1)).toBe(5); |
| 125 | + // TODO: |
| 126 | + // expectFn(() => { |
| 127 | + // let newBuff = new Buffer(1); |
| 128 | + // newBuff.readInt16LE(0); |
| 129 | + // }).toThrow(); |
83 | 130 | }); |
84 | 131 |
|
85 | | - test("#writeInt8", () => { |
86 | | - let buff = new Buffer(5); |
87 | | - expect<i32>(buff.writeInt8(9)).toBe(1); |
88 | | - expect<i32>(buff.writeInt8(-3,4)).toBe(5); |
89 | | - expect<i8>(buff[0]).toBe(9); |
90 | | - expect<i8>(buff[4]).toBe(-3); |
| 132 | + test("#readInt16BE", () => { |
| 133 | + let buff = create<Buffer>([0x0,0x05,0x0]); |
| 134 | + expect<i16>(buff.readInt16BE()).toBe(5); |
| 135 | + expect<i16>(buff.readInt16BE(1)).toBe(1280); |
| 136 | + // TODO: |
| 137 | + // expectFn(() => { |
| 138 | + // let newBuff = new Buffer(1); |
| 139 | + // newBuff.readInt16BE(0); |
| 140 | + // }).toThrow(); |
91 | 141 | }); |
92 | 142 |
|
93 | | - test("#readInt8", () => { |
94 | | - let buff = new Buffer(10); |
95 | | - buff[0] = 5; |
96 | | - buff[9] = 255; |
97 | | - expect<i8>(buff.readInt8(0)).toBe(5); |
98 | | - expect<i8>(buff.readInt8()).toBe(5); |
99 | | - // Testing offset, and casting between u8 and i8. |
100 | | - expect<i8>(buff.readInt8(9)).toBe(-1); |
| 143 | + test("#readUInt16LE", () => { |
| 144 | + let buff = create<Buffer>([0x0,0x05,0x0]); |
| 145 | + expect<u16>(buff.readUInt16LE()).toBe(1280); |
| 146 | + expect<u16>(buff.readUInt16LE(1)).toBe(5); |
101 | 147 | // TODO: |
102 | 148 | // expectFn(() => { |
103 | 149 | // let newBuff = new Buffer(1); |
104 | | - // newBuff.readInt8(5); |
| 150 | + // newBuff.readUInt16LE(0); |
| 151 | + // }).toThrow(); |
| 152 | + }); |
| 153 | + |
| 154 | + test("#readUInt16BE", () => { |
| 155 | + let buff = create<Buffer>([0x0,0x05,0x0]); |
| 156 | + expect<i16>(buff.readUInt16BE()).toBe(5); |
| 157 | + expect<i16>(buff.readUInt16BE(1)).toBe(1280); |
| 158 | + // TODO: |
| 159 | + // expectFn(() => { |
| 160 | + // let newBuff = new Buffer(1); |
| 161 | + // newBuff.readUInt16BE(0); |
| 162 | + // }).toThrow(); |
| 163 | + }); |
| 164 | + |
| 165 | + test("#writeInt16LE", () => { |
| 166 | + let buff = new Buffer(4); |
| 167 | + expect<i32>(buff.writeInt16LE(5)).toBe(2); |
| 168 | + expect<i32>(buff.writeInt16LE(1280,2)).toBe(4); |
| 169 | + let result = create<Buffer>([0x05, 0x0, 0x0, 0x5]); |
| 170 | + expect<Buffer>(buff).toStrictEqual(result); |
| 171 | + // TODO: |
| 172 | + // expectFn(() => { |
| 173 | + // let newBuff = new Buffer(1); |
| 174 | + // newBuff.writeInt16LE(0); |
| 175 | + // }).toThrow(); |
| 176 | + }); |
| 177 | + |
| 178 | + test("#writeInt16BE", () => { |
| 179 | + let buff = new Buffer(4); |
| 180 | + expect<i32>(buff.writeInt16BE(1280)).toBe(2); |
| 181 | + expect<i32>(buff.writeInt16BE(5,2)).toBe(4); |
| 182 | + let result = create<Buffer>([0x05, 0x0, 0x0, 0x5]); |
| 183 | + expect<Buffer>(buff).toStrictEqual(result); |
| 184 | + // TODO: |
| 185 | + // expectFn(() => { |
| 186 | + // let newBuff = new Buffer(1); |
| 187 | + // newBuff.writeInt16BE(0); |
| 188 | + // }).toThrow(); |
| 189 | + }); |
| 190 | + |
| 191 | + test("#writeUInt16LE", () => { |
| 192 | + let buff = new Buffer(4); |
| 193 | + expect<i32>(buff.writeUInt16LE(5)).toBe(2); |
| 194 | + expect<i32>(buff.writeUInt16LE(1280,2)).toBe(4); |
| 195 | + let result = create<Buffer>([0x05, 0x0, 0x0, 0x5]); |
| 196 | + expect<Buffer>(buff).toStrictEqual(result); |
| 197 | + // TODO: |
| 198 | + // expectFn(() => { |
| 199 | + // let newBuff = new Buffer(1); |
| 200 | + // newBuff.writeUInt16LE(0); |
| 201 | + // }).toThrow(); |
| 202 | + }); |
| 203 | + |
| 204 | + test("#writeUInt16BE", () => { |
| 205 | + let buff = new Buffer(4); |
| 206 | + expect<i32>(buff.writeUInt16BE(1280)).toBe(2); |
| 207 | + expect<i32>(buff.writeUInt16BE(5,2)).toBe(4); |
| 208 | + let result = create<Buffer>([0x05, 0x0, 0x0, 0x5]); |
| 209 | + expect<Buffer>(buff).toStrictEqual(result); |
| 210 | + // TODO: |
| 211 | + // expectFn(() => { |
| 212 | + // let newBuff = new Buffer(1); |
| 213 | + // newBuff.writeUInt16BE(0); |
105 | 214 | // }).toThrow(); |
106 | 215 | }); |
107 | 216 | }); |
0 commit comments