def testBinarySearch(self): # binary_search always returns an index, so we do the comparison here def found(A, result, value): if ((result < len(A)) and (A[result] == value)): return result else: return False for offset in xrange(1, 5): for length in xrange(1, 2049, 300): A = [i * offset for i in xrange(0, length)] ## check negative hits # search value too low # error if value is found # if search returns non-negative index, fail value = A[0] - 1 self.assertFalse(found(A, binary_search(A, value), value)) # search value too high # error if value is found # if search returns non-negative index, fail value = A[-1] + 1 self.assertFalse(found(A, binary_search(A, value), value)) ## check positive hits for i, a in enumerate(A): # error if value is NOT found # if search does not return correct value, fail self.assertEquals(binary_search(A, A[i]), i)
def _setThumbnail(self, time, pixbuf): # Q: Is "time" guaranteed to be nanosecond precise? # A: Not always. # => __tim says: "that's how it should be" # => also see gst-plugins-good/tests/icles/gdkpixbufsink-test # => Daniel: It is *not* nanosecond precise when we remove the videorate # element from the pipeline # => thiblahute: not the case with mpegts original_time = time if time in self.thumbs: thumb = self.thumbs[time] else: sorted_times = sorted(self.thumbs.keys()) index = binary_search(sorted_times, time) time = sorted_times[index] thumb = self.thumbs[time] if thumb.has_pixel_data: # If this happens, it means the precision of the thumbnail # generator is not good enough for the current thumbnail # interval. # We could consider shifting the thumbnails, but seems like # too much trouble for something which does not happen in # practice. My last words.. self.fixme("Thumbnail is already set for time: %s, %s", format_ns(time), format_ns(original_time)) return thumb.set_from_gdkpixbuf_animated(pixbuf) if time in self.queue: self.queue.remove(time) self.thumb_cache[time] = pixbuf
def test_missing(self): """Checks the result when the element is missing.""" self.assertEqual(binary_search([10, 20, 30], 1), 0) self.assertEqual(binary_search([10, 20, 30], 11), 0) self.assertEqual(binary_search([10, 20, 30], 16), 1) self.assertEqual(binary_search([10, 20, 30], 24), 1) self.assertEqual(binary_search([10, 20, 30], 29), 2) self.assertEqual(binary_search([10, 20, 30], 40), 2)
def _set_pixbuf(self, position, pixbuf): """Sets the pixbuf for the thumbnail at the specified position.""" if position in self.thumbs: thumb = self.thumbs[position] else: # The pixbufs we get from gdkpixbufsink are not always # exactly the ones requested, the reported position can differ. # Try to find the closest thumbnail for the specified position. sorted_times = sorted(self.thumbs.keys()) index = binary_search(sorted_times, position) position = sorted_times[index] thumb = self.thumbs[position] thumb.set_from_pixbuf(pixbuf) if position in self.queue: self.queue.remove(position) self.thumb_cache[position] = pixbuf self.queue_draw()
def _setThumbnail(self, time, pixbuf): # Q: Is "time" guaranteed to be nanosecond precise? # A: Not always. # => __tim says: "that's how it should be" # => also see gst-plugins-good/tests/icles/gdkpixbufsink-test # => Daniel: It is *not* nanosecond precise when we remove the videorate # element from the pipeline # => thiblahute: not the case with mpegts if time in self.thumbs: thumb = self.thumbs[time] else: sorted_times = sorted(self.thumbs.keys()) index = binary_search(sorted_times, time) time = sorted_times[index] thumb = self.thumbs[time] thumb.set_from_pixbuf(pixbuf) if time in self.queue: self.queue.remove(time) self.thumb_cache[time] = pixbuf self.queue_draw()
def testEmptyList(self): self.assertEquals(binary_search([], 10), -1)
def test_existing(self): """Checks the result when the element is present.""" self.assertEqual(binary_search([10, 20, 30], 10), 0) self.assertEqual(binary_search([10, 20, 30], 20), 1) self.assertEqual(binary_search([10, 20, 30], 30), 2)
def testEmptyList(self): self.assertEqual(binary_search([], 10), -1)
def testExisting(self): A = [10, 20, 30] for index, element in enumerate(A): self.assertEqual(binary_search([10, 20, 30], element), index)
def testMissingLeft(self): self.assertEqual(binary_search([10, 20, 30], 1), 0) self.assertEqual(binary_search([10, 20, 30], 16), 1) self.assertEqual(binary_search([10, 20, 30], 29), 2)
def testMissingRight(self): self.assertEquals(binary_search([10, 20, 30], 11), 0) self.assertEquals(binary_search([10, 20, 30], 24), 1) self.assertEquals(binary_search([10, 20, 30], 40), 2)
def testMissingLeft(self): self.assertEquals(binary_search([10, 20, 30], 1), 0) self.assertEquals(binary_search([10, 20, 30], 16), 1) self.assertEquals(binary_search([10, 20, 30], 29), 2)
def testExisting(self): A = [10, 20, 30] for index, element in enumerate(A): self.assertEquals(binary_search([10, 20, 30], element), index)
def test_empty_list(self): """Checks the result when the list is empty.""" self.assertEqual(binary_search([], 10), -1)
def testMissingRight(self): self.assertEqual(binary_search([10, 20, 30], 11), 0) self.assertEqual(binary_search([10, 20, 30], 24), 1) self.assertEqual(binary_search([10, 20, 30], 40), 2)