Debian Codegraph @

texmf-dist/tex/lualatex/luacas/test/luacas-main.lua

raw
1 ---@diagnostic disable: lowercase-global
2 -- Runs test code from test files.
3
4 require("_lib.luacas-pepperfish")
5 require("_lib.luacas-table")
6
7 local luacas = require("luacas_init")
8 luacas:initmodule("calculus")
9
10 for name, class in pairs(luacas.core) do
11 _G[name] = class
12 end
13
14 for name, class in pairs(luacas.algebra) do
15 _G[name] = class
16 end
17
18 for name, class in pairs(luacas.calculus) do
19 _G[name] = class
20 end
21
22 -- Stuff required for the basic parser.
23 local constants = {e="E", pi = "PI", ln = "LN", log = "LOG", Integer = "Integer", DD = "DD", int = "INT", abs = "ABS", fact="FACT"}
24
25 local function parser(s)
26 if string.find(s, "[0-9]+") then
27 return "Integer(\"" .. s .. "\")"
28 end
29
30 if s.find(s, "[%^%\\%[%]]") then
31 return string.gsub(s, "[^%^%\\%[%]]+", parser)
32 end
33
34 for string, replace in pairs(constants) do
35 if s == string then
36 return replace
37 end
38 end
39
40 return "SymbolExpression(\"" .. s .. "\")"
41 end
42
43 function parse(input)
44 local parsed = string.gsub(input, "[0-9]+", parser)
45 parsed = string.gsub(parsed, "[A-z']+", parser)
46 local exe, err = load("return " .. parsed)
47 if exe then
48 return exe():autosimplify()
49 else
50 print(err)
51 end
52 end
53
54 function dparse(input)
55 local parsed = string.gsub(input, "[0-9]+", parser)
56 parsed = string.gsub(parsed, "[A-z']+", parser)
57 local exe, err = load("return " .. parsed)
58 if exe then
59 return exe()
60 else
61 print(err)
62 end
63 end
64
65 -- Stuff required for test code.
66 local tests
67 local failures
68 local totaltests = 0
69 local totalfailures = 0
70 function starttest(name)
71 print("Testing " .. name .. "...")
72 print()
73 tests = 0
74 failures = 0
75 end
76
77 -- Tests two objects for equality, irrespective of order. If the object is a table or expression, the objects may be sorted to ensure the correct order.
78 function testeq(actual, expected, initial, sort)
79 if sort and type(actual) == "table" and not actual.type then
80 table.sort(actual, function (a, b)
81 return a:order(b)
82 end)
83 elseif sort and type(actual) == "table" and actual.type and actual:type() == BinaryOperation and actual:iscommutative() then
84 table.sort(actual.expressions, function (a, b)
85 return a:order(b)
86 end)
87 end
88
89 if initial then
90 if ToStringArray(expected) == ToStringArray(actual) then
91 print(ToStringArray(initial) .. " -> " .. ToStringArray(actual))
92 else
93 print(ToStringArray(initial) .. " -> " .. ToStringArray(actual) .. " (Expected: " .. ToStringArray(expected) .. ")")
94 failures = failures + 1
95 end
96 else
97 if ToStringArray(expected) == ToStringArray(actual) then
98 print("Result: " .. ToStringArray(actual))
99 else
100 print("Result: ".. ToStringArray(actual) .. " (Expected: " .. ToStringArray(expected) .. ")")
101 failures = failures + 1
102 end
103 end
104 tests = tests + 1
105 end
106
107 -- Tests whether converting an element to a different ring produces the expected object in the expected ring
108 function testringconvert(expression, toring, expected, expectedring)
109 testeq(expression:inring(toring), expected, expression)
110 testeq(expression:inring(toring):getring(), expectedring)
111 end
112
113 function endtest()
114 print()
115 print("Finished test without errors.")
116 print()
117 totaltests = totaltests + tests
118 totalfailures = totalfailures + failures
119 if failures == 0 then
120 print("Performed " .. tests .. " tests, all of which passed!")
121 else
122 print("Performed tests, " .. failures .. "/" .. tests .. " failed.")
123 end
124 print("=====================================================================================================================")
125 end
126
127 function endall()
128 if totalfailures == 0 then
129 print("Performed " .. totaltests .. " tests in total, all of which passed!")
130 else
131 print("Performed tests, " .. totalfailures .. "/" .. totaltests .. " failed.")
132 end
133 end
134
135
136 -- TODO: Add profiling and error catching options.
137 -- Comment out these lines to only run certain test code.
138
139 -- profiler = newProfiler()
140 -- profiler:start()
141
142 require("test.calculus.luacas-derivatives")
143 require("test.calculus.luacas-integrals")
144
145 require("test.expressions.luacas-autosimplify")
146 require("test.expressions.luacas-collect")
147 require("test.expressions.luacas-equations")
148 require("test.expressions.luacas-simplify")
149 require("test.expressions.luacas-functions")
150 require("test.expressions.luacas-logarithms")
151 -- require("test.expressions.luacas-rationalexponent")
152 require("test.expressions.luacas-substitute")
153
154 require("test.polynomials.luacas-polynomial")
155 require("test.polynomials.luacas-partialfractions")
156 require("test.polynomials.luacas-polynomialmod")
157 require("test.polynomials.luacas-roots")
158
159 require("test.rings.luacas-conversion")
160 require("test.rings.luacas-modulararithmetic")
161 require("test.rings.luacas-number")
162
163 endall()
164
165 -- profiler:stop()
166
167 -- local outfile = io.open( "profile.txt", "w+" )
168 -- profiler:report( outfile )
169 -- outfile:close()