change sizeof(a_cast) to sizeof(void*)
[liba.git] / lua / test / pid_fuzzy.lua
blob2bdef50133a64c8e53b4008dc564f4f61ac9fc44
1 #!/usr/bin/env lua
3 ---@diagnostic disable: redefined-local
4 package.path = arg[0]:sub(0, -arg[0]:match("([^/\\]*)$"):len() - 1) .. "?.lua;" .. package.path
5 local test = require("test")
6 local a = require("liba")
7 local NL = -3
8 local NM = -2
9 local NS = -1
10 local ZO = 0
11 local PS = 1
12 local PM = 2
13 local PL = 3
14 local me = {
15 -- LuaFormatter off
16 { a.mf.TRI, NL, NL, NM },
17 { a.mf.TRI, NL, NM, NS },
18 { a.mf.TRI, NM, NS, ZO },
19 { a.mf.TRI, NS, ZO, PS },
20 { a.mf.TRI, ZO, PS, PM },
21 { a.mf.TRI, PS, PM, PL },
22 { a.mf.TRI, PM, PL, PL },
23 -- LuaFormatter on
25 local NL = -6
26 local NM = -4
27 local NS = -3
28 local ZO = 0
29 local PS = 2
30 local PM = 4
31 local PL = 6
32 local mec = {
33 -- LuaFormatter off
34 { a.mf.TRI, NL, NL, NM },
35 { a.mf.TRI, NL, NM, NS },
36 { a.mf.TRI, NM, NS, ZO },
37 { a.mf.TRI, NS, ZO, PS },
38 { a.mf.TRI, ZO, PS, PM },
39 { a.mf.TRI, PS, PM, PL },
40 { a.mf.TRI, PM, PL, PL },
41 -- LuaFormatter on
43 local NL = -15
44 local NM = -10
45 local NS = -5
46 local ZO = 0
47 local PS = 5
48 local PM = 10
49 local PL = 15
50 local mkp = {
51 -- LuaFormatter off
52 { NL, NL, NM, NM, NS, ZO, ZO },
53 { NL, NL, NM, NS, NS, ZO, PS },
54 { NM, NM, NM, NS, ZO, PS, PS },
55 { NM, NM, NS, ZO, PS, PM, PM },
56 { NS, NS, ZO, PS, PS, PM, PM },
57 { NS, ZO, PS, PM, PM, PM, PL },
58 { ZO, ZO, PM, PM, PM, PL, PL },
59 -- LuaFormatter on
61 local NL = -3
62 local NM = -2
63 local NS = -1
64 local ZO = 0
65 local PS = 1
66 local PM = 2
67 local PL = 3
68 local mki = {
69 -- LuaFormatter off
70 { PL, PL, PM, PM, PS, ZO, ZO },
71 { PL, PL, PM, PS, PS, ZO, ZO },
72 { PL, PM, PS, PS, ZO, NS, NS },
73 { PM, PM, PS, ZO, NS, NM, NM },
74 { PM, PS, ZO, NS, NS, NM, NL },
75 { ZO, ZO, NS, NS, NM, NL, NL },
76 { ZO, ZO, NS, NM, NM, NL, NL },
77 -- LuaFormatter on
79 local mkd = {
80 -- LuaFormatter off
81 { NS, PS, PL, PL, PL, PM, NS },
82 { NS, PS, PL, PM, PM, PS, ZO },
83 { ZO, PS, PM, PM, PS, PS, ZO },
84 { ZO, PS, PS, PS, PS, PS, ZO },
85 { ZO, ZO, ZO, ZO, ZO, ZO, ZO },
86 { NL, NS, NS, NS, NS, NS, NL },
87 { NL, NM, NM, NM, NS, NS, NL },
88 -- LuaFormatter on
90 test.dir(getmetatable(a.pid_fuzzy))
91 local ctx = a.pid_fuzzy.new()
92 assert(ctx)
93 test.dir(getmetatable(ctx))
94 assert(type(a.pid_fuzzy.rule(ctx, me, mec, mkp, mki, mkd)) == "userdata")
95 assert(type(a.pid_fuzzy.kpid(ctx, 10, 0.1, 1)) == "userdata")
96 assert(type(a.pid_fuzzy.set_block(ctx, 2)) == "userdata")
97 assert(type(a.pid_fuzzy.run(ctx, 1, 0)) == "number")
98 assert(type(a.pid_fuzzy.pos(ctx, 1, 0)) == "number")
99 assert(type(a.pid_fuzzy.inc(ctx, 1, 0)) == "number")
100 assert(type(a.pid_fuzzy.zero(ctx)) == "userdata")
101 ctx:rule(me, mec, mkp, mki, mkd):kpid(3, 2, 1):set_block(2):op(a.pid_fuzzy.EQU):zero()
102 assert(type(ctx:run(1, 0)) == "number")
103 assert(type(ctx:pos(1, 0)) == "number")
104 assert(type(ctx:inc(1, 0)) == "number")
105 assert(type(ctx.kp) == "number")
106 assert(type(ctx.ki) == "number")
107 assert(type(ctx.kd) == "number")
108 assert(type(ctx.summax) == "number")
109 assert(type(ctx.summin) == "number")
110 assert(type(ctx.sum) == "number")
111 assert(type(ctx.outmax) == "number")
112 assert(type(ctx.outmin) == "number")
113 assert(type(ctx.out) == "number")
114 assert(type(ctx.fdb) == "number")
115 assert(type(ctx.err) == "number")
116 assert(type(ctx.order) == "number")
117 assert(type(ctx.block) == "number")
118 ctx.outmin = -10
119 ctx.outmax = 10
120 ctx.summin = -5
121 ctx.summax = 5
122 ctx.kp = 3
123 ctx.ki = 2
124 ctx.kd = 1
125 ctx.__name = nil
126 assert(ctx.__name)
127 ctx.__index = nil
128 assert(ctx.__index)
129 ctx.__newindex = nil
130 assert(ctx.__newindex)
131 ---@class a.pid_fuzzy
132 ---@field __name string
133 ---@field __index table
134 ---@field __newindex table