Coverage for tests/test_AngleIntervals.py: 24%

67 statements  

« 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/>. 

27 

28import pickle 

29import unittest 

30 

31from lsst.sphgeom import CONTAINS, DISJOINT, Angle, AngleInterval, NormalizedAngle, NormalizedAngleInterval 

32 

33 

34class IntervalTests: 

35 """Test intervals.""" 

36 

37 def testConstruction(self): 

38 i = self.Interval(self.Scalar(1)) 

39 self.assertEqual(i.getA(), i.getB()) 

40 self.assertEqual(i.getA(), self.Scalar(1)) 

41 i = self.Interval(self.Scalar(1), self.Scalar(2)) 

42 self.assertEqual(i, self.Interval.fromRadians(1, 2)) 

43 self.assertTrue(self.Interval.empty().isEmpty()) 

44 

45 def testComparisonOperators(self): 

46 self.assertEqual(self.Interval(self.Scalar(1)), self.Interval.fromRadians(1, 1)) 

47 self.assertEqual(self.Interval(self.Scalar(1)), self.Scalar(1)) 

48 self.assertNotEqual(self.Interval.fromDegrees(1, 1), self.Interval.fromRadians(1, 1)) 

49 self.assertNotEqual(self.Interval.fromDegrees(2, 2), self.Scalar(1)) 

50 

51 def testCenterAndSize(self): 

52 a = self.Interval.fromRadians(1, 2) 

53 self.assertEqual(a.getSize(), self.Scalar(1)) 

54 self.assertEqual(a.getCenter(), self.Scalar(1.5)) 

55 

56 def testRelationships(self): 

57 a02 = self.Interval.fromRadians(0, 2) 

58 a13 = self.Interval.fromRadians(1, 3) 

59 a46 = self.Interval.fromRadians(4, 6) 

60 a06 = self.Interval.fromRadians(0, 6) 

61 self.assertTrue(a02.contains(self.Scalar(1))) 

62 self.assertTrue(a02.contains(self.Interval.fromRadians(0.5, 1.5))) 

63 self.assertTrue(a02.isDisjointFrom(self.Scalar(3))) 

64 self.assertTrue(a02.isDisjointFrom(a46)) 

65 self.assertTrue(a02.intersects(self.Scalar(1))) 

66 self.assertTrue(a02.intersects(a13)) 

67 self.assertTrue(self.Interval.fromRadians(1, 1).isWithin(a02)) 

68 self.assertTrue(a02.isWithin(a06)) 

69 r = a02.relate(self.Scalar(1)) 

70 self.assertEqual(r, CONTAINS) 

71 r = a46.relate(a02) 

72 self.assertEqual(r, DISJOINT) 

73 

74 def testExpandingAndClipping(self): 

75 a = self.Interval.fromRadians(1, 2) 

76 b = ( 

77 a.expandedTo(self.Scalar(3)) 

78 .expandedTo(self.Interval.fromRadians(2, 4)) 

79 .clippedTo(self.Interval.fromRadians(0, 2)) 

80 .clippedTo(self.Scalar(1)) 

81 ) 

82 a.expandTo(self.Scalar(3)).expandTo(self.Interval.fromRadians(2, 4)) 

83 a.clipTo(self.Interval.fromRadians(0, 2)).clipTo(self.Scalar(1)) 

84 self.assertEqual(a, b) 

85 self.assertEqual(a, self.Scalar(1)) 

86 

87 def testDilationAndErosion(self): 

88 a = self.Interval.fromRadians(1, 3) 

89 b = a.dilatedBy(self.Scalar(1)).erodedBy(self.Scalar(2)) 

90 a.dilateBy(self.Scalar(1)).erodeBy(self.Scalar(2)) 

91 self.assertEqual(a, b) 

92 self.assertEqual(a, self.Scalar(2)) 

93 

94 def testString(self): 

95 a = self.Interval.fromRadians(0.5, 1.5) 

96 self.assertEqual(str(a), "[0.5, 1.5]") 

97 self.assertEqual(repr(a), self.Interval.__name__ + ".fromRadians(0.5, 1.5)") 

98 self.assertEqual(a, eval(repr(a), {self.Interval.__name__: self.Interval})) 

99 

100 def testPickle(self): 

101 a = self.Interval.fromRadians(1, 3) 

102 b = pickle.loads(pickle.dumps(a)) 

103 self.assertEqual(a, b) 

104 

105 

106class AngleIntervalTestCase(unittest.TestCase, IntervalTests): 

107 """Angle interval test cases.""" 

108 

109 def setUp(self): 

110 self.Interval = AngleInterval 

111 self.Scalar = Angle 

112 

113 

114class NormalizedAngleIntervalTestCase(unittest.TestCase, IntervalTests): 

115 """Normalized angle interval test cases.""" 

116 

117 def setUp(self): 

118 self.Interval = NormalizedAngleInterval 

119 self.Scalar = NormalizedAngle 

120 

121 

122if __name__ == "__main__": 

123 unittest.main()