#include "../src/scan.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

int main(void) {
	int success = EXIT_SUCCESS;
	errno = 0; /* `errno` is not set correctly to 0 in some debug situations. */
	struct journal jrnl = journal("../journal");
	struct scan scn = scan(&jrnl);
	if(errno) goto catch;

	const union line64 agenda = {{4,{{20,04,1996}}}},
		before = {{1,{{1,1,1900}}}},
		noentry = {{3,{{1,6,1995}}}};
	const struct source *source;

	source = scan_source_lookup(&scn, agenda);
	printf("agenda: <%.*s>\n",
		(int)(source->name.b - source->name.a), source->name.a);
	assert(pair_is_string(source->name, "1995agenda"));

	source = scan_source_lookup(&scn, before);
	printf("before: <%.*s>\n",
		(int)(source->name.b - source->name.a), source->name.a);
	assert(pair_is_string(source->name, 0));

	source = scan_source_lookup(&scn, noentry);
	printf("noentry: <%.*s>\n",
		(int)(source->name.b - source->name.a), source->name.a);
	assert(pair_is_string(source->name, 0));

	goto finally;
catch:
	success = EXIT_FAILURE, perror("source");
finally:
	scan_(&scn);
	journal_(&jrnl);
	return success;
}
