1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/*
* A battery of tests to help us read a series of uints
*/
var common = require('../common');
var ASSERT = require('assert');
/*
* We need to check the following things:
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test8(clazz) {
var data = new clazz(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
ASSERT.equal(23, data[0]);
ASSERT.equal(23, data[1]);
ASSERT.equal(23, data[2]);
ASSERT.equal(23, data[3]);
data.writeUInt8(255, 0);
ASSERT.equal(255, data[0]);
data.writeUInt8(255, 0);
ASSERT.equal(255, data[0]);
}
function test16(clazz) {
var value = 0x2343;
var data = new clazz(4);
data.writeUInt16BE(value, 0);
ASSERT.equal(0x23, data[0]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16BE(value, 1);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[2]);
data.writeUInt16BE(value, 2);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[3]);
data.writeUInt16LE(value, 0);
ASSERT.equal(0x23, data[1]);
ASSERT.equal(0x43, data[0]);
data.writeUInt16LE(value, 1);
ASSERT.equal(0x23, data[2]);
ASSERT.equal(0x43, data[1]);
data.writeUInt16LE(value, 2);
ASSERT.equal(0x23, data[3]);
ASSERT.equal(0x43, data[2]);
value = 0xff80;
data.writeUInt16LE(value, 0);
ASSERT.equal(0xff, data[1]);
ASSERT.equal(0x80, data[0]);
data.writeUInt16BE(value, 0);
ASSERT.equal(0xff, data[0]);
ASSERT.equal(0x80, data[1]);
}
function test32(clazz) {
var data = new clazz(6);
var value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
ASSERT.equal(0xe7, data[0]);
ASSERT.equal(0xf9, data[1]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[3]);
data.writeUInt32BE(value, 1);
ASSERT.equal(0xe7, data[1]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[3]);
ASSERT.equal(0x6d, data[4]);
data.writeUInt32BE(value, 2);
ASSERT.equal(0xe7, data[2]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[4]);
ASSERT.equal(0x6d, data[5]);
data.writeUInt32LE(value, 0);
ASSERT.equal(0xe7, data[3]);
ASSERT.equal(0xf9, data[2]);
ASSERT.equal(0x0a, data[1]);
ASSERT.equal(0x6d, data[0]);
data.writeUInt32LE(value, 1);
ASSERT.equal(0xe7, data[4]);
ASSERT.equal(0xf9, data[3]);
ASSERT.equal(0x0a, data[2]);
ASSERT.equal(0x6d, data[1]);
data.writeUInt32LE(value, 2);
ASSERT.equal(0xe7, data[5]);
ASSERT.equal(0xf9, data[4]);
ASSERT.equal(0x0a, data[3]);
ASSERT.equal(0x6d, data[2]);
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
|