aboutsummaryrefslogtreecommitdiff
path: root/test/integers.tm
blob: 514acd34fa22d6c788bb444106d202bc70465905 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
func main()
	assert 2 + 3 == 5

	assert 2 * 3 == 6

	assert 2 + 3 * 4 == 14

	assert 2 * 3 + 4 == 10

	assert Int8(1) + Int16(2) == Int16(3)

	assert 1 << 10 == 1024

	assert (3 and 2) == 2

	assert (3 or 4) == 7

	assert (3 xor 2) == 1

	nums := ""
	for x in 5
		nums ++= "$x,"
	assert nums == "1,2,3,4,5,"

	>> x := Int64(123)
	assert x.hex() == "0x7B"
	assert x.hex(digits=4) == "0x007B"
	assert x.octal() == "0o173"

	assert Int64.min == Int64(-9223372036854775808)
	assert Int64.max == Int64(9223372036854775807)


	assert Int32(123).hex() == "0x7B"
	assert Int16(123).hex() == "0x7B"
	assert Int8(123).hex() == "0x7B"

	assert Int(2.1, truncate=yes) == 2

	do
		small_int := 1
		assert small_int == 1
		max_small_int := 536870911
		assert max_small_int == 536870911
		max_i64 := 536870912
		assert max_i64 == 536870912
		super_big := 9999999999999999999999
		assert super_big == 9999999999999999999999
		assert max_small_int + 1 == 536870912

		assert max_small_int + max_small_int == 1073741822

		assert super_big + 1 == 10000000000000000000000

	do
		interesting_numerators := [-999999, -100, -23, -1, 0, 1, 23, 100, 999999]
		interesting_denominators := [-99, -20, -17, -1, 1, 17, 20, 99]
		for n in interesting_numerators
			for d in interesting_denominators
				assert (n/d)*d + (n mod d) == n

	assert 0.next_prime() == 2
	assert 7.next_prime() == 11
	assert (and: p.is_prime() for p in [
		2, 3, 5, 7,
		137372146048179869781170214707,
		811418847921670560768224995279,
		292590241572454328697048860273,
		754893741683930091960170890717,
		319651808258437169510475301537,
		323890224935694708770556249787,
		507626552342376235511933571091,
		548605069630614185274710840981,
		121475876690852432982324195553,
		771958616175795150904761471637,
	])!

	assert (or: p.is_prime() for p in [
		-1, 0, 1, 4, 6,
		137372146048179869781170214707*2,
		811418847921670560768224995279*3,
		292590241572454328697048860273*754893741683930091960170890717,
	])! == no

	assert Int(yes) == 1
	assert Int(no) == 0

	assert Int64(yes) == Int64(1)
	assert Int64(no) == Int64(0)

	assert 4.choose(2) == 6

	assert 4.factorial() == 24

	assert 3.is_between(1, 5) == yes
	assert 3.is_between(1, 3) == yes
	assert 3.is_between(5, 1) == yes
	assert 3.is_between(100, 200) == no

	assert 6.get_bit(1) == no
	assert 6.get_bit(2) == yes
	assert 6.get_bit(3) == yes
	assert 6.get_bit(4) == no

	assert Int64(6).get_bit(1) == no
	assert Int64(6).get_bit(2) == yes
	assert Int64(6).get_bit(3) == yes
	assert Int64(6).get_bit(4) == no

	assert Int.parse("123") == 123
	assert Int.parse("0x10") == 16
	assert Int.parse("0o10") == 8
	assert Int.parse("0b10") == 2
	assert Int.parse("abc") == none

	assert Int.parse("-123") == -123
	assert Int.parse("-0x10") == -16
	assert Int.parse("-0o10") == -8
	assert Int.parse("-0b10") == -2

	for base in 2.to(36)
		assert Int.parse("10", base=base) == base
	
	assert Int.parse("111", base=1) == 3

	assert Int.parse("z", base=36) == 35
	assert Int.parse("Z", base=36) == 35
	assert Int.parse("-z", base=36) == -35
	assert Int.parse("-Z", base=36) == -35