def test_numbits_union(self): res = self.cursor.execute("select numbits_union(" + "(select numbits from data where id = 7)," + "(select numbits from data where id = 9)" + ")") expected = [ 7, 9, 14, 18, 21, 27, 28, 35, 36, 42, 45, 49, 54, 56, 63, 70, 72, 77, 81, 84, 90, 91, 98, 99, ] answer = numbits_to_nums(list(res)[0][0]) assert expected == answer
def test_numbits_intersection(self): res = self.cursor.execute("select numbits_intersection(" + "(select numbits from data where id = 7)," + "(select numbits from data where id = 9)" + ")") answer = numbits_to_nums(list(res)[0][0]) assert [63] == answer
def test_numbits_intersection(self): res = self.cursor.execute("select numbits_intersection(" "(select numbits from data where id = 7)," "(select numbits from data where id = 9)" ")") answer = numbits_to_nums(list(res)[0][0]) self.assertEqual([63], answer)
def lines(self, filename): """Get the list of lines executed for a source file. If the file was not measured, returns None. A file might be measured, and have no lines executed, in which case an empty list is returned. If the file was executed, returns a list of integers, the line numbers executed in the file. The list is in no particular order. """ self._start_using() if self.has_arcs(): arcs = self.arcs(filename) if arcs is not None: all_lines = itertools.chain.from_iterable(arcs) return list({l for l in all_lines if l > 0}) with self._connect() as con: file_id = self._file_id(filename) if file_id is None: return None else: query = "select numbits from line_bits where file_id = ?" data = [file_id] if self._query_context_ids is not None: ids_array = ', '.join('?' * len(self._query_context_ids)) query += " and context_id in (" + ids_array + ")" data += self._query_context_ids bitmaps = list(con.execute(query, data)) nums = set() for row in bitmaps: nums.update(numbits_to_nums(row[0])) return list(nums)
def lines(self, filename, contexts=None): self._start_using() if self.has_arcs(): arcs = self.arcs(filename, contexts=contexts) if arcs is not None: all_lines = itertools.chain.from_iterable(arcs) return list(set(l for l in all_lines if l > 0)) with self._connect() as con: file_id = self._file_id(filename) if file_id is None: return None else: query = "select numbits from line_map where file_id = ?" data = [file_id] context_ids = self._get_query_context_ids(contexts) if context_ids is not None: ids_array = ', '.join('?'*len(context_ids)) query += " and context_id in (" + ids_array + ")" data += context_ids bitmaps = list(con.execute(query, data)) nums = set() for row in bitmaps: nums.update(numbits_to_nums(from_blob(row[0]))) return sorted(nums)
def test_intersection(self, nums1, nums2): nb1 = nums_to_numbits(nums1) good_numbits(nb1) nb2 = nums_to_numbits(nums2) good_numbits(nb2) nbi = numbits_intersection(nb1, nb2) good_numbits(nbi) intersection = numbits_to_nums(nbi) assert nums1 & nums2 == set(intersection)
def test_union(self, nums1, nums2): nb1 = nums_to_numbits(nums1) good_numbits(nb1) nb2 = nums_to_numbits(nums2) good_numbits(nb2) nbu = numbits_union(nb1, nb2) good_numbits(nbu) union = numbits_to_nums(nbu) assert nums1 | nums2 == set(union)
def test_union(self, nums1, nums2): nb1 = nums_to_numbits(nums1) good_numbits(nb1) nb2 = nums_to_numbits(nums2) good_numbits(nb2) nbu = numbits_union(nb1, nb2) good_numbits(nbu) union = numbits_to_nums(nbu) self.assertEqual(nums1 | nums2, set(union))
def test_numbits_union(self): res = self.cursor.execute("select numbits_union(" "(select numbits from data where id = 7)," "(select numbits from data where id = 9)" ")") answer = numbits_to_nums(list(res)[0][0]) self.assertEqual([ 7, 9, 14, 18, 21, 27, 28, 35, 36, 42, 45, 49, 54, 56, 63, 70, 72, 77, 81, 84, 90, 91, 98, 99 ], answer)
def contexts_by_lineno(self, filename): """Get the contexts for each line in a file. Returns: A dict mapping line numbers to a list of context names. .. versionadded:: 5.0 """ self._start_using() with self._connect() as con: file_id = self._file_id(filename) if file_id is None: return {} lineno_contexts_map = collections.defaultdict(set) if self.has_arcs(): query = ( "select arc.fromno, arc.tono, context.context " + "from arc, context " + "where arc.file_id = ? and arc.context_id = context.id") data = [file_id] if self._query_context_ids is not None: ids_array = ", ".join("?" * len(self._query_context_ids)) query += " and arc.context_id in (" + ids_array + ")" data += self._query_context_ids for fromno, tono, context in con.execute(query, data): if fromno > 0: lineno_contexts_map[fromno].add(context) if tono > 0: lineno_contexts_map[tono].add(context) else: query = ( "select l.numbits, c.context from line_bits l, context c " + "where l.context_id = c.id " + "and file_id = ?") data = [file_id] if self._query_context_ids is not None: ids_array = ", ".join("?" * len(self._query_context_ids)) query += " and l.context_id in (" + ids_array + ")" data += self._query_context_ids for numbits, context in con.execute(query, data): for lineno in numbits_to_nums(numbits): lineno_contexts_map[lineno].add(context) return { lineno: list(contexts) for lineno, contexts in lineno_contexts_map.items() }
def contexts_by_lineno(self, filename): lineno_contexts_map = collections.defaultdict(list) self._start_using() with self._connect() as con: file_id = self._file_id(filename) if file_id is None: return lineno_contexts_map if self.has_arcs(): query = ( "select arc.fromno, arc.tono, context.context " "from arc, context " "where arc.file_id = ? and arc.context_id = context.id" ) data = [file_id] context_ids = self._get_query_context_ids() if context_ids is not None: ids_array = ', '.join('?'*len(context_ids)) query += " and arc.context_id in (" + ids_array + ")" data += context_ids for fromno, tono, context in con.execute(query, data): if context not in lineno_contexts_map[fromno]: lineno_contexts_map[fromno].append(context) if context not in lineno_contexts_map[tono]: lineno_contexts_map[tono].append(context) else: query = ( "select l.numbits, c.context from line_map l, context c " "where l.context_id = c.id " "and file_id = ?" ) data = [file_id] context_ids = self._get_query_context_ids() if context_ids is not None: ids_array = ', '.join('?'*len(context_ids)) query += " and l.context_id in (" + ids_array + ")" data += context_ids for numbits, context in con.execute(query, data): for lineno in numbits_to_nums(from_blob(numbits)): lineno_contexts_map[lineno].append(context) return lineno_contexts_map
def test_conversion(self, nums): numbits = nums_to_numbits(nums) good_numbits(numbits) nums2 = numbits_to_nums(numbits) assert nums == set(nums2)
def test_conversion(self, nums): numbits = nums_to_numbits(nums) good_numbits(numbits) nums2 = numbits_to_nums(numbits) self.assertEqual(nums, set(nums2))
def test_merging(self, nums1, nums2): merged = numbits_to_nums( merge_numbits(nums_to_numbits(nums1), nums_to_numbits(nums2))) self.assertEqual(nums1 | nums2, set(merged))