Skip to content

Commit

Permalink
Complete support for unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Nov 18, 2024
1 parent 1e802a6 commit 648c155
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function encodeFieldValue(buffer, value, offset) {
tag('b');
buffer.writeInt8(val, offset); offset++;
break;
case 'unsigned byte':
case 'unsignedbyte':
case 'uint8':
tag('B');
buffer.writeUInt8(val, offset); offset++;
Expand All @@ -202,6 +202,7 @@ function encodeFieldValue(buffer, value, offset) {
case 'uint16':
tag('u');
buffer.writeUInt16BE(val, offset); offset += 2;
break;
case 'int':
case 'int32':
tag('I');
Expand Down
22 changes: 11 additions & 11 deletions test/codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,16 @@ var testCases = [
['null', {'void': null}, [4,118,111,105,100,86]],

// array, object
['array', {array: [6, true, "foo"]},
[5,97,114,114,97,121,65,0,0,0,12,98,6,116,1,83,0,0,0,3,102,111,111]],
['object', {object: {foo: "bar", baz: 12}},
[6,111,98,106,101,99,116,70,0,0,0,18,3,102,111,111,83,0,
0,0,3,98,97,114,3,98,97,122,98,12]],
['array', {array: [6, true, "foo"]},[5,97,114,114,97,121,65,0,0,0,12,98,6,116,1,83,0,0,0,3,102,111,111]],
['object', {object: {foo: "bar", baz: 12}},[6,111,98,106,101,99,116,70,0,0,0,18,3,102,111,111,83,0,0,0,3,98,97,114,3,98,97,122,98,12]],

// exotic types
['timestamp', {timestamp: {'!': 'timestamp', value: 1357212277527}},
[9,116,105,109,101,115,116,97,109,112,84,0,0,1,60,0,39,219,23]],
['decimal', {decimal: {'!': 'decimal', value: {digits: 2345, places: 2}}},
[7,100,101,99,105,109,97,108,68,2,0,0,9,41]],
['float', {float: {'!': 'float', value: 0.1}},
[5,102,108,111,97,116,102,61,204,204,205]],
['timestamp', {timestamp: {'!': 'timestamp', value: 1357212277527}},[9,116,105,109,101,115,116,97,109,112,84,0,0,1,60,0,39,219,23]],
['decimal', {decimal: {'!': 'decimal', value: {digits: 2345, places: 2}}},[7,100,101,99,105,109,97,108,68,2,0,0,9,41]],
['float', {float: {'!': 'float', value: 0.1}},[5,102,108,111,97,116,102,61,204,204,205]],
['unsignedbyte', {unsignedbyte:{'!': 'unsignedbyte', value: 255}}, [12,117,110,115,105,103,110,101,100,98,121,116,101,66,255]],
['unsignedshort', {unsignedshort:{'!': 'unsignedshort', value: 65535}}, [13,117,110,115,105,103,110,101,100,115,104,111,114,116,117,255,255]],
['unsignedint', {unsignedint:{'!': 'unsignedint', value: 4294967295}}, [11,117,110,115,105,103,110,101,100,105,110,116,105,255,255,255,255]],
];

function bufferToArray(b) {
Expand Down Expand Up @@ -109,6 +106,9 @@ suite("Roundtrip values", function() {
amqp.Bit,
amqp.Decimal,
amqp.Timestamp,
amqp.UnsignedByte,
amqp.UnsignedShort,
amqp.UnsignedInt,
amqp.Double,
amqp.Float,
amqp.FieldArray,
Expand Down
15 changes: 15 additions & 0 deletions test/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ var Decimal = label('decimal', transform(
function(args) {
return {'!': 'decimal', value: {places: args[1], digits: args[0]}};
}, sequence(arb.UInt, Octet)));
var UnsignedByte = label('unsignedbyte', transform(
function(n) {
return {'!': 'unsignedbyte', value: n};
}, Octet));
var UnsignedShort = label('unsignedshort', transform(
function(n) {
return {'!': 'unsignedshort', value: n};
}, UShort));
var UnsignedInt = label('unsignedint', transform(
function(n) {
return {'!': 'unsignedint', value: n};
}, ULong));

// Signed 8 bit int
var Byte = rangeInt('byte', -128, 127);
Expand Down Expand Up @@ -244,6 +256,9 @@ module.exports = {
Float: Float,
Timestamp: Timestamp,
Decimal: Decimal,
UnsignedByte: UnsignedByte,
UnsignedShort: UnsignedShort,
UnsignedInt: UnsignedInt,
FieldArray: FieldArray,
FieldTable: FieldTable,

Expand Down

0 comments on commit 648c155

Please sign in to comment.