<s> """<STR_LIT>""" <EOL> import os <EOL> import unittest <EOL> import dumbdbm <EOL> from test import test_support <EOL> _fname = test_support . TESTFN <EOL> def _delete_files ( ) : <EOL> for ext in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> try : <EOL> os . unlink ( _fname + ext ) <EOL> except OSError : <EOL> pass <EOL> class DumbDBMTestCase ( unittest . TestCase ) : <EOL> _dict = { '<STR_LIT:0>' : '<STR_LIT>' , <EOL> '<STR_LIT:a>' : '<STR_LIT>' , <EOL> '<STR_LIT:b>' : '<STR_LIT>' , <EOL> '<STR_LIT:c>' : '<STR_LIT>' , <EOL> '<STR_LIT:d>' : '<STR_LIT>' , <EOL> '<STR_LIT:f>' : '<STR_LIT>' , <EOL> '<STR_LIT:g>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , * args ) : <EOL> unittest . TestCase . __init__ ( self , * args ) <EOL> def test_dumbdbm_creation ( self ) : <EOL> f = dumbdbm . open ( _fname , '<STR_LIT:c>' ) <EOL> self . assertEqual ( f . keys ( ) , [ ] ) <EOL> for key in self . _dict : <EOL> f [ key ] = self . _dict [ key ] <EOL> self . read_helper ( f ) <EOL> f . close ( ) <EOL> def test_dumbdbm_creation_mode ( self ) : <EOL> if not ( hasattr ( os , '<STR_LIT>' ) and hasattr ( os , '<STR_LIT>' ) ) : <EOL> return <EOL> try : <EOL> old_umask = os . umask ( <NUM_LIT> <NUM_LIT:2> ) <EOL> f = dumbdbm . open ( _fname , '<STR_LIT:c>' , <NUM_LIT:0> <NUM_LIT> ) <EOL> f . close ( ) <EOL> finally : <EOL> os . umask ( old_umask ) <EOL> expected_mode = <NUM_LIT:0> <NUM_LIT> <EOL> if os . name != '<STR_LIT>' : <EOL> expected_mode = <NUM_LIT:0> <NUM_LIT> <EOL> import stat <EOL> st = os . stat ( _fname + '<STR_LIT>' ) <EOL> self . assertEqual ( stat . S_IMODE ( st . st_mode ) , expected_mode ) <EOL> st = os . stat ( _fname + '<STR_LIT>' ) <EOL> self . assertEqual ( stat . S_IMODE ( st . st_mode ) , expected_mode ) <EOL> def test_close_twice ( self ) : <EOL> f = dumbdbm . open ( _fname ) <EOL> f [ '<STR_LIT:a>' ] = '<STR_LIT:b>' <EOL> self . assertEqual ( f [ '<STR_LIT:a>' ] , '<STR_LIT:b>' ) <EOL> f . close ( ) <EOL> f . close ( ) <EOL> def test_dumbdbm_modification ( self ) : <EOL> self . init_db ( ) <EOL> f = dumbdbm . open ( _fname , '<STR_LIT:w>' ) <EOL> self . _dict [ '<STR_LIT:g>' ] = f [ '<STR_LIT:g>' ] = "<STR_LIT>" <EOL> self . read_helper ( f ) <EOL> f . close ( ) <EOL> def test_dumbdbm_read ( self ) : <EOL> self . init_db ( ) <EOL> f = dumbdbm . open ( _fname , '<STR_LIT:r>' ) <EOL> self . read_helper ( f ) <EOL> f . close ( ) <EOL> def test_dumbdbm_keys ( self ) : <EOL> self . init_db ( ) <EOL> f = dumbdbm . open ( _fname ) <EOL> keys = self . keys_helper ( f ) <EOL> f . close ( ) <EOL> def test_write_write_read ( self ) : <EOL> f = dumbdbm . open ( _fname ) <EOL> f [ '<STR_LIT:1>' ] = '<STR_LIT:hello>' <EOL> f [ '<STR_LIT:1>' ] = '<STR_LIT>' <EOL> f . close ( ) <EOL> f = dumbdbm . open ( _fname ) <EOL> self . assertEqual ( f [ '<STR_LIT:1>' ] , '<STR_LIT>' ) <EOL> f . close ( ) <EOL> def test_line_endings ( self ) : <EOL> f = dumbdbm . open ( _fname ) <EOL> f [ '<STR_LIT:1>' ] = '<STR_LIT:hello>' <EOL> f [ '<STR_LIT:2>' ] = '<STR_LIT>' <EOL> f . close ( ) <EOL> fp = open ( _fname + '<STR_LIT>' , '<STR_LIT>' ) <EOL> data = fp . read ( ) <EOL> data = data . replace ( '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ) <EOL> fp . seek ( <NUM_LIT:0> ) <EOL> fp . truncate ( ) <EOL> fp . write ( data ) <EOL> fp . close ( ) <EOL> f = dumbdbm . open ( _fname ) <EOL> self . assertEqual ( f [ '<STR_LIT:1>' ] , '<STR_LIT:hello>' ) <EOL> self . assertEqual ( f [ '<STR_LIT:2>' ] , '<STR_LIT>' ) <EOL> f . close ( ) <EOL> def read_helper ( self , f ) : <EOL> keys = self . keys_helper ( f ) <EOL> for key in self . _dict : <EOL> self . assertEqual ( self . _dict [ key ] , f [ key ] ) <EOL> def init_db ( self ) : <EOL> f = dumbdbm . open ( _fname , '<STR_LIT:w>' ) <EOL> for k in self . _dict : <EOL> f [ k ] = self . _dict [ k ] <EOL> f . close ( ) <EOL> def keys_helper ( self , f ) : <EOL> keys = f . keys ( ) <EOL> keys . sort ( ) <EOL> dkeys = self . _dict . keys ( ) <EOL> dkeys . sort ( ) <EOL> self . assertEqual ( keys , dkeys ) <EOL> return keys <EOL> def test_random ( self ) : <EOL> import random <EOL> d = { } <EOL> for dummy in range ( <NUM_LIT:5> ) : <EOL> f = dumbdbm . open ( _fname ) <EOL> for dummy in range ( <NUM_LIT:100> ) : <EOL> k = random . choice ( '<STR_LIT>' ) <EOL> if random . random ( ) < <NUM_LIT> : <EOL> if k in d : <EOL> del d [ k ] <EOL> del f [ k ] <EOL> else : <EOL> v = random . choice ( '<STR_LIT:abc>' ) * random . randrange ( <NUM_LIT> ) <EOL> d [ k ] = v <EOL> f [ k ] = v <EOL> self . assertEqual ( f [ k ] , v ) <EOL> f . close ( ) <EOL> f = dumbdbm . open ( _fname ) <EOL> expected = d . items ( ) <EOL> expected . sort ( ) <EOL> got = f . items ( ) <EOL> got . sort ( ) <EOL> self . assertEqual ( expected , got ) <EOL> f . close ( ) <EOL> def tearDown ( self ) : <EOL> _delete_files ( ) <EOL> def setUp ( self ) : <EOL> _delete_files ( ) <EOL> def test_main ( ) : <EOL> try : <EOL> test_support . run_unittest ( DumbDBMTestCase ) <EOL> finally : <EOL> _delete_files ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
