#!/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 # 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())