~sircmpwn/hare

ref: c66099085ccde84906ac368ee82214a7422bfd42 hare/strconv/+test/stou.ha -rw-r--r-- 1.1 KiB
c6609908Eyal Sawady README.md: fix typo 1 year, 5 months ago
                                                                                
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
fn is_invalid64(value: (u64 | invalid | overflow)) bool = {
	return match (value) {
		invalid => true,
		* => false,
	};
};

fn is_overflow64(value: (u64 | invalid | overflow)) bool = {
	return match (value) {
		overflow => true,
		* => false,
	};
};

fn is_number64(n: u64, value: (u64 | invalid | overflow)) bool = {
	return match (value) {
		v: u64 => v == n,
		* => false,
	};
};

@test fn stou() void = {
	assert(is_invalid64(stou64("")));
	assert(is_invalid64(stou64("abc")));
	assert(is_invalid64(stou64("1a")));
	assert(is_invalid64(stou64("-1")));

	assert(is_overflow64(stou64("18446744073709551616")));
	assert(is_overflow64(stou64("184467440737095516150")));

	assert(is_number64(0u64, stou64("0")));
	assert(is_number64(1u64, stou64("1")));
	assert(is_number64(18446744073709551615u64, stou64("18446744073709551615")));
};

@test fn stoub() void = {
	assert(is_number64(0x7fu64, stou64b("7f", 16u)));
	assert(is_number64(0x7fu64, stou64b("7F", 16u)));
	assert(is_number64(0o37u64, stou64b("37", 8u)));
	assert(is_number64(0b110101u64, stou64b("110101", 2u)));
};