import pytest

from armulator.armv6.bits_ops import *


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 1),
    (0b11, 2, -1),
    (0b1110, 4, -2),
])
def test_to_signed(bits, length, result):
    assert to_signed(bits, length) == result


@pytest.mark.parametrize('bits, msb, lsb, result', [
    (0b0110, 2, 0, 0b110),
    (0b1110010101, 8, 5, 0b1100),
])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 0),
    (0b11, 2, 0),
    (0b1110, 4, 1),
    (0b00111000, 8, 3),
])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


@pytest.mark.parametrize('bits, src_length, dst_length, result', [
    (0b00, 2, 4, 0b0000),
    (0b01, 2, 4, 0b0001),
    (0b10, 2, 4, 0b1110),
    (0b11, 2, 4, 0b1111),

])
def test_sign_extend(bits, src_length, dst_length, result):
    assert sign_extend(bits, src_length, dst_length) == result


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow', [
    (0x00000001, 0x00000001, 0, 32, 0x00000002, 0, 0),
    (0x00000001, 0xfffffffe, 1, 32, 0x00000000, 1, 0),
])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out, overflow)


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 0b10),
    (0b11, 2, 0b00),
    (0b1110, 4, 0b0001),
    (0x00006600, 32, 0xFFFF99FF),
])
def test_bit_not(bits, length, result):
    assert bit_not(bits, length) == result


@pytest.mark.parametrize('bits, length, result', [
    (0xAA, 1, 0xAA),
    (0xAABB, 2, 0xBBAA),
    (0x11223344, 4, 0x44332211),
    (0x44000000, 4, 0x00000044),
    (0x1122334455667788, 8, 0x8877665544332211),
])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result

53089dea93586ee18299701e36a58eb454eca88c
<mask token>


@pytest.mark.parametrize('bits, msb, lsb, result', [(6, 2, 0, 6), (917, 8, 
    5, 12)])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [(1, 2, 0), (3, 2, 0), (14,
    4, 1), (56, 8, 3)])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


<mask token>


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow',
    [(1, 1, 0, 32, 2, 0, 0), (1, 4294967294, 1, 32, 0, 1, 0)])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out,
        overflow)


<mask token>


@pytest.mark.parametrize('bits, length, result', [(170, 1, 170), (43707, 2,
    48042), (287454020, 4, 1144201745), (1140850688, 4, 68), (
    1234605616436508552, 8, 9833440827789222417)])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result

<mask token>


@pytest.mark.parametrize('bits, length, result', [(1, 2, 1), (3, 2, -1), (
    14, 4, -2)])
def test_to_signed(bits, length, result):
    assert to_signed(bits, length) == result


@pytest.mark.parametrize('bits, msb, lsb, result', [(6, 2, 0, 6), (917, 8, 
    5, 12)])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [(1, 2, 0), (3, 2, 0), (14,
    4, 1), (56, 8, 3)])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


<mask token>


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow',
    [(1, 1, 0, 32, 2, 0, 0), (1, 4294967294, 1, 32, 0, 1, 0)])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out,
        overflow)


@pytest.mark.parametrize('bits, length, result', [(1, 2, 2), (3, 2, 0), (14,
    4, 1), (26112, 32, 4294941183)])
def test_bit_not(bits, length, result):
    assert bit_not(bits, length) == result


@pytest.mark.parametrize('bits, length, result', [(170, 1, 170), (43707, 2,
    48042), (287454020, 4, 1144201745), (1140850688, 4, 68), (
    1234605616436508552, 8, 9833440827789222417)])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result

<mask token>


@pytest.mark.parametrize('bits, length, result', [(1, 2, 1), (3, 2, -1), (
    14, 4, -2)])
def test_to_signed(bits, length, result):
    assert to_signed(bits, length) == result


@pytest.mark.parametrize('bits, msb, lsb, result', [(6, 2, 0, 6), (917, 8, 
    5, 12)])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [(1, 2, 0), (3, 2, 0), (14,
    4, 1), (56, 8, 3)])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


@pytest.mark.parametrize('bits, src_length, dst_length, result', [(0, 2, 4,
    0), (1, 2, 4, 1), (2, 2, 4, 14), (3, 2, 4, 15)])
def test_sign_extend(bits, src_length, dst_length, result):
    assert sign_extend(bits, src_length, dst_length) == result


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow',
    [(1, 1, 0, 32, 2, 0, 0), (1, 4294967294, 1, 32, 0, 1, 0)])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out,
        overflow)


