stanford-spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { initChart } from './c3-helper'
  2. import { getRegionArea, compareEpochs, pointInRegion } from '../src/stanford'
  3. describe('c3 stanford tests', function() {
  4. 'use strict'
  5. var chart, args
  6. beforeEach(function(done) {
  7. chart = initChart(chart, args, done)
  8. })
  9. describe('count epochs in region', function() {
  10. beforeAll(function() {
  11. args = {
  12. data: {
  13. x: 'x',
  14. y: 'y',
  15. epochs: 'epochs',
  16. columns: [
  17. ['x', 25, 35],
  18. ['y', 25, 33],
  19. ['epochs', 30, 35]
  20. ],
  21. type: 'stanford'
  22. }
  23. }
  24. })
  25. it('should return 0 if the region has no epochs', function() {
  26. var region = [
  27. { x: 0, y: 0 },
  28. { x: 20, y: 0 },
  29. { x: 20, y: 20 },
  30. { x: 0, y: 20 }
  31. ]
  32. var result = chart.internal.countEpochsInRegion(region)
  33. expect(result.percentage).toBe(0)
  34. expect(result.value).toBe(0)
  35. })
  36. it('should return 100% if the region has all the epochs', function() {
  37. var region = [
  38. { x: 0, y: 0 },
  39. { x: 60, y: 0 },
  40. { x: 60, y: 60 },
  41. { x: 0, y: 60 }
  42. ]
  43. var result = chart.internal.countEpochsInRegion(region)
  44. expect(Number(result.percentage)).toBe(100)
  45. expect(result.value).toBe(65)
  46. })
  47. })
  48. describe('get centroid of region', function() {
  49. beforeAll(function() {
  50. args = {
  51. data: {
  52. x: 'x',
  53. y: 'y',
  54. epochs: 'epochs',
  55. columns: [
  56. ['x', 25, 35],
  57. ['y', 25, 33],
  58. ['epochs', 30, 35]
  59. ],
  60. type: 'stanford'
  61. }
  62. }
  63. })
  64. var region = [
  65. // a 20 x 20 square
  66. { x: 0, y: 0 },
  67. { x: 20, y: 0 },
  68. { x: 20, y: 20 },
  69. { x: 0, y: 20 }
  70. ]
  71. it('should return the centroid of a polygon', function() {
  72. var result = chart.internal.getCentroid(region)
  73. expect(result.x).toBe(10)
  74. expect(result.y).toBe(10)
  75. })
  76. })
  77. describe('get region area', function() {
  78. var square = [
  79. // a 20 x 20 square
  80. { x: 0, y: 0 },
  81. { x: 20, y: 0 },
  82. { x: 20, y: 20 },
  83. { x: 0, y: 20 }
  84. ]
  85. var squareArea = 400
  86. var triangle = [
  87. // A = b * h / 2
  88. { x: 0, y: 0 },
  89. { x: 20, y: 20 },
  90. { x: 0, y: 20 }
  91. ]
  92. var triangleArea = 200
  93. it('should return the correct area for a square', function() {
  94. expect(Math.abs(getRegionArea(square))).toBe(squareArea)
  95. })
  96. it('should return the correct area for a triangle', function() {
  97. expect(Math.abs(getRegionArea(triangle))).toBe(triangleArea)
  98. })
  99. })
  100. describe('compare epochs', function() {
  101. var dataBigger = { epochs: 2 }
  102. var dataLower = { epochs: 1 }
  103. it('should return -1 if epochs are lower', function() {
  104. expect(compareEpochs(dataLower, dataBigger)).toBe(-1)
  105. })
  106. it('should return 1 if epochs are bigger', function() {
  107. expect(compareEpochs(dataBigger, dataLower)).toBe(1)
  108. })
  109. it('should return 0 if epochs are equal', function() {
  110. expect(compareEpochs(dataLower, dataLower)).toBe(0)
  111. })
  112. })
  113. describe('check if point is in region', function() {
  114. var region = [
  115. { x: 0, y: 0 },
  116. { x: 20, y: 0 },
  117. { x: 20, y: 20 },
  118. { x: 20, y: 20 }
  119. ]
  120. var pointInside = { x: 0, value: 0 }
  121. var pointOutInside = { x: 21, value: 0 }
  122. it('should return true if point is inside region', function() {
  123. expect(pointInRegion(pointInside, region)).toBeTruthy()
  124. })
  125. it('should return false if point is outside region', function() {
  126. expect(pointInRegion(pointOutInside, region)).toBeFalsy()
  127. })
  128. })
  129. })