histogram_specs.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import { describe, beforeEach, it, expect } from '../../../../../test/lib/common';
  4. import { convertValuesToHistogram, getSeriesValues } from '../histogram';
  5. describe('Graph Histogam Converter', function () {
  6. describe('Values to histogram converter', () => {
  7. let values;
  8. let bucketSize = 10;
  9. beforeEach(() => {
  10. values = [1, 2, 10, 11, 17, 20, 29, 30, 31, 33];
  11. });
  12. it('Should convert to series-like array', () => {
  13. bucketSize = 10;
  14. let expected = [
  15. [0, 2], [10, 3], [20, 2], [30, 3]
  16. ];
  17. let histogram = convertValuesToHistogram(values, bucketSize);
  18. expect(histogram).to.eql(expected);
  19. });
  20. it('Should not add empty buckets', () => {
  21. bucketSize = 5;
  22. let expected = [
  23. [0, 2], [10, 2], [15, 1], [20, 1], [25, 1], [30, 3]
  24. ];
  25. let histogram = convertValuesToHistogram(values, bucketSize);
  26. expect(histogram).to.eql(expected);
  27. });
  28. it('Should normalize values', () => {
  29. bucketSize = 5;
  30. let normalize = true;
  31. let expected = [
  32. [0, 0.2], [10, 0.2], [15, 0.1], [20, 0.1], [25, 0.1], [30, 0.3]
  33. ];
  34. let histogram = convertValuesToHistogram(values, bucketSize, normalize);
  35. expect(histogram).to.eql(expected);
  36. });
  37. it('Sum of normalized values should be 1', () => {
  38. bucketSize = 5;
  39. let normalize = true;
  40. let expected = [
  41. [0, 0.2], [10, 0.2], [15, 0.1], [20, 0.1], [25, 0.1], [30, 0.3]
  42. ];
  43. let histogram = convertValuesToHistogram(values, bucketSize, normalize);
  44. let sum = _.reduce(histogram, (sum, point) => sum + point[1], 0);
  45. expect(sum).to.eql(1);
  46. });
  47. });
  48. describe('Series to values converter', () => {
  49. let data;
  50. beforeEach(() => {
  51. data = [
  52. {
  53. data: [[0, 1], [0, 2], [0, 10], [0, 11], [0, 17], [0, 20], [0, 29]]
  54. }
  55. ];
  56. });
  57. it('Should convert to values array', () => {
  58. let expected = [1, 2, 10, 11, 17, 20, 29];
  59. let values = getSeriesValues(data);
  60. expect(values).to.eql(expected);
  61. });
  62. it('Should skip null values', () => {
  63. data[0].data.push([0, null]);
  64. let expected = [1, 2, 10, 11, 17, 20, 29];
  65. let values = getSeriesValues(data);
  66. expect(values).to.eql(expected);
  67. });
  68. });
  69. });