|
16 | 16 | * See the License for the specific language governing permissions and |
17 | 17 | * limitations under the License. |
18 | 18 | */ |
19 | | - |
20 | | -var alloc = require('../../lib/v1/internal/buf').alloc, |
21 | | - packstream = require("../../lib/v1/internal/packstream"), |
22 | | - Integer = require("../../lib/v1/integer").default, |
23 | | - Packer = packstream.Packer, |
24 | | - Unpacker = packstream.Unpacker, |
25 | | - Structure = packstream.Structure; |
26 | 19 |
|
27 | | -describe('packstream', function() { |
| 20 | +import {alloc} from '../../src/v1/internal/buf'; |
| 21 | +import {Packer, Structure, Unpacker} from '../../src/v1/internal/packstream'; |
| 22 | +import {int} from '../../src/v1'; |
28 | 23 |
|
| 24 | +describe('packstream', () => { |
29 | 25 |
|
30 | | - it('should pack integers', function() { |
31 | | - var n, i; |
| 26 | + it('should pack integers', () => { |
| 27 | + let n, i; |
32 | 28 | // test small numbers |
33 | | - for(n = -999; n <= 999; n += 1) { |
34 | | - i = Integer.fromNumber(n); |
35 | | - expect( packAndUnpack( i ).toString() ).toBe( i.toString() ); |
| 29 | + for (n = -999; n <= 999; n += 1) { |
| 30 | + i = int(n); |
| 31 | + expect(packAndUnpack(i).toString()).toBe(i.toString()); |
36 | 32 | } |
37 | 33 | // positive numbers |
38 | | - for(n = 16; n <= 16 ; n += 1) { |
39 | | - i = Integer.fromNumber(Math.pow(2, n)); |
40 | | - expect( packAndUnpack( i ).toString() ).toBe( i.toString() ); |
| 34 | + for (n = 16; n <= 16; n += 1) { |
| 35 | + i = int(Math.pow(2, n)); |
| 36 | + expect(packAndUnpack(i).toString()).toBe(i.toString()); |
41 | 37 | } |
42 | 38 | // negative numbers |
43 | | - for(n = 0; n <= 63 ; n += 1) { |
44 | | - i = Integer.fromNumber(-Math.pow(2, n)); |
45 | | - expect( packAndUnpack( i ).toString() ).toBe( i.toString() ); |
| 39 | + for (n = 0; n <= 63; n += 1) { |
| 40 | + i = int(-Math.pow(2, n)); |
| 41 | + expect(packAndUnpack(i).toString()).toBe(i.toString()); |
46 | 42 | } |
47 | 43 | }); |
48 | | - it('should pack strings', function() { |
49 | | - expect( packAndUnpack( "" ) ).toBe( "" ); |
50 | | - expect( packAndUnpack( "abcdefg123567" ) ).toBe( "abcdefg123567" ); |
51 | | - var str = Array(65536 + 1).join('a'); // 2 ^ 16 + 1 |
52 | | - expect( packAndUnpack(str, str.length + 8)).toBe(str); |
| 44 | + |
| 45 | + it('should pack strings', () => { |
| 46 | + expect(packAndUnpack('')).toBe(''); |
| 47 | + expect(packAndUnpack('abcdefg123567')).toBe('abcdefg123567'); |
| 48 | + const str = Array(65536 + 1).join('a'); // 2 ^ 16 + 1 |
| 49 | + expect(packAndUnpack(str, str.length + 8)).toBe(str); |
53 | 50 | }); |
54 | | - it('should pack structures', function() { |
55 | | - expect( packAndUnpack( new Structure(1, ["Hello, world!!!"] ) ).fields[0] ) |
56 | | - .toBe( "Hello, world!!!" ); |
| 51 | + |
| 52 | + it('should pack structures', () => { |
| 53 | + expect(packAndUnpack(new Structure(1, ['Hello, world!!!'])).fields[0]) |
| 54 | + .toBe('Hello, world!!!'); |
57 | 55 | }); |
58 | | - it('should pack lists', function() { |
59 | | - var list = ['a', 'b']; |
60 | | - var roundtripped = packAndUnpack( list ); |
61 | | - expect( roundtripped[0] ).toBe( list[0] ); |
62 | | - expect( roundtripped[1] ).toBe( list[1] ); |
| 56 | + |
| 57 | + it('should pack lists', () => { |
| 58 | + const list = ['a', 'b']; |
| 59 | + const unpacked = packAndUnpack(list); |
| 60 | + expect(unpacked[0]).toBe(list[0]); |
| 61 | + expect(unpacked[1]).toBe(list[1]); |
63 | 62 | }); |
64 | 63 |
|
65 | | - it('should pack long lists', function() { |
66 | | - var listLength = 256; |
67 | | - var list = []; |
68 | | - for(var i = 0; i < listLength; i++) { |
69 | | - list.push(null) |
| 64 | + it('should pack long lists', () => { |
| 65 | + const listLength = 256; |
| 66 | + const list = []; |
| 67 | + for (let i = 0; i < listLength; i++) { |
| 68 | + list.push(null); |
70 | 69 | } |
71 | | - var roundtripped = packAndUnpack( list, 1400 ); |
72 | | - expect( roundtripped[0] ).toBe( list[0] ); |
73 | | - expect( roundtripped[1] ).toBe( list[1] ); |
| 70 | + const unpacked = packAndUnpack(list, 1400); |
| 71 | + expect(unpacked[0]).toBe(list[0]); |
| 72 | + expect(unpacked[1]).toBe(list[1]); |
74 | 73 | }); |
75 | 74 | }); |
76 | 75 |
|
77 | | -function packAndUnpack( val, bufferSize ) { |
| 76 | +function packAndUnpack(val, bufferSize) { |
78 | 77 | bufferSize = bufferSize || 128; |
79 | | - var buffer = alloc(bufferSize); |
80 | | - new Packer( buffer ).packable( val )(); |
| 78 | + const buffer = alloc(bufferSize); |
| 79 | + new Packer(buffer).packable(val)(); |
81 | 80 | buffer.reset(); |
82 | | - return new Unpacker().unpack( buffer ); |
| 81 | + return new Unpacker().unpack(buffer); |
83 | 82 | } |
0 commit comments