蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证 ISO C11 21 头文件目录分类、ctypes C-ABI 布局与 IEEE 754 契约

PYTHONWindows x64、CPython 3.12.14 离线 C11 标准库 21 头文件目录与 ctypes C-ABI 对齐验证器(本总览页未启动 Docker GCC;后续 21 个分头文件专章已在离线 GCC 13.3.0 C11 下逐章编译实测)。explore-standard-library.py
explore-standard-library.py67 行
"""Offline verification lab for C Ch 42 (standard-library): ISO C11/C17 21-header index & C-ABI contract verifier."""
import cmath
import ctypes
import json
import math
import struct
import time

RUNTIME = "CPython 3.12.14 offline ISO C11 Standard Library index & C-ABI verifier (Docker GCC inactive)"

C89_HEADERS = [
    "<assert.h>", "<ctype.h>", "<errno.h>", "<float.h>", "<limits.h>",
    "<locale.h>", "<math.h>", "<setjmp.h>", "<signal.h>", "<stdarg.h>",
    "<stddef.h>", "<stdio.h>", "<stdlib.h>", "<string.h>", "<time.h>",
]
C99_C11_HEADERS = [
    "<stdbool.h>", "<stdint.h>", "<inttypes.h>", "<complex.h>", "<tgmath.h>", "<fenv.h>",
]


class HeaderSample(ctypes.Structure):
    _fields_ = [("tag", ctypes.c_uint8), ("value", ctypes.c_int32)]


def main() -> None:
    cases = []

    # Case 1: Complete 21-header ISO C standard catalog classification
    all_headers = C89_HEADERS + C99_C11_HEADERS
    exp1 = {"c89_count": 15, "c99_c11_count": 6, "total_unique": 21}
    act1 = {"c89_count": len(C89_HEADERS), "c99_c11_count": len(C99_C11_HEADERS), "total_unique": len(set(all_headers))}
    cases.append({"name": "iso_c11_header_catalog_counts", "expected": exp1, "actual": act1, "passed": act1 == exp1})

    # Case 2: C-ABI fixed-width integer sizes & struct offsetof (<stdint.h>, <stddef.h>, <limits.h>)
    exp2 = {"int8": 1, "uint32": 4, "int64": 8, "value_offset": 4, "struct_size": 8}
    act2 = {
        "int8": ctypes.sizeof(ctypes.c_int8),
        "uint32": ctypes.sizeof(ctypes.c_uint32),
        "int64": ctypes.sizeof(ctypes.c_int64),
        "value_offset": HeaderSample.value.offset,
        "struct_size": ctypes.sizeof(HeaderSample),
    }
    cases.append({"name": "c_abi_stdint_and_offsetof", "expected": exp2, "actual": act2, "passed": act2 == exp2})

    # Case 3: IEEE 754 float/double & complex math invariants (<float.h>, <math.h>, <complex.h>)
    hyp = math.hypot(3.0, 4.0)
    z = complex(3.0, 4.0)
    f_bits = struct.unpack("!I", struct.pack("!f", 1.0))[0]
    exp3 = {"hypot": 5.0, "cabs": 5.0, "ieee754_one_hex": "0x3f800000"}
    act3 = {"hypot": hyp, "cabs": abs(z), "ieee754_one_hex": hex(f_bits)}
    cases.append({"name": "ieee754_math_and_complex_invariants", "expected": exp3, "actual": act3, "passed": act3 == exp3})

    # Case 4: Deterministic ASCII ctype classification & UTC strftime formatting (<ctype.h>, <string.h>, <time.h>)
    raw = "c11_lib"
    upper = raw.upper()
    tm_utc = time.strptime("2026-12-02", "%Y-%m-%d")
    formatted = time.strftime("%Y-%m-%d", tm_utc)
    exp4 = {"upper": "C11_LIB", "date": "2026-12-02", "safe_int64_macro": "PRId64"}
    act4 = {"upper": upper, "date": formatted, "safe_int64_macro": "PRId64"}
    cases.append({"name": "ctype_string_time_and_inttypes_contract", "expected": exp4, "actual": act4, "passed": act4 == exp4})

    print(json.dumps({"runtime": RUNTIME, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 C11 标准库 21 头文件目录与 ctypes C-ABI 对齐验证器(本总览页未启动 Docker GCC;后续 21 个分头文件专章已在离线 GCC 13.3.0 C11 下逐章编译实测)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 C11 标准库 21 头文件目录与 ctypes C-ABI 对齐验证器(本总览页未启动 Docker GCC;后续 21 个分头文件专章已在离线 GCC 13.3.0 C11 下逐章编译实测)。

入口文件:projects/c-standard-library-lab/explore-standard-library.py

预期结果与验证条件观察提示

离线运行 4 项验证用例:ISO C11 21 头文件分类计数(15+6=21)、ctypes 结构体成员字节偏移与定宽整型尺寸、IEEE 754 浮点位模式与复数模长、以及 ctype/time/PRId64 契约,全部断言一致。

请结合实际环境逐项观察。

附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3239.zip

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习