--- /dev/null
+/* -*-c-*-
+ *
+ * Demonstration of ad-doc testing
+ *
+ * (c) 2024 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * mLib is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib. If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <limits.h>
+#include <stdio.h>
+
+#include "macros.h"
+#include "report.h"
+#include "tvec.h"
+#include "tvec-adhoc.h"
+#include "tvec-bench.h"
+#include "tvec-types.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* Stupid but traditional recursive Fibonacci. */
+static unsigned long recfib(unsigned n)
+ { return (n <= 1 ? n : recfib(n - 1) + recfib(n - 2)); }
+
+/* Slightly less stupid but still traditional iterative Fibonacci. */
+static unsigned long iterfib(unsigned n)
+{
+ unsigned long u, v, t;
+
+ for (u = 0, v = 1; n--; t = v, v = u, u += t);
+ return (u);
+}
+
+/* Sadly nontraditional intelligent Fibonacci. */
+static unsigned long expfib(unsigned n)
+{
+ unsigned long a, b, u, v, t;
+
+ /* We work in %$\Q(\phi)$%, where %$\phi^2 = \phi + 1$%. I claim that
+ * %$\phi^k = F_k \phi + F_{k-1} \pmod f(\phi))$%. Proof by induction:
+ * note that * %$F_{-1} = F_1 - F_0 = 1$%, so %$\phi^0 = 1 = {}$%
+ * %$F_0 \phi + F_{-1}$%; and %$\phi^{k+1} = F_k \phi^2 + {}$%
+ * %$F_{k-1} \phi = F_k (\phi + 1) + F_{k-1} \phi = (F_k + {}$%
+ * %$F_{k-1} \phi + F_k = F_{k+1} \phi + F_k$% as claimed.
+ *
+ * Now, notice that %$(a \phi + b) (c \phi + d) = a c \phi^2 + {}$%
+ * $%(a d + b c) \phi + b d = a c (\phi + 1) + (a d + b c) \phi + {}$%
+ * %$b d = (a c + a d + b c) \phi + (a c + b d)$%. In particular,
+ * %$(u \phi + v)^2 \equiv (u^2 + 2 u v) \phi + (u^2 + v^2)$%.
+ */
+ a = 0, b = 1; u = 1, v = 0;
+ if (n)
+ for (;;) {
+ if (n%2) { t = a*u; a = t + a*v + b*u; b = t + b*v; }
+ n /= 2; if (!n) break;
+ t = u*u; u = t + 2*u*v; v = t + v*v;
+ }
+ return (a);
+}
+
+int main(int argc, char *argv[])
+{
+ struct tvec_state tvstate;
+ int argpos;
+ unsigned long i, a, b, t;
+
+ tvec_parseargs(argc, argv, &tvstate, &argpos, &tvec_adhocconfig);
+ if (argpos < argc) die(2, "no input files expected");
+ tvec_adhoc(&tvstate);
+
+#if (ULONG_MAX/65536 >> 16) >= 0xffffffff
+# define FIBLIMIT 94 /* F_94 = 19740274219868223167 > 2^64 */
+#else
+# define FIBLIMIT 48 /* F_48 = 4807526976 > 2^32 */
+#endif
+
+ TVEC_TESTGROUP(&tvstate, "fib-test") {
+ for (i = 0, a = 0, b = 1; i < FIBLIMIT; i++, t = b, b = a, a += t)
+ TVEC_TEST(&tvstate) {
+ if (i < 40) TVEC_CLAIMEQ_UINT(&tvstate, a, recfib(i));
+ TVEC_CLAIMEQ_UINT(&tvstate, a, iterfib(i));
+ TVEC_CLAIMEQ_UINT(&tvstate, a, expfib(i));
+ }
+ }
+
+ TVEC_TESTGROUP(&tvstate, "fib-bench") {
+ TVEC_BENCHMARK_DECLS;
+
+ if (tvec_benchprep(&tvstate, &tvec_benchstate, 0)) break;
+
+ TVEC_BENCHMARK("recfib, n = 40", &tvstate, tvec_benchstate, BTU_OP, 1)
+ while (_bench_n--) ADMIRE(recfib(40));
+ TVEC_BENCHMARK("iterfib, n = " STR(FIBLIMIT),
+ &tvstate, tvec_benchstate, BTU_OP, 1)
+ while (_bench_n--) ADMIRE(iterfib(FIBLIMIT));
+ TVEC_BENCHMARK("expfib, n = " STR(FIBLIMIT),
+ &tvstate, tvec_benchstate, BTU_OP, 1)
+ while (_bench_n--) ADMIRE(expfib(FIBLIMIT));
+ }
+
+ return (tvec_end(&tvstate));
+}
+
+/*----- That's all, folks -------------------------------------------------*/