33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
|
#####################################################
|
||
|
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.03 #
|
||
|
#####################################################
|
||
|
# pytest tests/test_math_static.py -s #
|
||
|
#####################################################
|
||
|
import unittest
|
||
|
|
||
|
from xautodl.datasets.math_core import QuadraticSFunc
|
||
|
from xautodl.datasets.math_core import ConstantFunc
|
||
|
|
||
|
|
||
|
class TestConstantFunc(unittest.TestCase):
|
||
|
"""Test the constant function."""
|
||
|
|
||
|
def test_simple(self):
|
||
|
function = ConstantFunc(0.1)
|
||
|
for i in range(100):
|
||
|
assert function(i) == 0.1
|
||
|
|
||
|
|
||
|
class TestQuadraticSFunc(unittest.TestCase):
|
||
|
"""Test the quadratic function."""
|
||
|
|
||
|
def test_simple(self):
|
||
|
function = QuadraticSFunc([[0, 1], [0.5, 4], [1, 1]])
|
||
|
print(function)
|
||
|
for x in (0, 0.5, 1):
|
||
|
print("f({:})={:}".format(x, function(x)))
|
||
|
thresh = 0.2
|
||
|
self.assertTrue(abs(function(0) - 1) < thresh)
|
||
|
self.assertTrue(abs(function(0.5) - 4) < thresh)
|
||
|
self.assertTrue(abs(function(1) - 1) < thresh)
|