| 1 |
local util = require 'lua_util' |
| 2 |
|
| 3 |
context("Lua util - unordered_table_digest", function() |
| 4 |
|
| 5 |
test('Simple map produces consistent digest', function() |
| 6 |
local t1 = { a = 1, b = 2, c = 3 } |
| 7 |
local d1 = util.unordered_table_digest(t1) |
| 8 |
local d2 = util.unordered_table_digest(t1) |
| 9 |
assert_equal(d1, d2) |
| 10 |
assert_equal(#d1, 16) -- 64 bits = 16 hex chars |
| 11 |
end) |
| 12 |
|
| 13 |
test('Order independence for maps', function() |
| 14 |
-- Create tables that may iterate in different orders |
| 15 |
local t1 = {} |
| 16 |
t1.alpha = "first" |
| 17 |
t1.beta = "second" |
| 18 |
t1.gamma = "third" |
| 19 |
|
| 20 |
local t2 = {} |
| 21 |
t2.gamma = "third" |
| 22 |
t2.alpha = "first" |
| 23 |
t2.beta = "second" |
| 24 |
|
| 25 |
local d1 = util.unordered_table_digest(t1) |
| 26 |
local d2 = util.unordered_table_digest(t2) |
| 27 |
assert_equal(d1, d2, "Digests should be equal regardless of insertion order") |
| 28 |
end) |
| 29 |
|
| 30 |
test('Different values produce different digests', function() |
| 31 |
local t1 = { a = 1, b = 2 } |
| 32 |
local t2 = { a = 1, b = 3 } |
| 33 |
local d1 = util.unordered_table_digest(t1) |
| 34 |
local d2 = util.unordered_table_digest(t2) |
| 35 |
assert_not_equal(d1, d2) |
| 36 |
end) |
| 37 |
|
| 38 |
test('Different keys produce different digests', function() |
| 39 |
local t1 = { a = 1, b = 2 } |
| 40 |
local t2 = { a = 1, c = 2 } |
| 41 |
local d1 = util.unordered_table_digest(t1) |
| 42 |
local d2 = util.unordered_table_digest(t2) |
| 43 |
assert_not_equal(d1, d2) |
| 44 |
end) |
| 45 |
|
| 46 |
test('Numeric values are included in digest', function() |
| 47 |
local t1 = { weight = 1.0 } |
| 48 |
local t2 = { weight = 2.0 } |
| 49 |
local d1 = util.unordered_table_digest(t1) |
| 50 |
local d2 = util.unordered_table_digest(t2) |
| 51 |
assert_not_equal(d1, d2, "Different numeric values should produce different digests") |
| 52 |
end) |
| 53 |
|
| 54 |
test('Boolean values are included in digest', function() |
| 55 |
local t1 = { enabled = true } |
| 56 |
local t2 = { enabled = false } |
| 57 |
local d1 = util.unordered_table_digest(t1) |
| 58 |
local d2 = util.unordered_table_digest(t2) |
| 59 |
assert_not_equal(d1, d2, "Different boolean values should produce different digests") |
| 60 |
end) |
| 61 |
|
| 62 |
test('Nested tables are handled correctly', function() |
| 63 |
local t1 = { outer = { inner = "value" } } |
| 64 |
local t2 = { outer = { inner = "value" } } |
| 65 |
local t3 = { outer = { inner = "other" } } |
| 66 |
|
| 67 |
local d1 = util.unordered_table_digest(t1) |
| 68 |
local d2 = util.unordered_table_digest(t2) |
| 69 |
local d3 = util.unordered_table_digest(t3) |
| 70 |
|
| 71 |
assert_equal(d1, d2, "Same nested structure should produce same digest") |
| 72 |
assert_not_equal(d1, d3, "Different nested values should produce different digest") |
| 73 |
end) |
| 74 |
|
| 75 |
test('Arrays preserve order', function() |
| 76 |
local t1 = { "a", "b", "c" } |
| 77 |
local t2 = { "c", "b", "a" } |
| 78 |
local d1 = util.unordered_table_digest(t1) |
| 79 |
local d2 = util.unordered_table_digest(t2) |
| 80 |
assert_not_equal(d1, d2, "Arrays with different order should have different digests") |
| 81 |
end) |
| 82 |
|
| 83 |
test('Empty table produces consistent digest', function() |
| 84 |
local t1 = {} |
| 85 |
local t2 = {} |
| 86 |
local d1 = util.unordered_table_digest(t1) |
| 87 |
local d2 = util.unordered_table_digest(t2) |
| 88 |
assert_equal(d1, d2) |
| 89 |
end) |
| 90 |
|
| 91 |
test('Complex nested structure with mixed types', function() |
| 92 |
local t1 = { |
| 93 |
providers = { |
| 94 |
{ type = "llm", model = "gpt-4", weight = 1.0 }, |
| 95 |
{ type = "symbols", weight = 0.5 }, |
| 96 |
}, |
| 97 |
fusion = { |
| 98 |
normalization = "none", |
| 99 |
include_meta = true, |
| 100 |
meta_weight = 1.0, |
| 101 |
}, |
| 102 |
max_inputs = 100, |
| 103 |
} |
| 104 |
|
| 105 |
local t2 = { |
| 106 |
max_inputs = 100, |
| 107 |
fusion = { |
| 108 |
meta_weight = 1.0, |
| 109 |
include_meta = true, |
| 110 |
normalization = "none", |
| 111 |
}, |
| 112 |
providers = { |
| 113 |
{ type = "llm", model = "gpt-4", weight = 1.0 }, |
| 114 |
{ type = "symbols", weight = 0.5 }, |
| 115 |
}, |
| 116 |
} |
| 117 |
|
| 118 |
local d1 = util.unordered_table_digest(t1) |
| 119 |
local d2 = util.unordered_table_digest(t2) |
| 120 |
assert_equal(d1, d2, "Same structure with different key order should produce same digest") |
| 121 |
end) |
| 122 |
|
| 123 |
end) |