forked from neoxic/lua-amf3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
172 lines (155 loc) · 3.3 KB
/
test.lua
File metadata and controls
172 lines (155 loc) · 3.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
--
-- Copyright (C) 2012-2013 Arseny Vakhrushev <arseny.vakhrushev at gmail dot com>
-- Please read the LICENSE file for license details
--
math.randomseed(os.time())
local amf3 = require 'amf3'
local amf3_encode = amf3.encode
local amf3_decode = amf3.decode
local error = error
local pairs = pairs
local print = print
local type = type
local unpack = unpack
local io_flush = io.flush
local io_write = io.write
local math_random = math.random
local string_char = string.char
local table_insert = table.insert
local vals = {
function () return nil end, -- nil
function () return math_random() < 0.5 end, -- boolean
function () return math_random(-268435456, 268435455) end, -- integer
function () return (math_random() - 0.5) * 1234567890 end, -- double
function () -- string
local t = {}
for i = 1, math_random(0, 30) do
t[i] = math_random(0, 10)
end
return string_char(unpack(t))
end,
}
local refs, any
local objs = {
function () -- reference
local n = #refs
return n > 0 and refs[math_random(n)] or nil
end,
function (d) -- dense array
local t = {}
for i = 1, math_random(0, 10) do
local v = any(d + 1)
if v ~= nil then
table_insert(t, v)
end
end
table_insert(refs, t)
return t
end,
function (d) -- associative array
local t = {}
for i = 1, math_random(0, 10) do
local k = vals[5]() -- random string key
local v = any(d + 1)
if #k > 0 and v ~= nil then
t[k] = v
end
end
table_insert(refs, t)
return t
end,
}
any = function (d)
if d < 4 and math_random() < 0.7 then
return objs[math_random(#objs)](d)
end
return vals[math_random(#vals)]()
end
local function spawn()
refs = {}
return any(0)
end
local function compare(v1, v2)
local r = {}
local function compare(v1, v2)
if type(v1) ~= 'table' or type(v2) ~= 'table' then
return v1 == v2
end
local t1 = r[v1]
local t2 = r[v2]
if t1 or t2 then
return t1 == v2 and t2 == v1
end
r[v1] = v2
r[v2] = v1
for k, v in pairs(v1) do
if not compare(v, v2[k]) then
return false
end
end
for k, v in pairs(v2) do
if not compare(v, v1[k]) then
return false
end
end
return true
end
return compare(v1, v2)
end
local function stdout(...)
io_write(...)
io_flush()
end
local function printf(fmt, ...)
print(fmt:format(...))
end
local function check(cond)
if not cond then
stdout '\n'
error('check failed!', 2)
end
end
-- Stress test
local total = 0
local cnt = 0
local max = 0
local stats = {}
stdout 'Testing'
for i = 1, 50 do
for j = 1, 20 do
local obj = spawn()
local str = amf3_encode(obj)
local size = #str
local _obj, _size = amf3_decode(str)
local _str = amf3_encode(_obj)
check(size == _size)
check(compare(obj, _obj))
total = total + size
cnt = cnt + 1
if max < size then
max = size
end
stats[size] = (stats[size] or 0) + 1
-- Additional decoder's robustness test
for pos = 1, size - 1 do
pcall(amf3_decode, str, pos)
end
end
stdout '.'
end
stdout '\n'
printf('Processed %d bytes in %d chunks', total, cnt)
printf('Max chunk size %d bytes', max)
print 'Size distribution:'
print '% of max size\t% of chunks'
for i = 1, 10 do
local a = (i - 1) / 10 * max
local b = i / 10 * max
local c = 0
for k, v in pairs(stats) do
if k > a and k <= b then
c = c + v
end
end
printf('%2d...%d \t%5.1f', (i - 1) * 10, i * 10, c / cnt * 100)
end