summaryrefslogtreecommitdiff
path: root/lib9p_util/nut_gen/test_nut.py
blob: 00a07be560059843bfccf5aa886711936705ee7f (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
# lib9p_util/nut_gen/test_nut.py - Tests for NUT file generation
# Keep this file in-sync with test_nut.c.
#
# Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

# pylint: disable=unused-variable

import hashlib

if __name__ == "__main__":
    import os.path
    import sys

    sys.path.append(os.path.normpath(os.path.join(__file__, "..", "..")))
    from nut_gen import app, nut
else:
    from . import app, nut


def test_crc32() -> None:
    assert nut.crc32_table(1) == [0, 0x04C11DB7]
    assert nut.crc32_table(4) == [
        0x00000000,
        0x04C11DB7,
        0x09823B6E,
        0x0D4326D9,
        0x130476DC,
        0x17C56B6B,
        0x1A864DB2,
        0x1E475005,
        0x2608EDB8,
        0x22C9F00F,
        0x2F8AD6D6,
        0x2B4BCB61,
        0x350C9B64,
        0x31CD86D3,
        0x3C8EA00A,
        0x384FBDBD,
    ]
    assert nut.crc32(b"hello", table_width=1) == 0xA1DE215E
    assert nut.crc32(b"hello", table_width=2) == 0xA1DE215E
    assert nut.crc32(b"hello", table_width=4) == 0xA1DE215E
    assert nut.crc32(b"hello", table_width=8) == 0xA1DE215E


font = [
    [
        "   ##   ",
        "  #  #  ",
        "  #  #  ",
        "   ##   ",
    ],
    [
        "    #   ",
        "   ##   ",
        "    #   ",
        "   ###  ",
    ],
    [
        "   ##   ",
        "  #  #  ",
        "    #   ",
        "  ####  ",
    ],
    [
        "   ##   ",
        "     #  ",
        "     #  ",
        "   ##   ",
    ],
    [
        "    ##  ",
        "   # #  ",
        "  ####  ",
        "     #  ",
    ],
    [
        "  ####  ",
        "  ###   ",
        "     #  ",
        "  ###   ",
    ],
    [
        "  #     ",
        "  ###   ",
        "  #  #  ",
        "   ##   ",
    ],
    [
        "  ####  ",
        "    #   ",
        "   #    ",
        "   #    ",
    ],
    [
        "   ##   ",
        "  #  #  ",
        "  ####  ",
        "   ##   ",
    ],
    [
        "   ##   ",
        "  #  #  ",
        "   ###  ",
        "     #  ",
    ],
]
font_w = 8
font_h = 4


def test_font() -> None:
    for char in font:
        assert len(char) == font_h
        for line in char:
            assert len(line) == font_w


def memset(mem: bytearray, val: int) -> None:
    for i, _ in enumerate(mem):
        mem[i] = val


def generate() -> bytes:
    buf = bytearray()

    app.append_intro(buf)

    state = app.State()
    framebuffer = bytearray(app.fb_size)

    SCALE = 10
    for i in range(10):
        memset(framebuffer, 0b00011000)
        for y in range(font_h * SCALE):
            for x in range(font_w * SCALE):
                char = font[i][y // SCALE][x // SCALE]
                color = 0b11000000 if char == " " else 0b00000011
                framebuffer[(y * app.fb_w) + x] = color
        state.append_frame_intro(buf, i * 1000000000)
        buf.extend(framebuffer)

    return bytes(buf)


def test_full_file() -> None:
    content = generate()
    csum = hashlib.sha256()
    csum.update(content)
    assert (
        csum.hexdigest()
        == "4927934a8d51a40c109caa9a9cfdaa683ce24292e4f27f861500a2eb1a718a4b"
    )


if __name__ == "__main__":
    sys.stdout.buffer.write(generate())