UINT32_div-test.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var assert = require('assert')
  2. var UINT32 = require('..').UINT32
  3. describe('div method', function () {
  4. describe('1/0', function () {
  5. it('should throw', function (done) {
  6. assert.throws(
  7. function () {
  8. UINT32(1).div( UINT32(0) )
  9. }
  10. , function (err) {
  11. if (err instanceof Error) return true
  12. }
  13. )
  14. done()
  15. })
  16. })
  17. describe('0/1', function () {
  18. it('should return 0', function (done) {
  19. var u = UINT32(2).div( UINT32(1) )
  20. assert.equal( u.toNumber(), 2 )
  21. done()
  22. })
  23. })
  24. describe('2/1', function () {
  25. it('should return 2', function (done) {
  26. var u = UINT32(0).div( UINT32(1) )
  27. assert.equal( u.toNumber(), 0 )
  28. done()
  29. })
  30. })
  31. describe('1/2', function () {
  32. it('should return 0', function (done) {
  33. var u = UINT32(1).div( UINT32(2) )
  34. assert.equal( u.toNumber(), 0 )
  35. done()
  36. })
  37. })
  38. describe('low bit/high bit', function () {
  39. it('should return n', function (done) {
  40. var n = Math.pow(2, 17)
  41. var u = UINT32(3).div( UINT32(n) )
  42. assert.equal( u.toNumber(), 0 )
  43. done()
  44. })
  45. })
  46. describe('high bit/low bit', function () {
  47. it('should return n', function (done) {
  48. var n = Math.pow(2, 17)
  49. var u = UINT32(n).div( UINT32(3) )
  50. assert.equal( u.toNumber(), (n/3)|0 )
  51. assert.equal( u.remainder.toNumber(), 2 )
  52. done()
  53. })
  54. })
  55. describe('high bit/high bit', function () {
  56. it('should return n', function (done) {
  57. var n = 'FFFFFFFF'
  58. var u = UINT32(n, 16).div( UINT32(n, 16) )
  59. assert.equal( u.toNumber(), 1 )
  60. done()
  61. })
  62. })
  63. describe('high bit/high bit 2', function () {
  64. it('should return n', function (done) {
  65. var u = UINT32('3266489917').div( UINT32('668265263') )
  66. assert.equal( u.toNumber(), 4 )
  67. done()
  68. })
  69. })
  70. describe('374761393/(16^3)', function () {
  71. it('should return 91494', function (done) {
  72. var u = UINT32('374761393').div( UINT32(16*16*16) )
  73. assert.equal( u.toNumber(), 91494 )
  74. done()
  75. })
  76. })
  77. })