[go: up one dir, main page]

File: test_util.py

package info (click to toggle)
luma.core 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,040 kB
  • sloc: python: 6,186; makefile: 204
file content (61 lines) | stat: -rw-r--r-- 1,442 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-20 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:mod:`luma.core.util` module.
"""


from luma.core import util

from helpers import get_reference_file


test_config_file = get_reference_file('config-test.txt')


def test_mutablestring():
    f = util.mutable_string('bar')
    f[1] = '2'
    assert f == 'b2r'
    assert repr(f) == repr('b2r')
    assert len(f) == 3
    assert hash(f) is not None


def test_mutablestring_unicode():
    f = util.mutable_string(u'bazül')
    f[4] = 'L'
    assert f == u'baz\xfcL'
    assert repr(f) == repr(u'baz\xfcL')
    assert len(f) == 5


def test_from_16_to_8_to_16():
    """
    Test the conversion from 16 bit to 8 bit values and back.  Ensure that conversion
    from 8 to 16 bit takes into account whether the 16 bit value is negative or
    positive
    """
    data = [1, 2, 3, 2**15, 2**16 - 1]
    expect = [0, 1, 0, 2, 0, 3, 0x80, 0, 0xFF, 0xFF]

    result = util.from_16_to_8(data)
    assert result == expect

    expect = [1, 2, 3, -32768, -1]
    result = util.from_8_to_16(result)
    assert result == expect


def test_bytes_to_nibbles():
    """
    Test the conversion from 8 bit values into 4 bit (nibbles)
    """
    data = [0, 1, 2, 3, 0xFF, 0x7F]
    expect = [0, 0, 0, 1, 0, 2, 0, 3, 15, 15, 7, 15]

    result = util.bytes_to_nibbles(data)
    assert result == expect