From 8877627ee3fcd5a8f0ce2fcccec7688973e9cb2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= <john@ankarstrom.se>
Date: Mon, 15 Aug 2022 22:22:20 +0200
Subject: Add testing system.

---
 Makefile   |  2 +-
 c/main.cpp |  5 +++++
 c/test.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 c/test.h   | 13 +++++++++++++
 4 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 c/test.cpp
 create mode 100644 c/test.h

diff --git a/Makefile b/Makefile
index 9873d7a..01e08af 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 EXE = EpisodeBrowser.exe
 OBJ = b/datalistview.obj b/debug.obj b/episodelistview.obj b/wcharptr.obj b/win.obj
-OBJ += b/layout.obj b/listview.obj b/pl.obj b/resource.obj
+OBJ += b/layout.obj b/listview.obj b/pl.obj b/test.obj b/resource.obj
 PL = pl/cfg.pl pl/episode_data.pl pl/local_episodes.pl pl/track_episodes.pl
 
 CL = swipl-ld
diff --git a/c/main.cpp b/c/main.cpp
index fb5764b..74c99ca 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -10,6 +10,7 @@
 #include "episodelistview.h"
 #include "layout.h"
 #include "pl.h"
+#include "test.h"
 #include "util.h"
 
 #ifdef DEBUG
@@ -161,6 +162,10 @@ int WINAPI WinMain(const HINSTANCE hInstance, const HINSTANCE, char* const, cons
 	g_elv->Update();
 	g_elv->RestoreFocus();
 
+#ifdef DEBUG
+	RunTests();
+#endif
+
 	MSG msg;
 	while (GetMessage(&msg, nullptr, 0, 0) > 0) {
 		if (IsDialogMessage(hWnd, &msg)) continue;
diff --git a/c/test.cpp b/c/test.cpp
new file mode 100644
index 0000000..9230974
--- /dev/null
+++ b/c/test.cpp
@@ -0,0 +1,50 @@
+#include "data.h"
+#include "test.h"
+#include "util.h"
+#include "win.h"
+
+Test::Test(const char* name_) : name(name_) {}
+
+#define CAT(a, b) a##b
+#define APPLY(a, ...) a(__VA_ARGS__)
+#define TESTS struct APPLY(CAT, tests_, __COUNTER__)
+#define TEST(id) }; struct id : public Test { id() : Test(#id)
+#define FAIL(...) do { Sprintf(error, __VA_ARGS__); return; } while (0)
+
+TESTS
+{
+	TEST(StrcpyWithSmallerDestination)
+	{
+		char src[15], dst[10];
+		Sprintf(src, "abcdefghijklmn");
+		Strcpy(dst, src);
+		if (dst[8] != 'i')
+			FAIL("dst[8] is not 'i', but '%c'", dst[8]);
+		if (dst[9] != 0)
+			FAIL("dst is not NUL-terminated");
+	}
+};
+
+int RunTests()
+{
+	const Test tests[] = {
+		StrcpyWithSmallerDestination{},
+	};
+
+	printf("Results (%llu tests):\n", sizeof(tests)/sizeof(*tests));
+
+	int cFailed = 0;
+	for (const Test& t : tests)
+	{
+		const char* msg;
+		if (t.error[0]) {
+			msg = t.error;
+			cFailed++;
+		} else
+			msg = const_cast<char*>("SUCCESS");
+		printf("%s: %s\n", t.name, msg);
+	}
+	printf("%d tests failed.\n", cFailed);
+
+	return cFailed;
+}
diff --git a/c/test.h b/c/test.h
new file mode 100644
index 0000000..5565b36
--- /dev/null
+++ b/c/test.h
@@ -0,0 +1,13 @@
+#ifndef TEST_H
+#define TEST_H
+
+struct Test
+{
+	const char* name = {0};
+	char error[64] = {0};
+	Test(const char* name);
+};
+
+int RunTests();
+
+#endif
-- 
cgit v1.2.3