diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-15 08:40:11 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-15 08:40:11 +0200 |
| commit | c4a1c97a09da6d8d88c2ac834bf6234506fc41bc (patch) | |
| tree | 81ce5675ce886aad0309a41f4d6a696826e0a061 /tests/test_safetensors.c | |
| parent | b377ba8656bfb807ed8dda6c9f26942b50ce626d (diff) | |
| download | ffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.tar.gz ffwd-c4a1c97a09da6d8d88c2ac834bf6234506fc41bc.zip | |
chore: run clang-format
Diffstat (limited to 'tests/test_safetensors.c')
| -rw-r--r-- | tests/test_safetensors.c | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c index d527951..36ec8c5 100644 --- a/tests/test_safetensors.c +++ b/tests/test_safetensors.c @@ -15,29 +15,31 @@ static int failures = 0; static char tmproot[1024]; /* Write u64-LE header length + JSON header + payload (safetensors layout). */ -static void write_st(const char *path, const char *header, - const void *payload, size_t payload_len) -{ +static void +write_st(const char *path, const char *header, const void *payload, size_t payload_len) { FILE *f = fopen(path, "wb"); - if (!f) { perror(path); exit(2); } + if (!f) { + perror(path); + exit(2); + } uint64_t n = strlen(header); unsigned char len8[8]; - for (int i = 0; i < 8; i++) len8[i] = (unsigned char)(n >> (8 * i)); + for (int i = 0; i < 8; i++) + len8[i] = (unsigned char)(n >> (8 * i)); fwrite(len8, 1, 8, f); fwrite(header, 1, n, f); - if (payload_len) fwrite(payload, 1, payload_len, f); + if (payload_len) + fwrite(payload, 1, payload_len, f); fclose(f); } -static const char *path_in(const char *rel) -{ +static const char *path_in(const char *rel) { static char buf[2048]; snprintf(buf, sizeof(buf), "%s/%s", tmproot, rel); return buf; } -static void check(const char *mode, const char *path, int want_ok) -{ +static void check(const char *mode, const char *path, int want_ok) { int got_ok; if (strcmp(mode, "file") == 0) { safetensors_file_t *sf = safetensors_open(path); @@ -49,8 +51,8 @@ static void check(const char *mode, const char *path, int want_ok) multi_safetensors_close(ms); } if (got_ok != want_ok) { - fprintf(stderr, "%s %s: got %s want %s\n", mode, path, - got_ok ? "ok" : "fail", want_ok ? "ok" : "fail"); + fprintf(stderr, "%s %s: got %s want %s\n", mode, path, got_ok ? "ok" : "fail", + want_ok ? "ok" : "fail"); failures++; } } @@ -62,11 +64,13 @@ static const char ONE_TENSOR_B[] = static const char ONE_TENSOR_X[] = "{\"x\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}"; -int main(void) -{ +int main(void) { snprintf(tmproot, sizeof(tmproot), "%s/embed-st-test-XXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp"); - if (!mkdtemp(tmproot)) { perror("mkdtemp"); return 2; } + if (!mkdtemp(tmproot)) { + perror("mkdtemp"); + return 2; + } static const char zeros[4 * 1025] = {0}; @@ -77,14 +81,15 @@ int main(void) { size_t cap = 1025 * 64 + 16; char *hdr = malloc(cap); - if (!hdr) return 2; + if (!hdr) + return 2; size_t off = 0; off += (size_t)snprintf(hdr + off, cap - off, "{"); for (int i = 0; i < 1025; i++) { off += (size_t)snprintf(hdr + off, cap - off, - "%s\"t%d\":{\"dtype\":\"F32\",\"shape\":[1]," - "\"data_offsets\":[%d,%d]}", - i ? "," : "", i, i * 4, i * 4 + 4); + "%s\"t%d\":{\"dtype\":\"F32\",\"shape\":[1]," + "\"data_offsets\":[%d,%d]}", + i ? "," : "", i, i * 4, i * 4 + 4); } snprintf(hdr + off, cap - off, "}"); write_st(path_in("too_many.safetensors"), hdr, zeros, sizeof(zeros)); @@ -94,27 +99,26 @@ int main(void) write_st(path_in("too_many_dims.safetensors"), "{\"x\":{\"dtype\":\"F32\",\"shape\":[1,1,1,1,1,1,1,1,1]," - "\"data_offsets\":[0,4]}}", zeros, 4); + "\"data_offsets\":[0,4]}}", + zeros, 4); check("file", path_in("too_many_dims.safetensors"), 0); write_st(path_in("bad_offsets.safetensors"), "{\"x\":{\"dtype\":\"F32\",\"shape\":[1]," - "\"data_offsets\":[4,0]}}", zeros, 4); + "\"data_offsets\":[4,0]}}", + zeros, 4); check("file", path_in("bad_offsets.safetensors"), 0); mkdir(path_in("valid_multi"), 0755); - write_st(path_in("valid_multi/model-00001-of-00002.safetensors"), - ONE_TENSOR_A, zeros, 4); - write_st(path_in("valid_multi/model-00002-of-00002.safetensors"), - ONE_TENSOR_B, zeros, 4); + write_st(path_in("valid_multi/model-00001-of-00002.safetensors"), ONE_TENSOR_A, zeros, 4); + write_st(path_in("valid_multi/model-00002-of-00002.safetensors"), ONE_TENSOR_B, zeros, 4); check("multi", path_in("valid_multi"), 1); /* Payload accounting across shards: two 4-byte F32 tensors. */ { multi_safetensors_t *ms = multi_safetensors_open(path_in("valid_multi")); size_t nbytes = 0; - if (!ms || multi_safetensors_data_nbytes(ms, &nbytes) != 0 || - nbytes != 8) { + if (!ms || multi_safetensors_data_nbytes(ms, &nbytes) != 0 || nbytes != 8) { fprintf(stderr, "multi data_nbytes: got %zu want 8\n", nbytes); failures++; } @@ -122,18 +126,15 @@ int main(void) } mkdir(path_in("missing"), 0755); - write_st(path_in("missing/model-00001-of-00002.safetensors"), - ONE_TENSOR_X, zeros, 4); + write_st(path_in("missing/model-00001-of-00002.safetensors"), ONE_TENSOR_X, zeros, 4); check("multi", path_in("missing"), 0); mkdir(path_in("too_many_shards"), 0755); - write_st(path_in("too_many_shards/model-00001-of-00009.safetensors"), - ONE_TENSOR_X, zeros, 4); + write_st(path_in("too_many_shards/model-00001-of-00009.safetensors"), ONE_TENSOR_X, zeros, 4); check("multi", path_in("too_many_shards"), 0); mkdir(path_in("bad_name"), 0755); - write_st(path_in("bad_name/model-foo.safetensors"), - ONE_TENSOR_X, zeros, 4); + write_st(path_in("bad_name/model-foo.safetensors"), ONE_TENSOR_X, zeros, 4); check("multi", path_in("bad_name"), 0); if (failures) { |
