def test_markov_image_generate_levels(args, kwargs, data, res): scanner = Scanner(lambda x: x) scanner.traversal = [HLines(), VLines()] scanner.levels = 2 scanner.level_scale = [2] markov = MarkovImage(levels=2, scanner=scanner) markov.imgtype.convert = lambda x: [x] if data: markov.data([ ['\x00', '\x01', '\x02', '\x03'], [(Scanner.START, '\x00'), '\x01', (Scanner.START, '\x01'), '\x02', (Scanner.START, '\x02'), '\x03', (Scanner.START, '\x03'), '\x00'] ]) if 'start_image' in kwargs: img = Image.new('P', (1, 1)) img.putpixel((0, 0), 1) kwargs['start_image'] = img if isinstance(res, type): with pytest.raises(res): markov(*args, **kwargs) else: assert list(markov(*args, **kwargs).getdata()) == res
def test_markov_image_generate(test, res): scanner = Scanner(lambda x: x) scanner.traversal = [HLines()] scanner.levels = 1 scanner.level_scale = [] markov = MarkovImage( scanner=scanner, parser=Parser() ) markov.imgtype.convert = lambda x: [x] markov.data([['\x00', '\x01', '\x02', '\x03']]) assert list(markov(*test).getdata()) == res
def test_properties(self): m = self.Markov(scanner=Scanner(lambda x: x)) m.links([(('x', 'y'), 'z')]) m.separator = '::' m.cursor.execute('SELECT value FROM nodes') nodes = m.cursor.fetchall() self.assertEqual(nodes, [('x::y', ), ('y::z', )])
def test_generate_state_size(self): m = self.Markov(separator=':', parser=Parser(state_sizes=[2, 3]), scanner=Scanner(lambda x: x)) m.data(['x', 'y', 'z']) self.assertEqual(''.join(m.generate(10, state_size=2)), 'xyz') self.assertEqual(''.join(m.generate(10, state_size=3)), 'xyz')
def test_generate(self): m = self.Markov(scanner=Scanner(lambda x: x)) m.data(['x', 'y']) self.assertEqual(''.join(m.generate(1, start='')), 'x') self.assertEqual(''.join(m.generate(10, start='x')), 'y') self.assertEqual(''.join(m.generate(10, start='y')), '') self.assertIn(''.join(m.generate(10)), ['y', 'xy'])
def test_properties_parse(self): scanner = Scanner(lambda x: x) parser = Parser(state_sizes=[2], reset_on_sentence_end=False) self.assertEqual(self.parse(parser, scanner, ['a', scanner.END, 'b'], True, '::'), [('::', 'a'), ('::a', 'b')]) self.assertEqual(self.parse(parser, scanner, 'c', separator='::'), [('a::b', 'c')])
def test_generate(self): scanner = Scanner(lambda x: x) scanner.traversal = [HLines()] scanner.levels = 1 scanner.level_scale = [] m = self.Markov(palette=self.palette, scanner=scanner, parser=Parser()) with self.assertRaises(RuntimeError): m.image(2, 2) m.data([['00', '01', '02', '03']]) data = list(m.image(2, 2).getdata()) self.assertEqual(data, [0, 1, 2, 3]) data = list(m.image(4, 2).getdata()) self.assertEqual(data, [0, 1, 2, 3, 0, 1, 2, 3])
def test_save_load(self): tests = [(), ((4, 4), 0, True, self.palette, 2, 2, Image.NEAREST, [HLines(), VLines()])] for test in tests: scanner = ImageScanner(*test) saved = scanner.save() loaded = Scanner.load(saved) self.assertEqual(scanner, loaded)
def test_state_size(self): scanner = Scanner(lambda x: x) parser = Parser(state_sizes=[3]) self.assertEqual(self.parse(parser, scanner, 'abcde'), [(' ', 'a'), (' a', 'b'), (' a b', 'c'), ('a b c', 'd'), ('b c d', 'e')]) self.assertEqual(self.parse(parser, scanner, ['a', 'b', 'c', Scanner.END, 'd', 'e']), [(' ', 'a'), (' a', 'b'), (' a b', 'c'), (' ', 'd'), (' d', 'e')]) self.assertEqual(self.parse(parser, scanner, ['a', 'b', 'c', (Scanner.START, 'd'), 'e']), [(' ', 'a'), (' a', 'b'), (' a b', 'c'), (' d', 'e')])
def test_default_parse(self): scanner = Scanner(lambda x: x) parser = Parser() self.assertEqual(self.parse(parser, scanner, ''), []) self.assertEqual(self.parse(parser, scanner, 'abc', True), [('', 'a'), ('a', 'b'), ('b', 'c')]) self.assertEqual(self.parse(parser, scanner, ['a', 'b', scanner.END, 'c']), [('c', 'a'), ('a', 'b'), ('', 'c')]) self.assertEqual(self.parse(parser, scanner, ['a', Scanner.END, Scanner.END, 'c']), [('', 'a'), ('', 'c')]) self.assertEqual(self.parse(parser, scanner, [Scanner.END] * 4), [])
def test_save_load(self): db = os.path.join(self.tmpdir.name, 'test.db') m = self.Markov(db=db, separator=':', parser=Parser(state_sizes=[2, 3]), scanner=Scanner(lambda x: x)) m.data(['x', 'y', 'z']) m.scanner = CharScanner() m.save() loaded = self.Markov.load(db) self.assertEqual(m, loaded) self.assertEqual(''.join(loaded.generate(10, state_size=2)), 'xyz') loaded = self.Markov.load(db, {'separator': ''}) self.assertNotEqual(m, loaded) self.assertEqual(loaded.separator, '')
def test_save_load(self): m = self.Markov(separator=':', parser=Parser(state_sizes=[2, 3]), scanner=Scanner(lambda x: x)) m.data(['', 'x', 'y', 'z', None]) m.scanner = CharScanner() fp = StringIO() m.save(fp) fp.seek(0) loaded = self.Markov.load(fp) self.assertEqual(m, loaded) fp.seek(0) fp1 = BytesIO() fp1.write(fp.read().encode('utf-8')) fp1.seek(0) loaded = self.Markov.load(fp1) self.assertEqual(m, loaded) fp.seek(0) loaded = self.Markov.load(fp, {'separator': ''}) self.assertNotEqual(m, loaded) self.assertEqual(loaded.separator, '')
def test_image_scanner_save_load(test): scanner = ImageScanner(*test) saved = scanner.save() loaded = Scanner.load(saved) assert isinstance(loaded, ImageScanner) assert scanner == loaded
def test_generate_levels(self): scanner = Scanner(lambda x: x) scanner.traversal = [HLines(), VLines()] scanner.levels = 2 scanner.level_scale = [2] scanner.set_palette = lambda img: img m = self.Markov(levels=2, palette=self.palette, scanner=scanner) with self.assertRaises(RuntimeError): m.image(2, 2) m.data([ ['00', '01', '02', '03',], [(Scanner.START, '0000'), '0101', (Scanner.START, '0001'), '0102', (Scanner.START, '0002'), '0103', (Scanner.START, '0003'), '0100',] ]) data = list(m.image(2, 2, levels=1).getdata()) # pylint: disable=no-member self.assertEqual(data, [0, 1, 2, 3]) data = list(m.image(2, 2, levels=2).getdata()) # pylint: disable=no-member self.assertEqual( data, [ 0, 1, 1, 2, 1, 1, 2, 2, 2, 3, 3, 0, 3, 3, 0, 0 ] ) with self.assertRaises(ValueError): m.image(2, 2, levels=3) with self.assertRaises(ValueError): m.image(2, 2, levels=0) img = Image.new('P', (1, 1)) img.putpalette(self.palette) img.putpixel((0, 0), 1) data = list(m.image( 2, 1, levels=1, start_level=-5, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [0, 1]) data = list(m.image( None, None, levels=1, start_level=0, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1]) data = list(m.image( None, None, levels=1, start_level=2, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1]) data = list(m.image( None, None, levels=2, start_level=0, start_image=img ).getdata()) # pylint: disable=no-member self.assertEqual(data, [1, 2, 2, 2])
def test_properties(self): m = self.Markov(scanner=Scanner(lambda x: x)) m.links([(('x', 'y'), 'z')]) m.separator = '::' self.assertEqual(list(m.nodes.keys()), ['x::y'])