~ knotless Search/
↑↓ move↵ open⇧↵ all results

spaces.cameron.stream/hello

Hello.

0
1
main
430 lines · 13.0 KB Raw History
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "census.h"
#include "fate.h"
#include "greeting.h"
#include "history.h"
#include "post.h"
#include "records.h"
#include "town.h"
#include "weather.h"

static int checks, failures;

#define CHECK(cond)                                                     \
	do {                                                            \
		checks++;                                               \
		if (!(cond)) {                                          \
			failures++;                                     \
			printf("%s:%d: %s\n", __FILE__, __LINE__, #cond); \
		}                                                       \
	} while (0)

#define CHECK_STR(a, b) CHECK(strcmp((a), (b)) == 0)

static const struct day census = {2026, 10, 1};

static const char *on(int y, int m, int d)
{
	static char text[64];
	return greeting_on((struct day){y, m, d}, text, sizeof text);
}

static const struct letter *find(const struct letter *letters, int n, const char *name)
{
	for (int i = 0; i < n; i++)
		if (strcmp(letters[i].sender.name, name) == 0)
			return &letters[i];
	return NULL;
}

static struct household *line(struct town *town, const char *name)
{
	for (int i = 0; i < town->lines; i++)
		if (strcmp(town->line[i].name, name) == 0)
			return &town->line[i];
	return NULL;
}

static void test_greeting(void)
{
	CHECK(same_day(census_day(), census));
	CHECK_STR(greeting(), "Hello, World!");
}

static void test_only_once_in_three_hundred_years(void)
{
	int hellos = 0, on_census = 0;
	for (int month = month_of((struct day){1900, 1, 1}); month < month_of((struct day){2201, 1, 1}); month++) {
		struct day day = month_day(month);
		if (strcmp(on(day.y, day.m, day.d), "Hello, World!") == 0) {
			hellos++;
			on_census = same_day(day, census);
		}
	}
	CHECK(hellos == 1);
	CHECK(on_census);
}

static void test_census_is_what_the_history_says(void)
{
	FILE *taken = tmpfile();
	take_census(taken, census);
	long size = ftell(taken);
	rewind(taken);
	char *now = calloc((size_t)size + 1, 1);
	CHECK(fread(now, 1, (size_t)size, taken) == (size_t)size);
	fclose(taken);

	FILE *kept = fopen(RECORDS "/census.txt", "r");
	CHECK(kept != NULL);
	char *then = calloc((size_t)size + 2, 1);
	size_t read = kept ? fread(then, 1, (size_t)size + 1, kept) : 0;
	if (kept)
		fclose(kept);
	CHECK(read == (size_t)size);
	CHECK_STR(now, then); /* Run make census if the history has changed. */
	free(now);
	free(then);
}

static void test_fate_is_fixed(void)
{
	CHECK(fate(42, 1520, 3, 24) == fate(42, 1520, 3, 24));
	CHECK(fate(42, 1520, 3, 1) == 0);
}

static void test_the_history_has_generations(void)
{
	struct town town = *town_on(census);
	CHECK(town.lines == 15);
	const struct household *edith = line(&town, "Edith");
	CHECK(edith != NULL);
	if (edith)
		CHECK(edith->generation == 3); /* The fourth of her line. */
	const struct household *church = line(&town, CHURCH);
	CHECK(church != NULL);
	if (church)
		CHECK(church->kind == BELL && fast(church, census) == 0);
}

static void test_clocks_drift_from_the_bell(void)
{
	struct household h = {.set = {2026, 9, 1}, .gains = 4};
	CHECK(fast(&h, census) == 2);
	h.gains = -1;
	CHECK(fast(&h, census) == 0); /* 30 seconds is not a minute. */
	h.gains = -2;
	CHECK(fast(&h, census) == -1);
	CHECK(real_time(&h, clock_time("07:00"), census) == clock_time("07:01"));
}

static void test_three_misses_and_you_get_up_earlier(void)
{
	struct household h = {.kind = INSTITUTION, .name = "Night Office", .home = "Mill", .sends = "?",
			      .weekdays = clock_time("10:00"), .weekends = clock_time("10:00"), .set = census};
	int month = month_of(census);
	live(&h, month);
	live(&h, month + 1);
	CHECK(h.weekdays == clock_time("10:00"));
	live(&h, month + 2);
	CHECK(h.weekdays == clock_time("09:50"));
	CHECK(h.misses == 0);
}

static void test_distance_is_shortest_walk(void)
{
	CHECK(distance(census, "Orchard", "Market") == 450);
	CHECK(distance(census, "Market", "Orchard") == 450);
	CHECK(distance(census, "Mill", "Green") == 700); /* Long Row and Church Lane, not by the quay. */
}

static void test_the_ferry_ran_until_the_bridge_opened(void)
{
	CHECK(distance((struct day){1910, 1, 1}, "Quay", "Well") == 400);   /* By the ferry. */
	CHECK(distance((struct day){1923, 4, 30}, "Quay", "Well") == 400);
	CHECK(distance((struct day){1923, 5, 1}, "Quay", "Well") == 500);   /* By the market and the bridge. */
	CHECK(distance((struct day){1920, 1, 1}, "Mill", "Green") == 900);  /* Before Long Row. */
	CHECK(distance((struct day){1931, 6, 1}, "Mill", "Green") == 700);
}

static void test_there_was_no_exclamation_before_the_fire_station(void)
{
	CHECK(strchr(on(1912, 2, 1), '!') == NULL);
	int heard = 0;
	for (int y = 1913; y < 2027; y++)
		heard += strchr(on(y, 3, 1), '!') != NULL;
	CHECK(heard > 0);
}

static void test_nearest_box_can_be_across_the_river(void)
{
	const char *const boxes[] = {"Green", "Hill", "Market", "Quay"};
	CHECK(north("Orchard"));
	CHECK(!north("Market"));
	CHECK_STR(nearest(census, "Orchard", boxes, 4), "Market");
}

static void test_north_writes_capitals(void)
{
	struct sender people[MAX_SENDERS];
	int n = senders(census, people);
	for (int i = 0; i < n; i++) {
		if (strcmp(people[i].name, "Hazel") == 0)
			CHECK_STR(people[i].body, "H");
		if (strcmp(people[i].name, "Walter") == 0)
			CHECK_STR(people[i].body, "W");
		if (strcmp(people[i].name, "Edith") == 0)
			CHECK_STR(people[i].body, "e");
	}
}

static void test_census_day_was_foggy(void)
{
	struct conditions c = conditions(census);
	CHECK_STR(c.weather, "fog");
	CHECK(c.walk == 80);
	CHECK(c.van == 200);
}

static void test_rounds_on_the_census_day(void)
{
	struct visit v[MAX_VISITS];
	int n = visits(census, v);
	int seen = 0;
	for (int i = 0; i < n; i++) {
		if (!strcmp(v[i].round, "Early") && !strcmp(v[i].box, "Hill")) {
			CHECK(v[i].at == clock_time("06:51") && v[i].back == clock_time("06:58"));
			seen++;
		}
		if (!strcmp(v[i].round, "Mid-morning") && !strcmp(v[i].box, "Market")) {
			CHECK(v[i].at == clock_time("07:58") && v[i].back == clock_time("08:07"));
			seen++;
		}
		if (!strcmp(v[i].round, "Mid-morning") && !strcmp(v[i].box, "Green")) {
			CHECK(v[i].at == clock_time("08:01") && v[i].back == clock_time("08:07"));
			seen++;
		}
	}
	CHECK(seen == 3);
}

static void test_one_van(void)
{
	struct visit v[MAX_VISITS];
	int n = visits(census, v);
	for (int i = 0; i < n; i++)
		if (!strcmp(v[i].round, "Late"))
			CHECK(v[i].left == clock_time("08:07")); /* Due at 08:00. Back from Mid-morning at 08:07. */
}

static void test_last_bag_in_the_van_is_opened_first(void)
{
	struct letter letters[MAX_SENDERS];
	int n = dead_letters(census, letters);
	char order[64] = "";
	for (int i = 0; i < n; i++)
		if (!strcmp(letters[i].visit.round, "Mid-morning"))
			strcat(order, letters[i].visit.box[0] == 'G' ? "G" : "M");
	CHECK_STR(order, "GGMMMMM");
}

static void test_last_posted_is_opened_first(void)
{
	struct letter letters[MAX_SENDERS];
	int n = dead_letters(census, letters);
	char bag[128] = "";
	for (int i = 0; i < n; i++) {
		if (!strcmp(letters[i].visit.round, "Morning")) {
			strcat(bag, letters[i].sender.name);
			strcat(bag, " ");
		}
	}
	CHECK_STR(bag, "Edith Lionel Lottie Oscar ");
}

static void test_walter_catches_the_van_by_no_minutes(void)
{
	struct letter letters[MAX_SENDERS];
	int n = dead_letters(census, letters);
	const struct letter *walter = find(letters, n, "Walter");
	CHECK(walter != NULL);
	if (walter)
		CHECK(walter->posted == walter->visit.at && walter->posted == clock_time("07:58"));
}

static void test_walter_is_on_time_because_of_his_clock(void)
{
	struct town honest = *town_on(census);
	struct household *walter = line(&honest, "Walter");
	CHECK(walter != NULL);
	if (!walter)
		return;
	CHECK(fast(walter, census) == 66);
	walter->set = census; /* As if he had been to church that morning. */
	walter->adjusted = 0;

	struct letter letters[MAX_SENDERS];
	int n = letters_from(&honest, census, letters);
	char text[64] = "";
	for (int i = 0; i < n; i++)
		strcat(text, letters[i].sender.body);
	CHECK_STR(text, "Hello, orld!"); /* Late, so the van has gone. */
}

static void test_the_exclamation_mark_is_there_because_the_van_was_late(void)
{
	struct letter letters[MAX_SENDERS];
	int n = dead_letters(census, letters);
	const struct letter *bang = find(letters, n, "Fire Station");
	CHECK(bang != NULL);
	if (!bang)
		return;
	CHECK(bang->posted == clock_time("08:12"));
	CHECK(bang->visit.at == clock_time("08:17"));
	/* Leaving at 08:00 the van would have been at Green by 08:10. */
	CHECK(clock_time("08:00") + 5 + STOP + 4 < bang->posted);
}

static void test_gwen_keeps_the_dead_letter_office(void)
{
	struct town town = *town_on(census);
	const struct household *gwen = line(&town, "Gwen");
	CHECK(gwen != NULL);
	if (gwen)
		CHECK(gwen->member && strcmp(gwen->line, "G1") == 0);
	struct sender people[MAX_SENDERS];
	int n = senders(census, people);
	for (int i = 0; i < n; i++)
		CHECK(strcmp(people[i].name, "Gwen") != 0); /* She reads. She does not write. */
	struct letter letters[MAX_SENDERS];
	n = dead_letters(census, letters);
	CHECK(find(letters, n, "Gwen") == NULL);
}

static void test_gwen_would_be_heard_if_she_lived_at_the_mill(void)
{
	struct sender gwen = {"Gwen", "Mill", 0, "g"};
	struct letter letter;
	gwen.ready = clock_time("08:00");
	CHECK(post(&gwen, census, &letter));
	CHECK_STR(letter.visit.round, "Late");
	CHECK_STR(letter.visit.box, "Quay");
	CHECK(letter.visit.back == clock_time("08:23")); /* Under the "!", so the greeting would end in "g". */
}

static void test_without_the_society(void)
{
	const struct act *acts;
	int n = society(&acts);
	struct act kept[MAX_ACTS];
	memcpy(kept, acts, sizeof kept[0] * (size_t)n);

	set_society(kept, 0);
	struct town alone;
	found(&alone);
	while (alone.month < month_of(census))
		advance(&alone);
	struct letter letters[MAX_SENDERS];
	int count = letters_from(&alone, census, letters);
	char text[64] = "";
	for (int i = 0; i < count; i++)
		strcat(text, letters[i].sender.body);
	CHECK_STR(text, "Ll!w o,Rdheo");
	set_society(kept, n);
}

static void test_the_society_is_careful(void)
{
	const struct act *acts;
	int n = society(&acts);
	CHECK(n > 0);
	for (int i = 0; i < n; i++) {
		CHECK(acts[i].month < month_of(census)); /* All of it done before the day. */
		if (acts[i].deed == CLOCKS)
			CHECK(abs(acts[i].minutes) <= 2); /* Nobody would notice. */
		if (acts[i].deed == WORDS)
			CHECK(acts[i].minutes % 5 == 0 && abs(acts[i].minutes) <= 15); /* A few minutes at a time. */
	}
}

static int unreached;

static void listen(struct day day, const struct household *h, const char *what)
{
	(void)day;
	(void)h;
	unreached += strstr(what, "could not reach") != NULL;
}

static void test_the_keeper_could_get_to_everyone(void)
{
	struct town town;
	found(&town);
	chronicle = listen;
	while (town.month < month_of(census))
		advance(&town);
	chronicle = NULL;
	CHECK(unreached == 0);

	struct household near = {.kind = RESIDENT, .line = "X1", .name = "Near", .home = "Market", .weekdays = 360};
	struct household far = {.kind = RESIDENT, .line = "X2", .name = "Far", .home = "Hill", .weekdays = 360};
	struct household keeper = {.kind = RESIDENT, .line = "X3", .name = "Keeper", .home = "Green", .member = 1};
	struct town three = {.month = month_of(census), .lines = 3, .line = {near, far, keeper}};
	struct act asked[] = {
		{.month = month_of(census), .line = "X1", .deed = WORDS, .minutes = 5},
		{.month = month_of(census), .line = "X2", .deed = WORDS, .minutes = 5},
	};
	const struct act *kept;
	int n = society(&kept);
	struct act saved[MAX_ACTS];
	memcpy(saved, kept, sizeof saved[0] * (size_t)n);
	set_society(asked, 2);
	carry_out(&three, month_of(census));
	set_society(saved, n);
	CHECK(three.line[0].weekdays == clock_time("06:05")); /* 300 metres from the Green. */
	CHECK(three.line[1].weekdays == clock_time("06:00")); /* 900 metres: too far to walk. */
}

static void test_other_days(void)
{
	CHECK_STR(on(2026, 9, 1), "Holl,eWodr "); /* The month before. */
	CHECK_STR(on(2026, 10, 2), "");            /* Not the first. */
	CHECK_STR(on(2026, 11, 1), "");            /* Sunday. */
	CHECK_STR(on(1900, 1, 1), "lwooRL");       /* The founding. */
	CHECK_STR(on(2100, 12, 1), "Hldl, l!OwOr");
}

int main(void)
{
	test_greeting();
	test_only_once_in_three_hundred_years();
	test_census_is_what_the_history_says();
	test_fate_is_fixed();
	test_the_history_has_generations();
	test_clocks_drift_from_the_bell();
	test_three_misses_and_you_get_up_earlier();
	test_distance_is_shortest_walk();
	test_the_ferry_ran_until_the_bridge_opened();
	test_there_was_no_exclamation_before_the_fire_station();
	test_nearest_box_can_be_across_the_river();
	test_north_writes_capitals();
	test_census_day_was_foggy();
	test_rounds_on_the_census_day();
	test_one_van();
	test_last_bag_in_the_van_is_opened_first();
	test_last_posted_is_opened_first();
	test_walter_catches_the_van_by_no_minutes();
	test_walter_is_on_time_because_of_his_clock();
	test_the_exclamation_mark_is_there_because_the_van_was_late();
	test_gwen_keeps_the_dead_letter_office();
	test_gwen_would_be_heard_if_she_lived_at_the_mill();
	test_without_the_society();
	test_the_society_is_careful();
	test_the_keeper_could_get_to_everyone();
	test_other_days();
	printf("%d checks, %d failed\n", checks, failures);
	return failures != 0;
}