summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_hash.c
blob: c1af385e042127bd100346b3e245bb9bd27de059 (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
/* libmisc/tests/test_hash.c - Tests for <libmisc/hash.h>
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <libmisc/hash.h>

#include "test.h"

int main() {
	test_assert(hash("hello", sizeof("hello")) == hash("hello", sizeof("hello")));
	test_assert(hash("hello", sizeof("hello")) != hash("Hello", sizeof("Hello")));
	int one = 1;
	int two = 2;
	test_assert(hash(&one, sizeof(int)) != hash("hello", sizeof("hello")));
	test_assert(hash(&one, sizeof(int)) == hash(&one, sizeof(int)));
	test_assert(hash(&one, sizeof(int)) != hash(&two, sizeof(int)));

	hash_t myhash;
	hash_init(&myhash);
	hash_write(&myhash, "hello", 5);
	test_assert(myhash == hash("hello", 5));
	hash_t myhash_a = myhash;
	hash_write(&myhash, "world", 5);
	test_assert(myhash != myhash_a);
	test_assert(myhash == hash("helloworld", 10));

	return 0;
}