#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: Exact-width integer sizes and boundary values
std::int8_t a = -128;
std::uint8_t b = 255;
std::int16_t c = -32768;
std::uint16_t d = 65535;
std::int32_t e = -2147483647 - 1;
std::uint32_t f = 4294967295U;
std::int64_t g = -9223372036854775807LL;
std::uint64_t h = 18446744073709551615ULL;
bool c1 =
sizeof(a) == 1 && sizeof(b) == 1 &&
sizeof(c) == 2 && sizeof(d) == 2 &&
sizeof(e) == 4 && sizeof(f) == 4 &&
sizeof(g) == 8 && sizeof(h) == 8 &&
static_cast<int>(a) == -128 &&
static_cast<unsigned int>(b) == 255U &&
c == -32768 && d == 65535 &&
e == INT32_MIN && f == UINT32_MAX &&
g == -9223372036854775807LL && h == UINT64_MAX;
cases.push_back({"exact_width_sizes_and_values", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: Least-width and fast-width integer guarantees
std::int_least8_t l8 = -128;
std::uint_least8_t ul8 = 255;
std::int_least32_t l32 = 2147483647;
std::uint_least64_t ul64 = 18446744073709551615ULL;
std::int_fast8_t f8 = -128;
std::uint_fast8_t uf8 = 255;
std::int_fast32_t f32 = 2147483647;
std::uint_fast64_t uf64 = 18446744073709551615ULL;
bool c2 =
sizeof(std::int_least8_t) >= 1 && sizeof(std::int_least16_t) >= 2 &&
sizeof(std::int_least32_t) >= 4 && sizeof(std::int_least64_t) >= 8 &&
sizeof(std::int_fast8_t) >= 1 && sizeof(std::int_fast16_t) >= 2 &&
sizeof(std::int_fast32_t) >= 4 && sizeof(std::int_fast64_t) >= 8 &&
l8 == -128 && ul8 == 255 && l32 == 2147483647 && ul64 == UINT64_MAX &&
f8 == -128 && uf8 == 255 && f32 == 2147483647 && uf64 == UINT64_MAX;
cases.push_back({"least_and_fast_width_types", lab::jbool(true), lab::jbool(c2), c2});
// Case 3: intmax_t / uintmax_t and intptr_t / uintptr_t pointer round-trip
std::intmax_t max_i = INTMAX_MAX;
std::uintmax_t max_u = UINTMAX_MAX;
int target = 2026;
std::uintptr_t addr_u = reinterpret_cast<std::uintptr_t>(&target);
std::intptr_t addr_s = reinterpret_cast<std::intptr_t>(&target);
int* restored_u = reinterpret_cast<int*>(addr_u);
int* restored_s = reinterpret_cast<int*>(addr_s);
bool c3 =
sizeof(std::intmax_t) >= 8 && sizeof(std::uintmax_t) >= 8 &&
max_i >= INT64_MAX && max_u >= UINT64_MAX &&
restored_u == &target && restored_s == &target && *restored_u == 2026;
cases.push_back({"intmax_and_intptr_roundtrip", lab::jbool(true), lab::jbool(c3), c3});
// Case 4: Limit macros verification
bool c4 =
INT8_MIN == -128 && INT8_MAX == 127 && UINT8_MAX == 255 &&
INT16_MIN == -32768 && INT16_MAX == 32767 && UINT16_MAX == 65535 &&
INT32_MIN == (-2147483647 - 1) && INT32_MAX == 2147483647 && UINT32_MAX == 4294967295U &&
INT64_MIN == (-9223372036854775807LL - 1) && INT64_MAX == 9223372036854775807LL &&
UINT64_MAX == 18446744073709551615ULL;
cases.push_back({"limit_macros_verification", lab::jbool(true), lab::jbool(c4), c4});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cpp-libs-cstdint-lab/explore-cpp-libs-cstdint.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-cstdint.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2341.zip