<s> import unittest <EOL> import re <EOL> from snap7 import util <EOL> test_spec = """<STR_LIT>""" <EOL> _bytearray = bytearray ( [ <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:4> , <NUM_LIT:4> , ord ( '<STR_LIT:t>' ) , ord ( '<STR_LIT:e>' ) , ord ( '<STR_LIT:s>' ) , ord ( '<STR_LIT:t>' ) , <EOL> <NUM_LIT> * <NUM_LIT:0> + <NUM_LIT:64> * <NUM_LIT:0> + <NUM_LIT:32> * <NUM_LIT:0> + <NUM_LIT:16> * <NUM_LIT:0> + <EOL> <NUM_LIT:8> * <NUM_LIT:1> + <NUM_LIT:4> * <NUM_LIT:1> + <NUM_LIT:2> * <NUM_LIT:1> + <NUM_LIT:1> * <NUM_LIT:1> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> <EOL> ] ) <EOL> class TestS7util ( unittest . TestCase ) : <EOL> def test_get_string ( self ) : <EOL> """<STR_LIT>""" <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == '<STR_LIT:test>' ) <EOL> def test_write_string ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> row [ '<STR_LIT>' ] = '<STR_LIT:abc>' <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == '<STR_LIT:abc>' ) <EOL> row [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> try : <EOL> row [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> except ValueError : <EOL> pass <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == '<STR_LIT>' ) <EOL> def test_get_int ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> x = row [ '<STR_LIT>' ] <EOL> self . assertTrue ( x == <NUM_LIT:0> ) <EOL> def test_set_int ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> row [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT> ) <EOL> def test_get_bool ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> def test_set_bool ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> row [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> row [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> def test_db_creation ( self ) : <EOL> test_array = bytearray ( _bytearray * <NUM_LIT:10> ) <EOL> test_db = util . DB ( <NUM_LIT:1> , test_array , test_spec , <EOL> row_size = len ( _bytearray ) , <EOL> size = <NUM_LIT:10> , <EOL> layout_offset = <NUM_LIT:4> , <EOL> db_offset = <NUM_LIT:0> ) <EOL> self . assertTrue ( len ( test_db . index ) == <NUM_LIT:10> ) <EOL> for i , row in test_db : <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:1> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == '<STR_LIT:test>' ) <EOL> def test_get_real ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> self . assertTrue ( <NUM_LIT> > ( row [ '<STR_LIT>' ] - <NUM_LIT> ) > - <NUM_LIT:0.1> ) <EOL> def test_set_real ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> row [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertTrue ( <NUM_LIT> > ( row [ '<STR_LIT>' ] - <NUM_LIT> ) > - <NUM_LIT> ) <EOL> def test_set_dword ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> row [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT> ) <EOL> def test_get_dword ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> self . assertTrue ( row [ '<STR_LIT>' ] == <NUM_LIT> ) <EOL> def test_export ( self ) : <EOL> test_array = bytearray ( _bytearray ) <EOL> row = util . DB_Row ( test_array , test_spec , layout_offset = <NUM_LIT:4> ) <EOL> data = row . export ( ) <EOL> self . assertTrue ( '<STR_LIT>' in data ) <EOL> self . assertTrue ( '<STR_LIT>' in data ) <EOL> self . assertTrue ( data [ '<STR_LIT>' ] == <NUM_LIT:0> ) <EOL> def print_row ( data ) : <EOL> """<STR_LIT>""" <EOL> index_line = "<STR_LIT>" <EOL> pri_line1 = "<STR_LIT>" <EOL> chr_line2 = "<STR_LIT>" <EOL> asci = re . compile ( '<STR_LIT>' ) <EOL> for i , xi in enumerate ( data ) : <EOL> if not i % <NUM_LIT:5> : <EOL> diff = len ( pri_line1 ) - len ( index_line ) <EOL> i = str ( i ) <EOL> index_line += diff * '<STR_LIT:U+0020>' <EOL> index_line += i <EOL> str_v = str ( xi ) <EOL> pri_line1 += str ( xi ) + '<STR_LIT:U+002C>' <EOL> c = chr ( xi ) <EOL> c = c if asci . match ( c ) else '<STR_LIT:U+0020>' <EOL> w = len ( str_v ) <EOL> c = c + ( w - <NUM_LIT:1> ) * '<STR_LIT:U+0020>' + '<STR_LIT:U+002C>' <EOL> chr_line2 += c <EOL> print ( index_line ) <EOL> print ( pri_line1 ) <EOL> print ( chr_line2 ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
