Skip to content

Commit

Permalink
test: add tests for submodule animation.controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaokang2022 committed Oct 29, 2024
1 parent ea353ba commit 8cdc365
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/test_animation/test_contollers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

import math
import unittest

from tkintertools.animation import controllers


class Test(unittest.TestCase):

def test_flat(self) -> None:
self.assertEqual(controllers.flat(0), 0)
self.assertEqual(controllers.flat(0.618), 0.618)
self.assertEqual(controllers.flat(1), 1)

def test_smooth(self) -> None:
self.assertEqual(controllers.smooth(0), 0)
self.assertEqual(controllers.smooth(1), 1)
self.assertAlmostEqual(controllers.smooth(1/4), 1/2 - math.sqrt(2)/4)
self.assertAlmostEqual(controllers.smooth(2/4), 1/2)
self.assertAlmostEqual(controllers.smooth(3/4), 1/2 + math.sqrt(2)/4)

def test_rebound(self) -> None:
self.assertEqual(controllers.rebound(0), 0)
self.assertEqual(controllers.rebound(1), 1)
self.assertAlmostEqual(controllers.rebound(1/4), 0.5639150830548166)
self.assertAlmostEqual(controllers.rebound(2/4), 0.9800394839160321)
self.assertAlmostEqual(controllers.rebound(3/4), 1.1393154544781496)

def test_controller_generator(self) -> None:
func_1 = controllers.controller_generator(
lambda x: x, 233, 666, map_y=False)
self.assertEqual(func_1(0), 233)
self.assertEqual(func_1(1), 666)

func_2 = controllers.controller_generator(
math.sin, 0, math.pi, map_y=False)
self.assertAlmostEqual(func_2(0), 0)
self.assertAlmostEqual(func_2(1), 0)
self.assertAlmostEqual(func_2(1/2), 1)
self.assertAlmostEqual(func_2(2), 0)

func_3 = controllers.controller_generator(lambda x: x, 233, 666)
self.assertAlmostEqual(func_3(0), 233/666)
self.assertAlmostEqual(func_3(0.618), (233 + 0.618*(666-233)) / 666)
self.assertAlmostEqual(func_3(1), 1)

self.assertWarns(UserWarning, controllers.controller_generator,
math.sin, math.pi, math.tau)


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

0 comments on commit 8cdc365

Please sign in to comment.