<commit_before>from ..base import BaseTopazTest
from topaz.modules.ffi import FFI
from topaz.objects.hashobject import W_HashObject
from topaz.objects.classobject import W_ClassObject
from topaz.objects.moduleobject import W_ModuleObject

class TestFFI(BaseTopazTest):

    primitive_types =  ['INT8', 'UINT8', 'INT16', 'UINT16',
                        'INT32', 'UINT32', 'INT64', 'UINT64',
                        'LONG', 'ULONG', 'FLOAT32', 'FLOAT64',
                        'VOID', 'LONGDOUBLE', 'POINTER', 'BOOL']
    alias_types = ['SCHAR', 'CHAR', 'UCHAR',
                   'SHORT', 'SSHORT', 'USHORT',
                   'INT', 'SINT', 'UINT',
                   'LONG_LONG', 'SLONG', 'SLONG_LONG', 'ULONG_LONG',
                   'FLOAT', 'DOUBLE', 'STRING',
                   'BUFFER_IN', 'BUFFER_OUT', 'BUFFER_INOUT', 'VARARGS']

    def test_basic(self, space):
        w_type_defs = space.execute('FFI::TypeDefs')
        assert isinstance(w_type_defs, W_HashObject)
        w_types = space.execute('FFI::Types')
        assert isinstance(w_types, W_HashObject)

    def test_FFI_type_constants(self, space):
        # just check, whether the constants even exist for now
        for pt in TestFFI.primitive_types:
            space.execute('FFI::TYPE_%s' % pt)

    def test_Type(self, space):
        w_type = space.execute('FFI::Type')
        assert isinstance(w_type, W_ClassObject)
        for pt in TestFFI.primitive_types:
            space.execute('FFI::Type::%s' %pt)
        for at in TestFFI.alias_types:
            space.execute('FFI::Type::%s' %at)
        w_mapped = space.execute('FFI::Type::Mapped')
        assert isinstance(w_mapped, W_ClassObject)
        w_res = space.execute('FFI::Type::Mapped.respond_to? :method_missing')
        assert self.unwrap(space, w_res)
        w_res = space.execute('FFI::Type::Mapped.new(42)')

    def test_DataConverter(self, space):
        w_dc = space.execute('FFI::DataConverter')
        assert isinstance(w_dc, W_ModuleObject)
        w_res = space.execute('FFI::DataConverter.native_type(0)')
        assert w_res == space.w_nil
        w_res = space.execute('FFI::DataConverter.to_native')
        assert w_res == space.w_nil
        w_res = space.execute('FFI::DataConverter.from_native')
        assert w_res == space.w_nil

    def test_DynamicLibrary(self, space):
        consts = {'LAZY':1 , 'NOW':2, 'GLOBAL':257, 'LOCAL':0}
        for name in consts:
            w_res = space.execute('FFI::DynamicLibrary::RTLD_%s' % name)
            space.int_w(w_res) == consts[name]
        w_res = space.execute("FFI::DynamicLibrary.open('something', 1)")
        assert w_res == space.w_nil
        w_res = space.execute("FFI::DynamicLibrary.open(nil, 2)")
        assert w_res == space.w_nil
        with self.raises(space, "TypeError", "can't convert Float into String"):
            space.execute("FFI::DynamicLibrary.open(3.142, 1)")
        # The next error message is different from the one in ruby 1.9.3.
        # But the meaning is the same.
        with self.raises(space, "TypeError", "can't convert String into Integer"):
            space.execute("FFI::DynamicLibrary.open('something', 'invalid flag')")

    def test_Pointer(self, space):<commit_msg>Split DynamicLibrary test into 2 tests<commit_after>from ..base import BaseTopazTest
from topaz.modules.ffi import FFI
from topaz.objects.hashobject import W_HashObject
from topaz.objects.classobject import W_ClassObject
from topaz.objects.moduleobject import W_ModuleObject

class TestFFI(BaseTopazTest):

    primitive_types =  ['INT8', 'UINT8', 'INT16', 'UINT16',
                        'INT32', 'UINT32', 'INT64', 'UINT64',
                        'LONG', 'ULONG', 'FLOAT32', 'FLOAT64',
                        'VOID', 'LONGDOUBLE', 'POINTER', 'BOOL']
    alias_types = ['SCHAR', 'CHAR', 'UCHAR',
                   'SHORT', 'SSHORT', 'USHORT',
                   'INT', 'SINT', 'UINT',
                   'LONG_LONG', 'SLONG', 'SLONG_LONG', 'ULONG_LONG',
                   'FLOAT', 'DOUBLE', 'STRING',
                   'BUFFER_IN', 'BUFFER_OUT', 'BUFFER_INOUT', 'VARARGS']

    def test_basic(self, space):
        w_type_defs = space.execute('FFI::TypeDefs')
        assert isinstance(w_type_defs, W_HashObject)
        w_types = space.execute('FFI::Types')
        assert isinstance(w_types, W_HashObject)

    def test_FFI_type_constants(self, space):
        # just check, whether the constants even exist for now
        for pt in TestFFI.primitive_types:
            space.execute('FFI::TYPE_%s' % pt)

    def test_Type(self, space):
        w_type = space.execute('FFI::Type')
        assert isinstance(w_type, W_ClassObject)
        for pt in TestFFI.primitive_types:
            space.execute('FFI::Type::%s' %pt)
        for at in TestFFI.alias_types:
            space.execute('FFI::Type::%s' %at)
        w_mapped = space.execute('FFI::Type::Mapped')
        assert isinstance(w_mapped, W_ClassObject)
        w_res = space.execute('FFI::Type::Mapped.respond_to? :method_missing')
        assert self.unwrap(space, w_res)
        w_res = space.execute('FFI::Type::Mapped.new(42)')

    def test_DataConverter(self, space):
        w_dc = space.execute('FFI::DataConverter')
        assert isinstance(w_dc, W_ModuleObject)
        w_res = space.execute('FFI::DataConverter.native_type(0)')
        assert w_res == space.w_nil
        w_res = space.execute('FFI::DataConverter.to_native')
        assert w_res == space.w_nil
        w_res = space.execute('FFI::DataConverter.from_native')
        assert w_res == space.w_nil

    def test_DynamicLibrary_consts(self, space):
        consts = {'LAZY':1 , 'NOW':2, 'GLOBAL':257, 'LOCAL':0}
        for name in consts:
            w_res = space.execute('FFI::DynamicLibrary::RTLD_%s' % name)
            space.int_w(w_res) == consts[name]

    def test_DynamicLibrary_open(self, space):
        w_res = space.execute("FFI::DynamicLibrary.open('something', 1)")
        assert w_res == space.w_nil
        w_res = space.execute("FFI::DynamicLibrary.open(nil, 2)")
        assert w_res == space.w_nil
        with self.raises(space, "TypeError", "can't convert Float into String"):
            space.execute("FFI::DynamicLibrary.open(3.142, 1)")
        # The next error message is different from the one in ruby 1.9.3.
        # But the meaning is the same.
        with self.raises(space, "TypeError", "can't convert String into Integer"):
            space.execute("FFI::DynamicLibrary.open('something', 'invalid flag')")

    def test_Pointer(self, space):
{'fraction_non_alphanumeric': 0.13485540334855403, 'fraction_numerical': 0.01141552511415525, 'mean_word_length': 2.8114849187935036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1328928', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2075, 'n_words': 443}