<commit_before>from tests.base import BaseTopazTest
from topaz.modules.ffi.buffer import W_BufferObject
from rpython.rtyper.lltypesystem.rffi import sizeof, LONG, ULONG

class TestBuffer(BaseTopazTest):

    sizes = {'char': 1,
                   'uchar': 1,
                   'short': 2,
                   'ushort': 2,
                   'int': 4,
                   'uint': 4,
                   'long': sizeof(LONG),
                   'ulong': sizeof(ULONG),
                   'long_long': 8,
                   'ulong_long': 8,
                   'float': 4,
                   'double': 8,
                   'pointer': 8}

    def test_total(self, space):
        for key in TestBuffer.sizes:
            w_res = space.execute("""
            buffer = FFI::Buffer.new(:%s, 3)
            buffer.total
            """ % key)
            expected = TestBuffer.sizes[key]*3
            assert self.unwrap(space, w_res) == expected

    def test_non_valid_init_symbol(self, space):
        with self.raises(space, 'ArgumentError',
                         "I don't know the megaint type."):
            space.execute("FFI::Buffer.new(:megaint, 1)")

    def test_instantiations(self, space):
        generic_init = "FFI::Buffer.%s(:int, 5)"
        total_should = TestBuffer.sizes['int']*5
        for init_method in ['new',
                            'new_inout',
                            'new_in',
                            'new_out',
                            'alloc_inout',
                            'alloc_in',
                            'alloc_out']:
            w_buffer = space.execute(generic_init % init_method)
            w_buffer_total = space.send(w_buffer, 'total')
            assert total_should == self.unwrap(space, w_buffer_total)

    def test_default_size_is_1(self, space):
        w_res = space.execute("FFI::Buffer.alloc_in(7).total")
        assert self.unwrap(space, w_res) == 7

    def test_default_length_is_1(self, space):
        w_res = space.execute("FFI::Buffer.alloc_in(:short).total")
        assert self.unwrap(space, w_res) == TestBuffer.sizes['short']

    def test_init_with_block(self, space):
        w_res = space.execute("""
        x = 0
        FFI::Buffer.new(:char, 5) { |len| x = len}
        x
        """)
        assert self.unwrap(space, w_res) == 5
        w_res = space.execute("""
        x = 0
        FFI::Buffer.new(3) { |len| x = len }
        x
        """)
        assert self.unwrap(space, w_res) == 3

    def test_puts_return_self(self, space):
        for put in ['put_char',
                    'put_uchar',
                    'put_ushort',
                    'put_uint',<commit_msg>Rewrote test_instantiations<commit_after>from tests.base import BaseTopazTest
from topaz.modules.ffi.buffer import W_BufferObject
from rpython.rtyper.lltypesystem.rffi import sizeof, LONG, ULONG

class TestBuffer(BaseTopazTest):

    sizes = {'char': 1,
                   'uchar': 1,
                   'short': 2,
                   'ushort': 2,
                   'int': 4,
                   'uint': 4,
                   'long': sizeof(LONG),
                   'ulong': sizeof(ULONG),
                   'long_long': 8,
                   'ulong_long': 8,
                   'float': 4,
                   'double': 8,
                   'pointer': 8}

    def test_total(self, space):
        for key in TestBuffer.sizes:
            w_res = space.execute("""
            buffer = FFI::Buffer.new(:%s, 3)
            buffer.total
            """ % key)
            expected = TestBuffer.sizes[key]*3
            assert self.unwrap(space, w_res) == expected

    def test_non_valid_init_symbol(self, space):
        with self.raises(space, 'ArgumentError',
                         "I don't know the megaint type."):
            space.execute("FFI::Buffer.new(:megaint, 1)")

    def test_instantiations(self, space):
        for init_method in ['new_inout',
                            'new_in',
                            'new_out',
                            'alloc_inout',
                            'alloc_in',
                            'alloc_out']:
            w_res = space.execute("""
            FFI::Buffer.method(:new) == FFI::Buffer.method(:%s)
            """ % init_method)
            assert self.unwrap(space, w_res)

    def test_default_size_is_1(self, space):
        w_res = space.execute("FFI::Buffer.alloc_in(7).total")
        assert self.unwrap(space, w_res) == 7

    def test_default_length_is_1(self, space):
        w_res = space.execute("FFI::Buffer.alloc_in(:short).total")
        assert self.unwrap(space, w_res) == TestBuffer.sizes['short']

    def test_init_with_block(self, space):
        w_res = space.execute("""
        x = 0
        FFI::Buffer.new(:char, 5) { |len| x = len}
        x
        """)
        assert self.unwrap(space, w_res) == 5
        w_res = space.execute("""
        x = 0
        FFI::Buffer.new(3) { |len| x = len }
        x
        """)
        assert self.unwrap(space, w_res) == 3

    def test_puts_return_self(self, space):
        for put in ['put_char',
                    'put_uchar',
                    'put_ushort',
                    'put_uint',
{'fraction_non_alphanumeric': 0.12468780019212296, 'fraction_numerical': 0.009606147934678195, 'mean_word_length': 1.535801266439357, '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': '2954883', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1543, 'n_words': 318}