Skip to content

Commit

Permalink
coconut petition tally correctly returns 0 when no signatures
Browse files Browse the repository at this point in the history
infinity conversion implemented for tally, nil converted to zero
  • Loading branch information
jaromil committed Mar 21, 2019
1 parent 8e00127 commit 753658b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/lua/zencode_coconut.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,23 @@ ZEN.add_schema(

petition_tally = {
import = function(obj)
local dec = { }
if obj.dec.neg ~= "Infinity" then dec.neg = get(ECP.new, obj.dec, 'neg')
else dec.neg = ECP.infinity() end
if obj.dec.pos ~= "Infinity" then dec.pos = get(ECP.new, obj.dec, 'pos')
else dec.pos = ECP.infinity() end
return { uid = get(nil, obj, 'uid'),
c = get(INT.new, obj, 'c'),
dec = { neg = get(ECP.new, obj.dec, 'neg'),
pos = get(ECP.new, obj.dec, 'pos') },
dec = dec,
rx = get(INT.new, obj, 'rx') }
end,
export = function(obj, conv)
local dec = { neg = "Infinity", pos = "Infinity" }
if not ECP.isinf(obj.dec.neg) then dec.neg = get(conv, obj.dec, 'neg') end
if not ECP.isinf(obj.dec.pos) then dec.pos = get(conv, obj.dec, 'pos') end
return { uid = get(nil, obj, 'uid'),
c = get(conv, obj, 'c'),
dec = { neg = get(conv, obj.dec, 'neg'),
pos = get(conv, obj.dec, 'pos') },
dec = dec,
rx = get(conv, obj, 'rx') }
end }
})
Expand Down Expand Up @@ -322,8 +328,10 @@ When("I count the petition results", function()
ZEN.assert(ACK.tally, "Tally not found")
ZEN.assert(ACK.tally.uid == ACK.petition.uid,
"Tally does not correspond to petition")
OUT = { result = COCONUT.count_signatures_petition(ACK.petition.scores,
ACK.tally).pos }
OUT = { }
local res = COCONUT.count_signatures_petition(ACK.petition.scores, ACK.tally)
-- handle no signatures correctly: res.pos is nil hence result: 0
if res.pos then OUT.result = res.pos else OUT.result = 0 end
OUT.uid = ACK.petition.uid
end)

Expand Down
1 change: 1 addition & 0 deletions src/lua/zencode_schemas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function import(obj, sname)
ZEN.assert(obj, "Import error: obj is nil ("..sname..")")
local s = ZEN.schemas[sname]
ZEN.assert(s ~= nil, "Import error: schema not found '"..sname.."'")
warn("import schema member: "..sname)
return s.import(obj)
end
function export(obj, sname, conv)
Expand Down

0 comments on commit 753658b

Please sign in to comment.