aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-21 05:54:26 +0000
committer_ov4 <ov4@ov4.org>2026-10-21 05:54:26 +0000
commitc56877035d6600b463a9cbf0e339f7548c0b46fd (patch)
tree5b2af13b7f8f097d8de5436a729b39e85427b9f0 /tests
parentdd5bbae055fd34ce2e12ef63f8a261c6b15dc83e (diff)
feat(codegen): instantiate function templates by deduction
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/i/template-functions/case.json1
-rw-r--r--tests/i/template-functions/expected.txt9
-rw-r--r--tests/i/template-functions/notes.md5
-rw-r--r--tests/i/template-functions/program.cps46
-rw-r--r--tests/i/template-multifile/case.json1
-rw-r--r--tests/i/template-multifile/expected.txt1
-rw-r--r--tests/i/template-multifile/lib.cps7
-rw-r--r--tests/i/template-multifile/notes.md5
-rw-r--r--tests/i/template-multifile/program.cps14
-rw-r--r--tests/iii/template-deduction-errors/case.json1
-rw-r--r--tests/iii/template-deduction-errors/program.cps10
-rw-r--r--tests/iii/template-main-error/case.json1
-rw-r--r--tests/iii/template-main-error/program.cps4
-rw-r--r--tests/iii/template-overload-errors/case.json1
-rw-r--r--tests/iii/template-overload-errors/program.cps8
15 files changed, 114 insertions, 0 deletions
diff --git a/tests/i/template-functions/case.json b/tests/i/template-functions/case.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/tests/i/template-functions/case.json
@@ -0,0 +1 @@
+{}
diff --git a/tests/i/template-functions/expected.txt b/tests/i/template-functions/expected.txt
new file mode 100644
index 0000000..0f27430
--- /dev/null
+++ b/tests/i/template-functions/expected.txt
@@ -0,0 +1,9 @@
+7
+2.500000
+pear
+3
+42
+9
+4
+200
+100
diff --git a/tests/i/template-functions/notes.md b/tests/i/template-functions/notes.md
new file mode 100644
index 0000000..eb4bffa
--- /dev/null
+++ b/tests/i/template-functions/notes.md
@@ -0,0 +1,5 @@
+# template-functions
+
+Human review: pending. Expected output was independently traced by AI, not captured from cpsc.
+
+Basis: TEMPLATE is a project extension. Max selects the larger INTEGER, REAL and STRING value; the nested call returns 3. First returns array element 1, which is 42. BYREF Swap exchanges 4 and 9. Pick selects the exact ordinary INTEGER overload (200), then the template for REAL (100).
diff --git a/tests/i/template-functions/program.cps b/tests/i/template-functions/program.cps
new file mode 100644
index 0000000..40cceac
--- /dev/null
+++ b/tests/i/template-functions/program.cps
@@ -0,0 +1,46 @@
+TEMPLATE <T>
+FUNCTION Max(A : T, B : T) RETURNS T
+ IF A > B THEN
+ RETURN A
+ ENDIF
+ RETURN B
+ENDFUNCTION
+
+TEMPLATE <T>
+FUNCTION First(Values : ARRAY OF T) RETURNS T
+ RETURN Values[1]
+ENDFUNCTION
+
+TEMPLATE <T>
+PROCEDURE Swap(BYREF Left : T, Right : T)
+ DECLARE Temp : T
+ Temp <- Left
+ Left <- Right
+ Right <- Temp
+ENDPROCEDURE
+
+TEMPLATE <T>
+OVERLOAD FUNCTION Pick(Value : T) RETURNS INTEGER
+ RETURN 100
+ENDFUNCTION
+OVERLOAD FUNCTION Pick(Value : INTEGER) RETURNS INTEGER
+ RETURN 200
+ENDFUNCTION
+
+OUTPUT Max(3, 7)
+OUTPUT Max(1.5, 2.5)
+OUTPUT Max("apple", "pear")
+OUTPUT Max(Max(1, 2), 3)
+DECLARE Values : ARRAY[1:2] OF INTEGER
+Values[1] <- 42
+Values[2] <- 99
+OUTPUT First(Values)
+DECLARE Left : INTEGER
+DECLARE Right : INTEGER
+Left <- 4
+Right <- 9
+CALL Swap(Left, Right)
+OUTPUT Left
+OUTPUT Right
+OUTPUT Pick(1)
+OUTPUT Pick(1.5)
diff --git a/tests/i/template-multifile/case.json b/tests/i/template-multifile/case.json
new file mode 100644
index 0000000..3415f39
--- /dev/null
+++ b/tests/i/template-multifile/case.json
@@ -0,0 +1 @@
+{"sources": ["program.cps", "lib.cps"]}
diff --git a/tests/i/template-multifile/expected.txt b/tests/i/template-multifile/expected.txt
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/tests/i/template-multifile/expected.txt
@@ -0,0 +1 @@
+9
diff --git a/tests/i/template-multifile/lib.cps b/tests/i/template-multifile/lib.cps
new file mode 100644
index 0000000..30ea7c4
--- /dev/null
+++ b/tests/i/template-multifile/lib.cps
@@ -0,0 +1,7 @@
+TEMPLATE <T>
+FUNCTION Max(A : T, B : T) RETURNS T
+ IF A > B THEN
+ RETURN A
+ ENDIF
+ RETURN B
+ENDFUNCTION
diff --git a/tests/i/template-multifile/notes.md b/tests/i/template-multifile/notes.md
new file mode 100644
index 0000000..24dbfe5
--- /dev/null
+++ b/tests/i/template-multifile/notes.md
@@ -0,0 +1,5 @@
+# template-multifile
+
+Human review: pending. Expected output was independently traced by AI, not captured from cpsc.
+
+Basis: TEMPLATE is a project extension. The library template deduces the requester-defined Score record. Its comparison sees the requester's OPERATOR>, selects the record with Value 9, and returns a record copy. This exercises requester visibility during a body emitted with its library owner's context.
diff --git a/tests/i/template-multifile/program.cps b/tests/i/template-multifile/program.cps
new file mode 100644
index 0000000..dd0684f
--- /dev/null
+++ b/tests/i/template-multifile/program.cps
@@ -0,0 +1,14 @@
+INCLUDE "lib.cps"
+TYPE Score
+ DECLARE Value : INTEGER
+ENDTYPE
+OVERLOAD FUNCTION OPERATOR>(Left : Score, Right : Score) RETURNS BOOLEAN
+ RETURN Left.Value > Right.Value
+ENDFUNCTION
+DECLARE Low : Score
+DECLARE High : Score
+Low.Value <- 3
+High.Value <- 9
+DECLARE Best : Score
+Best <- Max(Low, High)
+OUTPUT Best.Value
diff --git a/tests/iii/template-deduction-errors/case.json b/tests/iii/template-deduction-errors/case.json
new file mode 100644
index 0000000..f60833d
--- /dev/null
+++ b/tests/iii/template-deduction-errors/case.json
@@ -0,0 +1 @@
+{"errors": [{"reason": "No matching overload for Max", "line": 9}, {"reason": "No matching overload for Undeducible", "line": 10}]}
diff --git a/tests/iii/template-deduction-errors/program.cps b/tests/iii/template-deduction-errors/program.cps
new file mode 100644
index 0000000..7ec1584
--- /dev/null
+++ b/tests/iii/template-deduction-errors/program.cps
@@ -0,0 +1,10 @@
+TEMPLATE <T>
+FUNCTION Max(A : T, B : T) RETURNS T
+ RETURN A
+ENDFUNCTION
+TEMPLATE <T>
+FUNCTION Undeducible() RETURNS T
+ RETURN 0
+ENDFUNCTION
+OUTPUT Max(1, 2.5)
+OUTPUT Undeducible()
diff --git a/tests/iii/template-main-error/case.json b/tests/iii/template-main-error/case.json
new file mode 100644
index 0000000..7be7268
--- /dev/null
+++ b/tests/iii/template-main-error/case.json
@@ -0,0 +1 @@
+{"errors": [{"reason": "MAIN cannot be a template", "line": 1}]}
diff --git a/tests/iii/template-main-error/program.cps b/tests/iii/template-main-error/program.cps
new file mode 100644
index 0000000..98f218d
--- /dev/null
+++ b/tests/iii/template-main-error/program.cps
@@ -0,0 +1,4 @@
+TEMPLATE <T>
+FUNCTION MAIN() RETURNS INTEGER
+ RETURN 0
+ENDFUNCTION
diff --git a/tests/iii/template-overload-errors/case.json b/tests/iii/template-overload-errors/case.json
new file mode 100644
index 0000000..b3259c8
--- /dev/null
+++ b/tests/iii/template-overload-errors/case.json
@@ -0,0 +1 @@
+{"errors": [{"reason": "every overload must be declared OVERLOAD", "line": 1}]}
diff --git a/tests/iii/template-overload-errors/program.cps b/tests/iii/template-overload-errors/program.cps
new file mode 100644
index 0000000..9b60f76
--- /dev/null
+++ b/tests/iii/template-overload-errors/program.cps
@@ -0,0 +1,8 @@
+TEMPLATE <T>
+FUNCTION Pick(Value : T) RETURNS T
+ RETURN Value
+ENDFUNCTION
+OVERLOAD FUNCTION Pick(Value : INTEGER) RETURNS INTEGER
+ RETURN Value
+ENDFUNCTION
+OUTPUT Pick(1)