NumberUtils.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { assert } from 'chai';
  2. import { NumberUtils } from '../../../src/utils/NumberUtils';
  3. describe('NumberUtils', () => {
  4. describe('toHex', () => {
  5. describe('Variant #1: number `0`', () => {
  6. const number: number = 0;
  7. const expectedHexString = '0x0';
  8. let hexString: string;
  9. before(() => {
  10. hexString = NumberUtils.toHex(number);
  11. });
  12. it('should create a string with hexadecimal value from a given decimal number', () => {
  13. assert.equal(hexString, expectedHexString);
  14. });
  15. });
  16. describe('Variant #2: number `10`', () => {
  17. const number: number = 10;
  18. const expectedHexString = '0xa';
  19. let hexString: string;
  20. before(() => {
  21. hexString = NumberUtils.toHex(number);
  22. });
  23. it('should create a string with hexadecimal value from a given decimal number', () => {
  24. assert.equal(hexString, expectedHexString);
  25. });
  26. });
  27. describe('Variant #3: number `17`', () => {
  28. const number: number = 17;
  29. const expectedHexString = '0x11';
  30. let hexString: string;
  31. before(() => {
  32. hexString = NumberUtils.toHex(number);
  33. });
  34. it('should create a string with hexadecimal value from a given decimal number', () => {
  35. assert.equal(hexString, expectedHexString);
  36. });
  37. });
  38. describe('Variant #4: number `536870912`', () => {
  39. const number: number = 536870912;
  40. const expectedHexString = '0x20000000';
  41. let hexString: string;
  42. before(() => {
  43. hexString = NumberUtils.toHex(number);
  44. });
  45. it('should create a string with hexadecimal value from a given decimal number', () => {
  46. assert.equal(hexString, expectedHexString);
  47. });
  48. });
  49. describe('Variant #5: bigint number `10n`', () => {
  50. // @ts-ignore
  51. const number: bigint = 10n;
  52. const expectedHexString = '0xan';
  53. let hexString: string;
  54. before(() => {
  55. hexString = NumberUtils.toHex(number);
  56. });
  57. it('should create a string with hexadecimal value from a given bigint number', () => {
  58. assert.equal(hexString, expectedHexString);
  59. });
  60. });
  61. });
  62. describe('isCeil', () => {
  63. describe('given number is a ceil', () => {
  64. const number: number = 4;
  65. const expectedResult: boolean = true;
  66. let result: boolean;
  67. before(() => {
  68. result = NumberUtils.isCeil(number);
  69. });
  70. it('should return true', () => {
  71. assert.equal(result, expectedResult);
  72. });
  73. });
  74. describe('given number is a float', () => {
  75. const number: number = 4.5;
  76. const expectedResult: boolean = false;
  77. let result: boolean;
  78. before(() => {
  79. result = NumberUtils.isCeil(number);
  80. });
  81. it('should return false', () => {
  82. assert.equal(result, expectedResult);
  83. });
  84. });
  85. describe('bigint number', () => {
  86. // @ts-ignore
  87. const number: bigint = 10n;
  88. const expectedResult: boolean = true;
  89. let result: boolean;
  90. before(() => {
  91. result = NumberUtils.isCeil(number);
  92. });
  93. it('should return true', () => {
  94. assert.equal(result, expectedResult);
  95. });
  96. });
  97. });
  98. });