government

commit 9d8c149e89e015823df22a42b5d70029e7e44e25

tree

parent:
da006f04d8870dcf444b3b0380729d78f076b2ea

nmyk <nick@nmyk.io>

2026-11-14T17:24:30-05:00

use cooler examples

diff --git a/cmd/government/examples_test.go b/cmd/government/examples_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..c543117fb9fb33eab9d7c895af14bbab949b6048
--- /dev/null
+++ b/cmd/government/examples_test.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+	"path/filepath"
+	"testing"
+)
+
+// TestExamples compiles and runs every program in examples/ and checks its
+// output, so the examples stay working programs rather than prose. A new
+// example needs its output listed here; a removed one needs nothing.
+func TestExamples(t *testing.T) {
+	want := map[string]string{
+		"quicksort": "(1 1 2 3 3 4 5 5 5 6 9)\n" +
+			"(-3.25 -1 0 2.5 7)\n",
+		"fibonacci": "6765\n6765\n7540113804746346429\n",
+		"hanoi": "((left right) (left middle) (right middle) (left right) (middle left) (middle right) (left right))\n" +
+			"1023\n",
+	}
+	files, err := filepath.Glob("../../examples/*.gov.lisp")
+	if err != nil {
+		t.Fatal(err)
+	}
+	dir := t.TempDir()
+	for _, file := range files {
+		name := filepath.Base(file)
+		name = name[:len(name)-len(".gov.lisp")]
+		if _, ok := want[name]; !ok {
+			t.Errorf("%s: no expected output; add it to this test", name)
+			continue
+		}
+		gov := filepath.Join(dir, name+".gov")
+		if code, _, stderr := runCmd("compile", file, "-o", gov); code != exitOK {
+			t.Errorf("%s: compile: %s", name, stderr)
+			continue
+		}
+		code, stdout, stderr := runCmd("run", gov)
+		if code != exitOK || stdout != want[name] {
+			t.Errorf("%s: exit %d\nstdout %q\nwant   %q\nstderr %s", name, code, stdout, want[name], stderr)
+		}
+	}
+}
diff --git a/cmd/government/main_test.go b/cmd/government/main_test.go
index 0095087d5c7215bde630ab7e821d231437dda9bd..e6c3a571c1e8e4aceaff815ff6f6fd05be2d2f19 100644
--- a/cmd/government/main_test.go
+++ b/cmd/government/main_test.go
@@ -6,6 +6,8 @@ 	"os"
 	"path/filepath"
 	"strings"
 	"testing"
+
+	"government/ratify"
 )
 
 func runCmd(args ...string) (code int, stdout, stderr string) {
@@ -14,13 +16,27 @@ 	code = run(args, &out, &errb)
 	return code, out.String(), errb.String()
 }
 
