forked from FourierTransformer/lua-simdjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_spec.lua
More file actions
131 lines (118 loc) · 4.59 KB
/
compile_spec.lua
File metadata and controls
131 lines (118 loc) · 4.59 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
local simdjson = require("simdjson")
local cjson = require("cjson")
local function loadFile(textFile)
local file = io.open(textFile, "r")
if not file then error("File not found at " .. textFile) end
local allLines = file:read("*all")
file:close()
return allLines
end
local files = {
"apache_builds.json",
"canada.json",
"citm_catalog.json",
"github_events.json",
"google_maps_api_compact_response.json",
"google_maps_api_response.json",
"gsoc-2018.json",
"instruments.json",
"marine_ik.json",
"mesh.json",
"mesh.pretty.json",
"numbers.json",
"random.json",
"repeat.json",
"twitter_timeline.json",
"update-center.json",
"scalars/bool.json",
"scalars/null.json",
"scalars/number.json",
"scalars/string.json",
"small/adversarial.json",
"small/demo.json",
"small/flatadversarial.json",
"small/smalldemo.json",
"small/truenull.json"
}
describe("Make sure it parses strings correctly", function()
for _, file in ipairs(files) do
it("should parse the file: " .. file, function()
local fileContents = loadFile("jsonexamples/" .. file)
local cjsonDecodedValues = cjson.decode(fileContents)
assert.are.same(cjsonDecodedValues, simdjson.parse(fileContents))
end)
end
end)
describe("Make sure it parses files correctly", function()
for _, file in ipairs(files) do
it("should parse the file: " .. file, function()
local fileContents = loadFile("jsonexamples/" .. file)
local cjsonDecodedValues = cjson.decode(fileContents)
assert.are.same(cjsonDecodedValues, simdjson.parseFile("jsonexamples/" .. file))
end)
end
end)
describe("Make sure json pointer works with a string", function()
it("should handle a string", function()
local fileContents = loadFile("jsonexamples/small/demo.json")
local decodedFile = simdjson.open(fileContents)
assert.are.same(800, decodedFile:atPointer("/Image/Width"))
assert.are.same(600, decodedFile:atPointer("/Image/Height"))
assert.are.same(125, decodedFile:atPointer("/Image/Thumbnail/Height"))
assert.are.same(943, decodedFile:atPointer("/Image/IDs/1"))
end)
end)
describe("Make sure json pointer works with openfile", function()
it("should handle opening a file", function()
local decodedFile = simdjson.openFile("jsonexamples/small/demo.json")
assert.are.same(800, decodedFile:atPointer("/Image/Width"))
assert.are.same(600, decodedFile:atPointer("/Image/Height"))
assert.are.same(125, decodedFile:atPointer("/Image/Thumbnail/Height"))
assert.are.same(943, decodedFile:atPointer("/Image/IDs/1"))
end)
end)
local major, minor = _VERSION:match('([%d]+)%.(%d+)')
if tonumber(major) >= 5 and tonumber(minor) >= 3 then
describe("Make sure ints and floats parse correctly", function ()
it("should handle decoding numbers appropriately", function()
local numberCheck = simdjson.parse([[
{
"float": 1.2,
"min_signed_integer": -9223372036854775808,
"max_signed_integer": 9223372036854775807,
"one_above_max_signed_integer": 9223372036854775808,
"min_unsigned_integer": 0,
"max_unsigned_integer": 18446744073709551615
}
]])
assert.are.same("float", math.type(numberCheck["float"]))
assert.are.same("integer", math.type(numberCheck["max_signed_integer"]))
assert.are.same("integer", math.type(numberCheck["min_signed_integer"]))
assert.are.same("float", math.type(numberCheck["one_above_max_signed_integer"]))
assert.are.same("integer", math.type(numberCheck["min_unsigned_integer"]))
assert.are.same("float", math.type(numberCheck["max_unsigned_integer"]))
end)
end)
end
local invalid_files = {
"bool_trailing.json",
"nully_trailing.json",
"nil_token.json",
"nil_token_scalar.json",
"nully_token.json",
"nully_token_scalar.json",
"unknown_type.json"
}
describe("Make sure invalid files are not accepted", function()
for _, file in ipairs(invalid_files) do
it("should fail to parse: " .. file, function()
local fileContents = loadFile("jsonexamples/invalid/" .. file)
local cjsonValue, cjsonError = pcall(function() cjson.decode(fileContents) end)
local simdjsonValue, simdjsonError = pcall(function() simdjson.parse(fileContents) end)
assert.is.False(cjsonValue)
assert.is.False(simdjsonValue)
assert(cjsonError)
assert(simdjsonError)
end)
end
end)