Files
sfile/tests/test_library.c
cnmcdee a2106045a6 json: --json output and libsfile linkable library
- sfile --json: one JSON object per file (JSON Lines) carrying format,
  offsets, decoded fields and geotags when present; default CLI
  output byte-identical (verified against the pre-refactor binary).
- libsfile: src/sfile.h + src/sfile_lib.c expose the decoder
  (sfile_decode_file/mem, sfile_result_to_json, format table
  introspection); make lib builds libsfile.a and libsfile.so.1.1.0,
  make install installs them plus the header.
- tests/test_json.py: JSON parses and matches the text table on the
  same files (make test-json); tests/test_library.c + test_lib.sh:
  static and shared link test (make test-lib).
2026-12-02 09:28:39 +00:00

95 lines
3.0 KiB
C

/* test_library.c — link test for libsfile.
*
* Decodes a file through the public API only (no CLI code) and prints
* the result in a canonical form that tests/test_lib.sh diffs against
* the `sfile` CLI's own output on the same file. If the library and
* the CLI disagree, the test fails.
*
* Usage: test_library [--mem] <file>
* --mem decodes from a memory buffer (sfile_decode_mem) instead of
* from disk (sfile_decode_file).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sfile.h"
static uint8_t *slurp(const char *path, long *n_out) {
FILE *fp = fopen(path, "rb");
if (!fp) return NULL;
fseek(fp, 0, SEEK_END);
long n = ftell(fp);
fseek(fp, 0, SEEK_SET);
uint8_t *buf = malloc(n > 0 ? (size_t)n : 1);
if (!buf) { fclose(fp); return NULL; }
size_t got = fread(buf, 1, (size_t)n, fp);
fclose(fp);
*n_out = (long)got;
return buf;
}
int main(int argc, char **argv) {
int use_mem = 0;
const char *path = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--mem") == 0) use_mem = 1;
else path = argv[i];
}
if (!path) {
fprintf(stderr, "usage: %s [--mem] <file>\n", argv[0]);
return 1;
}
printf("version: %s\n", SFILE_VERSION);
printf("formats: %d\n", sfile_format_count());
sfile_result r;
int st;
if (use_mem) {
long n = 0;
uint8_t *buf = slurp(path, &n);
if (!buf) { fprintf(stderr, "cannot read %s\n", path); return 2; }
st = sfile_decode_mem(buf, (size_t)n, path, n, &r);
free(buf);
} else {
st = sfile_decode_file(path, &r);
}
printf("status: %d\n", st);
if (!r.identified) {
printf("identified: no\n");
if (r.error) printf("error: %s\n", r.error);
sfile_free_result(&r);
return st;
}
printf("format: %s (%s) task %d by %s\n",
r.name, r.ext, r.task, r.identified_by);
for (int i = 0; i < r.nfields; i++)
printf("field: %s=%s\n", r.fields[i].name, r.fields[i].value);
if (r.doc_type == SFILE_DOC_XML) {
printf("doc: xml root=%s\n", r.root_element ? r.root_element : "");
if (r.version_attr) printf("doc: version=%s\n", r.version_attr);
} else if (r.doc_type == SFILE_DOC_JSON) {
printf("doc: json\n");
if (r.first_key) printf("doc: first_key=%s\n", r.first_key);
} else if (r.doc_type == SFILE_DOC_TEXT) {
printf("doc: text first_line=%s\n", r.first_line ? r.first_line : "");
}
if (r.ngeotags > 0) {
for (int i = 0; i < r.ngeotags; i++)
printf("geotag: %.6f %.6f (%s)\n",
r.geotags[i].lat, r.geotags[i].lon, r.geotags[i].source);
} else if (r.geotag_status == 0) {
printf("geotag: none found\n");
}
char *js = sfile_result_to_json(&r);
if (js) {
printf("json: %s\n", js);
free(js);
} else {
printf("json: (null)\n");
}
sfile_free_result(&r);
return st;
}