コード例 #1
0
    def build_random_string_insert(self):
        doc = self.doc
        snapshot = doc.get_snapshot()

        val = ''
        if len(self.remaining_chars) < 4:
            val = self.remaining_chars
        else:
            # pick random sample size
            k = random.choice(xrange(2, 5))
            # pick random letters
            val = ''.join(random.sample(self.remaining_chars, k))
        # pick random insertion point
        offset = random.choice(xrange(len(snapshot) + 1))

        # The inserted characters should appear after any existing character
        # before the offset
        previous_chars = snapshot[:offset]
        for char in previous_chars:
            self.results[char]['after'].extend(list(val))
        # The inserted characters should appear before any existing character
        # after the insertion
        subsequent_chars = snapshot[offset:]
        for char in subsequent_chars:
            self.results[char]['before'].extend(list(val))

        for i, char in enumerate(val):
            r = self.results[char]
            r['before'] = list(snapshot[:offset])
            r['before'].extend(list(val[:i]))
            r['after'] = list(snapshot[offset:])
            r['after'].extend(list(val[i + 1:]))
            self.remaining_chars = self.remaining_chars.replace(char, '')

        op = Op('si', [], offset=offset, val=val)
        op.cheat = previous_chars + " " + val + " " + subsequent_chars
        return op
コード例 #2
0
    def build_random_string_delete(self):
        doc = self.doc
        snapshot = doc.get_snapshot()

        # pick random offset
        offset = 0
        if len(snapshot) > 1:
            offset = random.choice(xrange(len(snapshot)))

        # get random size for delete range
        characters_past_offset = len(snapshot[offset:])
        max_val = min(5, characters_past_offset)
        val = 0
        # as long as there is something in the snapshot, pick a delete size
        # larger than zero
        if characters_past_offset != 0:
            val = random.choice(xrange(max_val)) + 1

        deleted_chars = snapshot[offset:offset + val]
        for char in deleted_chars:
            self.results[char]['deleted'] = True
        op = Op('sd', [], offset=offset, val=val)
        op.cheat = "delete " + deleted_chars
        return op