Commit cfb7ad54 authored by Pierre Fernbach's avatar Pierre Fernbach
Browse files

[Tests][Python] add unit tests for constant_curve

parent 260a351a
Pipeline #9416 failed with stage
in 9 minutes and 9 seconds
......@@ -5,5 +5,6 @@ PYTHON_INSTALL_ON_SITE(${PROJECT_NAME} plot.py)
PYTHON_INSTALL_ON_SITE(${PROJECT_NAME} optimization.py)
ADD_PYTHON_UNIT_TEST("python-curves" "python/test/test.py" "python")
ADD_PYTHON_UNIT_TEST("python-constant" "python/test/test-constant.py" "python")
ADD_PYTHON_UNIT_TEST("python-optimization" "python/test/optimization.py" "python")
ADD_PYTHON_UNIT_TEST("python-notebook" "python/test/notebook.py" "python")
# Copyright (c) 2020, CNRS
# Authors: Pierre Fernbach <pfernbac@laas.fr>
import unittest
import curves
from curves import constant, constant3
import numpy as np
from numpy import array, isclose, array_equal
class ConstantCurveTest(unittest.TestCase):
def test_constructor(self):
# default constructor
c = constant()
self.assertEqual(c.min(), 0.)
self.assertEqual(c.max(), 0)
self.assertEqual(c.dim(), 0)
self.assertEqual(c.degree(), 0)
# constructor from a point
p = array([1, 23. ,5., 9, -5])
c = constant(p)
self.assertEqual(c.min(), 0.)
self.assertTrue(c.max() > 1e100) # convert std::numeric_limits<time_t>::max() to python ?
self.assertEqual(c.dim(), 5)
self.assertEqual(c.degree(), 0)
# constructor with timing
c = constant(p, 1., 5.)
self.assertEqual(c.min(), 1.)
self.assertEqual(c.max(), 5.)
self.assertEqual(c.dim(), 5)
self.assertEqual(c.degree(), 0)
with self.assertRaises(ValueError):
c = constant(p, 2., 1.)
def test_evaluate(self):
p = array([1, 23., 5., 9, -5])
c = constant(p, 1., 3.)
self.assertTrue(array_equal(c(1.), p))
self.assertTrue(array_equal(c(2.5), p))
self.assertTrue(array_equal(c(3.), p))
with self.assertRaises(ValueError):
c(0.5)
with self.assertRaises(ValueError):
c(4.)
def test_derivate(self):
p = array([1, 23., 5., 9, -5])
p0 = np.zeros(5)
c = constant(p, 1., 3.)
self.assertTrue(array_equal(c.derivate(1., 1), p0))
self.assertTrue(array_equal(c.derivate(1., 2), p0))
self.assertTrue(array_equal(c.derivate(3., 1), p0))
self.assertTrue(array_equal(c.derivate(1.5, 1), p0))
with self.assertRaises(ValueError):
c.derivate(0.5, 1)
with self.assertRaises(ValueError):
c.derivate(4., 3)
c_deriv = c.compute_derivate(1)
self.assertTrue(array_equal(c_deriv(1.5), p0))
self.assertEqual(c_deriv.min(), c.min())
self.assertEqual(c_deriv.max(), c.max())
self.assertEqual(c_deriv.dim(), c.dim())
def test_comparator(self):
p = array([1, 23., 5., 9, -5])
c1 = constant(p, 1., 3.)
c2 = constant(p, 1., 3.)
c3 = c1
pn = array([1,23., 5., 9])
p2 = array([1,17., 5., 9, -5])
cn1 = constant(pn, 1., 3.)
cn2 = constant(p, 1.5, 3.)
cn3 = constant(p, 1., 2.)
cn4 = constant(p2, 1., 3.)
self.assertEqual(c1, c2)
self.assertEqual(c1, c3)
self.assertNotEqual(c1, cn1)
self.assertNotEqual(c1, cn2)
self.assertNotEqual(c1, cn3)
self.assertNotEqual(c1, cn4)
self.assertTrue(c1.isEquivalent(c2))
def test_serialization(self):
p = array([1, 23., 5., 9, -5])
c = constant(p, 1., 3.)
c.saveAsText("serialization_curve.txt")
c.saveAsXML("serialization_curve.xml", "constant")
c.saveAsBinary("serialization_curve")
c_txt = constant()
c_txt.loadFromText("serialization_curve.txt")
self.assertEqual(c, c_txt)
c_xml = constant()
c_xml.loadFromXML("serialization_curve.xml", "constant")
self.assertEqual(c, c_xml)
c_bin = constant()
c_bin.loadFromBinary("serialization_curve")
self.assertEqual(c, c_bin)
class Constant3CurveTest(unittest.TestCase):
def test_constructor(self):
# default constructor
c = constant()
self.assertEqual(c.min(), 0.)
self.assertEqual(c.max(), 0)
self.assertEqual(c.dim(), 0)
self.assertEqual(c.degree(), 0)
# constructor from a point
p = array([1, 23., 5.])
c = constant3(p)
self.assertEqual(c.min(), 0.)
self.assertTrue(c.max() > 1e100) # convert std::numeric_limits<time_t>::max() to python ?
self.assertEqual(c.dim(), 3)
self.assertEqual(c.degree(), 0)
# constructor with timing
c = constant3(p, 1., 5.)
self.assertEqual(c.min(), 1.)
self.assertEqual(c.max(), 5.)
self.assertEqual(c.dim(), 3)
self.assertEqual(c.degree(), 0)
with self.assertRaises(ValueError):
c = constant(p, 2., 1.)
def test_serialization(self):
p = array([1, 23., 5.])
c = constant3(p, 0., 2.)
c.saveAsText("serialization_curve.txt")
c.saveAsXML("serialization_curve.xml", "constant")
c.saveAsBinary("serialization_curve")
c_txt = constant3()
c_txt.loadFromText("serialization_curve.txt")
self.assertEqual(c, c_txt)
c_xml = constant3()
c_xml.loadFromXML("serialization_curve.xml", "constant")
self.assertEqual(c, c_xml)
c_bin = constant3()
c_bin.loadFromBinary("serialization_curve")
self.assertEqual(c, c_bin)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment