Coverage for tests/test_RangeSet.py: 16%

66 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 sys 

30import unittest 

31 

32from lsst.sphgeom import RangeSet 

33 

34 

35class RangeSetTestCase(unittest.TestCase): 

36 """Test RangeSet.""" 

37 

38 def testConstruction(self): 

39 s1 = RangeSet(1) 

40 s2 = RangeSet() 

41 s3 = RangeSet(2, 1) 

42 s4 = RangeSet(s3) 

43 self.assertTrue(s2.empty()) 

44 self.assertEqual(s3, s4) 

45 self.assertEqual(s1, s3.complement()) 

46 

47 def testComparisonOperators(self): 

48 s1 = RangeSet(1) 

49 s2 = RangeSet(2) 

50 self.assertNotEqual(s1, s2) 

51 s1.insert(2) 

52 s2.insert(1) 

53 self.assertEqual(s1, s2) 

54 self.assertTrue(RangeSet(2, 1).contains(RangeSet(3, 4))) 

55 self.assertTrue(RangeSet(2, 1).contains(3, 4)) 

56 self.assertTrue(RangeSet(2, 1).contains(3)) 

57 self.assertTrue(RangeSet(2, 4).isWithin(RangeSet(1, 5))) 

58 self.assertTrue(RangeSet(2, 4).isWithin(1, 5)) 

59 self.assertFalse(RangeSet(2, 4).isWithin(3)) 

60 self.assertTrue(RangeSet(2, 4).intersects(RangeSet(3, 5))) 

61 self.assertTrue(RangeSet(2, 4).intersects(3, 5)) 

62 self.assertTrue(RangeSet(2, 4).intersects(3)) 

63 self.assertTrue(RangeSet(2, 4).isDisjointFrom(RangeSet(6, 8))) 

64 self.assertTrue(RangeSet(2, 4).isDisjointFrom(6, 8)) 

65 self.assertTrue(RangeSet(2, 4).isDisjointFrom(6)) 

66 

67 def testSetOperators(self): 

68 a = RangeSet(1) 

69 b = ~a 

70 self.assertTrue((a | b).full()) 

71 self.assertTrue((a & b).empty()) 

72 self.assertEqual(a - b, a) 

73 self.assertEqual(b - a, b) 

74 a &= a 

75 b &= b 

76 c = (a ^ b) - RangeSet(2, 4) 

77 self.assertEqual(c, RangeSet(4, 2)) 

78 c |= b 

79 self.assertTrue(c.full()) 

80 c ^= c 

81 self.assertTrue(c.empty()) 

82 

83 def testRanges(self): 

84 s = RangeSet() 

85 s.insert(0, 1) 

86 s.insert(2, 3) 

87 self.assertEqual(s.ranges(), [(0, 1), (2, 3)]) 

88 s = RangeSet(4, 2) 

89 self.assertEqual(list(s), [(0, 2), (4, 0)]) 

90 

91 def testString(self): 

92 s = RangeSet(1, 10) 

93 if sys.version_info[0] >= 3: 

94 self.assertEqual(str(s), "[(1, 10)]") 

95 self.assertEqual(repr(s), "RangeSet([(1, 10)])") 

96 else: 

97 # pybind11 maps C++ integers to Python long instances in Python 2. 

98 self.assertEqual(str(s), "[(1L, 10L)]") 

99 self.assertEqual(repr(s), "RangeSet([(1L, 10L)])") 

100 self.assertEqual(s, eval(repr(s), {"RangeSet": RangeSet})) 

101 

102 def testPickle(self): 

103 r = RangeSet([2, 3, 5, 7, 11, 13, 17, 19]) 

104 s = pickle.loads(pickle.dumps(r)) 

105 self.assertEqual(r, s) 

106 

107 

108if __name__ == "__main__": 

109 unittest.main()