summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/luacas/tex/test/main.lua
blob: 638f7734b5ae3f3bdce3b6cd602a6c7b40afa782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---@diagnostic disable: lowercase-global
-- Runs test code from test files.

require("calculus._init")
require("_lib.pepperfish")

-- Stuff required for the basic parser.
local constants = {e="E", pi = "PI", ln = "LN", log = "LOG", Integer = "Integer", DD = "DD", int = "INT", abs = "ABS", fact="FACT"}

local function parser(s)
    if string.find(s, "[0-9]+") then
        return "Integer(\"" .. s .. "\")"
    end

    if s.find(s, "[%^%\\%[%]]") then
        return string.gsub(s, "[^%^%\\%[%]]+", parser)
    end

    for string, replace in pairs(constants) do
        if s == string then
            return replace
        end
    end

    return "SymbolExpression(\"" .. s .. "\")"
end

function parse(input)
    local parsed = string.gsub(input, "[0-9]+", parser)
    parsed = string.gsub(parsed, "[A-z']+", parser)
    local exe, err = load("return " .. parsed)
    if exe then
        return exe():autosimplify()
    else
        print(err)
    end
end

function dparse(input)
    local parsed = string.gsub(input, "[0-9]+", parser)
    parsed = string.gsub(parsed, "[A-z']+", parser)
    local exe, err = load("return " .. parsed)
    if exe then
        return exe()
    else
        print(err)
    end
end

-- Stuff required for test code.
local tests
local failures
local totaltests = 0
local totalfailures = 0
function starttest(name)
    print("Testing " .. name .. "...")
    print()
    tests = 0
    failures = 0
end

-- 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.
function testeq(actual, expected, initial, sort)
    if sort and type(actual) == "table" and not actual.type then
        table.sort(actual, function (a, b)
            return a:order(b)
        end)
    elseif sort and type(actual) == "table" and actual.type and actual:type() == BinaryOperation and actual:iscommutative() then
        table.sort(actual.expressions, function (a, b)
            return a:order(b)
        end)
    end

    if initial then
        if ToStringArray(expected) == ToStringArray(actual) then
            print(ToStringArray(initial) .. " -> " .. ToStringArray(actual))
        else
            print(ToStringArray(initial) .. " -> " .. ToStringArray(actual) .. " (Expected: " .. ToStringArray(expected) .. ")")
            failures = failures + 1
        end
    else
        if ToStringArray(expected) == ToStringArray(actual) then
            print("Result: " .. ToStringArray(actual))
        else
            print("Result: ".. ToStringArray(actual) .. " (Expected: " .. ToStringArray(expected) .. ")")
            failures = failures + 1
        end
    end
    tests = tests + 1
end

-- Tests whether converting an element to a different ring produces the expected object in the expected ring
function testringconvert(expression, toring, expected, expectedring)
    testeq(expression:inring(toring), expected, expression)
    testeq(expression:inring(toring):getring(), expectedring)
end

function endtest()
    print()
    print("Finished test without errors.")
    print()
    totaltests = totaltests + tests
    totalfailures = totalfailures + failures
    if failures == 0 then
        print("Performed " .. tests .. " tests, all of which passed!")
    else
        print("Performed tests, " .. failures .. "/" .. tests .. " failed.")
    end
    print("=====================================================================================================================")
end

function endall()
    if totalfailures == 0 then
        print("Performed " .. totaltests .. " tests in total, all of which passed!")
    else
        print("Performed tests, " .. totalfailures .. "/" .. totaltests .. " failed.")
    end
end


-- TODO: Add profiling and error catching options.
-- Comment out these lines to only run certain test code.

-- profiler = newProfiler()
-- profiler:start()

require("test.calculus.derivatives")
require("test.calculus.integrals")

require("test.expressions.autosimplify")
require("test.expressions.collect")
require("test.expressions.equations")
require("test.expressions.simplify")
require("test.expressions.functions")
require("test.expressions.logarithms")
-- require("test.expressions.rationalexponent")
require("test.expressions.substitute")

require("test.polynomials.polynomial")
require("test.polynomials.partialfractions")
require("test.polynomials.polynomialmod")
require("test.polynomials.roots")

require("test.rings.conversion")
require("test.rings.modulararithmetic")
require("test.rings.number")

endall()

-- profiler:stop()

-- local outfile = io.open( "profile.txt", "w+" )
-- profiler:report( outfile )
-- outfile:close()