Coverage for tests / test_RangeSet.py: 16%

62 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-17 08:41 +0000

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 RangeSet 

32 

33 

34class RangeSetTestCase(unittest.TestCase): 

35 """Test RangeSet.""" 

36 

37 def testConstruction(self): 

38 s1 = RangeSet(1) 

39 s2 = RangeSet() 

40 s3 = RangeSet(2, 1) 

41 s4 = RangeSet(s3) 

42 self.assertTrue(s2.empty()) 

43 self.assertEqual(s3, s4) 

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

45 

46 def testComparisonOperators(self): 

47 s1 = RangeSet(1) 

48 s2 = RangeSet(2) 

49 self.assertNotEqual(s1, s2) 

50 s1.insert(2) 

51 s2.insert(1) 

52 self.assertEqual(s1, s2) 

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

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

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

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

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

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

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

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

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

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

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

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

65 

66 def testSetOperators(self): 

67 a = RangeSet(1) 

68 b = ~a 

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

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

71 self.assertEqual(a - b, a) 

72 self.assertEqual(b - a, b) 

73 a &= a 

74 b &= b 

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

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

77 c |= b 

78 self.assertTrue(c.full()) 

79 c ^= c 

80 self.assertTrue(c.empty()) 

81 

82 def testRanges(self): 

83 s = RangeSet() 

84 s.insert(0, 1) 

85 s.insert(2, 3) 

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

87 s = RangeSet(4, 2) 

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

89 

90 def testString(self): 

91 s = RangeSet(1, 10) 

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

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

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

95 

96 def testPickle(self): 

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

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

99 self.assertEqual(r, s) 

100 

101 

102if __name__ == "__main__": 

103 unittest.main()