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)
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)
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)
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)
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")
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()
def test16remove(self): R1 = Range(self.items1) R1._check() R1.remove(3) R1._check() self.assertRaises(KeyError, R1.remove, 3) R1._check()
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)
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()))
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))
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
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))
def test11equals(self): R1 = Range(self.items1) self.assertEqual(R1, R1) self.assertEqual(list(iter(R1)), sorted(self.items1))