#include "../src/kjv.h"
#include <stdio.h>
#include <inttypes.h> /* C99 */
#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. */
	size_t no_total, no_set = 0;
	struct kjvcount_table count = kjv_count(&no_total);
	struct kjvset_table set = kjv_set();
	union kjvcite verse = { .book = Genesis, .chapter = 1, .verse = 1 };
	fprintf(stderr, "%zu total words, %s.\n",
		no_total, kjv_count_to_string(&count));
	if(!no_total) goto catch;
	switch(kjv_set_add(&set, verse)) {
	case TABLE_ERROR: goto catch;
	case TABLE_PRESENT: assert(0); break;
	case TABLE_ABSENT: no_set += kjv_count_get(&count, verse); break;
	}
	verse.verse = 2;
	switch(kjv_set_add(&set, verse)) {
	case TABLE_ERROR: goto catch;
	case TABLE_PRESENT: assert(0); break;
	case TABLE_ABSENT: no_set += kjv_count_get(&count, verse); break;
	}
	verse.verse = 1;
	switch(kjv_set_add(&set, verse)) {
	case TABLE_ERROR: goto catch;
	case TABLE_PRESENT: break;
	case TABLE_ABSENT: assert(0); break;
	}
	fprintf(stderr, "%zu of which: %s.\n", no_set, kjv_set_to_string(&set));
	assert(no_total == 789633);
	assert(no_set == 39);
	goto finally;
catch:
	success = EXIT_FAILURE, perror("kjv");
finally:
	kjv_set_(&set);
	kjv_count_(&count);
	return success;
}
