diff --git a/src/lua/zencode_random.lua b/src/lua/zencode_random.lua index d087e899c..69e05dd4b 100644 --- a/src/lua/zencode_random.lua +++ b/src/lua/zencode_random.lua @@ -28,7 +28,7 @@ When("create the random ''", function(dest) new_codec(dest, { zentype = 'e' }) end) -function shuffle_array_f(tab) +local function shuffle_array_f(tab) -- do not enforce CODEC detection since some schemas are also 1st level arrays local count = isarray(tab) ZEN.assert( count > 0, "Randomized object is not an array") @@ -45,7 +45,7 @@ end -- random and hashing operations When("create the random object of '' bits", function(n) empty'random object' - local bits = tonumber(n) + local bits = tonumber(mayhave(n) or n) ZEN.assert(bits, 'Invalid number of bits: ' .. n) ACK.random_object = OCTET.random(math.ceil(bits / 8)) new_codec('random_object', { zentype = 'e' }) @@ -53,7 +53,7 @@ end ) When("create the random object of '' bytes",function(n) empty'random object' - local bytes = math.ceil(tonumber(n)) + local bytes = math.ceil(tonumber(mayhave(n) or n)) ZEN.assert(bytes, 'Invalid number of bytes: ' .. n) ACK.random_object = OCTET.random(bytes) new_codec('random_object', { zentype = 'e' }) @@ -61,63 +61,68 @@ end ) When("randomize the '' array", function(arr) - local A = ACK[arr] - ZEN.assert(A, "Object not found: "..arr) + local A = have(arr) -- ZEN.assert(ZEN.CODEC[arr].zentype == 'a', "Object is not an array: "..arr) ACK[arr] = shuffle_array_f(A) end) +local function _create_random_array(array_length, fun_input, fun) + empty 'array' + ACK.array = { } + local length = tonumber(mayhave(array_length) or array_length) + ZEN.assert(length, "Argument is not a number: "..array_length) + for i = length,1,-1 do + table.insert(ACK.array, fun(fun_input)) + end +end When("create the array of '' random objects", function(s) - ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array") - ACK.array = { } - for i = s,1,-1 do - table.insert(ACK.array,OCTET.random(64)) - end - new_codec('array', {zentype = 'a'}) + _create_random_array(s, 64, OCTET.random) + new_codec('array', {zentype = 'a'}) end) When("create the array of '' random objects of '' bits", function(s, b) - empty'array' - ACK.array = { } - local q = tonumber(s) - ZEN.assert(q, "Argument is not a number: "..s) - local bits = tonumber(b) - local bytes = math.ceil(bits/8) - for i = q,1,-1 do - table.insert(ACK.array,OCTET.random(bytes)) - end - new_codec('array', {zentype = 'a'}) + local bits = tonumber(mayhave(b) or b) + ZEN.assert(bits, "Argument is not a number: "..b) + local bytes = math.ceil(bits/8) + _create_random_array(s, bytes, OCTET.random) + new_codec('array', {zentype = 'a'}) end) When("create the array of '' random objects of '' bytes", function(s, b) - empty'array' - ACK.array = { } - local q = tonumber(s) - ZEN.assert(q, "Argument is not a number: "..s) - local bytes = math.ceil(tonumber(b)) - for i = q,1,-1 do - table.insert(ACK.array,OCTET.random(bytes)) - end - new_codec('array', {zentype = 'a'}) + local n_bytes = tonumber(mayhave(b) or b) + ZEN.assert(n_bytes, "Argument is not a number: "..b) + local bytes = math.ceil(n_bytes) + _create_random_array(s, bytes, OCTET.random) + new_codec('array', {zentype = 'a'}) end) When("create the array of '' random numbers", function(s) - ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array") - ACK.array = { } - for i = s,1,-1 do - table.insert(ACK.array,F.new(random_int16())) - end - new_codec('array', {zentype = 'a', encoding = 'number' }) + _create_random_array(s, null, BIG.random) + new_codec('array', {zentype = 'a', encoding = 'integer' }) end) When("create the array of '' random numbers modulo ''", function(s,m) - ZEN.assert(not ACK.array, "Cannot overwrite existing object: ".."array") - ACK.array = { } - for i = s,1,-1 do - table.insert(ACK.array,F.new(math.floor(random_int16() % m))) - end - new_codec('array', {zentype = 'a', encoding = 'number' }) + local modulo = mayhave(m) + if not modulo then + local mod = tonumber(m) + ZEN.assert(mod, "Argument is not a number: "..m) + modulo = BIG.new(mod) + end + local fun + local enc + local modulo_type = type(modulo) + if modulo_type == "zenroom.big" then + fun = function(input) return BIG.random() % input end + enc = 'integer' + elseif modulo_type == "zenroom.float" then + fun = function(input) return F.new(math.floor(random_int16() % tonumber(input))) end + enc = 'float' + else + error("Modulo is not a number nor an integer: "..modulo_type) + end + _create_random_array(s, modulo, fun) + new_codec('array', {zentype = 'a', encoding = enc }) end) local function _extract_random_elements(num, from, random_fun) diff --git a/test/zencode/hash.bats b/test/zencode/hash.bats index aaeb565a2..5fe63368b 100644 --- a/test/zencode/hash.bats +++ b/test/zencode/hash.bats @@ -185,7 +185,7 @@ Then print 'key derivation' Then print 'HMAC' EOF save_output 'array_random_nums.json' - assert_output '{"HMAC":"JxoWzE+K1YvSOtxkRBkZ53v2ufOAdIAuFNpKcQSpvI8=","key_derivation":"o5vX+ydRxacRDaSZZhyVOecaAdCpssoiUPPL6t0KS68=","sha256":"R0O4u7f3wsTAVUDf4/vVWvTY7AqfFDqxmwo4U+I5wtU=","sha512":"YP2kyOt75BuI/6sCUwBsN0/od81QwWzmg2Pwxh1pXneW3VIygTRO2mU17dO/CZ2XOy3euWb0hbyHlc1Jyn+VQQ=="}' + assert_output '{"HMAC":"dFGFO3vRONcS9DPewsSHoWdsx99vBatnzUVMVJ2W0sg=","key_derivation":"9geWtsqQwnvm/ukbHwMQ6rlKttjWvjAJJUNKdyihe5Y=","sha256":"c75TjcFMRKsBC8ZQHpQ5+Hp0PhZv2cl8LCGf6jEen2Q=","sha512":"0KKLgW/ZJDN4T7QZRfuFCKQMcWRoLUmkKkBIArvri27MXvVfWMb/oWuGyFYeqAAsvgy8y4eR1JKvoRsHhpq60g=="}' } diff --git a/test/zencode/output.bats b/test/zencode/output.bats index b561d4b76..dbb938bd7 100644 --- a/test/zencode/output.bats +++ b/test/zencode/output.bats @@ -11,7 +11,7 @@ Then print the 'array' as 'number' and print the 'aggregation' as 'number' EOF save_output 'random_array.out' - assert_output '{"aggregation":3271,"array":[89,80,83,31,98,10,79,97,11,39,82,39,38,59,94,95,91,92,61,18,4,67,77,67,36,10,0,22,42,3,49,12,17,66,45,49,77,60,21,66,50,20,67,37,4,95,86,51,53,12,96,22,20,17,82,98,54,39,24,91,81,19,20,57]}' + assert_output '{"aggregation":"3174","array":["37","87","51","22","91","18","70","7","58","38","0","11","92","29","86","46","72","69","10","79","98","83","77","98","27","12","84","6","66","47","75","59","12","38","17","45","79","44","55","93","9","14","19","20","53","86","74","17","39","20","52","50","1","37","53","28","78","53","87","49","32","86","54","75"]}' } @test "Report failure" { diff --git a/test/zencode/random.bats b/test/zencode/random.bats index 3ccb4ac1e..c83425788 100644 --- a/test/zencode/random.bats +++ b/test/zencode/random.bats @@ -85,23 +85,43 @@ EOF cat <