#!/usr/bin/env python3 # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2020, Kovid Goyal import sys import unittest from . import BaseTest _plat = sys.platform.lower() is_macos = 'darwin' in _plat class TestGLFW(BaseTest): @unittest.skipIf(is_macos, 'Skipping test on macOS because glfw-cocoa.so is not built with backend_utils') def test_utf_8_strndup(self): import ctypes from kitty.constants import glfw_path backend_utils = glfw_path('x11') lib = ctypes.CDLL(backend_utils) libc = ctypes.CDLL(None) class allocated_c_char_p(ctypes.c_char_p): def __del__(self): libc.free(self) utf_8_strndup = lib.utf_8_strndup utf_8_strndup.restype = allocated_c_char_p utf_8_strndup.argtypes = (ctypes.c_char_p, ctypes.c_size_t) def test(string): string_bytes = bytes(string, 'utf-8') prev_part_bytes = b'' prev_length_bytes = -1 for length in range(len(string) + 1): part = string[:length] part_bytes = bytes(part, 'utf-8') length_bytes = len(part_bytes) for length_bytes_2 in range(prev_length_bytes + 1, length_bytes): self.ae(utf_8_strndup(string_bytes, length_bytes_2).value, prev_part_bytes) self.ae(utf_8_strndup(string_bytes, length_bytes).value, part_bytes) prev_part_bytes = part_bytes prev_length_bytes = length_bytes self.ae(utf_8_strndup(string_bytes, len(string_bytes) + 1).value, string_bytes) # Try to go one character after the end of the string self.ae(utf_8_strndup(None, 2).value, None) self.ae(utf_8_strndup(b'', 2).value, b'') test('ö') test('>a<') test('>ä<') test('>ế<') test('>𐍈<') test('∮ E⋅da = Q, n → ∞, 𐍈∑ f(i) = ∏ g(i)')