2020-01-28 23:43:09 +01:00
|
|
|
import hash.fnv1a
|
|
|
|
|
2021-12-29 11:03:24 +01:00
|
|
|
struct MyStruct {
|
|
|
|
mut:
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
z int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Abc {
|
|
|
|
mut:
|
|
|
|
a [5]u64
|
|
|
|
}
|
|
|
|
|
2021-12-29 10:27:26 +01:00
|
|
|
fn test_fnv1a_sum32() {
|
2020-02-05 10:00:11 +01:00
|
|
|
$if windows {
|
|
|
|
return
|
|
|
|
}
|
2021-12-29 10:27:26 +01:00
|
|
|
ahash := '10bc2abf'
|
2020-01-28 23:43:09 +01:00
|
|
|
a := 'apple'
|
2021-12-29 10:27:26 +01:00
|
|
|
b := fnv1a.sum32_string(a)
|
|
|
|
c := fnv1a.sum32(a.bytes())
|
|
|
|
d := unsafe { fnv1a.sum32_bytes(a.str, a.len) }
|
|
|
|
assert b.hex() == ahash
|
|
|
|
assert c.hex() == ahash
|
|
|
|
assert d.hex() == ahash
|
2021-12-29 11:03:24 +01:00
|
|
|
//
|
|
|
|
mut aa := Abc{}
|
|
|
|
x := fnv1a.sum32_struct(aa)
|
|
|
|
aa.a[3] = 5
|
|
|
|
y := fnv1a.sum32_struct(aa)
|
|
|
|
assert x != y
|
|
|
|
mut ms := MyStruct{}
|
|
|
|
xx := fnv1a.sum32_struct(ms)
|
|
|
|
ms.x = 77
|
|
|
|
yy := fnv1a.sum32_struct(ms)
|
|
|
|
assert xx != yy
|
|
|
|
assert x != xx
|
|
|
|
assert y != yy
|
2021-12-29 10:27:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_fnv1a_sum64() {
|
|
|
|
$if windows {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a := 'apple'
|
|
|
|
ahash := 'f74a62a458befdbf'
|
2020-01-28 23:43:09 +01:00
|
|
|
b := fnv1a.sum64_string(a)
|
|
|
|
c := fnv1a.sum64(a.bytes())
|
2021-12-29 10:27:26 +01:00
|
|
|
d := unsafe { fnv1a.sum64_bytes(a.str, a.len) }
|
|
|
|
assert b.hex() == ahash
|
|
|
|
assert c.hex() == ahash
|
|
|
|
assert d.hex() == ahash
|
2021-12-29 11:03:24 +01:00
|
|
|
//
|
|
|
|
mut aa := Abc{}
|
|
|
|
x := fnv1a.sum64_struct(aa)
|
|
|
|
aa.a[3] = 5
|
|
|
|
y := fnv1a.sum64_struct(aa)
|
|
|
|
assert x != y
|
|
|
|
mut ms := MyStruct{}
|
|
|
|
xx := fnv1a.sum64_struct(ms)
|
|
|
|
ms.x = 77
|
|
|
|
yy := fnv1a.sum64_struct(ms)
|
|
|
|
assert xx != yy
|
|
|
|
assert x != xx
|
|
|
|
assert y != yy
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|