UINT64_add-test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var assert = require('assert')
  2. var UINT64 = require('..').UINT64
  3. describe('add method', function () {
  4. describe('0+0', function () {
  5. it('should return 0', function (done) {
  6. var u = UINT64(0).add( UINT64(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 = UINT64(0).add( UINT64(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 = UINT64(1).add( UINT64(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 = UINT64(1).add( UINT64(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 = UINT64(123).add( UINT64(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 = UINT64(n).add( UINT64(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 = UINT64(n).add( UINT64(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 = UINT64(n, 16).add( UINT64(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 = UINT64('326648991').add( UINT64('265443576') )
  67. assert.equal( u.toNumber(), 592092567 )
  68. done()
  69. })
  70. })
  71. describe('high bit+high bit 3', function () {
  72. it('should return n', function (done) {
  73. var u = UINT64('800000000000', 16).add( UINT64('100000000000', 16) )
  74. assert.equal( u.toString(16), '900000000000' )
  75. done()
  76. })
  77. })
  78. })