~sircmpwn/hare

ref: c66099085ccde84906ac368ee82214a7422bfd42 hare/strconv/+test/stoi.ha -rw-r--r-- 1.4 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn is_invalid_i64(value: (i64 | invalid | overflow)) bool = {
	return match (value) {
		invalid => true,
		* => false,
	};
};

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

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

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

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

@test fn stoi() void = {
	assert(is_invalid_i64(stoi64("")));
	assert(is_invalid_i64(stoi64("abc")));
	assert(is_invalid_i64(stoi64("1a")));

	assert(is_overflow_i64(stoi64("9223372036854775808")));
	assert(is_overflow_i64(stoi64("-9223372036854775809")));

	assert(is_number_i64(0i64, stoi64("0")));
	assert(is_number_i64(1i64, stoi64("1")));
	assert(is_number_i64(-1i64, stoi64("-1")));
	assert(is_number_i64(9223372036854775807i64, stoi64("9223372036854775807")));
	assert(is_number_i64(-9223372036854775808i64, stoi64("-9223372036854775808")));

	assert(is_overflow_i32(stoi32("2147483648")));
	assert(is_overflow_i32(stoi32("-2147483649")));

	assert(is_number_i32(2147483647i32, stoi32("2147483647")));
	assert(is_number_i32(-2147483648i32, stoi32("-2147483648")));
};