UINT32_add-test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var assert = require('assert')
  2. var UINT32 = require('..').UINT32
  3. describe('add method', function () {
  4. describe('0+0', function () {
  5. it('should return 0', function (done) {
  6. var u = UINT32(0).add( UINT32(0) )
  7. assert.equal( u.toNumber(), 0 )
  8. done()
  9. })
  10. })
  11. describe('0+1', function () {
  12. it('should return 1', function (done) {
  13. var u = UINT32(0).add( UINT32(1) )
  14. assert.equal( u.toNumber(), 1 )
  15. done()
  16. })
  17. })
  18. describe('1+0', function () {
  19. it('should return 0', function (done) {
  20. var u = UINT32(1).add( UINT32(0) )
  21. assert.equal( u.toNumber(), 1 )
  22. done()
  23. })
  24. })
  25. describe('1+1', function () {
  26. it('should return 2', function (done) {
  27. var u = UINT32(1).add( UINT32(1) )
  28. assert.equal( u.toNumber(), 2 )
  29. done()
  30. })
  31. })
  32. describe('low bit+high bit', function () {
  33. it('should return n', function (done) {
  34. var n = Math.pow(2, 17)
  35. var u = UINT32(123).add( UINT32(n) )
  36. assert.equal( u.toNumber(), 123 + n )
  37. done()
  38. })
  39. })
  40. describe('high bit+low bit', function () {
  41. it('should return n', function (done) {
  42. var n = Math.pow(2, 17)
  43. var u = UINT32(n).add( UINT32(123) )
  44. assert.equal( u.toNumber(), 123 + n )
  45. done()
  46. })
  47. })
  48. describe('high bit+high bit', function () {
  49. it('should return n', function (done) {
  50. var n = Math.pow(2, 17)
  51. var u = UINT32(n).add( UINT32(n) )
  52. assert.equal( u.toNumber(), n + n )
  53. done()
  54. })
  55. })
  56. describe('overflow', function () {
  57. it('should return n', function (done) {
  58. var n = 'FFFFFFFF'
  59. var u = UINT32(n, 16).add( UINT32(n, 16) )
  60. assert.equal( u.toNumber(), -2 )
  61. done()
  62. })
  63. })
  64. describe('high bit+high bit 2', function () {
  65. it('should return n', function (done) {
  66. var u = UINT32('326648991').add( UINT32('265443576') )
  67. assert.equal( u.toNumber(), 592092567 )
  68. done()
  69. })
  70. })
  71. })