-func TestCompileExample(t *testing.T) {
+// sqSource is a small program the CLI tests write to a temp dir, so they
+// do not depend on which examples exist.
+const sqSource = "; sq.gov.lisp: the square of a number.\n(define sq (lambda (x) (* x x)))\n(sq 12)\n"
+
+func writeSource(t *testing.T, name, src string) string {
+	t.Helper()
+	path := filepath.Join(t.TempDir(), name+".gov.lisp")
+	if err := os.WriteFile(path, []byte(src), 0o644); err != nil {
+		t.Fatal(err)
+	}
+	return path
+}
+
+func TestCompileIsDeterministic(t *testing.T) {
+	src := writeSource(t, "sq", sqSource)
 	dir := t.TempDir()
 	a, b := filepath.Join(dir, "a.gov"), filepath.Join(dir, "b.gov")
-	if code, _, stderr := runCmd("compile", "../../examples/sq.gov.lisp", "-o", a); code != exitOK {
+	if code, _, stderr := runCmd("compile", src, "-o", a); code != exitOK {
 		t.Fatalf("exit %d: %s", code, stderr)
 	}
-	if code, _, stderr := runCmd("compile", "-o", b, "../../examples/sq.gov.lisp"); code != exitOK {
+	if code, _, stderr := runCmd("compile", "-o", b, src); code != exitOK {
 		t.Fatalf("exit %d: %s", code, stderr)
 	}
 	x, _ := os.ReadFile(a)
@@ -34,7 +50,7 @@ 	}
 }
 
 func TestCompileSourceFlag(t *testing.T) {
-	code, stdout, stderr := runCmd("compile", "-source", "../../examples/sq.gov.lisp")
+	code, stdout, stderr := runCmd("compile", "-source", writeSource(t, "sq", sqSource))
 	if code != exitOK {
 		t.Fatalf("exit %d: %s", code, stderr)
 	}
@@ -44,9 +60,17 @@ 	}
 }
 
 func TestCompileToStdout(t *testing.T) {
-	code, stdout, _ := runCmd("compile", "../../examples/sq.gov.lisp")
+	code, stdout, _ := runCmd("compile", writeSource(t, "sq", sqSource))
 	if code != exitOK || !strings.Contains(stdout, "-----BEGIN CERTIFICATE-----") {
 		t.Errorf("exit %d, output %.60q", code, stdout)
+	}
+}
+
+func TestCompileNamesProgramAfterFile(t *testing.T) {
+	code, stdout, _ := runCmd("compile", writeSource(t, "sq", sqSource))
+	certs, err := ratify.Load([]byte(stdout))
+	if code != exitOK || err != nil || certs[0].Subject.Organization[0] != "sq" {
+		t.Errorf("exit %d, %v; want O=sq", code, err)
 	}
 }
 
diff --git a/cmd/government/run_test.go b/cmd/government/run_test.go
index 7fbc92e9d5b2845f39b9dc4945e1f41c1c52114c..4514bef27a112b35eefb16a52785bd7dcc85c509 100644
--- a/cmd/government/run_test.go
+++ b/cmd/government/run_test.go
@@ -19,11 +19,8 @@ 	}
 	return gov
 }
 
