aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/strconv/atoi_test.v
blob: b356db6a7d2bddcf056db81eb924965a6810ef06 (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
import strconv

fn test_atoi() ? {
	assert strconv.atoi('16') ? == 16
	assert strconv.atoi('+16') ? == 16
	assert strconv.atoi('-16') ? == -16

	// invalid strings
	if x := strconv.atoi('str') {
		println(x)
		assert false
	} else {
		assert true
	}
	if x := strconv.atoi('string_longer_than_10_chars') {
		println(x)
		assert false
	} else {
		assert true
	}
	if x := strconv.atoi('') {
		println(x)
		assert false
	} else {
		assert true
	}
}

fn test_parse_int() ? {
	// Different bases
	assert strconv.parse_int('16', 16, 0) ? == 0x16
	assert strconv.parse_int('16', 8, 0) ? == 0o16
	assert strconv.parse_int('11', 2, 0) ? == 3
	// Different bit sizes
	assert strconv.parse_int('127', 10, 8) ? == 127
	assert strconv.parse_int('128', 10, 8) ? == 127
	assert strconv.parse_int('32767', 10, 16) ? == 32767
	assert strconv.parse_int('32768', 10, 16) ? == 32767
	assert strconv.parse_int('2147483647', 10, 32) ? == 2147483647
	assert strconv.parse_int('2147483648', 10, 32) ? == 2147483647
	assert strconv.parse_int('9223372036854775807', 10, 64) ? == 9223372036854775807
	assert strconv.parse_int('9223372036854775808', 10, 64) ? == 9223372036854775807
	assert strconv.parse_int('baobab', 36, 64) ? == 683058467
	// Invalid bit sizes
	if x := strconv.parse_int('123', 10, -1) {
		println(x)
		assert false
	} else {
		assert true
	}
	if x := strconv.parse_int('123', 10, 65) {
		println(x)
		assert false
	} else {
		assert true
	}
}

fn test_common_parse_uint2() {
	mut result, mut error := strconv.common_parse_uint2('1', 10, 8)
	assert result == 1
	assert error == 0
	result, error = strconv.common_parse_uint2('123', 10, 8)
	assert result == 123
	assert error == 0
	result, error = strconv.common_parse_uint2('123', 10, 65)
	assert result == 0
	assert error == -2
	result, error = strconv.common_parse_uint2('123', 10, -1)
	assert result == 0
	assert error == -2
	result, error = strconv.common_parse_uint2('', 10, 8)
	assert result == 0
	assert error == 1
	result, error = strconv.common_parse_uint2('1a', 10, 8)
	assert result == 1
	assert error == 2
	result, error = strconv.common_parse_uint2('12a', 10, 8)
	assert result == 12
	assert error == 3
	result, error = strconv.common_parse_uint2('123a', 10, 8)
	assert result == 123
	assert error == 4
}