@pytest.mark.parametrize('bits, length, result', [(1, 2, 2), (3, 2, 0), (14,
    4, 1), (26112, 32, 4294941183)])
def test_bit_not(bits, length, result):
    assert bit_not(bits, length) == result


@pytest.mark.parametrize('bits, length, result', [(170, 1, 170), (43707, 2,
    48042), (287454020, 4, 1144201745), (1140850688, 4, 68), (
    1234605616436508552, 8, 9833440827789222417)])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result

import pytest
from armulator.armv6.bits_ops import *


@pytest.mark.parametrize('bits, length, result', [(1, 2, 1), (3, 2, -1), (
    14, 4, -2)])
def test_to_signed(bits, length, result):
    assert to_signed(bits, length) == result


@pytest.mark.parametrize('bits, msb, lsb, result', [(6, 2, 0, 6), (917, 8, 
    5, 12)])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [(1, 2, 0), (3, 2, 0), (14,
    4, 1), (56, 8, 3)])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


@pytest.mark.parametrize('bits, src_length, dst_length, result', [(0, 2, 4,
    0), (1, 2, 4, 1), (2, 2, 4, 14), (3, 2, 4, 15)])
def test_sign_extend(bits, src_length, dst_length, result):
    assert sign_extend(bits, src_length, dst_length) == result


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow',
    [(1, 1, 0, 32, 2, 0, 0), (1, 4294967294, 1, 32, 0, 1, 0)])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out,
        overflow)


@pytest.mark.parametrize('bits, length, result', [(1, 2, 2), (3, 2, 0), (14,
    4, 1), (26112, 32, 4294941183)])
def test_bit_not(bits, length, result):
    assert bit_not(bits, length) == result


@pytest.mark.parametrize('bits, length, result', [(170, 1, 170), (43707, 2,
    48042), (287454020, 4, 1144201745), (1140850688, 4, 68), (
    1234605616436508552, 8, 9833440827789222417)])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result

import pytest

from armulator.armv6.bits_ops import *


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 1),
    (0b11, 2, -1),
    (0b1110, 4, -2),
])
def test_to_signed(bits, length, result):
    assert to_signed(bits, length) == result


@pytest.mark.parametrize('bits, msb, lsb, result', [
    (0b0110, 2, 0, 0b110),
    (0b1110010101, 8, 5, 0b1100),
])
def test_substring(bits, msb, lsb, result):
    assert substring(bits, msb, lsb) == result


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 0),
    (0b11, 2, 0),
    (0b1110, 4, 1),
    (0b00111000, 8, 3),
])
def test_lowest_set_bit_ref(bits, length, result):
    assert lowest_set_bit_ref(bits, length) == result


@pytest.mark.parametrize('bits, src_length, dst_length, result', [
    (0b00, 2, 4, 0b0000),
    (0b01, 2, 4, 0b0001),
    (0b10, 2, 4, 0b1110),
    (0b11, 2, 4, 0b1111),

])
def test_sign_extend(bits, src_length, dst_length, result):
    assert sign_extend(bits, src_length, dst_length) == result


@pytest.mark.parametrize('x, y, carry_in, x_size, result, carry_out, overflow', [
    (0x00000001, 0x00000001, 0, 32, 0x00000002, 0, 0),
    (0x00000001, 0xfffffffe, 1, 32, 0x00000000, 1, 0),
])
def test_add_with_carry(x, y, carry_in, x_size, result, carry_out, overflow):
    assert add_with_carry(x, y, carry_in, x_size) == (result, carry_out, overflow)


@pytest.mark.parametrize('bits, length, result', [
    (0b01, 2, 0b10),
    (0b11, 2, 0b00),
    (0b1110, 4, 0b0001),
    (0x00006600, 32, 0xFFFF99FF),
])
def test_bit_not(bits, length, result):
    assert bit_not(bits, length) == result


@pytest.mark.parametrize('bits, length, result', [
    (0xAA, 1, 0xAA),
    (0xAABB, 2, 0xBBAA),
    (0x11223344, 4, 0x44332211),
    (0x44000000, 4, 0x00000044),
    (0x1122334455667788, 8, 0x8877665544332211),
])
def test_big_endian_reverse(bits, length, result):
    assert big_endian_reverse(bits, length) == result
