| 1 | import hash.fnv1a |
| 2 | |
| 3 | struct MyStruct { |
| 4 | mut: |
| 5 | x int |
| 6 | y int |
| 7 | z int |
| 8 | } |
| 9 | |
| 10 | struct Abc { |
| 11 | mut: |
| 12 | a [5]u64 |
| 13 | } |
| 14 | |
| 15 | fn test_fnv1a_sum32() { |
| 16 | $if windows { |
| 17 | return |
| 18 | } |
| 19 | ahash := '10bc2abf' |
| 20 | a := 'apple' |
| 21 | b := fnv1a.sum32_string(a) |
| 22 | c := fnv1a.sum32(a.bytes()) |
| 23 | d := unsafe { fnv1a.sum32_bytes(a.str, a.len) } |
| 24 | assert b.hex() == ahash |
| 25 | assert c.hex() == ahash |
| 26 | assert d.hex() == ahash |
| 27 | |
| 28 | mut aa := Abc{} |
| 29 | x := fnv1a.sum32_struct(aa) |
| 30 | aa.a[3] = 5 |
| 31 | y := fnv1a.sum32_struct(aa) |
| 32 | assert x != y |
| 33 | mut ms := MyStruct{} |
| 34 | xx := fnv1a.sum32_struct(ms) |
| 35 | ms.x = 77 |
| 36 | yy := fnv1a.sum32_struct(ms) |
| 37 | assert xx != yy |
| 38 | assert x != xx |
| 39 | assert y != yy |
| 40 | } |
| 41 | |
| 42 | fn test_fnv1a_sum64() { |
| 43 | $if windows { |
| 44 | return |
| 45 | } |
| 46 | a := 'apple' |
| 47 | ahash := 'f74a62a458befdbf' |
| 48 | b := fnv1a.sum64_string(a) |
| 49 | c := fnv1a.sum64(a.bytes()) |
| 50 | d := unsafe { fnv1a.sum64_bytes(a.str, a.len) } |
| 51 | assert b.hex() == ahash |
| 52 | assert c.hex() == ahash |
| 53 | assert d.hex() == ahash |
| 54 | |
| 55 | mut aa := Abc{} |
| 56 | x := fnv1a.sum64_struct(aa) |
| 57 | aa.a[3] = 5 |
| 58 | y := fnv1a.sum64_struct(aa) |
| 59 | assert x != y |
| 60 | mut ms := MyStruct{} |
| 61 | xx := fnv1a.sum64_struct(ms) |
| 62 | ms.x = 77 |
| 63 | yy := fnv1a.sum64_struct(ms) |
| 64 | assert xx != yy |
| 65 | assert x != xx |
| 66 | assert y != yy |
| 67 | } |
| 68 | |