-func TestRunExample(t *testing.T) {
-	gov := filepath.Join(t.TempDir(), "sq.gov")
-	if code, _, stderr := runCmd("compile", "../../examples/sq.gov.lisp", "-o", gov); code != exitOK {
-		t.Fatal(stderr)
-	}
+func TestRun(t *testing.T) {
+	gov := compileTo(t, t.TempDir(), "sq", sqSource)
 	code, stdout, stderr := runCmd("run", gov)
 	if code != exitOK || stdout != "144\n" {
 		t.Errorf("exit %d, stdout %q, stderr %q", code, stdout, stderr)
diff --git a/examples/fibonacci.gov.lisp b/examples/fibonacci.gov.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..3b728f13aedd7ba204f37658727b55c5e006884d
--- /dev/null
+++ b/examples/fibonacci.gov.lisp
@@ -0,0 +1,32 @@
+; fibonacci.gov.lisp: the Fibonacci numbers, two ways.
+
+; The definition read straight off: fib(n) = fib(n-1) + fib(n-2). Each call
+; makes two more, so the work doubles with every step of n.
+(define fib
+  (lambda (n)
+    (if (< n 2)
+        n
+        (+ (fib (- n 1)) (fib (- n 2))))))
+
+(fib 20)
+
+; Carrying the last two numbers along instead takes n steps: a and b are
+; fib(i-1) and fib(i). loop calls itself as its last act, a tail call, so
+; it runs in constant stack space. It is defined inside fast-fib's body,
+; where it can see n.
+(define fast-fib
+  (lambda (n)
+    (define loop
+      (lambda (i a b)
+        (if (== i n)
+            b
+            (loop (+ i 1) b (+ a b)))))
+    (if (== n 0)
+        0
+        (loop 1 0 1))))
+
+(fast-fib 20)
+
+; The largest Fibonacci number that fits in 64 bits. (fast-fib 93) would
+; overflow, which is an error rather than a wrapped-around number.
+(fast-fib 92)
diff --git a/examples/hanoi.gov.lisp b/examples/hanoi.gov.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..2e95d3173c0bf479f053fb57b8dfd715be33e527
--- /dev/null
+++ b/examples/hanoi.gov.lisp
@@ -0,0 +1,35 @@
+; hanoi.gov.lisp: the moves that solve the Towers of Hanoi.
+;
+; To move n disks from one peg to another, move the top n-1 out of the way
+; onto the spare peg, move the largest disk, then move the n-1 back on top
+; of it. The answer is data: a list of (from to) moves, built from quoted
+; symbols.
+
+(define append
+  (lambda (a b)
+    (if (== a '())
+        b
+        (cons (car a) (append (cdr a) b)))))
+
+(define move
+  (lambda (from to)
+    (cons from (cons to '()))))
+
+(define hanoi
+  (lambda (n from to spare)
+    (if (== n 0)
+        '()
+        (append (hanoi (- n 1) from spare to)
+                (cons (move from to)
+                      (hanoi (- n 1) spare to from))))))
+
+(hanoi 3 'left 'right 'middle)
+
+(define length
+  (lambda (l)
+    (if (== l '())
+        0
+        (+ 1 (length (cdr l))))))
+
+; n disks always take 2^n - 1 moves.
+(length (hanoi 10 'left 'right 'middle))
diff --git a/examples/quicksort.gov.lisp b/examples/quicksort.gov.lisp
new file mode 100644
index 0000000000000000000000000000000000000000..f81639fe6c95803b5dc66e77dc757c65b4f6baf2
--- /dev/null
+++ b/examples/quicksort.gov.lisp
@@ -0,0 +1,36 @@
+; quicksort.gov.lisp: sort a list of numbers.
+;
+; There are no loops and no mutation, so everything is recursion over
+; lists. Definitions are sequential, so each helper comes before the
+; functions that use it.
+
+(define append
+  (lambda (a b)
+    (if (== a '())
+        b
+        (cons (car a) (append (cdr a) b)))))
+
+; filter keeps the elements of l for which (keep x) is true.
+(define filter
+  (lambda (keep l)
+    (if (== l '())
+        '()
+        (if (keep (car l))
+            (cons (car l) (filter keep (cdr l)))
+            (filter keep (cdr l))))))
+
+; Partition around the first element, sort each side, and join them. The
+; inner lambda binds pivot and rest; the two lambdas passed to filter are
+; closures over pivot.
+(define quicksort
+  (lambda (l)
+    (if (== l '())
+        '()
+        ((lambda (pivot rest)
+           (append (quicksort (filter (lambda (x) (< x pivot)) rest))
+                   (cons pivot (quicksort (filter (lambda (x) (>= x pivot)) rest)))))
+         (car l)
+         (cdr l)))))
+
+(quicksort '(3 1 4 1 5 9 2 6 5 3 5))
+(quicksort '(2.5 -1 0 7 -3.25))
diff --git a/examples/sq.gov.lisp b/examples/sq.gov.lisp
deleted file mode 100644
index 3b277b5e6fe614ad4ebc799f7cc5a473f74220fc..0000000000000000000000000000000000000000
--- a/examples/sq.gov.lisp
+++ /dev/null
@@ -1,3 +0,0 @@
-; sq.gov.lisp: the square of a number.
-(define sq (lambda (x) (* x x)))
-(sq 12)
diff --git a/policy/strict_test.go b/policy/strict_test.go
index e34c6b3dcc9bf854851fc2a55b54be7db9967df8..a760c97de1ad02d1f4bd065d8bf3d9c268582921 100644
--- a/policy/strict_test.go
+++ b/policy/strict_test.go
@@ -62,7 +62,8 @@ }
 
 func TestTestdataDecodesToItsSource(t *testing.T) {
 	files, _ := filepath.Glob("../testdata/*.gov.lisp")
-	files = append(files, "../examples/sq.gov.lisp")
+	examples, _ := filepath.Glob("../examples/*.gov.lisp")
+	files = append(files, examples...)
 	for _, file := range files {
 		src, err := os.ReadFile(file)
 		if err != nil {
diff --git a/ratify/load_test.go b/ratify/load_test.go
index 303e8da75ef240768491b9088759c6941fec63ea..cf17380b31eb11f41394ad34cb88541243d9efcb 100644
--- a/ratify/load_test.go
+++ b/ratify/load_test.go
@@ -35,7 +35,8 @@ }
 
 func TestLoadCompiledPrograms(t *testing.T) {
 	files, _ := filepath.Glob("../testdata/*.gov.lisp")
-	files = append(files, "../examples/sq.gov.lisp")
+	examples, _ := filepath.Glob("../examples/*.gov.lisp")
+	files = append(files, examples...)
 	for _, file := range files {
 		src, err := os.ReadFile(file)
 		if err != nil {