@@ -233,6 +233,123 @@ describe('long.test.js', function () {
233233 buf . should . eql ( new Buffer ( [ 0x4c , 0x00 , 0x00 , 0x00 , 0x00 , 0x80 , 0x00 , 0x00 , 0x00 ] ) ) ;
234234 } ) ;
235235
236+ it ( 'should write { $class: "long": $: "..." } ok' , function ( ) {
237+ // -8 ~ 15
238+ var buf = hessian . encode ( java . long ( '0' ) , '2.0' ) ;
239+ buf . should . length ( 1 ) ;
240+ buf [ 0 ] . should . equal ( 0xe0 ) ;
241+ buf . should . eql ( new Buffer ( [ 0xe0 ] ) ) ;
242+
243+ buf = hessian . encode ( java . long ( '-8' ) , '2.0' ) ;
244+ buf . should . length ( 1 ) ;
245+ buf [ 0 ] . should . equal ( 0xd8 ) ;
246+ buf . should . eql ( new Buffer ( [ 0xd8 ] ) ) ;
247+
248+ buf = hessian . encode ( java . long ( '-7' ) , '2.0' ) ;
249+ buf . should . length ( 1 ) ;
250+ buf [ 0 ] . should . equal ( 0xd9 ) ;
251+ buf . should . eql ( new Buffer ( [ 0xd9 ] ) ) ;
252+
253+ buf = hessian . encode ( java . long ( '15' ) , '2.0' ) ;
254+ buf . should . length ( 1 ) ;
255+ buf [ 0 ] . should . equal ( 0xef ) ;
256+ buf . should . eql ( new Buffer ( [ 0xef ] ) ) ;
257+
258+ buf = hessian . encode ( java . long ( '14' ) , '2.0' ) ;
259+ buf . should . length ( 1 ) ;
260+ buf [ 0 ] . should . equal ( 0xee ) ;
261+ buf . should . eql ( new Buffer ( [ 0xee ] ) ) ;
262+
263+ // -2048 ~ 2047
264+ buf = hessian . encode ( java . long ( '-9' ) , '2.0' ) ;
265+ buf . should . length ( 2 ) ;
266+ buf [ 0 ] . should . equal ( 0xf7 ) ;
267+ buf [ 1 ] . should . equal ( 0xf7 ) ;
268+ buf . should . eql ( new Buffer ( [ 0xf7 , 0xf7 ] ) ) ;
269+
270+ buf = hessian . encode ( java . long ( '16' ) , '2.0' ) ;
271+ buf . should . length ( 2 ) ;
272+ buf [ 0 ] . should . equal ( 0xf8 ) ;
273+ buf [ 1 ] . should . equal ( 0x10 ) ;
274+ buf . should . eql ( new Buffer ( [ 0xf8 , 0x10 ] ) ) ;
275+
276+ buf = hessian . encode ( java . long ( '255' ) , '2.0' ) ;
277+ buf . should . length ( 2 ) ;
278+ buf [ 0 ] . should . equal ( 0xf8 ) ;
279+ buf [ 1 ] . should . equal ( 0xff ) ;
280+ buf . should . eql ( new Buffer ( [ 0xf8 , 0xff ] ) ) ;
281+
282+ buf = hessian . encode ( java . long ( '-2048' ) , '2.0' ) ;
283+ buf . should . length ( 2 ) ;
284+ buf [ 0 ] . should . equal ( 0xf0 ) ;
285+ buf [ 1 ] . should . equal ( 0 ) ;
286+ buf . should . eql ( new Buffer ( [ 0xf0 , 0x00 ] ) ) ;
287+
288+ buf = hessian . encode ( java . long ( '2047' ) , '2.0' ) ;
289+ buf . should . length ( 2 ) ;
290+ buf [ 0 ] . should . equal ( 0xff ) ;
291+ buf [ 1 ] . should . equal ( 0xff ) ;
292+ buf . should . eql ( new Buffer ( [ 0xff , 0xff ] ) ) ;
293+
294+ // -262144 ~ 262143
295+ buf = hessian . encode ( java . long ( '262143' ) , '2.0' ) ;
296+ buf . should . length ( 3 ) ;
297+ buf [ 0 ] . should . equal ( 0x3f ) ;
298+ buf [ 1 ] . should . equal ( 0xff ) ;
299+ buf [ 2 ] . should . equal ( 0xff ) ;
300+ buf . should . eql ( new Buffer ( [ 0x3f , 0xff , 0xff ] ) ) ;
301+
302+ buf = hessian . encode ( java . long ( '-262144' ) , '2.0' ) ;
303+ buf . should . length ( 3 ) ;
304+ buf [ 0 ] . should . equal ( 0x38 ) ;
305+ buf [ 1 ] . should . equal ( 0 ) ;
306+ buf [ 2 ] . should . equal ( 0 ) ;
307+ buf . should . eql ( new Buffer ( [ 0x38 , 0x00 , 0x00 ] ) ) ;
308+
309+ buf = hessian . encode ( java . long ( '2048' ) , '2.0' ) ;
310+ buf . should . length ( 3 ) ;
311+ buf [ 0 ] . should . equal ( 0x3c ) ;
312+ buf [ 1 ] . should . equal ( 0x08 ) ;
313+ buf [ 2 ] . should . equal ( 0x00 ) ;
314+ buf . should . eql ( new Buffer ( [ 0x3c , 0x08 , 0x00 ] ) ) ;
315+
316+ buf = hessian . encode ( java . long ( '-2049' ) , '2.0' ) ;
317+ buf . should . length ( 3 ) ;
318+ buf [ 0 ] . should . equal ( 0x3b ) ;
319+ buf [ 1 ] . should . equal ( 0xf7 ) ;
320+ buf [ 2 ] . should . equal ( 0xff ) ;
321+ buf . should . eql ( new Buffer ( [ 0x3b , 0xf7 , 0xff ] ) ) ;
322+
323+ // -2147483648 ~ 2147483647
324+ buf = hessian . encode ( java . long ( '-2147483648' ) , '2.0' ) ;
325+ buf . should . length ( 5 ) ;
326+ buf [ 0 ] . should . equal ( 0x59 ) ;
327+ buf [ 1 ] . should . equal ( 0x80 ) ;
328+ buf [ 2 ] . should . equal ( 0x00 ) ;
329+ buf [ 3 ] . should . equal ( 0x00 ) ;
330+ buf [ 4 ] . should . equal ( 0x00 ) ;
331+ buf . should . eql ( new Buffer ( [ 0x59 , 0x80 , 0x00 , 0x00 , 0x00 ] ) ) ;
332+
333+ buf = hessian . encode ( java . long ( '2147483647' ) , '2.0' ) ;
334+ buf . should . length ( 5 ) ;
335+ buf [ 0 ] . should . equal ( 0x59 ) ;
336+ buf [ 1 ] . should . equal ( 0x7f ) ;
337+ buf [ 2 ] . should . equal ( 0xff ) ;
338+ buf [ 3 ] . should . equal ( 0xff ) ;
339+ buf [ 4 ] . should . equal ( 0xff ) ;
340+ buf . should . eql ( new Buffer ( [ 0x59 , 0x7f , 0xff , 0xff , 0xff ] ) ) ;
341+
342+ // L
343+ buf = hessian . encode ( java . long ( '2147483648' ) , '2.0' ) ;
344+ buf . should . length ( 9 ) ;
345+ buf . should . eql ( new Buffer ( [ 0x4c , 0x00 , 0x00 , 0x00 , 0x00 , 0x80 , 0x00 , 0x00 , 0x00 ] ) ) ;
346+
347+ // unsafe long value
348+ buf = hessian . encode ( java . long ( '9007199254740992' ) , '2.0' ) ;
349+ buf . should . length ( 9 ) ;
350+ buf . should . eql ( new Buffer ( [ 0x4c , 0x00 , 0x20 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ] ) ) ;
351+ } ) ;
352+
236353 it ( 'should write and read equal java impl' , function ( ) {
237354 hessian . encode ( java . long ( 0 ) , '2.0' ) . should . eql ( utils . bytes ( 'v2/long/0' ) ) ;
238355 hessian . decode ( utils . bytes ( 'v2/long/0' ) , '2.0' ) . should . equal ( 0 ) ;
0 commit comments