ref: 5d40b4ead1f20776f23bed271412af829bbe0e96
parent: 83d26331a5419033927800760e8a59f1bd84d2b9
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Nov 2 06:52:42 EDT 2017
Fix tests to use new hashable/comparable traits.
--- a/lib/std/test/bitset.myr
+++ b/lib/std/test/bitset.myr
@@ -49,11 +49,11 @@
}],
[.name="hash", .fn={ctx
var bs = mkset([][:])
- testr.check(ctx, std.bshash(bs) == 0x726fdb47dd0e0e31, "wrong hash, got {}", std.bshash(bs))
+ testr.check(ctx, std.hash(bs) == 0x726fdb47dd0e0e31, "wrong hash, got {}", std.hash(bs))
std.bsput(bs, 123456)
- testr.check(ctx, std.bshash(bs) == 0x778abc1d7706143b, "wrong hash, got {}", std.bshash(bs))
+ testr.check(ctx, std.hash(bs) == 0x778abc1d7706143b, "wrong hash, got {}", std.hash(bs))
std.bsdel(bs, 123456)
- testr.check(ctx, std.bshash(bs) == 0x726fdb47dd0e0e31, "wrong hash, got {}", std.bshash(bs))
+ testr.check(ctx, std.hash(bs) == 0x726fdb47dd0e0e31, "wrong hash, got {}", std.hash(bs))
std.bsfree(bs)
}]
][:])
--- a/lib/std/test/hashfuncs.myr
+++ b/lib/std/test/hashfuncs.myr
@@ -4,28 +4,20 @@
const main = {
testr.run([
[.name="string hash and equality", .fn={ctx
- testr.check(ctx, std.strhash("abc") == 0x5dbcfa53aa2007a5, "wrong hash\n")
- testr.check(ctx, std.streq("abc\0def", "abc\0def"), "equal strings not equal\n")
- testr.check(ctx, !std.streq("abc\0def", "abcdef"), "unequal strings are equal\n")
+ testr.check(ctx, std.hash("abc") == 0x5dbcfa53aa2007a5, "wrong hash\n")
+ testr.check(ctx, std.cmp("abc\0def", "abc\0def"), "equal strings not equal\n")
+ testr.check(ctx, !std.cmp("abc\0def", "abcdef"), "unequal strings are equal\n")
}],
- [.name="case insensitive hash and equality", .fn={ctx
- testr.check(ctx, std.strcasehash("abc") == std.strcasehash("AbC"), "wrong case insensitive hash\n")
- testr.check(ctx, std.strcaseeq("abc", "AbC"), "equal case insensitive strings not equal")
- testr.check(ctx, !std.strcaseeq("abc", "AbCd"), "unequal case insensitive strings equal")
- }],
[.name="pointer equality", .fn={ctx
var x, y: int
/* can't sanely test ptrhash; it will change every time */
- testr.check(ctx, std.ptreq(&x, &x), "equal pointers not equal")
- testr.check(ctx, !std.ptreq(&x, &y), "unequal pointers are equal")
+ testr.check(ctx, std.cmp(&x, &x), "equal pointers not equal")
+ testr.check(ctx, !std.cmp(&x, &y), "unequal pointers are equal")
}],
[.name="int hash and equality", .fn={ctx
- testr.check(ctx, std.inthash(123) == 0x5671db246859d5b6, "wrong int hash")
- testr.check(ctx, std.inteq(123, 123), "equal integers not equal")
- testr.check(ctx, !std.inteq(123, 456), "unequal integers are equal")
- }],
- [.name="murmurhash test", .fn={ctx
- testr.check(ctx, std.murmurhash2("foobar", 1234) == 2203212445, "wrong murmurhash value")
+ testr.check(ctx, std.hash(123) == 0x5671db246859d5b6, "wrong int hash")
+ testr.check(ctx, std.cmp(123, 123), "equal integers not equal")
+ testr.check(ctx, !std.cmp(123, 456), "unequal integers are equal")
}],
[.name="siphash test", .fn={ctx
siphashreferencetestvector(ctx)
--- a/lib/std/test/htab.myr
+++ b/lib/std/test/htab.myr
@@ -28,22 +28,22 @@
var ht
var i
- ht = std.mkht(idhash, ideq)
+ ht = std.mkht()
/* create a hash table with a few hundred values */
for i = 0; i < 4000; i++
- std.htput(ht, i, i)
+ std.htput(ht, (i : collisionprone), (i : collisionprone))
;;
for i = 0; i < 200; i++
- std.htdel(ht, i*2)
+ std.htdel(ht, (i*2 : collisionprone))
;;
for i = 0; i < 200; i++
- std.assert(!std.hthas(ht, i*2), "deleted item still present")
+ std.assert(!std.hthas(ht, (i*2 : collisionprone)), "deleted item still present")
;;
for i = 0; i < 200; i++
- std.assert(std.hthas(ht, i*2+1), "undeleted item missing")
+ std.assert(std.hthas(ht, (i*2+1 : collisionprone)), "undeleted item missing")
;;
for i = 400; i < 4000; i++
- std.assert(std.hthas(ht, i), "undeleted item missing")
+ std.assert(std.hthas(ht, (i : collisionprone)), "undeleted item missing")
;;
}
@@ -52,15 +52,15 @@
var ht
var i
- ht = std.mkht(idhash, ideq)
+ ht = std.mkht()
/* insert an element a few hundred times */
for i = 0; i < 500; i++
- std.htput(ht, 0, i)
+ std.htput(ht, 0, (i : collisionprone))
;;
- std.assert(std.hthas(ht, 0), "inserted element not present")
- std.assert(std.htgetv(ht, 0, -1) == 499, "inserted element has wrong value")
+ std.assert(std.hthas(ht, (0 : collisionprone)), "inserted element not present")
+ std.assert(std.htgetv(ht, (0 : collisionprone), -1) == 499, "inserted element has wrong value")
std.htdel(ht, 0)
- std.assert(!std.hthas(ht, 0), "element left in table")
+ std.assert(!std.hthas(ht, (0 : collisionprone)), "element left in table")
}
const tombstonefill = {
@@ -67,7 +67,7 @@
var ht
var i
- ht = std.mkht(idhash, ideq)
+ ht = std.mkht()
/*
insert an element into each slot in the hash table, and
delete it. With direct hashing, this is guaranteed to have
@@ -74,12 +74,12 @@
put a tombstone into each slot.
*/
for i = 0; i <= ht.keys.len; i++
- std.htput(ht, i, i)
- std.htdel(ht, i)
+ std.htput(ht, (i : collisionprone), i)
+ std.htdel(ht, (i : collisionprone))
;;
/* make sure we haven't actually got anything in the table */
std.assert(ht.nelt == 0, "elements left in hash table")
- std.assert(!std.hthas(ht, 1), "found phantom element")
+ std.assert(!std.hthas(ht, (1 : collisionprone)), "found phantom element")
}
const main = {
@@ -95,10 +95,16 @@
tombstonefill()
}
-const idhash = {x
- -> x
-}
+type collisionprone = int
-const ideq = {a, b
- -> a == b
-}
+impl std.hashable collisionprone =
+ hash = {x
+ -> (x : uint64)
+ }
+;;
+
+impl std.comparable collisionprone =
+ cmp = {a, b
+ -> a == b
+ }
+;;
--- a/lib/std/test/striter.myr
+++ b/lib/std/test/striter.myr
@@ -13,7 +13,7 @@
i = 0
for sp : std.bysplit("foo+++bar", "++")
- std.assert(std.streq(splits[i++], sp), "wrong split {}", sp)
+ std.assert(std.cmp(splits[i++], sp), "wrong split {}", sp)
;;
std.assert(i == splits.len, "wrong split count")
}