Esempio n. 1
0
def test_split_into_blocks():
    # <http://www.thonky.com/qr-code-tutorial/error-correction-coding>
    # HELLO WORLD as a 5-Q code
    s = '01000011 01010101 01000110 10000110 01010111 00100110 01010101 ' \
        '11000010 01110111 00110010 00000110 00010010 00000110 01100111 ' \
        '00100110 11110110 11110110 01000010 00000111 01110110 10000110 ' \
        '11110010 00000111 00100110 01010110 00010110 11000110 11000111 ' \
        '10010010 00000110 10110110 11100110 11110111 01110111 00110010 ' \
        '00000111 01110110 10000110 01010111 00100110 01010010 00000110 ' \
        '10000110 10010111 00110010 00000111 01000110 11110111 01110110 ' \
        '01010110 11000010 00000110 10010111 00110010 11100000 11101100 ' \
        '00010001 11101100 00010001 11101100 00010001 11101100'
    data = bits(s)
    buff = Buffer(data)
    codewords = list(buff.toints())
    assert 62 == len(codewords)
    ec_infos = consts.ECC[5][consts.ERROR_LEVEL_Q]
    assert ec_infos
    assert 2 == len(ec_infos)
    blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert 4 == len(blocks)
    assert 15 == len(blocks[0])
    assert bytearray(codewords[:15]) == blocks[0]
    assert 15 == len(blocks[1])
    assert 16 == len(blocks[2])
    assert 16 == len(blocks[3])
Esempio n. 2
0
def test_make_error_block1():
    # <http://www.thonky.com/qr-code-tutorial/structure-final-message>
    # 5-Q
    codeword = '01000011 01010101 01000110 10000110 01010111 00100110 01010101 ' \
               '11000010 01110111 00110010 00000110 00010010 00000110 01100111 ' \
               '00100110'
    codeword_ints = [67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([213, 199, 11, 45, 115, 247, 241, 223, 229, 248, 154, 117, 154, 111, 86, 161, 111, 39])
    ec_infos = consts.ECC[5][consts.ERROR_LEVEL_Q]
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert error_block == error_blocks[0]
Esempio n. 3
0
def test_make_error_block4():
    # <http://www.thonky.com/qr-code-tutorial/structure-final-message>
    # 5-Q
    codeword = '01000110 11110111 01110110 01010110 11000010 00000110 10010111 ' \
               '00110010 00010000 11101100 00010001 11101100 00010001 11101100 ' \
               '00010001 11101100'
    codeword_ints = [70, 247, 118, 86, 194, 6, 151, 50, 16, 236, 17, 236, 17,
                     236, 17, 236]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([235, 159, 5, 173, 24, 147, 59, 33, 106, 40, 255,
                             172, 82, 2, 131, 32, 178, 236])
    ec_infos = (consts.ECC[5][consts.ERROR_LEVEL_Q][1],)
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert error_block == error_blocks[0]
Esempio n. 4
0
def test_make_error_block3():
    # <http://www.thonky.com/qr-code-tutorial/structure-final-message>
    # 5-Q
    codeword = '10110110 11100110 11110111 01110111 00110010 00000111 01110110 ' \
               '10000110 01010111 00100110 01010010 00000110 10000110 10010111 ' \
               '00110010 00000111'
    codeword_ints = [182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134,
                     151, 50, 7]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([148, 116, 177, 212, 76, 133, 75, 242, 238, 76,
                             195, 230, 189, 10, 108, 240, 192, 141])
    ec_infos = (consts.ECC[5][consts.ERROR_LEVEL_Q][1],)
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert error_block == error_blocks[0]
Esempio n. 5
0
def test_make_error_block2():
    # <http://www.thonky.com/qr-code-tutorial/structure-final-message>
    # 5-Q
    codeword = '11110110 11110110 01000010 00000111 01110110 10000110 11110010 ' \
               '00000111 00100110 01010110 00010110 11000110 11000111 10010010 ' \
               '00000110'
    codeword_ints = [246, 246, 66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199,
                     146, 6]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([87, 204, 96, 60, 202, 182, 124, 157, 200, 134, 27,
                             129, 209, 17, 163, 163, 120, 133])
    ec_infos = consts.ECC[5][consts.ERROR_LEVEL_Q]
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert error_block == error_blocks[0]
Esempio n. 6
0
def test_make_error_block0():
    # <http://www.thonky.com/qr-code-tutorial/error-correction-coding>
    # 1-M
    codeword = '00100000 01011011 00001011 01111000 11010001 01110010 11011100 ' \
               '01001101 01000011 01000000 11101100 00010001 11101100 00010001 ' \
               '11101100 00010001'
    codeword_ints = [32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([196, 35, 39, 119, 235, 215, 231, 226, 93, 23])
    ec_infos = consts.ECC[1][consts.ERROR_LEVEL_M]
    assert 1 == len(ec_infos)
    ec_info = ec_infos[0]
    assert 1 == ec_info.num_blocks
    assert 10 == ec_info.num_total - ec_info.num_data
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert len(error_block) == len(error_blocks[0])
    assert error_block == error_blocks[0]
Esempio n. 7
0
def test_make_final_message_thonky():
    # <http://www.thonky.com/qr-code-tutorial/structure-final-message>
    # 5-Q
    codewords = '01000011 01010101 01000110 10000110 01010111 00100110 01010101 11000010 01110111 00110010 00000110 00010010 00000110 01100111 00100110' \
                '11110110 11110110 01000010 00000111 01110110 10000110 11110010 00000111 00100110 01010110 00010110 11000110 11000111 10010010 00000110' \
                '10110110 11100110 11110111 01110111 00110010 00000111 01110110 10000110 01010111 00100110 01010010 00000110 10000110 10010111 00110010 00000111' \
                '01000110 11110111 01110110 01010110 11000010 00000110 10010111 00110010 00010000 11101100 00010001 11101100 00010001 11101100 00010001 11101100'
    codewords_int = [
        67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38, 246, 246,
        66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199, 146, 6, 182, 230, 247,
        119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7, 70, 247, 118, 86,
        194, 6, 151, 50, 16, 236, 17, 236, 17, 236, 17, 236
    ]
    s = '01000011111101101011011001000110010101011111011011100110111101110100011001000010111101110111011010000110000001110111011101010110010101110111011000110010110000100010011010000110000001110000011001010101111100100111011010010111110000100000011110000110001100100111011100100110010101110001000000110010010101100010011011101100000001100001011001010010000100010001001011000110000001101110110000000110110001111000011000010001011001111001001010010111111011000010011000000110001100100001000100000111111011001101010101010111100101001110101111000111110011000111010010011111000010110110000010110001000001010010110100111100110101001010110101110011110010100100110000011000111101111011011010000101100100111111000101111100010010110011101111011111100111011111001000100001111001011100100011101110011010101111100010000110010011000010100010011010000110111100001111111111011101011000000111100110101011001001101011010001101111010101001001101111000100010000101000000010010101101010001101101100100000111010000110100011111100000010000001101111011110001100000010110010001001111000010110001101111011000000000'
    expected = bits(s)
    buff = Buffer(bits(codewords))
    assert codewords_int == list(buff.toints())
    res = encoder.make_final_message(5, consts.ERROR_LEVEL_Q, buff)
    assert len(expected) == len(res)
    assert expected == res.getbits()