summaryrefslogtreecommitdiff
path: root/test/pkstr.c
blob: 8e7193bdcbd9d3367fcb5b1d5e8b3e0ab5516354 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#define PK_IMPL_MEM
#define PK_IMPL_STR

#include "../pkstr.h"

#include "../pkmacros.h"

#include <stdio.h>

int main(int argc, char *argv[])
{
	(void)argc;
	(void)argv;
	(void)stdout;

	struct pk_str s;
	struct pk_cstr cs;
	struct pk_str os;
	struct pk_cstr ocs;

	struct pk_membucket *bkt = pk_mem_bucket_create("test-pkstr", PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE);
	pk_mem_bucket_set_client_mem_bucket(bkt);

	// cstring_to_pk_str
	{
		s = cstring_to_pk_str("this is a test");
		PK_LOGV_INF("cstring_to_pk_str: '%s' '%i' '%i'\n", s.val, s.length, s.reserved);
	}

	// cstring_to_pk_cstr
	{
		cs = cstring_to_pk_cstr("this is a const test");
		PK_LOGV_INF("cstring_to_pk_cstr: '%s' '%i' '%i'\n", cs.val, cs.length, cs.reserved);
	}

	// pk_cstr_to_pk_str
	{
		os = pk_cstr_to_pk_str(&cs);
		PK_LOGV_INF("pk_cstr_to_pk_str: '%s' '%i' '%i'\n", os.val, os.length, os.reserved);
	}

	// pk_str_to_pk_cstr
	{
		ocs = pk_str_to_pk_cstr(&s);
		PK_LOGV_INF("pk_str_to_pk_cstr: '%s' '%i' '%i'\n", ocs.val, ocs.length, ocs.reserved);
	}

	// pk_compare_str
	{
		os = cstring_to_pk_str("this is a different test");
		PK_LOGV_INF("pk_compare_str, ('%s' cmp '%s') = '%i'\n", s.val, os.val, pk_compare_str(&s, &os));
	}

	// pk_compare_cstr
	{
		ocs = cstring_to_pk_cstr("this is a different const test");
		PK_LOGV_INF("pk_compare_cstr, ('%s' cmp '%s') = '%i'\n", ocs.val, cs.val, pk_compare_cstr(&ocs, &cs));
	}

	// pk_clone_pk_str
	{
		s = cstring_to_pk_str("this is a clone test");
		os = pk_str_clone(&s, nullptr);
		if (s.length != os.length) return 1;
		if (os.reserved == 0) return 1;
		if (s.val == os.val) return 1;
		if (os.val == nullptr) return 1;
		PK_LOGV_INF("pk_clone_pk_str, ('%s' to '%s')\n", s.val, os.val);
	}

	// pk_clone_pk_cstr
	{
		cs = cstring_to_pk_cstr("this is a const clone test");
		ocs = pk_cstr_clone(&cs, nullptr);
		if (cs.length != ocs.length) return 1;
		if (ocs.reserved == 0) return 1;
		if (cs.val == ocs.val) return 1;
		if (ocs.val == nullptr) return 1;
		PK_LOGV_INF("pk_clone_pk_cstr, ('%s' to '%s')\n", cs.val, ocs.val);
	}

	pk_mem_bucket_destroy(bkt);
	bkt = nullptr;

	return 0;
}