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