68
|
1 #!/usr/bin/env python
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 """
|
|
5 unit tests for histograms
|
|
6 """
|
|
7
|
|
8 import os
|
|
9 import sys
|
|
10 import unittest
|
|
11 from numerics.histogram import Histogram
|
|
12
|
|
13 # globals
|
|
14 here = os.path.dirname(os.path.abspath(__file__))
|
|
15
|
|
16 class HistogramUnitTest(unittest.TestCase):
|
|
17
|
|
18 def test_histogram(self):
|
|
19 """basic histogram test"""
|
|
20
|
|
21 # make some test data
|
73
|
22 data = [0.1, 0.1,
|
|
23 1.1, 1.2,
|
|
24 2,
|
|
25 3,
|
|
26 5,
|
|
27 6,
|
|
28 7,
|
|
29 8, 8, 8, 8.1]
|
68
|
30 bins = range(0,10)
|
|
31
|
|
32 # make a histogram
|
|
33 h = Histogram(bins)
|
|
34
|
73
|
35 # add some data to it
|
|
36 h.add(*data)
|
|
37
|
|
38 # now make sure what we did works
|
|
39
|
|
40 # First, let's check the bins
|
|
41 expected_bins = [(0,1),
|
|
42 (1,2),
|
|
43 (2,3),
|
|
44 (3,4),
|
|
45 (4,5),
|
|
46 (5,6),
|
|
47 (6,7),
|
|
48 (7,8),
|
|
49 (8,9)]
|
|
50 self.assertEqual(h.keys(), expected_bins)
|
|
51
|
|
52 # now let's check the values
|
|
53 values = h()
|
|
54 self.assertEqual(values[(0,1)], 2)
|
|
55 self.assertEqual(values[(1,2)], 2)
|
|
56 self.assertEqual(values[(2,3)], 1)
|
|
57 self.assertEqual(values[(3,4)], 1)
|
|
58 self.assertEqual(values[(4,5)], 0)
|
|
59 self.assertEqual(values[(5,6)], 1)
|
|
60 self.assertEqual(values[(6,7)], 1)
|
|
61 self.assertEqual(values[(7,8)], 1)
|
|
62 self.assertEqual(values[(8,9)], 4)
|
|
63
|
|
64
|
68
|
65 if __name__ == '__main__':
|
|
66 unittest.main()
|
|
67
|