view tests/test_datasets.py @ 89:800f3938ebaa

test our pet datasets
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:10:53 -0800
parents
children
line wrap: on
line source

#!/usr/bin/env python

"""
test dataset generation
"""

import math
import unittest
from tvii.dataset import circle
from tvii.dataset import line

class DatasetGenerationTest(unittest.TestCase):
    """test our dataset generation"""

    def test_random_circle(self):
        """test generating points in a circle"""

        # test variables
        center = (2.5, 3.5)
        radius = 10.
        number = 100000

        # generate points
        c = circle.CircularRandom(center, radius)
        points = c(number)

        # validate
        assert len(points) == number
        offsets = [(point[0] - center[0], point[1] - center[1])
                   for point in points]
        distances = [math.hypot(*offset) for offset in offsets]
        assert max(distances) <= radius
        # TODO: ensure the distribution is fair  (it's not)

    def test_x_range(self):

        x = line.x_range(0., 10., 11)
        for expected, actual in enumerate(x):
            self.assertAlmostEqual(actual, float(expected))

    def test_line(self):

        # make a line
        l = line.Line(1., 1.)

        self.assertAlmostEquals(l.y(1.), 2.)
        self.assertAlmostEquals(l(2.)[0], 3.)


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