summaryrefslogtreecommitdiff
path: root/lib/std/test/hashfuncs.myr
blob: 5322c8e52d756ae7c1501bf3709f4ad87f7b1a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std
use testr

const main = {
	testr.run([
		[.name="string hash and equality", .fn={ctx
			testr.check(ctx, std.strhash("abc") == 1241861192, "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")
		}],
		[.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")
		}],
		[.name="int hash and equality", .fn={ctx
			testr.check(ctx, std.inthash(123) == 3497506805, "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")
		}],
	][:])

}