#include <climits>
#include <cstdio>
#include <iostream>
#include <limits>
#include <string>
#include <type_traits>
#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: char and short limits
bool char_sign_ok = std::is_signed_v<char>
? (CHAR_MIN == SCHAR_MIN && CHAR_MAX == SCHAR_MAX)
: (CHAR_MIN == 0 && CHAR_MAX == UCHAR_MAX);
bool c1 = (CHAR_BIT == 8) &&
(SCHAR_MIN == -128) && (SCHAR_MAX == 127) && (UCHAR_MAX == 255) &&
char_sign_ok &&
(SHRT_MIN == -32768) && (SHRT_MAX == 32767) && (USHRT_MAX == 65535);
cases.push_back({"char_and_short_limits", lab::jbool(true), lab::jbool(c1), c1});
// Case 2: int, long, and long long limits match std::numeric_limits<T>
bool c2 =
(INT_MIN == std::numeric_limits<int>::min()) &&
(INT_MAX == std::numeric_limits<int>::max()) &&
(UINT_MAX == std::numeric_limits<unsigned int>::max()) &&
(LONG_MIN == std::numeric_limits<long>::min()) &&
(LONG_MAX == std::numeric_limits<long>::max()) &&
(ULONG_MAX == std::numeric_limits<unsigned long>::max()) &&
(LLONG_MIN == std::numeric_limits<long long>::min()) &&
(LLONG_MAX == std::numeric_limits<long long>::max()) &&
(ULLONG_MAX == std::numeric_limits<unsigned long long>::max());
cases.push_back({"int_long_llong_limits", lab::jbool(true), lab::jbool(c2), c2});
// Case 3: Standard width hierarchy and unsigned max invariants
bool c3 =
(sizeof(char) <= sizeof(short)) &&
(sizeof(short) <= sizeof(int)) &&
(sizeof(int) <= sizeof(long)) &&
(sizeof(long) <= sizeof(long long)) &&
(UINT_MAX == 2U * static_cast<unsigned int>(INT_MAX) + 1U) &&
(ULLONG_MAX == 2ULL * static_cast<unsigned long long>(LLONG_MAX) + 1ULL);
cases.push_back({"platform_width_invariants", lab::jbool(true), lab::jbool(c3), c3});
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-climits-lab/explore-cpp-libs-climits.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-climits.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2336.zip