style: 应用 clang-format
This commit is contained in:
1 parent
016a45bc6e
commit
d1612f1d31
65 files changed
+7288
-5983
No files matched your search
+56
-35
@@ -1,7 +1,7 @@
|
||||
/* test_attach.c - /include 的暂存、glob 展开与体积/类型判断
|
||||
*
|
||||
* 交互式挑选 (attach_pick)要真终端才能测, 由 e2e 第 55 段覆盖;
|
||||
* 这里测的是不需要人操作的那一半.
|
||||
* 交互式挑选 (attach_pick)要真终端才能测, 由 e2e 第 55 段覆盖;
|
||||
* 这里测的是不需要人操作的那一半.
|
||||
*/
|
||||
#include "attach.h"
|
||||
#include "image.h"
|
||||
@@ -17,57 +17,69 @@
|
||||
static int failures = 0;
|
||||
static int checks = 0;
|
||||
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static void write_file(const char *path, const void *data, size_t n)
|
||||
{
|
||||
static void write_file(const char *path, const void *data, size_t n) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) { perror(path); exit(2); }
|
||||
if (n && fwrite(data, 1, n, f) != n) { perror("fwrite"); exit(2); }
|
||||
if (!f) {
|
||||
perror(path);
|
||||
exit(2);
|
||||
}
|
||||
if (n && fwrite(data, 1, n, f) != n) {
|
||||
perror("fwrite");
|
||||
exit(2);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static const unsigned char PNG[] = {
|
||||
0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A, 'p', 'a', 'y', 'l', 'o', 'a', 'd'
|
||||
};
|
||||
static const unsigned char PNG[] = {0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A,
|
||||
'p', 'a', 'y', 'l', 'o', 'a', 'd'};
|
||||
|
||||
/* 数一数暂存里某类块有几个 */
|
||||
static size_t count_type(const JsonValue *parts, const char *type)
|
||||
{
|
||||
if (!parts) return 0;
|
||||
static size_t count_type(const JsonValue *parts, const char *type) {
|
||||
if (!parts)
|
||||
return 0;
|
||||
size_t n = 0;
|
||||
for (size_t i = 0; i < json_array_size((JsonValue *)parts); i++) {
|
||||
const char *t = json_get_string(json_array_get((JsonValue *)parts, i), "type");
|
||||
if (t && strcmp(t, type) == 0) n++;
|
||||
const char *t =
|
||||
json_get_string(json_array_get((JsonValue *)parts, i), "type");
|
||||
if (t && strcmp(t, type) == 0)
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* 记一下回调被问了几次 —— 它只在遇到图片时该被问到.
|
||||
/* 记一下回调被问了几次 —— 它只在遇到图片时该被问到.
|
||||
* 答案和计数放在一起, 以免"计数版只实现了一种答案"这种测试自己的错. */
|
||||
static int ask_count;
|
||||
static int ask_answer;
|
||||
static int counting_ask(void *ud) { (void)ud; ask_count++; return ask_answer; }
|
||||
static int counting_ask(void *ud) {
|
||||
(void)ud;
|
||||
ask_count++;
|
||||
return ask_answer;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
char tmpl[] = "/tmp/ligent_attach_XXXXXX";
|
||||
char *dir = mkdtemp(tmpl);
|
||||
if (!dir) { perror("mkdtemp"); return 2; }
|
||||
if (!dir) {
|
||||
perror("mkdtemp");
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* 备份目录也指到临时目录, 别污染仓库 */
|
||||
image_set_bindir(dir);
|
||||
|
||||
char *f_c = xasprintf("%s/a.c", dir);
|
||||
char *f_c = xasprintf("%s/a.c", dir);
|
||||
char *f_txt = xasprintf("%s/b.txt", dir);
|
||||
char *f_bin = xasprintf("%s/bin.dat", dir);
|
||||
char *f_png = xasprintf("%s/pic.png", dir);
|
||||
@@ -133,24 +145,28 @@ int main(void)
|
||||
CHECK(added == 1, "png 被收下");
|
||||
CHECK(attach_has_image(), "暂存里有图片");
|
||||
parts = attach_take_parts();
|
||||
CHECK(count_type(parts, "ligent_image") == 1, "应当是一个 ligent_image 引用块");
|
||||
CHECK(count_type(parts, "ligent_image") == 1,
|
||||
"应当是一个 ligent_image 引用块");
|
||||
json_free(parts);
|
||||
|
||||
puts("模型不收图时跳过图片 (回调只在遇到图片时被问)");
|
||||
attach_clear();
|
||||
ask_count = 0; ask_answer = 1;
|
||||
ask_count = 0;
|
||||
ask_answer = 1;
|
||||
added = attach_glob(f_txt, counting_ask, NULL, &skipped, err, sizeof err);
|
||||
CHECK(added == 1, "纯文本照带");
|
||||
CHECK(ask_count == 0, "纯文本不该去问'能不能收图' (问了一次就是白探测)");
|
||||
attach_clear();
|
||||
ask_count = 0; ask_answer = 0;
|
||||
ask_count = 0;
|
||||
ask_answer = 0;
|
||||
added = attach_glob(f_png, counting_ask, NULL, &skipped, err, sizeof err);
|
||||
CHECK(added == 0 && skipped == 1, "回调说不行就跳过");
|
||||
CHECK(ask_count == 1, "遇到图片时问了一次, 实际 %d", ask_count);
|
||||
|
||||
puts("匹配不到 / 参数非法");
|
||||
attach_clear();
|
||||
added = attach_glob("/tmp/绝对不存在的模式*.zzz", NULL, NULL, &skipped, err, sizeof err);
|
||||
added = attach_glob("/tmp/绝对不存在的模式*.zzz", NULL, NULL, &skipped, err,
|
||||
sizeof err);
|
||||
CHECK(added == -1 && err[0], "匹配不到返回 -1 并给出原因: %s", err);
|
||||
added = attach_glob(NULL, NULL, NULL, &skipped, err, sizeof err);
|
||||
CHECK(added == -1, "NULL 模式返回 -1");
|
||||
@@ -166,10 +182,15 @@ int main(void)
|
||||
free(pat);
|
||||
attach_clear();
|
||||
|
||||
free(f_c); free(f_txt); free(f_bin); free(f_png); free(f_big);
|
||||
free(f_c);
|
||||
free(f_txt);
|
||||
free(f_bin);
|
||||
free(f_png);
|
||||
free(f_big);
|
||||
image_set_bindir(".");
|
||||
char *cmd = xasprintf("rm -rf %s", tmpl);
|
||||
if (system(cmd) != 0) { }
|
||||
if (system(cmd) != 0) {
|
||||
}
|
||||
free(cmd);
|
||||
|
||||
printf("\n%d 项检查, %d 项失败\n", checks, failures);
|
||||
|
||||
Reference in new issue
Block a user