number_utils_test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var numberUtils = require('utils/number_utils');
  19. describe('', function() {
  20. describe('#bytesToSize', function() {
  21. describe('check bytes', function() {
  22. var tests = Em.A([
  23. {
  24. bytes: null,
  25. precision: null,
  26. parseType: null,
  27. multiplyBy: null,
  28. e: 'n/a',
  29. m: '"n/a" if bytes is null'
  30. },
  31. {
  32. bytes: undefined,
  33. precision: null,
  34. parseType: null,
  35. multiplyBy: null,
  36. e: 'n/a',
  37. m: '"n/a" if bytes is undefined'
  38. }
  39. ]);
  40. tests.forEach(function(test) {
  41. it(test.m, function() {
  42. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy)).to.equal(test.e);
  43. });
  44. });
  45. });
  46. describe('check sizes', function() {
  47. var tests = Em.A([
  48. {
  49. bytes: 12,
  50. precision: null,
  51. parseType: 'parseInt',
  52. multiplyBy: 1,
  53. e: 'Bytes',
  54. m: 'Bytes'
  55. },
  56. {
  57. bytes: 1024 + 12,
  58. precision: null,
  59. parseType: 'parseInt',
  60. multiplyBy: 1,
  61. e: 'KB',
  62. m: 'KB'
  63. },
  64. {
  65. bytes: 1024 * 1024 + 12,
  66. precision: null,
  67. parseType: 'parseInt',
  68. multiplyBy: 1,
  69. e: 'MB',
  70. m: 'MB'
  71. },
  72. {
  73. bytes: 1024 * 1024 * 1024 + 12,
  74. precision: null,
  75. parseType: 'parseInt',
  76. multiplyBy: 1,
  77. e: 'GB',
  78. m: 'GB'
  79. },
  80. {
  81. bytes: 1024 * 1024 * 1024 * 1024 + 12,
  82. precision: null,
  83. parseType: 'parseInt',
  84. multiplyBy: 1,
  85. e: 'TB',
  86. m: 'TB'
  87. },
  88. {
  89. bytes: 1024 * 1024 * 1024 * 1024 * 1024 + 12,
  90. precision: null,
  91. parseType: 'parseInt',
  92. multiplyBy: 1,
  93. e: 'PB',
  94. m: 'PB'
  95. }
  96. ]);
  97. tests.forEach(function(test) {
  98. it(test.m, function() {
  99. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).endsWith(test.e)).to.equal(true);
  100. });
  101. });
  102. });
  103. describe('check calculated result', function() {
  104. var tests = Em.A([
  105. {
  106. bytes: 42,
  107. precision: null,
  108. parseType: 'parseInt',
  109. multiplyBy: 1,
  110. e: '42',
  111. m: 'Bytes'
  112. },
  113. {
  114. bytes: 1024 * 12,
  115. precision: null,
  116. parseType: 'parseInt',
  117. multiplyBy: 1,
  118. e: '12',
  119. m: 'KB'
  120. },
  121. {
  122. bytes: 1024 * 1024 * 23,
  123. precision: null,
  124. parseType: 'parseInt',
  125. multiplyBy: 1,
  126. e: '23',
  127. m: 'MB'
  128. },
  129. {
  130. bytes: 1024 * 1024 * 1024 * 34,
  131. precision: null,
  132. parseType: 'parseInt',
  133. multiplyBy: 1,
  134. e: '34',
  135. m: 'GB'
  136. },
  137. {
  138. bytes: 1024 * 1024 * 1024 * 1024 * 45,
  139. precision: null,
  140. parseType: 'parseInt',
  141. multiplyBy: 1,
  142. e: '45',
  143. m: 'TB'
  144. },
  145. {
  146. bytes: 1024 * 1024 * 1024 * 1024 * 1024 * 56,
  147. precision: null,
  148. parseType: 'parseInt',
  149. multiplyBy: 1,
  150. e: '56',
  151. m: 'PB'
  152. }
  153. ]);
  154. tests.forEach(function(test) {
  155. it(test.m, function() {
  156. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).startsWith(test.e)).to.equal(true);
  157. });
  158. });
  159. });
  160. });
  161. });