forked from kengonakajima/luvit-base64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
158 lines (130 loc) · 2.99 KB
/
test.lua
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
local table = require("table")
local math = require("math")
local uv = require("uv_native")
local b64 = require("./init.lua")
-- copied from: https://github.com/luvit/luvit/wiki/Snippets
local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
function base64_pure(data)
return ((data:gsub( '.', function(x)
local r, b = '', x:byte()
for i = 8, 1, -1 do
r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0')
end
return r
end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if #x < 6 then
return ''
end
local c = 0
for i = 1, 6 do
c = c + (x:sub( i, i) == '1' and 2 ^ (6 - i) or 0)
end
return base64_table:sub( c + 1, c + 1)
end) .. ({
'',
'==',
'='
})[#data % 3 + 1])
end
print("1")
local out = b64.encode("a")
if out ~= "YQ==" then
error("fatal 1-enc :"..out )
end
out = b64.decode(out)
if out ~= "a" then
error("fatal 1-dec :"..out)
end
print("2")
out = b64.encode("ab")
if out ~= "YWI=" then
error("fatal 2-enc :"..out)
end
out = b64.decode(out)
if out ~= "ab" then
error("fatal 2-dec :"..out)
end
print("3")
out = b64.encode("abc")
if out ~= "YWJj" then
error("fatal 3-enc :"..out)
end
out = b64.decode(out)
if out ~= "abc" then
error("fatal 3-dec :"..out)
end
print("4")
out = b64.encode("abcd")
if out ~= "YWJjZA==" then
error("fatal 4-enc :"..out)
end
out = b64.decode(out)
if out ~= "abcd" then
error("fatal 4-dec :"..out)
end
print("hello..")
local orig = "hello world!"
local expect = "aGVsbG8gd29ybGQh"
local out = b64.encode(orig)
if out ~= expect then
print("expect:",expect, "got:",out )
error("fatal")
end
local rev = b64.decode(out)
if rev ~= orig then
print("expect:",orig, "got:",rev)
error("fatal")
end
-- longer bufs
function test_2way(orig)
local enc = b64.encode(orig)
local pure = base64_pure(orig)
if enc ~= pure then
print("enc-mismatch. pure:", pure, "enc:",enc )
error("fatal")
end
local dec = b64.decode(enc)
if dec ~= orig then
print("mismatch. orig:",orig, "enc:",enc, "dec:",dec)
error("fatal")
end
end
print("long..")
for i=1,200,3 do
local ary={}
for j=1,i do
table.insert(ary,"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
end
local long = table.concat(ary)
test_2way(long)
end
-- benchmark
local s=""
for i=1,1000 do s = s .. "0123456789" end
local n = 10000
local st = uv.hrtime()
local encoded
for i=1,n do
encoded = b64.encode(s)
end
local et = uv.hrtime()
local dt =(et-st)/1000
print("bench-enc len:",#s,"n:",n,dt,"sec", (#s*n)/dt/1024/1024, "Mbytes/sec" )
--
st = uv.hrtime()
for i=1,n do
b64.decode(encoded)
end
et = uv.hrtime()
dt =(et-st)/1000
print("bench-dec len:",#s,"n:",n,dt,"sec", (#s*n)/dt/1024/1024, "Mbytes/sec" )
-- benchmark to compare with pure
st = uv.hrtime()
local puren = 100
for i=1,puren do
base64_pure(s)
end
et = uv.hrtime()
dt = (et-st)/1000
print("bench-pure-enc len:",#s,"n:",puren,dt,"sec", (#s*puren)/dt/1024/1024, "Mbytes/sec" )
print("test ok")