mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
std/hashes: hash(ref|ptr|pointer) + other improvements (#17731)
This commit is contained in:
@@ -3,7 +3,7 @@ discard """
|
||||
"""
|
||||
|
||||
import std/hashes
|
||||
|
||||
from stdtest/testutils import disableVm, whenVMorJs
|
||||
|
||||
when not defined(js) and not defined(cpp):
|
||||
block:
|
||||
@@ -177,5 +177,25 @@ proc main() =
|
||||
doAssert hash(Obj5(t: false, x: 1)) == hash(Obj5(t: true, y: 1))
|
||||
doAssert hash(Obj5(t: false, x: 1)) != hash(Obj5(t: true, y: 2))
|
||||
|
||||
block: # hash(ref|ptr|pointer)
|
||||
var a: array[10, uint8]
|
||||
# disableVm:
|
||||
whenVMorJs:
|
||||
# pending fix proposed in https://github.com/nim-lang/Nim/issues/15952#issuecomment-786312417
|
||||
discard
|
||||
do:
|
||||
assert a[0].addr.hash != a[1].addr.hash
|
||||
assert cast[pointer](a[0].addr).hash == a[0].addr.hash
|
||||
|
||||
block: # hash(ref)
|
||||
type A = ref object
|
||||
x: int
|
||||
let a = A(x: 3)
|
||||
disableVm: # xxx Error: VM does not support 'cast' from tyRef to tyPointer
|
||||
let ha = a.hash
|
||||
assert ha != A(x: 3).hash # A(x: 3) is a different ref object from `a`.
|
||||
a.x = 4
|
||||
assert ha == a.hash # the hash only depends on the address
|
||||
|
||||
static: main()
|
||||
main()
|
||||
|
||||
@@ -3,7 +3,7 @@ discard """
|
||||
"""
|
||||
|
||||
import std/strutils
|
||||
|
||||
from stdtest/testutils import disableVm
|
||||
# xxx each instance of `disableVm` and `when not defined js:` should eventually be fixed
|
||||
|
||||
template rejectParse(e) =
|
||||
@@ -12,10 +12,6 @@ template rejectParse(e) =
|
||||
raise newException(AssertionDefect, "This was supposed to fail: $#!" % astToStr(e))
|
||||
except ValueError: discard
|
||||
|
||||
template disableVm(body) =
|
||||
when nimvm: discard
|
||||
else: body
|
||||
|
||||
template main() =
|
||||
block: # strip
|
||||
doAssert strip(" ha ") == "ha"
|
||||
|
||||
Reference in New Issue
Block a user