overflow 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -35
- data/Rakefile +1 -1
- data/example/murmurhash.rb +44 -0
- data/ext/overflow/extconf.rb +2 -0
- data/ext/overflow/overflow.c +455 -0
- data/lib/overflow.rb +0 -1
- data/overflow.gemspec +2 -2
- data/spec/numeric_spec.rb +167 -0
- data/spec/overflow_spec.rb +150 -101
- data/spec/spec_helper.rb +2 -0
- metadata +20 -4
- data/ext/overflow/init.c +0 -323
- data/lib/overflow/version.rb +0 -3
data/lib/overflow.rb
CHANGED
data/overflow.gemspec
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'overflow/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
6
|
spec.name = "overflow"
|
8
|
-
spec.version =
|
7
|
+
spec.version = "0.1.0"
|
9
8
|
spec.author = "ksss"
|
10
9
|
spec.email = "co000ri@gmail.com"
|
11
10
|
spec.description = %q{Overflow is a class to overflow calculated as C language in Ruby.}
|
@@ -23,4 +22,5 @@ Gem::Specification.new do |spec|
|
|
23
22
|
spec.add_development_dependency "rake"
|
24
23
|
spec.add_development_dependency "rspec", ['~> 2.11']
|
25
24
|
spec.add_development_dependency "rake-compiler", ["~> 0.8.3"]
|
25
|
+
spec.add_development_dependency "limits"
|
26
26
|
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Overflow do
|
4
|
+
it "class" do
|
5
|
+
expect(Overflow).to be_include(Comparable)
|
6
|
+
expect(Overflow.superclass).to eq(Numeric)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "clone" do
|
10
|
+
over = Overflow.new "c", 1
|
11
|
+
expect(over.clone).to be_a_kind_of(Overflow)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "coerce" do
|
15
|
+
over = Overflow.new "c", 1
|
16
|
+
c = Overflow.new "c", 2
|
17
|
+
expect(over.coerce(2)).to eq([2,1])
|
18
|
+
expect(over.coerce(c)).to eq([2,1])
|
19
|
+
expect(2 + over).to eq(3)
|
20
|
+
expect(2.1 + over).to eq(3.1)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "i" do
|
24
|
+
over = Overflow.new "c", 1
|
25
|
+
expect(over.i).to be_a_kind_of(Complex)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "<=>" do
|
29
|
+
over = Overflow.new "Q", 1
|
30
|
+
expect(over <=> 1).to eq(0)
|
31
|
+
expect(over <=> 0xffffffffffffffff).to eq(-1)
|
32
|
+
|
33
|
+
over.set 0xffffffffffffffff
|
34
|
+
expect(over <=> 1).to eq(1)
|
35
|
+
expect(over <=> 0xffffffffffffffff).to eq(0)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "+@" do
|
39
|
+
over = Overflow.new "c", 1
|
40
|
+
expect((+over).to_i).to eq(1)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "-@" do
|
44
|
+
over = Overflow.new "c", 1
|
45
|
+
expect((-over).to_i).to eq(-1)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "hash" do
|
49
|
+
a = Overflow.new "i", 10
|
50
|
+
b = Overflow.new "i", 10
|
51
|
+
c = Overflow.new "Q", 10
|
52
|
+
d = Overflow.new "i", 100
|
53
|
+
expect(a.hash == b.hash).to be true
|
54
|
+
expect(a.hash == c.hash).to be false
|
55
|
+
expect(a.hash == d.hash).to be false
|
56
|
+
|
57
|
+
ary = []
|
58
|
+
hash = {}
|
59
|
+
100.times { ary << c *= 3 }
|
60
|
+
ary.each_with_index {|x, i| hash[x] = i}
|
61
|
+
ary.each_with_index do |x, i|
|
62
|
+
expect(hash[x]).to eq(i)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "eql?" do
|
67
|
+
a = Overflow.new "i", 10
|
68
|
+
b = Overflow.new "i", 10
|
69
|
+
c = Overflow.new "Q", 10
|
70
|
+
d = Overflow.new "i", 100
|
71
|
+
fix = 10
|
72
|
+
expect(a.eql?(fix)).to be false
|
73
|
+
expect(a.eql?(b)).to be true
|
74
|
+
expect(a.eql?(c)).to be false
|
75
|
+
expect(a.eql?(d)).to be false
|
76
|
+
expect(fix.eql?(a)).to be false
|
77
|
+
end
|
78
|
+
|
79
|
+
it "to_f" do
|
80
|
+
a = Overflow.new "c", 10
|
81
|
+
expect(a.to_f).to eq(10.0)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "fdiv" do
|
85
|
+
a = Overflow.new "c", 10
|
86
|
+
expect(a.fdiv(2)).to eq(5.0)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "div" do
|
90
|
+
a = Overflow.new "c", 10
|
91
|
+
expect(a.div(3)).to eq(3)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "divmod" do
|
95
|
+
a = Overflow.new "c", 10
|
96
|
+
expect(a.divmod(3)).to eq([3, 1])
|
97
|
+
end
|
98
|
+
|
99
|
+
it "modulo" do
|
100
|
+
a = Overflow.new "c", 10
|
101
|
+
expect(a.modulo(3)).to eq(1)
|
102
|
+
expect(a % 3).to eq(1)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "remainder" do
|
106
|
+
a = Overflow.new "c", 13
|
107
|
+
expect(a.remainder(4)).to eq(1)
|
108
|
+
expect(a.remainder(4.25)).to eq(0.25)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "abs" do
|
112
|
+
a = Overflow.new "c", -10
|
113
|
+
b = Overflow.new "c", 10
|
114
|
+
expect(a.abs).to eq(10)
|
115
|
+
expect(a.magnitude).to eq(10)
|
116
|
+
expect(b.abs).to eq(10)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "to_int" do
|
120
|
+
a = Overflow.new "c", 10
|
121
|
+
expect(a.to_int).to eq(10)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "real?" do
|
125
|
+
a = Overflow.new "c", 10
|
126
|
+
expect(a.real?).to be true
|
127
|
+
end
|
128
|
+
|
129
|
+
it "integer?" do
|
130
|
+
a = Overflow.new "c", 10
|
131
|
+
expect(a.integer?).to be true
|
132
|
+
end
|
133
|
+
|
134
|
+
it "zero?" do
|
135
|
+
a = Overflow.new "c", 0
|
136
|
+
b = Overflow.new "c", 10
|
137
|
+
expect(a.zero?).to be true
|
138
|
+
expect(b.zero?).to be false
|
139
|
+
end
|
140
|
+
|
141
|
+
it "nonzero?" do
|
142
|
+
a = Overflow.new "c", 0
|
143
|
+
b = Overflow.new "c", 10
|
144
|
+
expect(a.nonzero?).to be nil
|
145
|
+
expect(b.nonzero?).to eq(b)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "floor" do
|
149
|
+
a = Overflow.new "c", 10
|
150
|
+
expect(a.floor).to eq(10)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "ceil" do
|
154
|
+
a = Overflow.new "c", 10
|
155
|
+
expect(a.ceil).to eq(10)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "round" do
|
159
|
+
a = Overflow.new "c", 10
|
160
|
+
expect(a.round).to eq(10)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "truncate" do
|
164
|
+
a = Overflow.new "c", 10
|
165
|
+
expect(a.truncate).to eq(10)
|
166
|
+
end
|
167
|
+
end
|
data/spec/overflow_spec.rb
CHANGED
@@ -1,29 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Overflow do
|
4
|
-
CHAR_MAX = 0x7f
|
5
|
-
CHAR_MIN = -0x80
|
6
|
-
UCHAR_MAX = 0xff
|
7
|
-
|
8
|
-
SHRT_MAX = 0x7fff
|
9
|
-
SHRT_MIN = -0x8000
|
10
|
-
USHRT_MAX = 0xffff
|
11
|
-
|
12
|
-
INT_MAX = 0x7fffffff
|
13
|
-
INT_MIN = -0x80000000
|
14
|
-
UINT_MAX = 0xffffffff
|
15
|
-
|
16
|
-
LLONG_MAX = 0x7fffffffffffffff
|
17
|
-
LLONG_MIN = -0x8000000000000000
|
18
|
-
ULLONG_MAX = 0xffffffffffffffff
|
19
|
-
|
20
4
|
it "initialize" do
|
21
5
|
%w{c C s S i I l L q Q}.each do |i|
|
22
|
-
Overflow.new i
|
6
|
+
expect(Overflow.new i).to be_a_kind_of(Overflow)
|
7
|
+
expect(Overflow.new i, 1).to be_a_kind_of(Overflow)
|
23
8
|
end
|
24
9
|
end
|
25
10
|
|
26
|
-
it "
|
11
|
+
it "to_i" do
|
12
|
+
expect((Overflow.new "c").to_i).to eq(0)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "set type 'c'" do
|
27
16
|
over = Overflow.new "c"
|
28
17
|
expect(over.set(0).to_i).to eq(0)
|
29
18
|
expect(over.set(CHAR_MAX).to_i).to eq(CHAR_MAX)
|
@@ -33,7 +22,16 @@ describe Overflow do
|
|
33
22
|
expect(over.set(UCHAR_MAX + 2).to_i).to eq(1)
|
34
23
|
end
|
35
24
|
|
36
|
-
it "set
|
25
|
+
it "set type 's'" do
|
26
|
+
over = Overflow.new "s"
|
27
|
+
expect(over.set(SHRT_MAX).to_i).to eq(SHRT_MAX)
|
28
|
+
expect(over.set(SHRT_MAX + 1).to_i).to eq(SHRT_MIN)
|
29
|
+
expect(over.set(USHRT_MAX).to_i).to eq(-1)
|
30
|
+
expect(over.set(USHRT_MAX + 1).to_i).to eq(0)
|
31
|
+
expect(over.set(USHRT_MAX + 2).to_i).to eq(1)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "set type 'i'" do
|
37
35
|
over = Overflow.new "i"
|
38
36
|
expect(over.set(INT_MAX).to_i).to eq(INT_MAX)
|
39
37
|
expect(over.set(INT_MAX + 1).to_i).to eq(INT_MIN)
|
@@ -42,7 +40,17 @@ describe Overflow do
|
|
42
40
|
expect(over.set(UINT_MAX + 2).to_i).to eq(1)
|
43
41
|
end
|
44
42
|
|
45
|
-
|
43
|
+
# FIXME write test
|
44
|
+
# it "set type 'l'" do
|
45
|
+
# over = Overflow.new "l"
|
46
|
+
# expect(over.set(INT_MAX).to_i).to eq(INT_MAX)
|
47
|
+
# expect(over.set(INT_MAX + 1).to_i).to eq(INT_MIN)
|
48
|
+
# expect(over.set(UINT_MAX).to_i).to eq(-1)
|
49
|
+
# expect(over.set(UINT_MAX + 1).to_i).to eq(0)
|
50
|
+
# expect(over.set(UINT_MAX + 2).to_i).to eq(1)
|
51
|
+
# end
|
52
|
+
|
53
|
+
it "set type 'q'" do
|
46
54
|
over = Overflow.new "q"
|
47
55
|
expect(over.set(LLONG_MAX).to_i).to eq(LLONG_MAX)
|
48
56
|
expect(over.set(LLONG_MAX + 1).to_i).to eq(LLONG_MIN)
|
@@ -51,201 +59,204 @@ describe Overflow do
|
|
51
59
|
expect(over.set(ULLONG_MAX + 2).to_i).to eq(1)
|
52
60
|
end
|
53
61
|
|
62
|
+
it "<<" do
|
63
|
+
over = Overflow.new "c", 1
|
64
|
+
expect(over << 1).to be_a_kind_of(Overflow)
|
65
|
+
expect(over.to_i).to eq(1)
|
66
|
+
end
|
67
|
+
|
54
68
|
it "<< 8bit" do
|
55
|
-
over = Overflow.new "c"
|
56
|
-
over.set(CHAR_MAX)
|
69
|
+
over = Overflow.new "c", CHAR_MAX
|
57
70
|
over = over << 7
|
58
71
|
expect(over.to_i).to eq(CHAR_MIN)
|
59
72
|
|
60
|
-
over = Overflow.new "C"
|
61
|
-
over.set(UCHAR_MAX)
|
73
|
+
over = Overflow.new "C", UCHAR_MAX
|
62
74
|
over = over << 7
|
63
75
|
expect(over.to_i).to eq(CHAR_MAX + 1)
|
64
76
|
end
|
65
77
|
|
66
78
|
it "<< 16bit" do
|
67
|
-
over = Overflow.new "s"
|
68
|
-
over.set(SHRT_MAX)
|
79
|
+
over = Overflow.new "s", SHRT_MAX
|
69
80
|
over = over << 15
|
70
81
|
expect(over.to_i).to eq(SHRT_MIN)
|
71
82
|
|
72
|
-
over = Overflow.new "S"
|
73
|
-
over.set(USHRT_MAX)
|
83
|
+
over = Overflow.new "S", USHRT_MAX
|
74
84
|
over = over << 15
|
75
85
|
expect(over.to_i).to eq(SHRT_MAX + 1)
|
76
86
|
end
|
77
87
|
|
78
88
|
it "<< 32bit" do
|
79
|
-
over = Overflow.new "i"
|
80
|
-
over.set(INT_MAX)
|
89
|
+
over = Overflow.new "i", INT_MAX
|
81
90
|
over = over << 31
|
82
91
|
expect(over.to_i).to eq(INT_MIN)
|
83
92
|
|
84
|
-
over = Overflow.new "I"
|
85
|
-
over.set(UINT_MAX)
|
93
|
+
over = Overflow.new "I", UINT_MAX
|
86
94
|
over = over << 31
|
87
95
|
expect(over.to_i).to eq(INT_MAX + 1)
|
88
96
|
end
|
89
97
|
|
90
98
|
it "<< 64bit" do
|
91
|
-
over = Overflow.new "q"
|
92
|
-
over.set(LLONG_MAX)
|
99
|
+
over = Overflow.new "q", LLONG_MAX
|
93
100
|
over = over << 63
|
94
101
|
expect(over.to_i).to eq(LLONG_MIN)
|
95
102
|
|
96
|
-
over = Overflow.new "Q"
|
97
|
-
over.set(ULLONG_MAX)
|
103
|
+
over = Overflow.new "Q", ULLONG_MAX
|
98
104
|
over = over << 63
|
99
105
|
expect(over.to_i).to eq(LLONG_MAX + 1)
|
100
106
|
end
|
101
107
|
|
108
|
+
it ">>" do
|
109
|
+
over = Overflow.new "c", 1
|
110
|
+
expect(over >> 1).to be_a_kind_of(Overflow)
|
111
|
+
expect(over.to_i).to eq(1)
|
112
|
+
end
|
113
|
+
|
102
114
|
it ">> 8bit" do
|
103
|
-
over = Overflow.new "c"
|
104
|
-
over.set(CHAR_MAX)
|
115
|
+
over = Overflow.new "c", CHAR_MAX
|
105
116
|
over = over >> 7
|
106
117
|
expect(over.to_i).to eq(0)
|
107
118
|
|
108
|
-
over = Overflow.new "C"
|
109
|
-
over.set(UCHAR_MAX)
|
119
|
+
over = Overflow.new "C", UCHAR_MAX
|
110
120
|
over = over >> 7
|
111
121
|
expect(over.to_i).to eq(1)
|
112
122
|
end
|
113
123
|
|
114
124
|
it ">> 16bit" do
|
115
|
-
over = Overflow.new "s"
|
116
|
-
over.set(SHRT_MAX)
|
125
|
+
over = Overflow.new "s", SHRT_MAX
|
117
126
|
over = over >> 15
|
118
127
|
expect(over.to_i).to eq(0)
|
119
128
|
|
120
|
-
over = Overflow.new "S"
|
121
|
-
over.set(USHRT_MAX)
|
129
|
+
over = Overflow.new "S", USHRT_MAX
|
122
130
|
over = over >> 15
|
123
131
|
expect(over.to_i).to eq(1)
|
124
132
|
end
|
125
133
|
|
126
134
|
it ">> 32bit" do
|
127
|
-
over = Overflow.new "i"
|
128
|
-
over.set(INT_MAX)
|
135
|
+
over = Overflow.new "i", INT_MAX
|
129
136
|
over = over >> 31
|
130
137
|
expect(over.to_i).to eq(0)
|
131
138
|
|
132
|
-
over = Overflow.new "I"
|
133
|
-
over.set(UINT_MAX)
|
139
|
+
over = Overflow.new "I", UINT_MAX
|
134
140
|
over = over >> 31
|
135
141
|
expect(over.to_i).to eq(1)
|
136
142
|
end
|
137
143
|
|
138
144
|
it ">> 64bit" do
|
139
|
-
over = Overflow.new "q"
|
140
|
-
over.set(LLONG_MAX)
|
145
|
+
over = Overflow.new "q", LLONG_MAX
|
141
146
|
over = over >> 63
|
142
147
|
expect(over.to_i).to eq(0)
|
143
148
|
|
144
|
-
over = Overflow.new "Q"
|
145
|
-
over.set(ULLONG_MAX)
|
149
|
+
over = Overflow.new "Q", ULLONG_MAX
|
146
150
|
over = over >> 63
|
147
151
|
expect(over.to_i).to eq(1)
|
148
152
|
end
|
149
153
|
|
154
|
+
it "+" do
|
155
|
+
over = Overflow.new "c", 1
|
156
|
+
clone = over.clone
|
157
|
+
expect(over + 1).to be_a_kind_of(Overflow)
|
158
|
+
expect(over.to_i).to eq(clone.to_i)
|
159
|
+
expect((over + clone).to_i).to eq(2)
|
160
|
+
end
|
161
|
+
|
150
162
|
it "+ 8bit" do
|
151
|
-
over = Overflow.new "c"
|
152
|
-
over.set(CHAR_MAX)
|
163
|
+
over = Overflow.new "c", CHAR_MAX
|
153
164
|
over += 1
|
154
165
|
expect(over.to_i).to eq(CHAR_MIN)
|
155
166
|
|
156
|
-
over = Overflow.new "C"
|
157
|
-
over.set(UCHAR_MAX)
|
167
|
+
over = Overflow.new "C", UCHAR_MAX
|
158
168
|
over += 1
|
159
169
|
expect(over.to_i).to eq(0)
|
160
170
|
end
|
161
171
|
|
162
172
|
it "+ 16bit" do
|
163
|
-
over = Overflow.new "s"
|
164
|
-
over.set(SHRT_MAX)
|
173
|
+
over = Overflow.new "s", SHRT_MAX
|
165
174
|
over += 1
|
166
175
|
expect(over.to_i).to eq(SHRT_MIN)
|
167
176
|
|
168
|
-
over = Overflow.new "S"
|
169
|
-
over.set(USHRT_MAX)
|
177
|
+
over = Overflow.new "S", USHRT_MAX
|
170
178
|
over += 1
|
171
179
|
expect(over.to_i).to eq(0)
|
172
180
|
end
|
173
181
|
|
174
182
|
it "+ 32bit" do
|
175
|
-
over = Overflow.new "l"
|
176
|
-
over.set(INT_MAX)
|
183
|
+
over = Overflow.new "l", INT_MAX
|
177
184
|
over += 1
|
178
185
|
expect(over.to_i).to eq(INT_MIN)
|
179
186
|
|
180
|
-
over = Overflow.new "L"
|
181
|
-
over.set(UINT_MAX)
|
187
|
+
over = Overflow.new "L", UINT_MAX
|
182
188
|
over += 1
|
183
189
|
expect(over.to_i).to eq(0)
|
184
190
|
end
|
185
191
|
|
186
192
|
it "+ 64bit" do
|
187
|
-
over = Overflow.new "q"
|
188
|
-
over.set(LLONG_MAX)
|
193
|
+
over = Overflow.new "q", LLONG_MAX
|
189
194
|
over += 1
|
190
195
|
expect(over.to_i).to eq(LLONG_MIN)
|
191
196
|
|
192
|
-
over = Overflow.new "Q"
|
193
|
-
over.set(ULLONG_MAX)
|
197
|
+
over = Overflow.new "Q", ULLONG_MAX
|
194
198
|
over += 1
|
195
199
|
expect(over.to_i).to eq(0)
|
196
200
|
end
|
197
201
|
|
202
|
+
it "-" do
|
203
|
+
over = Overflow.new "c", 1
|
204
|
+
clone = over.clone
|
205
|
+
expect(over - 1).to be_a_kind_of(Overflow)
|
206
|
+
expect(over.to_i).to eq(clone.to_i)
|
207
|
+
expect((over - clone).to_i).to eq(0)
|
208
|
+
end
|
209
|
+
|
198
210
|
it "- 8bit" do
|
199
|
-
over = Overflow.new "c"
|
200
|
-
over.set(CHAR_MIN)
|
211
|
+
over = Overflow.new "c", CHAR_MIN
|
201
212
|
over -= 1
|
202
213
|
expect(over.to_i).to eq(CHAR_MAX)
|
203
214
|
|
204
|
-
over = Overflow.new "C"
|
205
|
-
over.set(0)
|
215
|
+
over = Overflow.new "C", 0
|
206
216
|
over -= 1
|
207
217
|
expect(over.to_i).to eq(UCHAR_MAX)
|
208
218
|
end
|
209
219
|
|
210
220
|
it "- 16bit" do
|
211
|
-
over = Overflow.new "s"
|
212
|
-
over.set(SHRT_MIN)
|
221
|
+
over = Overflow.new "s", SHRT_MIN
|
213
222
|
over -= 1
|
214
223
|
expect(over.to_i).to eq(SHRT_MAX)
|
215
224
|
|
216
|
-
over = Overflow.new "S"
|
217
|
-
over.set(0)
|
225
|
+
over = Overflow.new "S", 0
|
218
226
|
over -= 1
|
219
227
|
expect(over.to_i).to eq(USHRT_MAX)
|
220
228
|
end
|
221
229
|
|
222
230
|
it "- 32bit" do
|
223
|
-
over = Overflow.new "l"
|
224
|
-
over.set(INT_MIN)
|
231
|
+
over = Overflow.new "l", INT_MIN
|
225
232
|
over -= 1
|
226
233
|
expect(over.to_i).to eq(INT_MAX)
|
227
234
|
|
228
|
-
over = Overflow.new "L"
|
229
|
-
over.set(0)
|
235
|
+
over = Overflow.new "L", 0
|
230
236
|
over -= 1
|
231
237
|
expect(over.to_i).to eq(UINT_MAX)
|
232
238
|
end
|
233
239
|
|
234
240
|
it "- 64bit" do
|
235
|
-
over = Overflow.new "q"
|
236
|
-
over.set(LLONG_MIN)
|
241
|
+
over = Overflow.new "q", LLONG_MIN
|
237
242
|
over -= 1
|
238
243
|
expect(over.to_i).to eq(LLONG_MAX)
|
239
244
|
|
240
|
-
over = Overflow.new "Q"
|
241
|
-
over.set(0)
|
245
|
+
over = Overflow.new "Q", 0
|
242
246
|
over -= 1
|
243
247
|
expect(over.to_i).to eq(ULLONG_MAX)
|
244
248
|
end
|
245
249
|
|
250
|
+
it "*" do
|
251
|
+
over = Overflow.new "c", 1
|
252
|
+
clone = over.clone
|
253
|
+
expect(over * 2).to be_a_kind_of(Overflow)
|
254
|
+
expect(over.to_i).to eq(clone.to_i)
|
255
|
+
expect((over * clone).to_i).to eq(1)
|
256
|
+
end
|
257
|
+
|
246
258
|
it "* 8bit" do
|
247
|
-
over = Overflow.new "c"
|
248
|
-
over.set(CHAR_MAX)
|
259
|
+
over = Overflow.new "c", CHAR_MAX
|
249
260
|
over *= CHAR_MAX
|
250
261
|
expect(over.to_i).to eq(1)
|
251
262
|
|
@@ -253,15 +264,13 @@ describe Overflow do
|
|
253
264
|
over *= CHAR_MIN
|
254
265
|
expect(over.to_i).to eq(0)
|
255
266
|
|
256
|
-
over = Overflow.new "C"
|
257
|
-
over.set(UCHAR_MAX)
|
267
|
+
over = Overflow.new "C", UCHAR_MAX
|
258
268
|
over *= UCHAR_MAX
|
259
269
|
expect(over.to_i).to eq(1)
|
260
270
|
end
|
261
271
|
|
262
272
|
it "* 16bit" do
|
263
|
-
over = Overflow.new "s"
|
264
|
-
over.set(SHRT_MAX)
|
273
|
+
over = Overflow.new "s", SHRT_MAX
|
265
274
|
over *= SHRT_MAX
|
266
275
|
expect(over.to_i).to eq(1)
|
267
276
|
|
@@ -269,15 +278,13 @@ describe Overflow do
|
|
269
278
|
over *= SHRT_MIN
|
270
279
|
expect(over.to_i).to eq(0)
|
271
280
|
|
272
|
-
over = Overflow.new "S"
|
273
|
-
over.set(USHRT_MAX)
|
281
|
+
over = Overflow.new "S", USHRT_MAX
|
274
282
|
over *= USHRT_MAX
|
275
283
|
expect(over.to_i).to eq(1)
|
276
284
|
end
|
277
285
|
|
278
286
|
it "* 32bit" do
|
279
|
-
over = Overflow.new "l"
|
280
|
-
over.set(INT_MAX)
|
287
|
+
over = Overflow.new "l", INT_MAX
|
281
288
|
over *= INT_MAX
|
282
289
|
expect(over.to_i).to eq(1)
|
283
290
|
|
@@ -285,15 +292,13 @@ describe Overflow do
|
|
285
292
|
over *= INT_MIN
|
286
293
|
expect(over.to_i).to eq(0)
|
287
294
|
|
288
|
-
over = Overflow.new "L"
|
289
|
-
over.set(UINT_MAX)
|
295
|
+
over = Overflow.new "L", UINT_MAX
|
290
296
|
over *= UINT_MAX
|
291
297
|
expect(over.to_i).to eq(1)
|
292
298
|
end
|
293
299
|
|
294
300
|
it "* 64bit" do
|
295
|
-
over = Overflow.new "q"
|
296
|
-
over.set(LLONG_MAX)
|
301
|
+
over = Overflow.new "q", LLONG_MAX
|
297
302
|
over *= LLONG_MAX
|
298
303
|
expect(over.to_i).to eq(1)
|
299
304
|
|
@@ -301,13 +306,57 @@ describe Overflow do
|
|
301
306
|
over *= LLONG_MIN
|
302
307
|
expect(over.to_i).to eq(0)
|
303
308
|
|
304
|
-
over = Overflow.new "Q"
|
305
|
-
over.set(ULLONG_MAX)
|
309
|
+
over = Overflow.new "Q", ULLONG_MAX
|
306
310
|
over *= ULLONG_MAX
|
307
311
|
expect(over.to_i).to eq(1)
|
308
312
|
end
|
309
313
|
|
310
|
-
|
314
|
+
it "/" do
|
315
|
+
over = Overflow.new "c", 5
|
316
|
+
clone = over.clone
|
317
|
+
expect(over / 2).to be_a_kind_of(Overflow)
|
318
|
+
expect((over / 2).to_i).to eq(2)
|
319
|
+
expect(over.to_i).to eq(clone.to_i)
|
320
|
+
expect((over / clone).to_i).to eq(1)
|
321
|
+
end
|
311
322
|
|
323
|
+
it "~" do
|
324
|
+
over = Overflow.new "C", 0xaa
|
325
|
+
rev = ~over
|
326
|
+
expect(over.to_i).to eq(0xaa)
|
327
|
+
expect(rev.to_i).to eq(0x55)
|
328
|
+
end
|
329
|
+
|
330
|
+
it "&" do
|
331
|
+
over = Overflow.new "C", 0xaa
|
332
|
+
clone = over.clone
|
333
|
+
a = over & 0x99
|
334
|
+
expect(over.to_i).to eq(0xaa)
|
335
|
+
expect(a.to_i).to eq(0x88)
|
336
|
+
over &= 0xffffffffffffffffff
|
337
|
+
expect(over.to_i).to eq(0xaa)
|
338
|
+
expect((over & clone).to_i).to eq(0xaa)
|
339
|
+
end
|
312
340
|
|
341
|
+
it "|" do
|
342
|
+
over = Overflow.new "C", 0xaa
|
343
|
+
clone = over.clone
|
344
|
+
a = over | 0x99
|
345
|
+
expect(over.to_i).to eq(0xaa)
|
346
|
+
expect(a.to_i).to eq(0xbb)
|
347
|
+
over |= 0xffffffffffffffffff
|
348
|
+
expect(over.to_i).to eq(0xff)
|
349
|
+
expect((over | clone).to_i).to eq(0xff)
|
350
|
+
end
|
313
351
|
|
352
|
+
it "^" do
|
353
|
+
over = Overflow.new "C", 0xaa
|
354
|
+
clone = over.clone
|
355
|
+
a = over ^ 0x99
|
356
|
+
expect(over.to_i).to eq(0xaa)
|
357
|
+
expect(a.to_i).to eq(0x33)
|
358
|
+
over ^= 0xffffffffffffffffff
|
359
|
+
expect(over.to_i).to eq(0x55)
|
360
|
+
expect((over ^ clone).to_i).to eq(0xff)
|
361
|
+
end
|
362
|
+
end
|