#!/usr/bin/env python3 """MIT. Independent byte-identity checks; Python standard library only. Download cases-v1.json and results-v1.json beside this script, then run it. """ import json from pathlib import Path root = Path(__file__).resolve().parent cases = json.loads((root / 'cases-v1.json').read_text())['cases'] results = {r['id']: r for r in json.loads((root / 'results-v1.json').read_text())['results']} def codepoints(s): return [f'U+{ord(c):04X}' for c in s] def hexbytes(b): return ' '.join(f'{v:02X}' for v in b) changed = [] for case in cases: raw = bytes(case['bytes']) if case['kind'] == 'text': assert case['text'].encode('utf-8') == raw, case['id'] try: raw.decode('utf-8', errors='strict') valid = True # TextDecoder's default BOM treatment removes one initial UTF-8 BOM. decoded = raw.decode('utf-8-sig', errors='strict') except UnicodeDecodeError: valid = False # Site-specific whole-input fallback, not Windows-1252. decoded = raw.decode('latin-1') row = results[case['id']] encoded = decoded.encode('utf-8') assert row['strictUtf8'] == valid, case['id'] assert row['inputHex'] == hexbytes(raw), case['id'] assert row['decodedText'] == decoded, case['id'] assert row['decodedCodePoints'] == codepoints(decoded), case['id'] assert row['reencodedHex'] == hexbytes(encoded), case['id'] assert row['byteIdentityPreserved'] == (raw == encoded), case['id'] assert ('latin1-fallback' in row['notices']) == (not valid), case['id'] if case['kind'] == 'text': assert row['sevenBitAccepted'] == case['text'].isascii(), case['id'] if raw != encoded: changed.append(case['id']) assert len(results) == len(cases) == 36 print(json.dumps({'passed': len(cases), 'changed_byte_identity': len(changed), 'changed_cases': changed})) # SPDX-License-Identifier: MIT # Copyright (c) 2026 Binary Translator # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE.