mug 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug.rb +1 -0
- data/lib/mug/array/extend.rb +1 -1
- data/lib/mug/bittest.rb +72 -0
- data/test/test-array-samples.rb +3 -1
- data/test/test-bittest.rb +121 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed5f24e06ddab5269fc3e36f7ad473aa0767c68e
|
4
|
+
data.tar.gz: 1d018601b7dc7f8012d08b21005f49bb6cf2fabc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1f704861c0d8d01dc643d25e0b8d5bc3cbb3db3e9f19baa4e239c8e92b1fdabaaf7ea8f4498f66d89b2a24d2c2d7ca5055567794456ef86acef4955517224fe
|
7
|
+
data.tar.gz: 22736ef5315007ae6f140e9ecc50198518208fbf2f4d19eca219e3a4b56846f631044dab7f0b0975322a88c63bde7fb348345108e685e49dee438fa36886e53f
|
data/lib/mug.rb
CHANGED
@@ -6,6 +6,7 @@ require_relative 'mug/array/extend'
|
|
6
6
|
require_relative 'mug/array/minus'
|
7
7
|
require_relative 'mug/array/samples'
|
8
8
|
require_relative 'mug/bool'
|
9
|
+
require_relative 'mug/bittest'
|
9
10
|
require_relative 'mug/clamp'
|
10
11
|
require_relative 'mug/counts'
|
11
12
|
require_relative 'mug/fragile-method-chain'
|
data/lib/mug/array/extend.rb
CHANGED
@@ -25,7 +25,7 @@ class Array
|
|
25
25
|
raise ArgumentError, "wrong number of arguments (#{rest.length+1} for 1..2)" if rest.length > 1
|
26
26
|
|
27
27
|
# Same logic as array.c/rb_ary_initialize
|
28
|
-
if rest.empty? && !size.is_a?(
|
28
|
+
if rest.empty? && !size.is_a?(Integer)
|
29
29
|
warn 'warning: given block not used' if block_given?
|
30
30
|
concat size.to_ary
|
31
31
|
return self
|
data/lib/mug/bittest.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
class Integer
|
3
|
+
|
4
|
+
##
|
5
|
+
# Tests common bits in +this+ AND +other+.
|
6
|
+
#
|
7
|
+
# test:
|
8
|
+
# :any => true if any bits are set
|
9
|
+
# :all => true if all bits are set
|
10
|
+
#
|
11
|
+
def and? other, test: :any
|
12
|
+
case test.to_sym
|
13
|
+
when :any
|
14
|
+
and_any? other
|
15
|
+
when :all
|
16
|
+
and_all? other
|
17
|
+
else
|
18
|
+
raise ArgumentError, "invalid value for 'test' (given #{test.inspect}, should be :any or :all)"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
##
|
23
|
+
# True if +this+ AND +other+ is non-zero.
|
24
|
+
#
|
25
|
+
# i.e. if any set bits in +other+ are set in +this+.
|
26
|
+
#
|
27
|
+
def and_any? other
|
28
|
+
return false if other.zero?
|
29
|
+
self & other != 0
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# True if +this+ AND +other+ is +other+.
|
34
|
+
#
|
35
|
+
# i.e. if all set bits in +other+ are set in +this+.
|
36
|
+
#
|
37
|
+
def and_all? other
|
38
|
+
return false if other.zero?
|
39
|
+
self & other == other
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# True if +this+ OR +other+ is non-zero.
|
44
|
+
#
|
45
|
+
def or? other
|
46
|
+
self | other != 0
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# True if +this+ XOR +other+ is non-zero.
|
51
|
+
#
|
52
|
+
def xor? other
|
53
|
+
self ^ other != 0
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
=begin
|
59
|
+
Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
60
|
+
|
61
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
62
|
+
purpose with or without fee is hereby granted, provided that the above
|
63
|
+
copyright notice and this permission notice appear in all copies.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
66
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
67
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
68
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
69
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
70
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
71
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
72
|
+
=end
|
data/test/test-array-samples.rb
CHANGED
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
$and_any = [
|
5
|
+
[0b00,0b00,false],
|
6
|
+
[0b00,0b01,false],
|
7
|
+
[0b00,0b10,false],
|
8
|
+
[0b00,0b11,false],
|
9
|
+
[0b01,0b00,false],
|
10
|
+
[0b01,0b01,true],
|
11
|
+
[0b01,0b10,false],
|
12
|
+
[0b01,0b11,true],
|
13
|
+
[0b10,0b00,false],
|
14
|
+
[0b10,0b01,false],
|
15
|
+
[0b10,0b10,true],
|
16
|
+
[0b10,0b11,true],
|
17
|
+
[0b11,0b00,false],
|
18
|
+
[0b11,0b01,true],
|
19
|
+
[0b11,0b10,true],
|
20
|
+
[0b11,0b11,true],
|
21
|
+
]
|
22
|
+
$and_all = [
|
23
|
+
[0b00,0b00,false],
|
24
|
+
[0b00,0b01,false],
|
25
|
+
[0b00,0b10,false],
|
26
|
+
[0b00,0b11,false],
|
27
|
+
[0b01,0b00,false],
|
28
|
+
[0b01,0b01,true],
|
29
|
+
[0b01,0b10,false],
|
30
|
+
[0b01,0b11,false],
|
31
|
+
[0b10,0b00,false],
|
32
|
+
[0b10,0b01,false],
|
33
|
+
[0b10,0b10,true],
|
34
|
+
[0b10,0b11,false],
|
35
|
+
[0b11,0b00,false],
|
36
|
+
[0b11,0b01,true],
|
37
|
+
[0b11,0b10,true],
|
38
|
+
[0b11,0b11,true],
|
39
|
+
]
|
40
|
+
$or = [
|
41
|
+
[0b00,0b00,false],
|
42
|
+
[0b00,0b01,true],
|
43
|
+
[0b00,0b10,true],
|
44
|
+
[0b00,0b11,true],
|
45
|
+
[0b01,0b00,true],
|
46
|
+
[0b01,0b01,true],
|
47
|
+
[0b01,0b10,true],
|
48
|
+
[0b01,0b11,true],
|
49
|
+
[0b10,0b00,true],
|
50
|
+
[0b10,0b01,true],
|
51
|
+
[0b10,0b10,true],
|
52
|
+
[0b10,0b11,true],
|
53
|
+
[0b11,0b00,true],
|
54
|
+
[0b11,0b01,true],
|
55
|
+
[0b11,0b10,true],
|
56
|
+
[0b11,0b11,true],
|
57
|
+
]
|
58
|
+
$xor = [
|
59
|
+
[0b00,0b00,false],
|
60
|
+
[0b00,0b01,true],
|
61
|
+
[0b00,0b10,true],
|
62
|
+
[0b00,0b11,true],
|
63
|
+
[0b01,0b00,true],
|
64
|
+
[0b01,0b01,false],
|
65
|
+
[0b01,0b10,true],
|
66
|
+
[0b01,0b11,true],
|
67
|
+
[0b10,0b00,true],
|
68
|
+
[0b10,0b01,true],
|
69
|
+
[0b10,0b10,false],
|
70
|
+
[0b10,0b11,true],
|
71
|
+
[0b11,0b00,true],
|
72
|
+
[0b11,0b01,true],
|
73
|
+
[0b11,0b10,true],
|
74
|
+
[0b11,0b11,false],
|
75
|
+
]
|
76
|
+
|
77
|
+
require_relative '../lib/mug/bittest'
|
78
|
+
class Test_bittest < Test::Unit::TestCase
|
79
|
+
|
80
|
+
def test_and?
|
81
|
+
$and_any.each do |l,r,x|
|
82
|
+
assert_equal( x, l.and?(r), "#{l} & #{r} #{x ? '=' : '!'}= 0" )
|
83
|
+
end
|
84
|
+
assert_raise( ArgumentError ) { 1.and?(0, :test=>:invalid) }
|
85
|
+
end
|
86
|
+
def test_and_any
|
87
|
+
$and_any.each do |l,r,x|
|
88
|
+
assert_equal( x, l.and?(r, :test=>:any), "#{l} & #{r} #{x ? '!' : '='}= 0" )
|
89
|
+
end
|
90
|
+
end
|
91
|
+
def test_and_all
|
92
|
+
$and_all.each do |l,r,x|
|
93
|
+
assert_equal( x, l.and?(r, :test=>:all), "#{l} & #{r} #{x ? '=' : '!'}= #{r}" )
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_and_any?
|
98
|
+
$and_any.each do |l,r,x|
|
99
|
+
assert_equal( x, l.and_any?(r), "#{l} & #{r} #{x ? '!' : '='}= 0" )
|
100
|
+
end
|
101
|
+
end
|
102
|
+
def test_and_all?
|
103
|
+
$and_all.each do |l,r,x|
|
104
|
+
assert_equal( x, l.and_all?(r), "#{l} & #{r} #{x ? '=' : '!'}= #{r}" )
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_or?
|
109
|
+
$or.each do |l,r,x|
|
110
|
+
assert_equal( x, l.or?(r), "#{l} | #{r} #{x ? '=' : '!'}= 0" )
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_xor?
|
115
|
+
$xor.each do |l,r,x|
|
116
|
+
assert_equal( x, l.xor?(r), "#{l} ^ #{r} #{x ? '!' : '='}= 0" )
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
== MUG: Matty's Ultimate Gem
|
@@ -30,6 +30,7 @@ files:
|
|
30
30
|
- lib/mug/array/extend.rb
|
31
31
|
- lib/mug/array/minus.rb
|
32
32
|
- lib/mug/array/samples.rb
|
33
|
+
- lib/mug/bittest.rb
|
33
34
|
- lib/mug/bool.rb
|
34
35
|
- lib/mug/clamp.rb
|
35
36
|
- lib/mug/counts.rb
|
@@ -63,6 +64,7 @@ files:
|
|
63
64
|
- test/test-array-extend.rb
|
64
65
|
- test/test-array-minus.rb
|
65
66
|
- test/test-array-samples.rb
|
67
|
+
- test/test-bittest.rb
|
66
68
|
- test/test-bool.rb
|
67
69
|
- test/test-clamp.rb
|
68
70
|
- test/test-counts.rb
|
@@ -112,6 +114,7 @@ test_files:
|
|
112
114
|
- test/test-rexproc.rb
|
113
115
|
- test/test-tau.rb
|
114
116
|
- test/test-matchdata_each.rb
|
117
|
+
- test/test-bittest.rb
|
115
118
|
- test/test-matchdata_hash.rb
|
116
119
|
- test/test-not.rb
|
117
120
|
- test/test-array-extend.rb
|