@@ -9,32 +9,56 @@ describe('Int32', function () {
9
9
const hexValue = 0x2a ;
10
10
const octalValue = 0o52 ;
11
11
const value = 42 ;
12
+ const upperBoundValue = 0x7fffffff ;
13
+ const lowerBoundValue = - 0x80000000 ;
14
+ const outOfUpperBoundValue = 0x80000000 ;
15
+ const outOfLowerBoundValue = - 0x80000001 ;
12
16
13
- it ( 'Primitive number ' , function ( done ) {
17
+ it ( 'should accept primitive numbers ' , function ( done ) {
14
18
expect ( new Int32 ( value ) . valueOf ( ) ) . to . equal ( value ) ;
15
19
done ( ) ;
16
20
} ) ;
17
21
18
- it ( 'Number object ' , function ( done ) {
22
+ it ( 'should accept number objects ' , function ( done ) {
19
23
expect ( new Int32 ( new Number ( value ) ) . valueOf ( ) ) . to . equal ( value ) ;
20
24
done ( ) ;
21
25
} ) ;
22
26
23
- it ( 'String Hex' , function ( done ) {
27
+ it ( 'should accept string Hex' , function ( done ) {
24
28
expect ( new Int32 ( strHexValue ) . valueOf ( ) ) . to . equal ( value ) ;
25
29
done ( ) ;
26
30
} ) ;
27
31
28
- it ( 'Hex ' , function ( done ) {
32
+ it ( 'should accept hex ' , function ( done ) {
29
33
expect ( new Int32 ( hexValue ) . valueOf ( ) ) . to . equal ( value ) ;
30
34
done ( ) ;
31
35
} ) ;
32
36
33
- it ( 'Octal ' , function ( done ) {
37
+ it ( 'should accept octal ' , function ( done ) {
34
38
expect ( new Int32 ( octalValue ) . valueOf ( ) ) . to . equal ( value ) ;
35
39
done ( ) ;
36
40
} ) ;
37
41
42
+ it ( 'should accept int32 minimum input of -0x80000000' , function ( done ) {
43
+ expect ( new Int32 ( lowerBoundValue ) . valueOf ( ) ) . to . equal ( lowerBoundValue ) ;
44
+ done ( ) ;
45
+ } ) ;
46
+
47
+ it ( 'should accept int32 maximum input of 0x7fffffff' , function ( done ) {
48
+ expect ( new Int32 ( upperBoundValue ) . valueOf ( ) ) . to . equal ( upperBoundValue ) ;
49
+ done ( ) ;
50
+ } ) ;
51
+
52
+ it ( 'should truncate the input bits to int32 for inputs smaller than -0x80000000' , function ( done ) {
53
+ expect ( new Int32 ( outOfLowerBoundValue ) . valueOf ( ) ) . to . equal ( 0x7fffffff ) ;
54
+ done ( ) ;
55
+ } ) ;
56
+
57
+ it ( 'should truncate the input bits to int32 for inputs larger than 0x7fffffff' , function ( done ) {
58
+ expect ( new Int32 ( outOfUpperBoundValue ) . valueOf ( ) ) . to . equal ( - 0x80000000 ) ;
59
+ done ( ) ;
60
+ } ) ;
61
+
38
62
it ( 'should equal zero' , function ( ) {
39
63
const prop = 'key' ;
40
64
const zero = BSON . serialize ( { [ prop ] : new Int32 ( 0 ) } ) . toString ( ) ;
@@ -45,7 +69,7 @@ describe('Int32', function () {
45
69
} ) ;
46
70
} ) ;
47
71
48
- it ( 'should equal fortyTwo ' , function ( ) {
72
+ it ( 'should have serialization consistency across different representations of 42 ' , function ( ) {
49
73
const prop = 'key' ;
50
74
const fortyTwo = BSON . serialize ( { [ prop ] : new Int32 ( value ) } ) . toString ( ) ;
51
75
// should equal fortyTwo
@@ -58,7 +82,7 @@ describe('Int32', function () {
58
82
59
83
describe ( 'toString' , ( ) => {
60
84
it ( 'should serialize to a string' , ( ) => {
61
- const testNumber = Math . floor ( Math . random ( ) * 0xffffffff ) ;
85
+ const testNumber = 0x7fffffff ;
62
86
const int32 = new Int32 ( testNumber ) ;
63
87
expect ( int32 . toString ( ) ) . to . equal ( testNumber . toString ( ) ) ;
64
88
} ) ;
@@ -67,7 +91,7 @@ describe('Int32', function () {
67
91
68
92
for ( const radix of testRadices ) {
69
93
it ( `should support radix argument: ${ radix } ` , ( ) => {
70
- const testNumber = Math . floor ( Math . random ( ) * 0xffffffff ) ;
94
+ const testNumber = 0x7fffffff ;
71
95
const int32 = new Int32 ( testNumber ) ;
72
96
expect ( int32 . toString ( radix ) ) . to . equal ( testNumber . toString ( radix ) ) ;
73
97
} ) ;
0 commit comments