shithub: mc

Download patch

ref: a0172160d6cde82a878dfdcaafec6cd3f950b708
parent: faf16f0242436077c86358ccbe1a8d93966fdff7
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Jun 27 17:29:02 EDT 2016

Fix case insensitive hash, and add tests.

--- a/bench/runbench.myr
+++ b/bench/runbench.myr
@@ -54,6 +54,6 @@
 		| `std.Wsuccess:	/* nothing */
 		;;
 	;;
-	-> (std.now() - (tm : flt64)) / 1_000_000.0
+	-> (std.now() - tm : flt64) / 1_000_000.0
 }
 
--- a/lib/std/hashfuncs.myr
+++ b/lib/std/hashfuncs.myr
@@ -64,7 +64,7 @@
 	chars = [][:]
 	while s.len != 0
 		(c, s) = std.strstep(s)
-		std.slpush(&chars, c)
+		std.slpush(&chars, std.tolower(c))
 	;;
 	h = murmurhash2(slbytes(chars), Seed)
 	slfree(chars)
--- /dev/null
+++ b/lib/std/test/hashfuncs.myr
@@ -1,0 +1,24 @@
+use std
+
+const main = {
+	var x, y: int
+
+	std.assert(std.strhash("abc") == 1241861192, "wrong hash\n")
+	std.assert(std.streq("abc\0def", "abc\0def"), "equal strings not equal\n")
+	std.assert(!std.streq("abc\0def", "abcdef"), "unstrings are equal\n")
+
+	std.assert(std.strcasehash("abc") == std.strcasehash("AbC"), "wrong case insensitive hash\n")
+	std.assert(std.strcaseeq("abc", "AbC"), "equal case insensitive strings not equal")
+	std.assert(!std.strcaseeq("abc", "AbCd"), "unequal case insensitive strings equal")
+
+	/* can't sanely test ptrhash; it will change every time */
+	std.assert(std.ptreq(&x, &x), "equal pointers not equal")
+	std.assert(!std.ptreq(&x, &y), "unequal pointers are equal")
+
+	std.assert(std.inthash(123) == 2379201998, "wrong int hash")
+	std.assert(std.inteq(123, 123), "equal integers not equal")
+	std.assert(!std.inteq(123, 456), "unequal integers are equal")
+
+	std.assert(std.murmurhash2("foobar", 1234) == 2203212445, "wrong murmurhash value")
+
+}