Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.findBin = function(val, bins, linelow) {
c = 0,
n,
test;
if(bins[bins.length - 1] > bins[0]) {
if(bins[bins.length - 1] >= bins[0]) {
test = linelow ? lessThan : lessOrEqual;
} else {
test = linelow ? greaterOrEqual : greaterThan;
Expand Down
37 changes: 37 additions & 0 deletions test/jasmine/tests/search_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var Plotly = require('@src/plotly');

describe('Test search.js:', function() {
'use strict';

describe('findBin', function() {
it('should work on ascending arrays', function() {
expect(Plotly.Lib.findBin(-10000, [0, 1, 3])).toBe(-1);
expect(Plotly.Lib.findBin(0.5, [0, 1, 3])).toBe(0);
expect(Plotly.Lib.findBin(2, [0, 1, 3])).toBe(1);
expect(Plotly.Lib.findBin(10000, [0, 1, 3])).toBe(2);
// default: linelow falsey, so the line is in the higher bin
expect(Plotly.Lib.findBin(1, [0, 1, 3])).toBe(1);
// linelow truthy, so the line is in the lower bin
expect(Plotly.Lib.findBin(1, [0, 1, 3], true)).toBe(0);
});

it('should work on decending arrays', function() {
expect(Plotly.Lib.findBin(-10000, [3, 1, 0])).toBe(2);
expect(Plotly.Lib.findBin(0.5, [3, 1, 0])).toBe(1);
expect(Plotly.Lib.findBin(2, [3, 1, 0])).toBe(0);
expect(Plotly.Lib.findBin(10000, [3, 1, 0])).toBe(-1);

expect(Plotly.Lib.findBin(1, [3, 1, 0])).toBe(0);
expect(Plotly.Lib.findBin(1, [3, 1, 0], true)).toBe(1);
});

it('should treat a length-1 array as ascending', function() {
expect(Plotly.Lib.findBin(-1, [0])).toBe(-1);
expect(Plotly.Lib.findBin(1, [0])).toBe(0);

expect(Plotly.Lib.findBin(0, [0])).toBe(0);
expect(Plotly.Lib.findBin(0, [0], true)).toBe(-1);
});
// TODO: didn't test bins as objects {start, stop, size}
});
});