def test_read_1_by_1_from_iterable_class(self): class DataGen(object): def __init__(self): self.data = b"abcd" self.pos = 0 def __iter__(self): return self def next(self): # Python 2 if self.pos >= len(self.data): raise StopIteration() self.pos += 1 return self.data[self.pos - 1] def __next__(self): # Python 3, yield bytes, not int return bytes((self.next(), )) gen = GeneratorIO(DataGen()) self.assertEqual(gen.read(1), b"a") self.assertEqual(gen.read(1), b"b") self.assertEqual(gen.read(1), b"c") self.assertEqual(gen.read(1), b"d") self.assertEqual(gen.read(1), b"")
def test_read_empty_data(self): data = [] gen = GeneratorIO(data) self.assertEqual(gen.read(10), b'') data = ["", "", ""] gen = GeneratorIO(data) self.assertEqual(gen.read(10), b'')
def test_read_1_by_1_byte_variable_input(self): data = [b"a", b"bc", b"d"] gen = GeneratorIO(data) self.assertEqual(gen.read(1), b"a") self.assertEqual(gen.read(1), b"b") self.assertEqual(gen.read(1), b"c") self.assertEqual(gen.read(1), b"d") self.assertEqual(gen.read(1), b"")
def test_read_1_by_1_from_tuple(self): data = (b"a", b"bc", b"d") gen = GeneratorIO(data) self.assertEqual(gen.read(1), b"a") self.assertEqual(gen.read(1), b"b") self.assertEqual(gen.read(1), b"c") self.assertEqual(gen.read(1), b"d") self.assertEqual(gen.read(1), b"")
def test_read_1_by_1_byte_from_list(self): data = [b"a", b"b", b"c", b"d"] gen = GeneratorIO(data) self.assertEqual(gen.read(1), b"a") self.assertEqual(gen.read(1), b"b") self.assertEqual(gen.read(1), b"c") self.assertEqual(gen.read(1), b"d") self.assertEqual(gen.read(1), b"")
def test_read_1_by_1_byte(self): data = ["a", "bc", "d"] gen = GeneratorIO(iter(data)) self.assertEqual(gen.read(1), "a") self.assertEqual(gen.read(1), "b") self.assertEqual(gen.read(1), "c") self.assertEqual(gen.read(1), "d") self.assertEqual(gen.read(1), "")
def test_read_1_by_1_from_generator(self): def gen_data(): yield b'a' yield b'bc' yield b'd' gen = GeneratorIO(gen_data()) self.assertEqual(gen.read(1), b"a") self.assertEqual(gen.read(1), b"b") self.assertEqual(gen.read(1), b"c") self.assertEqual(gen.read(1), b"d") self.assertEqual(gen.read(1), b"")
def test_read_1_by_1_from_iterable_class(self): class DataGen(object): def __init__(self): self.data = "abcd" self.pos = 0 def __iter__(self): return self def next(self): if self.pos >= len(self.data): raise StopIteration() self.pos += 1 return self.data[self.pos - 1] gen = GeneratorIO(DataGen()) self.assertEqual(gen.read(1), "a") self.assertEqual(gen.read(1), "b") self.assertEqual(gen.read(1), "c") self.assertEqual(gen.read(1), "d") self.assertEqual(gen.read(1), "")
def test_read_more_than_data_size(self): data = [b"a", b"bc", b"d"] gen = GeneratorIO(data, True) self.assertEqual(gen.read(10), b"abcd") self.assertEqual(gen.read(10), b"")
def test_read_more_than_data_size(self): data = ["a", "bc", "d"] gen = GeneratorIO(iter(data)) self.assertEqual(gen.read(10), "abcd") self.assertEqual(gen.read(10), "")