/usr/lib/python2.7/dist-packages/chaco/tests/base_utils_test_case.py is in python-chaco 4.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 158 159 160 161 162 163 164 165 166 167 168 169 170 | """
Unit tests for utility functions in chaco.base
"""
import unittest
from math import sqrt
from numpy import arange, array
from numpy.testing import assert_equal, assert_almost_equal
from chaco.api import bin_search, find_runs, reverse_map_1d, point_line_distance
class BinSearchTestCase(unittest.TestCase):
def test_ascending_data(self):
ary = arange(10.0)
# inside bounds
self.assert_(bin_search(ary, 0.0, 1) == 0)
self.assert_(bin_search(ary, 5.0, 1) == 5)
self.assert_(bin_search(ary, 9.0, 1) == 9)
# out of bounds
self.assert_(bin_search(ary, 10.0, 1) == -1)
self.assert_(bin_search(ary, -1.0, 1) == -1)
self.assert_(bin_search(ary, 9.00001, 1) == -1)
self.assert_(bin_search(ary, -0.00001, 1) == -1)
# rounding
self.assert_(bin_search(ary, 5.1, 1) == 5)
self.assert_(bin_search(ary, 4.9, 1) == 4)
return
def test_descending_data(self):
ary = arange(10.0, 0.0, -1.0)
# inside bounds
self.assert_(bin_search(ary, 10.0, -1) == 0)
self.assert_(bin_search(ary, 5.0, -1) == 5)
self.assert_(bin_search(ary, 1.0, -1) == 9)
# out of bounds
self.assert_(bin_search(ary, 10.1, -1) == -1)
self.assert_(bin_search(ary, 0.9, -1) == -1)
# rounding
self.assert_(bin_search(ary, 5.1, -1) == 4)
self.assert_(bin_search(ary, 4.9, -1) == 5)
return
class ReverseMap1DTestCase(unittest.TestCase):
def test_ascending(self):
ary = arange(10.0)
rmap = lambda x: reverse_map_1d(ary, x, 'ascending')
# inside bounds
self.assert_(rmap(0.0) == 0)
self.assert_(rmap(5.0) == 5)
self.assert_(rmap(9.0) == 9)
# out of bounds
self.assertRaises(IndexError, rmap, 10.0)
self.assertRaises(IndexError, rmap, -1.0)
# rounding
self.assert_(rmap(3.4) == 3)
self.assert_(rmap(3.5) == 3)
self.assert_(rmap(3.6) == 4)
return
def test_ascending_floor(self):
ary = arange(10.0)
rmap = lambda x: reverse_map_1d(ary, x, 'ascending', floor_only=True)
# test rounding
self.assert_(rmap(3.4) == 3)
self.assert_(rmap(3.5) == 3)
self.assert_(rmap(3.6) == 3)
return
def test_descending(self):
ary = arange(10.0, 0.0, -1.0)
rmap = lambda x: reverse_map_1d(ary, x, 'descending')
# inside bounds
self.assert_(rmap(10.0) == 0)
self.assert_(rmap(5.0) == 5)
self.assert_(rmap(1.0) == 9)
# out of bounds
self.assertRaises(IndexError, rmap, 0.0)
self.assertRaises(IndexError, rmap, 11.0)
# rounding
self.assert_(rmap(8.6) == 1)
self.assert_(rmap(8.5) == 1)
self.assert_(rmap(8.4) == 2)
return
def test_descending_floor(self):
ary = arange(10.0, 0.0, -1.0)
rmap = lambda x: reverse_map_1d(ary, x, 'descending', floor_only=True)
# test rounding
self.assert_(rmap(8.6) == 1)
self.assert_(rmap(8.5) == 1)
self.assert_(rmap(8.4) == 1)
return
class FindRunsTestCase(unittest.TestCase):
def test_find_runs_middle(self):
x = array([0,8,7,8,9,2,3,4,10])
assert_equal(find_runs(x) , [[0], [8], [7,8,9], [2,3,4], [10]])
def test_find_runs_start(self):
x = array([3,4,5,12,9,17])
assert_equal(find_runs(x) , [[3,4,5],[12],[9],[17]])
def test_find_runs_end(self):
x = array([18,23,24,25])
assert_equal(find_runs(x) , [[18],[23,24,25]])
def test_find_runs_offset(self):
# because of the nature of the find_runs algorithm, there may be
# fencepost errors with runs that start at x[1] or x[-2]
x = array([10,12,13,14,28,16])
assert_equal(find_runs(x) , [[10],[12,13,14],[28],[16]])
x = array([10,15,16,17,34])
assert_equal(find_runs(x) , [[10],[15,16,17],[34]])
def test_find_runs_none(self):
x = array([])
assert_equal(find_runs(x) , [])
x = array([12,15,27])
assert_equal(find_runs(x) , [[12],[15],[27]])
def test_find_runs_descending(self):
x = array([30,41,40,39,38,37,12])
assert_equal(find_runs(x, order='descending') , \
[[30], [41,40,39,38,37], [12]])
class PointLineDistanceTestCase(unittest.TestCase):
def test_horizontal_line(self):
p1 = (10.0, 10.0)
p2 = (60.0, 10.0)
test = (35.0, 30.0)
dist = point_line_distance(test, p1, p2)
assert_equal(dist, 20.0)
def test_vertical_line(self):
p1 = (10.0, 10.0)
p2 = (10.0, 60.0)
test = (30.0, 35.0)
dist = point_line_distance(test, p1, p2)
assert_equal(dist, 20.0)
def test_diag_lines(self):
p1 = (0.0, 0.0)
p2 = (10.0, 10.0)
test = (0.0, 5.0)
dist = point_line_distance(test, p1, p2)
assert_almost_equal(dist, 2.5 * sqrt(2.0))
def test_point_on_line(self):
p1 = (-5.0, 5.0)
p2 = (10.0, -10.0)
test = (3.0, -3.0)
dist = point_line_distance(test, p1, p2)
assert_almost_equal(dist, 0.0)
if __name__ == '__main__':
import nose
nose.run()
|