#!/usr/bin/env python
#!/usr/local/pypy-1.3/bin/pypy

'''Unit tests for gdbm_ctypes module'''

from __future__ import with_statement

import sys

import gdbm_ctypes

def main():
    '''Main function - does it all'''
    # this doesn't need to free - we're creating a ctypes buffer behind the scenes, which is garbage collected
    for string in [ 'abc', 'def', 'ghi' ]:
        sys.stderr.write('testing %s\n' % string)
        datum = gdbm_ctypes.datum(string)
        sys.stderr.write('asserting\n')
        if str(datum) == string:
            pass
        else:
            sys.stderr.write("%s != %s\n" % (str(datum), string))
            sys.exit(1)

    # test nesting too, just for fun
    # this also doesn't need to free - we're creating ctypes buffers behind the scenes, which are garbage collected
    jkl = gdbm_ctypes.datum('jkl')
    mno = gdbm_ctypes.datum('mno')
    assert str(jkl) == 'jkl'
    assert str(mno) == 'mno'

    with gdbm_ctypes.datum() as empty:
        print 'empty.dptr is', empty.dptr
        print dir(empty.dptr)
        assert not empty.dptr


        

main()