Coverage for tests/test_Interval1d.py: 20%
59 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
1# This file is part of sphgeom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import pickle
29import unittest
31from lsst.sphgeom import CONTAINS, DISJOINT, Interval1d
34class Interval1dTestCase(unittest.TestCase):
35 """Test 1D intervals."""
37 def testConstruction(self):
38 i = Interval1d(1)
39 self.assertEqual(i.getA(), i.getB())
40 self.assertEqual(i.getA(), 1)
41 i = Interval1d(1, 2)
42 self.assertEqual(i, Interval1d(1, 2))
43 self.assertTrue(Interval1d.empty().isEmpty())
45 def testComparisonOperators(self):
46 self.assertEqual(Interval1d(1), Interval1d(1, 1))
47 self.assertEqual(Interval1d(1), 1)
48 self.assertNotEqual(Interval1d(1, 1), Interval1d(2, 2))
49 self.assertNotEqual(Interval1d(2, 2), 1)
51 def testCenterAndSize(self):
52 i = Interval1d(1, 2)
53 self.assertEqual(i.getSize(), 1)
54 self.assertEqual(i.getCenter(), 1.5)
56 def testRelationships(self):
57 i02 = Interval1d(0, 2)
58 i13 = Interval1d(1, 3)
59 i46 = Interval1d(4, 6)
60 i06 = Interval1d(0, 6)
61 self.assertTrue(i02.contains(1))
62 self.assertTrue(i02.contains(Interval1d(0.5, 1.5)))
63 self.assertTrue(i02.isDisjointFrom(3))
64 self.assertTrue(i02.isDisjointFrom(i46))
65 self.assertTrue(i02.intersects(1))
66 self.assertTrue(i02.intersects(i13))
67 self.assertTrue(Interval1d(1, 1).isWithin(i02))
68 self.assertTrue(i02.isWithin(i06))
69 r = i02.relate(1)
70 self.assertEqual(r, CONTAINS)
71 r = i46.relate(i02)
72 self.assertEqual(r, DISJOINT)
74 def testExpandingAndClipping(self):
75 a = Interval1d(1, 2)
76 b = a.expandedTo(3).expandedTo(Interval1d(2, 4)).clippedTo(Interval1d(0, 2)).clippedTo(1)
77 a.expandTo(3).expandTo(Interval1d(2, 4))
78 a.clipTo(Interval1d(0, 2)).clipTo(1)
79 self.assertEqual(a, b)
80 self.assertEqual(a, 1)
82 def testDilationAndErosion(self):
83 a = Interval1d(1, 3)
84 b = a.dilatedBy(1).erodedBy(2)
85 a.dilateBy(1).erodeBy(2)
86 self.assertEqual(a, b)
87 self.assertEqual(a, 2)
89 def testString(self):
90 i = Interval1d(1, 2)
91 self.assertEqual(str(i), "[1.0, 2.0]")
92 self.assertEqual(repr(i), "Interval1d(1.0, 2.0)")
93 self.assertEqual(i, eval(repr(i), {"Interval1d": Interval1d}))
95 def testPickle(self):
96 a = Interval1d(1.5, 3.5)
97 b = pickle.loads(pickle.dumps(a))
98 self.assertEqual(a, b)
101if __name__ == "__main__":
102 unittest.main()