chore(M0): 建立 CLion/CMake 工程骨架与测试框架
- CMake 多目标工程:shs_core 静态库 + smart_home 可执行 + shs_tests 测试 - CMakePresets.json:debug/release/asan/coverage 四个预设(debug 告警即错误) - 自研零依赖测试框架 tests/framework/test_framework.h + 运行器(套件过滤、CTest 集成) - 工具:tools/header_check.sh 头文件独立编译检查(接口冻结门) - 已实现公共层:errors / string_utils / num_utils,main 启动自检 - 实测:debug 预设 0 告警、ctest 3/3 通过(45 项断言)
This commit is contained in:
commit
578a770737
36 files changed
+2914
No files matched your search
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* @file errors.c
|
||||
* @brief 错误码转字符串
|
||||
*/
|
||||
#include "shs/errors.h"
|
||||
|
||||
const char *shs_strerror(shs_err_t err)
|
||||
{
|
||||
switch (err) {
|
||||
case SHS_OK: return "SHS_OK";
|
||||
case SHS_ERR_PARAM: return "SHS_ERR_PARAM";
|
||||
case SHS_ERR_NOT_FOUND: return "SHS_ERR_NOT_FOUND";
|
||||
case SHS_ERR_FULL: return "SHS_ERR_FULL";
|
||||
case SHS_ERR_STATE: return "SHS_ERR_STATE";
|
||||
case SHS_ERR_IO: return "SHS_ERR_IO";
|
||||
case SHS_ERR_PARSE: return "SHS_ERR_PARSE";
|
||||
case SHS_ERR_RANGE: return "SHS_ERR_RANGE";
|
||||
case SHS_ERR_TIMEOUT: return "SHS_ERR_TIMEOUT";
|
||||
case SHS_ERR_BUSY: return "SHS_ERR_BUSY";
|
||||
case SHS_ERR_UNSUPPORTED: return "SHS_ERR_UNSUPPORTED";
|
||||
case SHS_ERR_NOT_IMPLEMENTED: return "SHS_ERR_NOT_IMPLEMENTED";
|
||||
case SHS_ERR_INTERNAL: return "SHS_ERR_INTERNAL";
|
||||
default: return "SHS_ERR_UNKNOWN";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
/**
|
||||
* @file num_utils.c
|
||||
* @brief 数值计算与进制转换工具的实现
|
||||
*
|
||||
* 知识点落点:
|
||||
* - 项目二:整型/浮点型的转换与截断、算术与关系运算符、进制
|
||||
* - 项目三:分支判断输入是否合法
|
||||
* - 项目四:for 循环累乘
|
||||
* - 项目六:递归(阶乘、最大公约数)
|
||||
* - 项目七:指针遍历数组、指针出参
|
||||
* - 项目九:位运算(哈希、十六进制转换)
|
||||
*/
|
||||
#include "shs/num_utils.h"
|
||||
#include "shs/string_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
int shs_int_clamp(int value, int lo, int hi)
|
||||
{
|
||||
if (lo > hi) {
|
||||
int tmp = lo;
|
||||
lo = hi;
|
||||
hi = tmp;
|
||||
}
|
||||
if (value < lo) { return lo; }
|
||||
if (value > hi) { return hi; }
|
||||
return value;
|
||||
}
|
||||
|
||||
double shs_double_clamp(double value, double lo, double hi)
|
||||
{
|
||||
if (lo > hi) {
|
||||
double tmp = lo;
|
||||
lo = hi;
|
||||
hi = tmp;
|
||||
}
|
||||
if (value < lo) { return lo; }
|
||||
if (value > hi) { return hi; }
|
||||
return value;
|
||||
}
|
||||
|
||||
int shs_round_to_int(double value)
|
||||
{
|
||||
return (int)((value >= 0.0) ? (value + 0.5) : (value - 0.5)); /* 显式类型转换 */
|
||||
}
|
||||
|
||||
int shs_floor_to_int(double value)
|
||||
{
|
||||
return (int)value; /* C 的强制转换对正数是截断(向零取整) */
|
||||
}
|
||||
|
||||
int shs_abs_int(int value)
|
||||
{
|
||||
return (value < 0) ? -value : value;
|
||||
}
|
||||
|
||||
int shs_min_of(const int *values, int count)
|
||||
{
|
||||
if (values == NULL || count <= 0) {
|
||||
return 0;
|
||||
}
|
||||
int min = values[0];
|
||||
for (int i = 1; i < count; i++) { /* 项目四 for 循环遍历数组 */
|
||||
if (values[i] < min) {
|
||||
min = values[i];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
int shs_max_of(const int *values, int count)
|
||||
{
|
||||
if (values == NULL || count <= 0) {
|
||||
return 0;
|
||||
}
|
||||
int max = values[0];
|
||||
for (int i = 1; i < count; i++) {
|
||||
if (values[i] > max) {
|
||||
max = values[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
double shs_average_of(const int *values, int count)
|
||||
{
|
||||
if (values == NULL || count <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
long sum = 0; /* 先整型累加,最后做浮点除法 */
|
||||
for (int i = 0; i < count; i++) {
|
||||
sum += values[i];
|
||||
}
|
||||
return (double)sum / (double)count; /* 隐式 + 显式混合转换 */
|
||||
}
|
||||
|
||||
long shs_pow_int(int base, int exp)
|
||||
{
|
||||
if (exp < 0) {
|
||||
return 0;
|
||||
}
|
||||
long result = 1; /* 项目四:循环累乘 */
|
||||
for (int i = 0; i < exp; i++) {
|
||||
result *= base;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
long shs_factorial(int n)
|
||||
{
|
||||
if (n < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (n <= 1) {
|
||||
return 1; /* 递归出口 */
|
||||
}
|
||||
return (long)n * shs_factorial(n - 1); /* 项目六:递归调用 */
|
||||
}
|
||||
|
||||
int shs_gcd(int a, int b)
|
||||
{
|
||||
if (b == 0) {
|
||||
return (a < 0) ? -a : a;
|
||||
}
|
||||
return shs_gcd(b, a % b); /* 递归欧几里得算法 */
|
||||
}
|
||||
|
||||
shs_err_t shs_parse_int(const char *text, int *out)
|
||||
{
|
||||
if (text == NULL || out == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
const char *p = text;
|
||||
while (*p != '\0' && isspace((unsigned char)*p)) {
|
||||
p++;
|
||||
}
|
||||
if (*p == '\0') {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
char *end = NULL;
|
||||
long v = strtol(p, &end, 10);
|
||||
if (end == p) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
while (*end != '\0') {
|
||||
if (!isspace((unsigned char)*end)) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
if (v < -2147483647L - 1L || v > 2147483647L) {
|
||||
return SHS_ERR_RANGE;
|
||||
}
|
||||
*out = (int)v;
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
shs_err_t shs_parse_double(const char *text, double *out)
|
||||
{
|
||||
if (text == NULL || out == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
const char *p = text;
|
||||
while (*p != '\0' && isspace((unsigned char)*p)) {
|
||||
p++;
|
||||
}
|
||||
if (*p == '\0') {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
char *end = NULL;
|
||||
double v = strtod(p, &end);
|
||||
if (end == p) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
while (*end != '\0') {
|
||||
if (!isspace((unsigned char)*end)) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
*out = v;
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
shs_err_t shs_parse_bool(const char *text, bool *out)
|
||||
{
|
||||
if (text == NULL || out == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
if (shs_strcasecmp_equals(text, "true") || shs_strcasecmp_equals(text, "on") || shs_strcasecmp_equals(text, "1") ||
|
||||
shs_strcasecmp_equals(text, "yes") || shs_strcasecmp_equals(text, "是")) {
|
||||
*out = true;
|
||||
return SHS_OK;
|
||||
}
|
||||
if (shs_strcasecmp_equals(text, "false") || shs_strcasecmp_equals(text, "off") || shs_strcasecmp_equals(text, "0") ||
|
||||
shs_strcasecmp_equals(text, "no") || shs_strcasecmp_equals(text, "否")) {
|
||||
*out = false;
|
||||
return SHS_OK;
|
||||
}
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
|
||||
double shs_percent(int value, int total)
|
||||
{
|
||||
if (total <= 0) {
|
||||
return 0.0; /* 除零保护:项目三 分支 + 项目二 类型转换 */
|
||||
}
|
||||
return (double)value * 100.0 / (double)total;
|
||||
}
|
||||
|
||||
shs_err_t shs_bytes_to_hex(const uint8_t *data, size_t len, char *out, size_t out_size)
|
||||
{
|
||||
static const char digits[] = "0123456789ABCDEF";
|
||||
if (data == NULL || out == NULL || out_size < len * 2u + 1u) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
size_t w = 0;
|
||||
for (size_t i = 0; i < len; i++) { /* 项目九:位运算取高低四位 */
|
||||
out[w++] = digits[(data[i] >> 4) & 0x0Fu];
|
||||
out[w++] = digits[data[i] & 0x0Fu];
|
||||
}
|
||||
out[w] = '\0';
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
static int hex_value(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9') { return c - '0'; }
|
||||
if (c >= 'a' && c <= 'f') { return c - 'a' + 10; }
|
||||
if (c >= 'A' && c <= 'F') { return c - 'A' + 10; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
int shs_hex_to_bytes(const char *hex, uint8_t *out, size_t out_size)
|
||||
{
|
||||
if (hex == NULL || out == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
size_t len = strlen(hex);
|
||||
if (len % 2u != 0u) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
size_t n = len / 2u;
|
||||
if (n > out_size) {
|
||||
return SHS_ERR_FULL;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
int hi = hex_value(hex[i * 2u]);
|
||||
int lo = hex_value(hex[i * 2u + 1u]);
|
||||
if (hi < 0 || lo < 0) {
|
||||
return SHS_ERR_PARSE;
|
||||
}
|
||||
out[i] = (uint8_t)((hi << 4) | lo); /* 位运算移位 + 按位或 */
|
||||
}
|
||||
return (int)n;
|
||||
}
|
||||
|
||||
uint32_t shs_hash_string(const char *text)
|
||||
{
|
||||
/* FNV-1a 32 位:异或 + 乘法 + 移位,位运算综合示例 */
|
||||
uint32_t hash = 2166136261u;
|
||||
if (text == NULL) {
|
||||
return hash;
|
||||
}
|
||||
for (const char *p = text; *p != '\0'; ++p) {
|
||||
hash ^= (uint32_t)(unsigned char)*p;
|
||||
hash *= 16777619u;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
/**
|
||||
* @file string_utils.c
|
||||
* @brief 字符串与字符数组处理工具的实现
|
||||
*
|
||||
* 知识点落点:
|
||||
* - 项目五:字符数组、字符遍历、字符串函数族
|
||||
* - 项目六:递归调用(shs_wildcard_match)
|
||||
* - 项目七:指针遍历字符串(const char * 游标)
|
||||
* - 项目九:数据安全(所有写入都带长度上限)
|
||||
*/
|
||||
#include "shs/string_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
size_t shs_strlen(const char *s)
|
||||
{
|
||||
size_t n = 0;
|
||||
if (s == NULL) {
|
||||
return 0;
|
||||
}
|
||||
while (s[n] != '\0') { /* 项目五:字符数组以 '\0' 结束 */
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
shs_err_t shs_strcpy(char *dst, size_t dst_size, const char *src)
|
||||
{
|
||||
if (dst == NULL || src == NULL || dst_size == 0) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (i + 1 < dst_size && src[i] != '\0') {
|
||||
dst[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
dst[i] = '\0'; /* 一定以 '\0' 收尾,避免越界读 */
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
shs_err_t shs_strcat(char *dst, size_t dst_size, const char *src)
|
||||
{
|
||||
if (dst == NULL || src == NULL || dst_size == 0) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
size_t len = shs_strlen(dst);
|
||||
if (len + 1 >= dst_size) {
|
||||
return SHS_ERR_FULL;
|
||||
}
|
||||
return shs_strcpy(dst + len, dst_size - len, src);
|
||||
}
|
||||
|
||||
int shs_strcmp(const char *a, const char *b)
|
||||
{
|
||||
if (a == NULL || b == NULL) {
|
||||
if (a == b) { return 0; }
|
||||
return (a == NULL) ? -1 : 1;
|
||||
}
|
||||
return strcmp(a, b);
|
||||
}
|
||||
|
||||
int shs_strcasecmp(const char *a, const char *b)
|
||||
{
|
||||
if (a == NULL || b == NULL) {
|
||||
if (a == b) { return 0; }
|
||||
return (a == NULL) ? -1 : 1;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (a[i] != '\0' && b[i] != '\0') {
|
||||
int ca = tolower((unsigned char)a[i]);
|
||||
int cb = tolower((unsigned char)b[i]);
|
||||
if (ca != cb) {
|
||||
return ca - cb;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return tolower((unsigned char)a[i]) - tolower((unsigned char)b[i]);
|
||||
}
|
||||
|
||||
bool shs_strcasecmp_equals(const char *a, const char *b)
|
||||
{
|
||||
return shs_strcasecmp(a, b) == 0;
|
||||
}
|
||||
|
||||
const char *shs_str_find(const char *haystack, const char *needle)
|
||||
{
|
||||
if (haystack == NULL || needle == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (needle[0] == '\0') {
|
||||
return haystack;
|
||||
}
|
||||
for (const char *p = haystack; *p != '\0'; ++p) { /* 项目七:指针遍历 */
|
||||
const char *h = p;
|
||||
const char *n = needle;
|
||||
while (*h != '\0' && *n != '\0' && *h == *n) {
|
||||
h++;
|
||||
n++;
|
||||
}
|
||||
if (*n == '\0') {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool shs_str_contains(const char *haystack, const char *needle)
|
||||
{
|
||||
return shs_str_find(haystack, needle) != NULL;
|
||||
}
|
||||
|
||||
static bool shs_is_space(char c)
|
||||
{
|
||||
return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f');
|
||||
}
|
||||
|
||||
shs_err_t shs_str_trim(char *s)
|
||||
{
|
||||
if (s == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
size_t len = shs_strlen(s);
|
||||
size_t begin = 0;
|
||||
while (begin < len && shs_is_space(s[begin])) {
|
||||
begin++;
|
||||
}
|
||||
size_t end = len;
|
||||
while (end > begin && shs_is_space(s[end - 1])) {
|
||||
end--;
|
||||
}
|
||||
size_t out = 0;
|
||||
for (size_t i = begin; i < end; i++) {
|
||||
s[out++] = s[i];
|
||||
}
|
||||
s[out] = '\0';
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
shs_err_t shs_str_to_upper(char *s)
|
||||
{
|
||||
if (s == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
for (size_t i = 0; s[i] != '\0'; i++) {
|
||||
s[i] = (char)toupper((unsigned char)s[i]);
|
||||
}
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
shs_err_t shs_str_to_lower(char *s)
|
||||
{
|
||||
if (s == NULL) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
for (size_t i = 0; s[i] != '\0'; i++) {
|
||||
s[i] = (char)tolower((unsigned char)s[i]);
|
||||
}
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
bool shs_str_starts_with(const char *s, const char *prefix)
|
||||
{
|
||||
if (s == NULL || prefix == NULL) {
|
||||
return false;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (prefix[i] != '\0') {
|
||||
if (s[i] != prefix[i]) {
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool shs_str_ends_with(const char *s, const char *suffix)
|
||||
{
|
||||
if (s == NULL || suffix == NULL) {
|
||||
return false;
|
||||
}
|
||||
size_t ls = shs_strlen(s);
|
||||
size_t lf = shs_strlen(suffix);
|
||||
if (lf > ls) {
|
||||
return false;
|
||||
}
|
||||
return shs_strcmp(s + (ls - lf), suffix) == 0;
|
||||
}
|
||||
|
||||
int shs_str_escape(const char *in, char *out, size_t out_size)
|
||||
{
|
||||
if (in == NULL || out == NULL || out_size == 0) {
|
||||
return -1;
|
||||
}
|
||||
size_t w = 0;
|
||||
for (size_t i = 0; in[i] != '\0'; i++) {
|
||||
const char *rep = NULL;
|
||||
switch (in[i]) { /* 项目二:转义字符 */
|
||||
case '\t': rep = "\\t"; break;
|
||||
case '\n': rep = "\\n"; break;
|
||||
case '\r': rep = "\\r"; break;
|
||||
case '\\': rep = "\\\\"; break;
|
||||
case '"': rep = "\\\""; break;
|
||||
default: break;
|
||||
}
|
||||
if (rep == NULL) {
|
||||
if (w + 1 >= out_size) { break; }
|
||||
out[w++] = in[i];
|
||||
} else {
|
||||
for (size_t k = 0; rep[k] != '\0'; k++) {
|
||||
if (w + 1 >= out_size) { break; }
|
||||
out[w++] = rep[k];
|
||||
}
|
||||
}
|
||||
}
|
||||
out[w] = '\0';
|
||||
return (int)w;
|
||||
}
|
||||
|
||||
int shs_str_split(const char *src, char sep, char *items[], int max_items,
|
||||
char *workspace, size_t workspace_size)
|
||||
{
|
||||
if (src == NULL || items == NULL || workspace == NULL || max_items <= 0) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
if (shs_strcpy(workspace, workspace_size, src) != SHS_OK) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
int count = 0;
|
||||
char *p = workspace;
|
||||
items[count++] = p;
|
||||
for (; *p != '\0'; ++p) {
|
||||
if (*p == sep) {
|
||||
*p = '\0';
|
||||
if (count < max_items) {
|
||||
items[count++] = p + 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
shs_err_t shs_int_to_str(int value, char *out, size_t out_size)
|
||||
{
|
||||
if (out == NULL || out_size == 0) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
int n = snprintf(out, out_size, "%d", value);
|
||||
if (n < 0 || (size_t)n >= out_size) {
|
||||
return SHS_ERR_FULL;
|
||||
}
|
||||
return SHS_OK;
|
||||
}
|
||||
|
||||
int shs_parse_int_list(const char *src, char sep, int values[], int max_values)
|
||||
{
|
||||
if (src == NULL || values == NULL || max_values <= 0) {
|
||||
return SHS_ERR_PARAM;
|
||||
}
|
||||
int count = 0;
|
||||
const char *p = src;
|
||||
while (*p != '\0' && count < max_values) {
|
||||
char *end = NULL;
|
||||
long v = strtol(p, &end, 10);
|
||||
if (end == p) {
|
||||
break;
|
||||
}
|
||||
if (v < -2147483647L - 1L || v > 2147483647L) {
|
||||
break;
|
||||
}
|
||||
values[count++] = (int)v;
|
||||
p = end;
|
||||
if (*p == sep) {
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
bool shs_wildcard_match(const char *pattern, const char *text)
|
||||
{
|
||||
if (pattern == NULL || text == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (pattern[0] == '\0') {
|
||||
return text[0] == '\0';
|
||||
}
|
||||
if (pattern[0] == '*') {
|
||||
/* 递归:'*' 吃掉 0 个或多个字符(项目六 递归调用) */
|
||||
if (shs_wildcard_match(pattern + 1, text)) {
|
||||
return true;
|
||||
}
|
||||
return (text[0] != '\0') && shs_wildcard_match(pattern, text + 1);
|
||||
}
|
||||
if (pattern[0] == '?' || pattern[0] == text[0]) {
|
||||
return shs_wildcard_match(pattern + 1, text + 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @brief 程序入口:智能家居控制系统(项目一 —— 第一个 C 程序 / 程序结构)
|
||||
*
|
||||
* 教材对应:项目一 任务三「第一个 C 程序」;后续里程碑将在此接入
|
||||
* CLI 交互模式(--cli)与网页模式(--web)。
|
||||
*/
|
||||
#include "shs/common.h"
|
||||
#include "shs/errors.h"
|
||||
#include "shs/string_utils.h"
|
||||
#include "shs/num_utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void shs_print_banner(void)
|
||||
{
|
||||
printf("==========================================================\n");
|
||||
printf(" %s (Smart Home Control System) v%s\n", SHS_PRODUCT_NAME, SHS_VERSION_STRING);
|
||||
printf(" 《C语言程序设计项目化教程——智能家居控制系统开发实战》配套工程\n");
|
||||
printf("==========================================================\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 启动自检:检查已实现的公共模块是否工作正常
|
||||
* @return 通过的检查项数量
|
||||
*/
|
||||
static int shs_bootstrap_self_check(void)
|
||||
{
|
||||
int passed = 0;
|
||||
|
||||
/* 项目五:字符数组与字符串 */
|
||||
char name[SHS_NAME_MAX];
|
||||
if (shs_strcpy(name, sizeof(name), "客厅主灯") == SHS_OK && shs_strlen(name) > 0) {
|
||||
printf("[自检] 字符串模块 : 通过(设备名 \"%s\")\n", name);
|
||||
passed++;
|
||||
}
|
||||
|
||||
/* 项目二:运算符与类型转换 */
|
||||
char hex[SHS_FRAME_MAX * 2 + 1];
|
||||
const uint8_t sample[4] = { 0x5A, 0x01, 0x02, 0x32 };
|
||||
if (shs_bytes_to_hex(sample, sizeof(sample), hex, sizeof(hex)) == SHS_OK) {
|
||||
printf("[自检] 进制转换模块 : 通过(帧样本 %s)\n", hex);
|
||||
passed++;
|
||||
}
|
||||
|
||||
/* 项目六:递归调用 */
|
||||
if (shs_factorial(5) == 120L && shs_gcd(48, 18) == 6) {
|
||||
printf("[自检] 递归函数模块 : 通过(5! = %ld,gcd(48,18) = %d)\n",
|
||||
shs_factorial(5), shs_gcd(48, 18));
|
||||
passed++;
|
||||
}
|
||||
|
||||
/* 项目三:分支判断错误码 */
|
||||
int value = 0;
|
||||
if (shs_parse_int("42", &value) == SHS_OK && value == 42 &&
|
||||
shs_parse_int("abc", &value) == SHS_ERR_PARSE) {
|
||||
printf("[自检] 输入校验模块 : 通过(合法与非法输入均正确分支)\n");
|
||||
passed++;
|
||||
}
|
||||
|
||||
return passed;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
SHS_UNUSED(argc);
|
||||
SHS_UNUSED(argv);
|
||||
|
||||
shs_print_banner();
|
||||
|
||||
const int passed = shs_bootstrap_self_check();
|
||||
printf("----------------------------------------------------------\n");
|
||||
printf("启动自检:%d 项通过\n", passed);
|
||||
printf("当前为 M0/M1 骨架版本,CLI 菜单与网页界面在后续里程碑接入。\n");
|
||||
printf("构建/运行/测试方式见 README.md 与 docs/05-用户手册.md\n");
|
||||
printf("==========================================================\n");
|
||||
return (passed == 4) ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user