# Path: aiokafka/record/util.py
# def encode_varint_py(value, write):
# def size_of_varint_py(value):
# def decode_varint_py(buffer, pos=0):
# def calc_crc32c_py(memview):


import struct
import pytest
from aiokafka.record import util
def test_encode_varint(encoded, decoded):
    res = bytearray()
    util.encode_varint(decoded, res.append)
    assert res == encoded


@pytest.mark.parametrize("encoded, decoded", varint_data)
def test_decode_varint(encoded, decoded):
    # We add a bit of bytes around just to check position is calculated
    # correctly
    value, pos = util.decode_varint(
        bytearray(b"\x01\xf0" + encoded + b"\xff\x01"), 2)
    assert value == decoded
    assert pos - 2 == len(encoded)


@pytest.mark.parametrize("encoded, decoded", varint_data)
def test_size_of_varint(encoded, decoded):
    assert util.size_of_varint(decoded) == len(encoded)


def test_crc32c():
    def make_crc(data):
        crc = util.calc_crc32c(data)
        return struct.pack(">I", crc)
    assert make_crc(b"") == b"\x00\x00\x00\x00"
    assert make_crc(b"a") == b"\xc1\xd0\x43\x30"

    # Took from librdkafka testcase
    long_text = b"""\

# Path: aiokafka/record/util.py
# def encode_varint_py(value, write):
# def size_of_varint_py(value):
# def decode_varint_py(buffer, pos=0):
# def calc_crc32c_py(memview):


# Path: tests/record/test_util.py
import struct
import pytest
from aiokafka.record import util

def test_encode_varint(encoded, decoded):
    res = bytearray()
    util.encode_varint(decoded, res.append)
    assert res == encoded


@pytest.mark.parametrize("encoded, decoded", varint_data)
def test_decode_varint(encoded, decoded):
    # We add a bit of bytes around just to check position is calculated
    # correctly
    value, pos = util.decode_varint(
        bytearray(b"\x01\xf0" + encoded + b"\xff\x01"), 2)
    assert value == decoded
    assert pos - 2 == len(encoded)


@pytest.mark.parametrize("encoded, decoded", varint_data)
def test_size_of_varint(encoded, decoded):
    assert util.size_of_varint(decoded) == len(encoded)


def test_crc32c():
    def make_crc(data):
        crc = util.calc_crc32c(data)
        return struct.pack(">I", crc)
    assert make_crc(b"") == b"\x00\x00\x00\x00"
    assert make_crc(b"a") == b"\xc1\xd0\x43\x30"

    # Took from librdkafka testcase
    long_text = b"""\

  This software is provided 'as-is', without any express or implied