overflow 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: da45bcd93d214011fe0426b29f28127b9db55af8
4
+ data.tar.gz: ab817a7f4ccfbe748cb8911ed5ce11acd0d37c86
5
+ SHA512:
6
+ metadata.gz: 94ab24a96c978d9f1d556109114b2177fdcd9c9ba84c24a23d23dde617b7db169ad516419a197fe7e254724595c1b8cec426b55fa7ee087b008d974b5f2fb9a9
7
+ data.tar.gz: 22cc6db2db596113391d75120589109e96b201361cdc02cb558f17063248ea62e93f61da1798b0ab0495543b236845cad1f2896dd246e1413e26b151cbb333ac
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ *.bundle
4
+ *.so
5
+ *.o
6
+ .bundle
7
+ .config
8
+ .yardoc
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 ksss
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Overflow
2
+
3
+ Overflow is a class to overflow calculated as C language in Ruby.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require 'overflow'
9
+
10
+ def murmur_hash str
11
+ data = str.dup.unpack("C*")
12
+ m = 0x5bd1e995
13
+ r = 16
14
+ length = Overflow.new "C" # "C" mean 32bit unsigned char (same as pack template)
15
+ length.set str.bytesize
16
+ h = length * m
17
+
18
+ while 4 <= length
19
+ d = data.shift(4).pack("C*").unpack("I")[0]
20
+ h += d # calculate not need `& 0xffffffff`
21
+ h *= m
22
+ h ^= h >> r
23
+ length -= 4
24
+ end
25
+
26
+ if 2 < length
27
+ h += (data[2] << 16) & 0xffffffff
28
+ end
29
+ if 1 < length
30
+ h += (data[1] << 8) & 0xffffffff
31
+ end
32
+ if 0 < length
33
+ h += data[0]
34
+ h *= m
35
+ h ^= h >> r
36
+ end
37
+
38
+ h *= m
39
+ h ^= h >> 10
40
+ h *= m
41
+ h ^= h >> 17
42
+
43
+ h
44
+ end
45
+ ```
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ gem 'overflow'
52
+
53
+ And then execute:
54
+
55
+ $ bundle
56
+
57
+ Or install it yourself as:
58
+
59
+ $ gem install overflow
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
68
+
69
+ ## License
70
+
71
+ See the file LICENSE.txt
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = ["-c", "-f progress", "-Ilib"]
8
+ t.pattern = "spec/**/*_spec.rb"
9
+ t.verbose = true
10
+ end
11
+ task :spec => :compile
12
+
13
+ require 'rake/extensiontask'
14
+ spec = Bundler::GemHelper.gemspec
15
+ Rake::ExtensionTask.new('overflow', spec) do |ext|
16
+ ext.ext_dir = 'ext/overflow'
17
+ ext.lib_dir = 'lib/overflow'
18
+ end
19
+
20
+
21
+ task :default => [:spec]
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('overflow/overflow')
@@ -0,0 +1,323 @@
1
+ #include "ruby.h"
2
+
3
+ typedef enum {
4
+ i8,
5
+ ui8,
6
+ i16,
7
+ ui16,
8
+ i32,
9
+ ui32,
10
+ i64,
11
+ ui64
12
+ } types;
13
+
14
+ typedef uint64_t value_t;
15
+
16
+ typedef struct {
17
+ value_t value;
18
+ types type;
19
+ } overflow_t;
20
+
21
+ types char2type (char c)
22
+ {
23
+ switch (c) {
24
+ case 'c': return i8;
25
+ case 'C': return ui8;
26
+ case 's': return i16;
27
+ case 'S': return ui16;
28
+ case 'i': return i32;
29
+ case 'I': return ui32;
30
+ case 'l': return i32;
31
+ case 'L': return ui32;
32
+ case 'q': return i64;
33
+ case 'Q': return ui64;
34
+ default:
35
+ rb_raise(rb_eArgError, "type %c is not support", c);
36
+ }
37
+ }
38
+
39
+ static void
40
+ overflow_free(overflow_t* ptr)
41
+ {
42
+ xfree(ptr);
43
+ }
44
+
45
+ static VALUE
46
+ overflow_alloc(VALUE self)
47
+ {
48
+ overflow_t* ptr = ALLOC(overflow_t);
49
+ return Data_Wrap_Struct(self, 0, overflow_free, ptr);
50
+ }
51
+
52
+ static VALUE
53
+ overflow_initialize(VALUE self, VALUE obj)
54
+ {
55
+ overflow_t *ptr;
56
+ char *p;
57
+ char c;
58
+
59
+ if (rb_type(obj) != T_STRING) {
60
+ rb_raise(rb_eArgError, "set a type char for `pack' template");
61
+ }
62
+
63
+ Data_Get_Struct(self, overflow_t, ptr);
64
+ p = RSTRING_PTR(obj);
65
+
66
+ ptr->type = char2type(*p);
67
+ return self;
68
+ }
69
+
70
+ static VALUE
71
+ overflow_set(VALUE self, VALUE obj)
72
+ {
73
+ overflow_t *ptr;
74
+ Data_Get_Struct(self, overflow_t, ptr);
75
+ VALUE other;
76
+
77
+ switch (rb_type(obj)) {
78
+ case T_FIXNUM:
79
+ switch (ptr->type) {
80
+ case i8: ptr->value = (int8_t) NUM2LL(obj); break;
81
+ case ui8: ptr->value = (uint8_t) NUM2LL(obj); break;
82
+ case i16: ptr->value = (int16_t) NUM2LL(obj); break;
83
+ case ui16: ptr->value = (uint16_t) NUM2LL(obj); break;
84
+ case i32: ptr->value = (int32_t) NUM2LL(obj); break;
85
+ case ui32: ptr->value = (uint32_t) NUM2LL(obj); break;
86
+ case i64: ptr->value = (int64_t) NUM2LL(obj); break;
87
+ case ui64: ptr->value = (uint64_t) NUM2LL(obj); break;
88
+ }
89
+ break;
90
+ case T_BIGNUM:
91
+ if (RBIGNUM_POSITIVE_P(obj)) {
92
+ other = rb_funcall(obj, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
93
+ ptr->value = (uint64_t) NUM2ULL(other);
94
+ } else {
95
+ ptr->value = (int64_t) NUM2LL(obj);
96
+ }
97
+ break;
98
+ }
99
+ return self;
100
+ }
101
+
102
+ static VALUE
103
+ overflow_to_i(VALUE self)
104
+ {
105
+ overflow_t *ptr;
106
+ Data_Get_Struct(self, overflow_t, ptr);
107
+
108
+ switch (ptr->type) {
109
+ case i8:
110
+ return INT2NUM((int8_t)ptr->value);
111
+ case ui8:
112
+ return UINT2NUM((uint8_t)ptr->value);
113
+ case i16:
114
+ return INT2NUM((int16_t)ptr->value);
115
+ case ui16:
116
+ return UINT2NUM((uint16_t)ptr->value);
117
+ case i32:
118
+ return LONG2NUM((int32_t)ptr->value);
119
+ case ui32:
120
+ return ULONG2NUM((uint32_t)ptr->value);
121
+ case i64:
122
+ return LL2NUM((int64_t)ptr->value);
123
+ case ui64:
124
+ return ULL2NUM((uint64_t)ptr->value);
125
+ }
126
+ }
127
+
128
+ #define TYPE_PLUS(type, value, other) ((type)((type)(value) + (type)(other)))
129
+
130
+ static uint64_t
131
+ plus(types type, uint64_t a, uint64_t b)
132
+ {
133
+ switch (type) {
134
+ case i8: return TYPE_PLUS(int8_t, a, b);
135
+ case ui8: return TYPE_PLUS(uint8_t, a, b);
136
+ case i16: return TYPE_PLUS(int16_t, a, b);
137
+ case ui16: return TYPE_PLUS(uint16_t, a, b);
138
+ case i32: return TYPE_PLUS(int32_t, a, b);
139
+ case ui32: return TYPE_PLUS(uint32_t, a, b);
140
+ case i64: return TYPE_PLUS(int64_t, a, b);
141
+ case ui64: return TYPE_PLUS(uint64_t, a, b);
142
+ }
143
+ }
144
+
145
+ static VALUE
146
+ overflow_plus(VALUE self, VALUE num)
147
+ {
148
+ overflow_t *ptr;
149
+ Data_Get_Struct(self, overflow_t, ptr);
150
+
151
+ uint64_t a;
152
+ uint64_t b;
153
+
154
+ if (RB_TYPE_P(num, T_BIGNUM)) {
155
+ num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
156
+ }
157
+ a = ptr->value;
158
+ b = NUM2ULL(num);
159
+
160
+ ptr->value = plus(ptr->type, a, b);
161
+ return self;
162
+ }
163
+
164
+ #define TYPE_MINUS(type, value, other) ((type)((type)(value) - (type)(other)))
165
+
166
+ static uint64_t
167
+ minus(types type, uint64_t a, uint64_t b)
168
+ {
169
+ switch (type) {
170
+ case i8: return TYPE_MINUS(int8_t, a, b);
171
+ case ui8: return TYPE_MINUS(uint8_t, a, b);
172
+ case i16: return TYPE_MINUS(int16_t, a, b);
173
+ case ui16: return TYPE_MINUS(uint16_t, a, b);
174
+ case i32: return TYPE_MINUS(int32_t, a, b);
175
+ case ui32: return TYPE_MINUS(uint32_t, a, b);
176
+ case i64: return TYPE_MINUS(int64_t, a, b);
177
+ case ui64: return TYPE_MINUS(uint64_t, a, b);
178
+ }
179
+ }
180
+
181
+ static VALUE
182
+ overflow_minus(VALUE self, VALUE num)
183
+ {
184
+ overflow_t *ptr;
185
+ Data_Get_Struct(self, overflow_t, ptr);
186
+
187
+ uint64_t a;
188
+ uint64_t b;
189
+
190
+ if (RB_TYPE_P(num, T_BIGNUM)) {
191
+ num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
192
+ }
193
+ a = ptr->value;
194
+ b = NUM2ULL(num);
195
+
196
+ ptr->value = minus(ptr->type, a, b);
197
+ return self;
198
+ }
199
+
200
+ #define TYPE_MUL(type, value, other) ((type)((type)(value) * (type)(other)))
201
+
202
+ static uint64_t
203
+ mul(types type, uint64_t a, uint64_t b)
204
+ {
205
+ switch (type) {
206
+ case i8: return TYPE_MUL(int8_t, a, b);
207
+ case ui8: return TYPE_MUL(uint8_t, a, b);
208
+ case i16: return TYPE_MUL(int16_t, a, b);
209
+ case ui16: return TYPE_MUL(uint16_t, a, b);
210
+ case i32: return TYPE_MUL(int32_t, a, b);
211
+ case ui32: return TYPE_MUL(uint32_t, a, b);
212
+ case i64: return TYPE_MUL(int64_t, a, b);
213
+ case ui64: return TYPE_MUL(uint64_t, a, b);
214
+ }
215
+ }
216
+
217
+ static VALUE
218
+ overflow_mul(VALUE self, VALUE num)
219
+ {
220
+ overflow_t *ptr;
221
+ Data_Get_Struct(self, overflow_t, ptr);
222
+
223
+ uint64_t a;
224
+ uint64_t b;
225
+
226
+ if (RB_TYPE_P(num, T_BIGNUM)) {
227
+ num = rb_funcall(num, rb_intern("&"), 1, ULL2NUM(0xffffffffffffffffLL));
228
+ }
229
+
230
+ a = ptr->value;
231
+ b = NUM2ULL(num);
232
+
233
+ ptr->value = mul(ptr->type, a, b);
234
+ return self;
235
+ }
236
+
237
+ static VALUE
238
+ lshift(overflow_t *ptr, long width)
239
+ {
240
+ switch (ptr->type) {
241
+ case i8: return INT2NUM((int8_t)(ptr->value << width));
242
+ case ui8: return UINT2NUM((uint8_t)(ptr->value << width));
243
+ case i16: return INT2NUM((int16_t)(ptr->value << width));
244
+ case ui16: return UINT2NUM((uint16_t)(ptr->value << width));
245
+ case i32: return LONG2NUM((int32_t)(ptr->value << width));
246
+ case ui32: return ULONG2NUM((uint32_t)(ptr->value << width));
247
+ case i64: return LONG2NUM((int64_t)(ptr->value << width));
248
+ case ui64: return ULONG2NUM((uint64_t)(ptr->value << width));
249
+ }
250
+ }
251
+
252
+ static VALUE
253
+ rshift(overflow_t *ptr, long width)
254
+ {
255
+ switch (ptr->type) {
256
+ case i8: return INT2NUM((int8_t)(ptr->value >> width));
257
+ case ui8: return UINT2NUM((uint8_t)(ptr->value >> width));
258
+ case i16: return INT2NUM((int16_t)(ptr->value >> width));
259
+ case ui16: return UINT2NUM((uint16_t)(ptr->value >> width));
260
+ case i32: return LONG2NUM((int32_t)(ptr->value >> width));
261
+ case ui32: return ULONG2NUM((uint32_t)(ptr->value >> width));
262
+ case i64: return LONG2NUM((int64_t)(ptr->value >> width));
263
+ case ui64: return ULONG2NUM((uint64_t)(ptr->value >> width));
264
+ }
265
+ }
266
+
267
+ static VALUE
268
+ overflow_lshift(VALUE self, VALUE obj)
269
+ {
270
+ long width;
271
+ overflow_t *ptr;
272
+ Data_Get_Struct(self, overflow_t, ptr);
273
+
274
+ if (!FIXNUM_P(obj))
275
+ rb_raise(rb_eArgError, "over 64 left shift not support");
276
+
277
+ width = FIX2LONG(obj);
278
+ if (64 <= width)
279
+ rb_raise(rb_eArgError, "over 64 left shift not support");
280
+
281
+ if (width < 0) {
282
+ return rshift(ptr, -width);
283
+ } else {
284
+ return lshift(ptr, width);
285
+ }
286
+ }
287
+
288
+ static VALUE
289
+ overflow_rshift(VALUE self, VALUE obj)
290
+ {
291
+ long width;
292
+ overflow_t *ptr;
293
+ Data_Get_Struct(self, overflow_t, ptr);
294
+
295
+ if (!FIXNUM_P(obj))
296
+ rb_raise(rb_eArgError, "over 64 right shift not support");
297
+
298
+ width = FIX2LONG(obj);
299
+ if (64 <= width)
300
+ rb_raise(rb_eArgError, "over 64 right shift not support");
301
+
302
+ if (width < 0) {
303
+ return lshift(ptr, -width);
304
+ } else {
305
+ return rshift(ptr, width);
306
+ }
307
+ }
308
+
309
+ void
310
+ Init_overflow(void)
311
+ {
312
+ VALUE cOverflow;
313
+ cOverflow = rb_define_class("Overflow", rb_cObject);
314
+ rb_define_alloc_func(cOverflow, overflow_alloc);
315
+ rb_define_method(cOverflow, "initialize", overflow_initialize, 1);
316
+ rb_define_method(cOverflow, "set", overflow_set, 1);
317
+ rb_define_method(cOverflow, "to_i", overflow_to_i, 0);
318
+ rb_define_method(cOverflow, "+", overflow_plus, 1);
319
+ rb_define_method(cOverflow, "-", overflow_minus, 1);
320
+ rb_define_method(cOverflow, "*", overflow_mul, 1);
321
+ rb_define_method(cOverflow, "<<", overflow_lshift, 1);
322
+ rb_define_method(cOverflow, ">>", overflow_rshift, 1);
323
+ }
data/lib/overflow.rb ADDED
@@ -0,0 +1,6 @@
1
+ begin
2
+ require "overflow/#{RUBY_VERSION[/\d+.\d+/]}/overflow"
3
+ rescue LoadError
4
+ require "overflow/overflow"
5
+ end
6
+ require "overflow/version"
@@ -0,0 +1,3 @@
1
+ class Overflow
2
+ VERSION = "0.0.1"
3
+ end
data/overflow.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'overflow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "overflow"
8
+ spec.version = Overflow::VERSION
9
+ spec.author = "ksss"
10
+ spec.email = "co000ri@gmail.com"
11
+ spec.description = %q{Overflow is a class to overflow calculated as C language in Ruby.}
12
+ spec.summary = %q{Overflow is a class to overflow calculated as C language in Ruby.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.extensions = ["ext/overflow/extconf.rb"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", ['~> 2.11']
25
+ spec.add_development_dependency "rake-compiler", ["~> 0.8.3"]
26
+ end
data/spec/mem_spec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Overflow do
4
+ it "gc safe" do
5
+ over = Overflow.new "c"
6
+ GC.start
7
+ over.set 1
8
+ GC.start
9
+ expect(over.to_i).to eq(1)
10
+ end
11
+ end
@@ -0,0 +1,313 @@
1
+ require 'spec_helper'
2
+
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
+ it "initialize" do
21
+ %w{c C s S i I l L q Q}.each do |i|
22
+ Overflow.new i
23
+ end
24
+ end
25
+
26
+ it "set and to_i type 'c'" do
27
+ over = Overflow.new "c"
28
+ expect(over.set(0).to_i).to eq(0)
29
+ expect(over.set(CHAR_MAX).to_i).to eq(CHAR_MAX)
30
+ expect(over.set(CHAR_MAX + 1).to_i).to eq(CHAR_MIN)
31
+ expect(over.set(UCHAR_MAX).to_i).to eq(-1)
32
+ expect(over.set(UCHAR_MAX + 1).to_i).to eq(0)
33
+ expect(over.set(UCHAR_MAX + 2).to_i).to eq(1)
34
+ end
35
+
36
+ it "set and to_i type 'i'" do
37
+ over = Overflow.new "i"
38
+ expect(over.set(INT_MAX).to_i).to eq(INT_MAX)
39
+ expect(over.set(INT_MAX + 1).to_i).to eq(INT_MIN)
40
+ expect(over.set(UINT_MAX).to_i).to eq(-1)
41
+ expect(over.set(UINT_MAX + 1).to_i).to eq(0)
42
+ expect(over.set(UINT_MAX + 2).to_i).to eq(1)
43
+ end
44
+
45
+ it "set and to_i type 'q'" do
46
+ over = Overflow.new "q"
47
+ expect(over.set(LLONG_MAX).to_i).to eq(LLONG_MAX)
48
+ expect(over.set(LLONG_MAX + 1).to_i).to eq(LLONG_MIN)
49
+ expect(over.set(ULLONG_MAX).to_i).to eq(-1)
50
+ expect(over.set(ULLONG_MAX + 1).to_i).to eq(0)
51
+ expect(over.set(ULLONG_MAX + 2).to_i).to eq(1)
52
+ end
53
+
54
+ it "<< 8bit" do
55
+ over = Overflow.new "c"
56
+ over.set(CHAR_MAX)
57
+ over = over << 7
58
+ expect(over.to_i).to eq(CHAR_MIN)
59
+
60
+ over = Overflow.new "C"
61
+ over.set(UCHAR_MAX)
62
+ over = over << 7
63
+ expect(over.to_i).to eq(CHAR_MAX + 1)
64
+ end
65
+
66
+ it "<< 16bit" do
67
+ over = Overflow.new "s"
68
+ over.set(SHRT_MAX)
69
+ over = over << 15
70
+ expect(over.to_i).to eq(SHRT_MIN)
71
+
72
+ over = Overflow.new "S"
73
+ over.set(USHRT_MAX)
74
+ over = over << 15
75
+ expect(over.to_i).to eq(SHRT_MAX + 1)
76
+ end
77
+
78
+ it "<< 32bit" do
79
+ over = Overflow.new "i"
80
+ over.set(INT_MAX)
81
+ over = over << 31
82
+ expect(over.to_i).to eq(INT_MIN)
83
+
84
+ over = Overflow.new "I"
85
+ over.set(UINT_MAX)
86
+ over = over << 31
87
+ expect(over.to_i).to eq(INT_MAX + 1)
88
+ end
89
+
90
+ it "<< 64bit" do
91
+ over = Overflow.new "q"
92
+ over.set(LLONG_MAX)
93
+ over = over << 63
94
+ expect(over.to_i).to eq(LLONG_MIN)
95
+
96
+ over = Overflow.new "Q"
97
+ over.set(ULLONG_MAX)
98
+ over = over << 63
99
+ expect(over.to_i).to eq(LLONG_MAX + 1)
100
+ end
101
+
102
+ it ">> 8bit" do
103
+ over = Overflow.new "c"
104
+ over.set(CHAR_MAX)
105
+ over = over >> 7
106
+ expect(over.to_i).to eq(0)
107
+
108
+ over = Overflow.new "C"
109
+ over.set(UCHAR_MAX)
110
+ over = over >> 7
111
+ expect(over.to_i).to eq(1)
112
+ end
113
+
114
+ it ">> 16bit" do
115
+ over = Overflow.new "s"
116
+ over.set(SHRT_MAX)
117
+ over = over >> 15
118
+ expect(over.to_i).to eq(0)
119
+
120
+ over = Overflow.new "S"
121
+ over.set(USHRT_MAX)
122
+ over = over >> 15
123
+ expect(over.to_i).to eq(1)
124
+ end
125
+
126
+ it ">> 32bit" do
127
+ over = Overflow.new "i"
128
+ over.set(INT_MAX)
129
+ over = over >> 31
130
+ expect(over.to_i).to eq(0)
131
+
132
+ over = Overflow.new "I"
133
+ over.set(UINT_MAX)
134
+ over = over >> 31
135
+ expect(over.to_i).to eq(1)
136
+ end
137
+
138
+ it ">> 64bit" do
139
+ over = Overflow.new "q"
140
+ over.set(LLONG_MAX)
141
+ over = over >> 63
142
+ expect(over.to_i).to eq(0)
143
+
144
+ over = Overflow.new "Q"
145
+ over.set(ULLONG_MAX)
146
+ over = over >> 63
147
+ expect(over.to_i).to eq(1)
148
+ end
149
+
150
+ it "+ 8bit" do
151
+ over = Overflow.new "c"
152
+ over.set(CHAR_MAX)
153
+ over += 1
154
+ expect(over.to_i).to eq(CHAR_MIN)
155
+
156
+ over = Overflow.new "C"
157
+ over.set(UCHAR_MAX)
158
+ over += 1
159
+ expect(over.to_i).to eq(0)
160
+ end
161
+
162
+ it "+ 16bit" do
163
+ over = Overflow.new "s"
164
+ over.set(SHRT_MAX)
165
+ over += 1
166
+ expect(over.to_i).to eq(SHRT_MIN)
167
+
168
+ over = Overflow.new "S"
169
+ over.set(USHRT_MAX)
170
+ over += 1
171
+ expect(over.to_i).to eq(0)
172
+ end
173
+
174
+ it "+ 32bit" do
175
+ over = Overflow.new "l"
176
+ over.set(INT_MAX)
177
+ over += 1
178
+ expect(over.to_i).to eq(INT_MIN)
179
+
180
+ over = Overflow.new "L"
181
+ over.set(UINT_MAX)
182
+ over += 1
183
+ expect(over.to_i).to eq(0)
184
+ end
185
+
186
+ it "+ 64bit" do
187
+ over = Overflow.new "q"
188
+ over.set(LLONG_MAX)
189
+ over += 1
190
+ expect(over.to_i).to eq(LLONG_MIN)
191
+
192
+ over = Overflow.new "Q"
193
+ over.set(ULLONG_MAX)
194
+ over += 1
195
+ expect(over.to_i).to eq(0)
196
+ end
197
+
198
+ it "- 8bit" do
199
+ over = Overflow.new "c"
200
+ over.set(CHAR_MIN)
201
+ over -= 1
202
+ expect(over.to_i).to eq(CHAR_MAX)
203
+
204
+ over = Overflow.new "C"
205
+ over.set(0)
206
+ over -= 1
207
+ expect(over.to_i).to eq(UCHAR_MAX)
208
+ end
209
+
210
+ it "- 16bit" do
211
+ over = Overflow.new "s"
212
+ over.set(SHRT_MIN)
213
+ over -= 1
214
+ expect(over.to_i).to eq(SHRT_MAX)
215
+
216
+ over = Overflow.new "S"
217
+ over.set(0)
218
+ over -= 1
219
+ expect(over.to_i).to eq(USHRT_MAX)
220
+ end
221
+
222
+ it "- 32bit" do
223
+ over = Overflow.new "l"
224
+ over.set(INT_MIN)
225
+ over -= 1
226
+ expect(over.to_i).to eq(INT_MAX)
227
+
228
+ over = Overflow.new "L"
229
+ over.set(0)
230
+ over -= 1
231
+ expect(over.to_i).to eq(UINT_MAX)
232
+ end
233
+
234
+ it "- 64bit" do
235
+ over = Overflow.new "q"
236
+ over.set(LLONG_MIN)
237
+ over -= 1
238
+ expect(over.to_i).to eq(LLONG_MAX)
239
+
240
+ over = Overflow.new "Q"
241
+ over.set(0)
242
+ over -= 1
243
+ expect(over.to_i).to eq(ULLONG_MAX)
244
+ end
245
+
246
+ it "* 8bit" do
247
+ over = Overflow.new "c"
248
+ over.set(CHAR_MAX)
249
+ over *= CHAR_MAX
250
+ expect(over.to_i).to eq(1)
251
+
252
+ over.set(CHAR_MIN)
253
+ over *= CHAR_MIN
254
+ expect(over.to_i).to eq(0)
255
+
256
+ over = Overflow.new "C"
257
+ over.set(UCHAR_MAX)
258
+ over *= UCHAR_MAX
259
+ expect(over.to_i).to eq(1)
260
+ end
261
+
262
+ it "* 16bit" do
263
+ over = Overflow.new "s"
264
+ over.set(SHRT_MAX)
265
+ over *= SHRT_MAX
266
+ expect(over.to_i).to eq(1)
267
+
268
+ over.set(SHRT_MIN)
269
+ over *= SHRT_MIN
270
+ expect(over.to_i).to eq(0)
271
+
272
+ over = Overflow.new "S"
273
+ over.set(USHRT_MAX)
274
+ over *= USHRT_MAX
275
+ expect(over.to_i).to eq(1)
276
+ end
277
+
278
+ it "* 32bit" do
279
+ over = Overflow.new "l"
280
+ over.set(INT_MAX)
281
+ over *= INT_MAX
282
+ expect(over.to_i).to eq(1)
283
+
284
+ over.set(INT_MIN)
285
+ over *= INT_MIN
286
+ expect(over.to_i).to eq(0)
287
+
288
+ over = Overflow.new "L"
289
+ over.set(UINT_MAX)
290
+ over *= UINT_MAX
291
+ expect(over.to_i).to eq(1)
292
+ end
293
+
294
+ it "* 64bit" do
295
+ over = Overflow.new "q"
296
+ over.set(LLONG_MAX)
297
+ over *= LLONG_MAX
298
+ expect(over.to_i).to eq(1)
299
+
300
+ over.set(LLONG_MIN)
301
+ over *= LLONG_MIN
302
+ expect(over.to_i).to eq(0)
303
+
304
+ over = Overflow.new "Q"
305
+ over.set(ULLONG_MAX)
306
+ over *= ULLONG_MAX
307
+ expect(over.to_i).to eq(1)
308
+ end
309
+
310
+ end
311
+
312
+
313
+
@@ -0,0 +1 @@
1
+ require 'overflow'
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: overflow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake-compiler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.3
69
+ description: Overflow is a class to overflow calculated as C language in Ruby.
70
+ email: co000ri@gmail.com
71
+ executables: []
72
+ extensions:
73
+ - ext/overflow/extconf.rb
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - ext/overflow/extconf.rb
83
+ - ext/overflow/init.c
84
+ - lib/overflow.rb
85
+ - lib/overflow/version.rb
86
+ - overflow.gemspec
87
+ - spec/mem_spec.rb
88
+ - spec/overflow_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: ''
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.0
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Overflow is a class to overflow calculated as C language in Ruby.
114
+ test_files:
115
+ - spec/mem_spec.rb
116
+ - spec/overflow_spec.rb
117
+ - spec/spec_helper.rb
118
+ has_rdoc: