-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmix_cpp_test.lua
166 lines (142 loc) · 4.38 KB
/
mix_cpp_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
158
159
160
161
162
163
164
165
166
package.cpath = "../lib64/?.so;../lib/?.so;" .. package.cpath
local libdifflua = require("libdifflua")
local src = {
a = 1,
b = "2",
c = 3.3,
d = true,
sub = {
a = 11,
b = "22",
c = 33.3,
d = true,
},
array = { 1, 2, 3, 4 },
obj_array = {
{ id = 1, a = 1, b = 2 },
{ id = 2, a = 3, b = 4 },
{ id = 3, a = 5, b = 6 },
{ id = 4, a = 7, b = 8 },
}
}
local dst = {
b = "2",
c = 3.4,
d = false,
sub = {
a = 12,
b = "11",
},
array = { 2, 3 },
obj_array = {
{ id = 2, a = 3, b = 4 },
{ id = 3, a = 5, b = 6 },
{ id = 4, a = 70, b = 80 },
}
}
local function print_table(node)
if type(node) ~= "table" then
print(tostring(node))
return
end
local cache, stack, output = {}, {}, {}
local depth = 1
local output_str = "{\n"
while true do
local size = 0
for k, v in pairs(node) do
size = size + 1
end
local cur_index = 1
for k, v in pairs(node) do
if (cache[node] == nil) or (cur_index >= cache[node]) then
if (string.find(output_str, "}", output_str:len())) then
output_str = output_str .. ",\n"
elseif not (string.find(output_str, "\n", output_str:len())) then
output_str = output_str .. "\n"
end
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = ""
local key
if (type(k) == "number" or type(k) == "boolean") then
key = "[" .. tostring(k) .. "]"
else
key = "['" .. tostring(k) .. "']"
end
if (type(v) == "number" or type(v) == "boolean") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = " .. tostring(v)
elseif (type(v) == "table") then
output_str = output_str .. string.rep('\t', depth) .. key .. " = {\n"
table.insert(stack, node)
table.insert(stack, v)
cache[node] = cur_index + 1
break
else
output_str = output_str .. string.rep('\t', depth) .. key .. " = '" .. tostring(v) .. "'"
end
if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
else
output_str = output_str .. ","
end
else
-- close the table
if (cur_index == size) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
end
end
cur_index = cur_index + 1
end
if (size == 0) then
output_str = output_str .. "\n" .. string.rep('\t', depth - 1) .. "}"
end
if (#stack > 0) then
node = stack[#stack]
stack[#stack] = nil
depth = cache[node] == nil and depth + 1 or depth - 1
else
break
end
end
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output, output_str)
output_str = table.concat(output)
print(output_str)
end
function equals(o1, o2)
if o1 == o2 then
return true
end
local o1Type = type(o1)
local o2Type = type(o2)
if o1Type ~= o2Type then
return false
end
if o1Type ~= 'table' then
return false
end
local keySet = {}
for key1, value1 in pairs(o1) do
local value2 = o2[key1]
if value2 == nil or equals(value1, value2) == false then
return false
end
keySet[key1] = true
end
for key2, _ in pairs(o2) do
if not keySet[key2] then
return false
end
end
return true
end
local diff = libdifflua.cal_diff(src, dst, _G.lua_get_id, _G.lua_new_func)
print_table(diff)
local new_dst = libdifflua.patch_diff(src, diff, _G.lua_get_id, _G.lua_new_func)
print_table(new_dst)
if equals(dst, new_dst) then
print("patch success")
else
error("patch failed")
end