Пример #1
0
 def test14union(self):
     R1 = Range(self.items1)
     R1._check()
     R2 = Range(self.items2)
     R2._check()
     R3 = R1.union(R2)
     R3._check()
     self.assertEqual(list(R3), self.items1plus2)
     self.assertEqual(list(list(R3.spans())), self.spans1plus2)
Пример #2
0
 def test13update02fromRange(self):
     R1 = Range(self.items1)
     R1._check()
     R2 = Range(self.items2)
     R2._check()
     R1.update(R2)
     R1._check()
     self.assertEqual(list(R1), self.items1plus2)
     self.assertEqual(list(R1.spans()), self.spans1plus2)
Пример #3
0
 def test15discard(self):
     R1 = Range(self.items1)
     R1._check()
     R2 = Range(self.items2)
     R2._check()
     ##X("R1 = %s", R1)
     ##X("R2 = %s", R2)
     R1.discard(R2)
     R1._check()
     ##X("post discard, R1 = %s", R1)
     ##X("items1minus2 = %s", self.items1minus2)
     self.assertEqual(list(R1), self.items1minus2)
     self.assertEqual(list(list(R1.spans())), self.spans1minus2)
Пример #4
0
 def test10init(self):
     R0 = Range()
     R0._check()
     self.assertEqual(list(R0.spans()), [])
     R0.update(self.items1)
     R0._check()
     self.assertEqual(list(R0.spans()), self.spans1)
     R1 = Range(self.items1)
     R1._check()
     self.assertEqual(list(R1.spans()), self.spans1)
     self.assertEqual(R0, R1)
     R2 = Range(self.items2)
     R2._check()
     self.assertEqual(list(R2.spans()), self.spans2)
Пример #5
0
 def test17symmetric_difference(self):
     R1 = Range(self.items1)
     R1._check()
     R2 = Range(self.items2)
     R2._check()
     R3 = R1.symmetric_difference(R2)
     R3._check()
     self.assertEqual(list(R3), self.items1xor2)
     self.assertEqual(list(list(R3.spans())), self.spans1xor2)
     R4 = R1 ^ R2
     R4._check()
     self.assertEqual(list(R4), self.items1xor2)
     self.assertEqual(list(list(R4.spans())), self.spans1xor2)
     self.assertEqual(R4, R3)
     self.assertTrue(R4 is not R3, "R4 is R3")
Пример #6
0
 def __init__(self, fs):
   self.fs = fs  # main filesystem
   self._allocated = Range()  # range of allocated inode numbers
   self._by_inum = {}
   self._by_uuid = {}
   self._by_dirent = {}
   self._lock = RLock()
Пример #7
0
 def test16remove(self):
     R1 = Range(self.items1)
     R1._check()
     R1.remove(3)
     R1._check()
     self.assertRaises(KeyError, R1.remove, 3)
     R1._check()
Пример #8
0
 def test13update01fromSpans(self):
     R1 = Range(self.items1)
     R1._check()
     for span in self.spans2:
         R1.add_span(span[0], span[1])
         R1._check()
     self.assertEqual(list(R1), self.items1plus2)
     self.assertEqual(list(R1.spans()), self.spans1plus2)
Пример #9
0
 def test12copy(self):
     R1 = Range(self.items1)
     R2 = R1.copy()
     R2._check()
     self.assertTrue(R1 is not R2, "R1 is R2")
     self.assertEqual(R1, R2)
     self.assertEqual(R1._spans, R2._spans)
     self.assertEqual(list(R1.spans()), list(R2.spans()))
Пример #10
0
 def test17difference_subset_superset(self):
     R1 = Range(self.items1)
     R1._check()
     R2 = Range(self.items2)
     R2._check()
     R3 = R1.difference(R2)
     R3._check()
     self.assertEqual(list(R3), self.items1minus2)
     self.assertEqual(list(list(R3.spans())), self.spans1minus2)
     self.assertTrue(R1.issuperset(R3))
     self.assertTrue(R3.issubset(R1))
     R4 = R1 - R2
     R4._check()
     self.assertEqual(list(R4), self.items1minus2)
     self.assertEqual(list(list(R4.spans())), self.spans1minus2)
     self.assertTrue(R1.issuperset(R4))
     self.assertTrue(R4.issubset(R1))
Пример #11
0
    def __init__(self,
                 pathname,
                 read_only=False,
                 write_only=False,
                 binary=False,
                 newline=None,
                 lock_ext=None,
                 lock_timeout=None,
                 poll_interval=None):
        ''' Initialise this SharedAppendFile.

        Parameters:
        * `pathname`: the pathname of the file to open.
        * `read_only`: set to true if we will not write updates.
        * `write_only`: set to true if we will not read updates.
        * `binary`: if the file is to be opened in binary mode, otherwise text mode.
        * 'newline`: passed to `open()`
        * `lock_ext`: lock file extension.
        * `lock_timeout`: maxmimum time to wait for obtaining the lock file.
        * `poll_interval`: poll time when taking a lock file,
          default `DEFAULT_POLL_INTERVAL`
    '''
        with Pfx("SharedAppendFile(%r): __init__", pathname):
            if poll_interval is None:
                poll_interval = DEFAULT_POLL_INTERVAL
            self.pathname = abspath(pathname)
            self.binary = binary
            self.newline = newline
            self.read_only = read_only
            self.write_only = write_only
            self.lock_ext = lock_ext
            self.lock_timeout = lock_timeout
            self.poll_interval = poll_interval
            if self.read_only:
                if self.write_only:
                    raise ValueError(
                        "only one of read_only and write_only may be true")
                o_flags = O_RDONLY
            elif self.write_only:
                o_flags = O_WRONLY | O_APPEND
            else:
                o_flags = O_RDWR | O_APPEND
            self._fd = os.open(self.pathname, o_flags)
            self._rfp = None
            self._read_offset = 0
            self._read_skip = Range()
            self._readlock = RLock()
            if not self.write_only:
                self._readopen()
            self.closed = False
Пример #12
0
 def test30random_set_equivalence(self):
     R1 = Range()
     S = set()
     self.assertEqual(S, set(R1))
     for i in range(100):
         n = randint(0, 99)
         if randint(0, 1):
             ##X("add %d (new=%s)", n, n in S)
             R1.add(n)
             S.add(n)
         else:
             ##X("discard %d (extant=%s)", n, n in S)
             R1.discard(n)
             S.discard(n)
         ##X("S = %s", S)
         ##X("R1 = %s", R1)
         self.assertEqual(S, set(R1))  ## "set:%s vs Range:%s" % (S, R1))
Пример #13
0
 def test11equals(self):
     R1 = Range(self.items1)
     self.assertEqual(R1, R1)
     self.assertEqual(list(iter(R1)), sorted(self.items1))