view tests/test_histogram.py @ 78:9b9bfbcdd749

find min, max
author Jeff Hammel <k0scist@gmail.com>
date Sun, 01 Mar 2015 09:04:36 -0800
parents 8e93d7357c6b
children
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
unit tests for histograms
"""

import os
import sys
import unittest
from numerics.histogram import Histogram

# globals
here = os.path.dirname(os.path.abspath(__file__))

class HistogramUnitTest(unittest.TestCase):

    def test_histogram(self):
        """basic histogram test"""

        # make some test data
        data = [0.1, 0.1,
                1.1, 1.2,
                2,
                3,
                5,
                6,
                7,
                8, 8, 8, 8.1]
        bins = range(0,10)

        # make a histogram
        h = Histogram(bins)

        # add some data to it
        h.add(*data)

        # now make sure what we did works

        # First, let's check the bins
        expected_bins = [(0,1),
                         (1,2),
                         (2,3),
                         (3,4),
                         (4,5),
                         (5,6),
                         (6,7),
                         (7,8),
                         (8,9)]
        self.assertEqual(h.keys(), expected_bins)

        # now let's check the values
        values = h()
        self.assertEqual(values[(0,1)], 2)
        self.assertEqual(values[(1,2)], 2)
        self.assertEqual(values[(2,3)], 1)
        self.assertEqual(values[(3,4)], 1)
        self.assertEqual(values[(4,5)], 0)
        self.assertEqual(values[(5,6)], 1)
        self.assertEqual(values[(6,7)], 1)
        self.assertEqual(values[(7,8)], 1)
        self.assertEqual(values[(8,9)], 4)


if __name__ == '__main__':
    unittest.main()