Welcome to mirror list, hosted at ThFree Co, Russian Federation.

common.lua « tests - github.com/mpx/lua-cjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9a7ed194e9c71bb7b6dd631aad94b06ad1001d9b (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require "cjson"
require "posix"

-- Misc routines to assist with CJSON testing
--
-- Mark Pulford <mark@kyne.com.au>

-- Determine with a Lua table can be treated as an array.
-- Explicitly returns "not an array" for very sparse arrays.
-- Returns:
-- -1   Not an array
-- 0    Empty table
-- >0   Highest index in the array
function is_array(table)
    local max = 0
    local count = 0
    for k, v in pairs(table) do
        if type(k) == "number" then
            if k > max then max = k end
            count = count + 1
        else
            return -1
        end
    end
    if max > count * 2 then
        return -1
    end

    return max
end

function serialise_table(value, indent)
    local spacing, spacing2, indent2
    if indent then 
        spacing = "\n" .. indent
        spacing2 = spacing .. "  "
        indent2 = indent .. "  "
    else
        spacing, spacing2, indent2 = " ", " ", false
    end

    local max = is_array(value)

    local comma = false
    local fragment = { "{" .. spacing2 }
    if max > 0 then
        -- Serialise array
        for i = 1, max do
            if comma then
                table.insert(fragment, "," .. spacing2)
            end
            table.insert(fragment, serialise_value(value[i], indent2))
            comma = true
        end
    elseif max < 0 then
        -- Serialise table
        for k, v in pairs(value) do
            if comma then
                table.insert(fragment, "," .. spacing2)
            end
            table.insert(fragment, string.format(
                "[%s] = %s", serialise_value(k, indent2),
                             serialise_value(v, indent2))
            )
            comma = true
        end
    end
    table.insert(fragment, spacing .. "}")

    return table.concat(fragment)
end

function serialise_value(value, indent)
    if indent == nil then indent = "" end

    if value == cjson.null then
        return "cjson.null"
    elseif type(value) == "string" then
        return string.format("%q", value)
    elseif type(value) == "nil" or type(value) == "number" or
           type(value) == "boolean" then
        return tostring(value)
    elseif type(value) == "table" then
        return serialise_table(value, indent)
    else
        return "\"<" .. type(value) .. ">\""
    end
end

function dump_value(value)
    print(serialise_value(value))
end

function file_load(filename)
    local file, err = io.open(filename)
    if file == nil then
        error("Unable to read " .. filename)
    end
    local data = file:read("*a")
    file:close()

    return data
end

function file_save(filename, data)
    local file, err = io.open(filename, "w")
    if file == nil then
        error("Unable to write " .. filename)
    end
    file:write(data)
    file:close()
end

function gettimeofday()
    local tv_sec, tv_usec = posix.gettimeofday()

    return tv_sec + tv_usec / 1000000
end

function benchmark(tests, iter, rep)
    local function bench(func, iter)
        collectgarbage("collect")
        local t = gettimeofday()
        for i = 1, iter do
            func(i)
        end
        t = gettimeofday() - t
        return (iter / t)
    end

    local test_results = {}
    for name, func in pairs(tests) do
        -- k(number), v(string)
        -- k(string), v(function)
        -- k(number), v(function)
        if type(func) == "string" then
            name = func
            func = _G[name]
        end
        local result = {}
        for i = 1, rep do
            result[i] = bench(func, iter)
        end
        table.sort(result)
        test_results[name] = result[rep]
    end

    return test_results
end

function compare_values(val1, val2)
    local type1 = type(val1)
    local type2 = type(val2)
    if type1 ~= type2 then
        return false
    end

    -- Check for NaN
    if type1 == "number" and val1 ~= val1 and val2 ~= val2 then
        return true
    end

    if type1 ~= "table" then
        return val1 == val2
    end

    -- check_keys stores all the keys that must be checked in val2
    local check_keys = {}
    for k, _ in pairs(val1) do
        check_keys[k] = true
    end

    for k, v in pairs(val2) do
        if not check_keys[k] then
            return false
        end

        if not compare_values(val1[k], val2[k]) then
            return false
        end

        check_keys[k] = nil
    end
    for k, _ in pairs(check_keys) do
        -- Not the same if any keys from val1 were not found in val2
        return false
    end
    return true
end

function run_test(testname, func, input, should_work, output)
    local function status_line(name, status, value)
        local statusmap = { [true] = ":success", [false] = ":error" }
        if status ~= nil then
            name = name .. statusmap[status]
        end
        print(string.format("[%s] %s", name, serialise_value(value, false)))
    end

    local result = { pcall(func, unpack(input)) }
    local success = table.remove(result, 1)

    local correct = false
    if success == should_work and compare_values(result, output) then
        correct = true
    end

    local teststatus = { [true] = "PASS", [false] = "FAIL" }
    print("==> Test " .. testname .. ": " .. teststatus[correct])

    status_line("Input", nil, input)
    if not correct then
        status_line("Expected", should_work, output)
    end
    status_line("Received", success, result)
    print()

    return correct, result
end

function run_test_group(testgroup, tests)
    for k, v in ipairs(tests) do
        if type(v) == "function" then
            -- Useful for changing configuration during a batch
            msg = v()
            print(string.format("==> Config %s [%d]: %s", testgroup, k, msg))
            print()
        elseif type(v) == "table" then
            run_test(testgroup .. " [" .. k .. "]", unpack(v))
        else
            error("Testgroup can only contain functions and tables")
        end
    end
end

-- vi:ai et sw=4 ts=4: