circuits 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzQwY2JkYzJjMDVkZWNmMjFlZTI0ZWQzZDNmOGQwMWIxODE5YmVlMw==
4
+ NzM4MzA1YTA1NmFhMjY1ZmMzYzY1ZTgxOWY2MDlhMTQ3YzVkY2Q3OA==
5
5
  data.tar.gz: !binary |-
6
- YTY3NGI2Nzc3MzBlZjZjZDg1MWEyMzg0NDc3MWY5ODBhZDYxNWJjYg==
6
+ MzhmN2Y4ZTdiODliY2ZkYjQyNjRjODk0OWZmNmYwNmE4NDRjMGQ1Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjcwM2U4YzY1N2JiYWVmZWIwNWE3ZGFmYmFmNWRkMTg4MWVlYTU2MzRiMzM0
10
- ZmFkMDI2MmM0NDBjY2I1Y2ViODJjMTMwN2EzNTY3ZmRhYjA1Zjk1MDhiNTA1
11
- OGZlM2ZkYjBkMzU5ZjNkNmZhOGFmNTNkNWQxNmJjZTQzNjE2M2U=
9
+ ZGRlNzY4OThhNmRmNWE4MDQ1NzdjMDk2NmEzNzM1YTQ3ZWRiMGMzNGExMTM5
10
+ N2JiMmVhNDI3ODQ1MzM1YmQ1NmNiNjExZWJmZDA4MzI0YmUzMTc2YjE4NGI3
11
+ OWY5NGIzNjE3Nzc4NmQ4NDA4MTE2ZTExMWVjZGFmMTM3ZGViNzg=
12
12
  data.tar.gz: !binary |-
13
- MWRjYmY5MjhhZTJiMWZiNjgwZjgyZjMzMTkxYWUwODBkZTVjMDRkNTVjZTUy
14
- MWMxOTVmYWQ4M2RkZDdiYzUwYWMyMjAwNTkyMmQ1NzgyNGE4ZTgzZTlkYWMx
15
- ZmU4M2M0ZjU5Mzg4NzhhYTkwYzZhNTIwOGEyMjUzZTk4NjE4MjA=
13
+ ZjFmOTRjYWM1NDExNDgzMDEwMTFiNTQ1ZTI3NjYyNzA4MzUyNWIwNjYzNjk1
14
+ MDU5ODQ4ZDY5M2Y2MWVjYjJmNTE3OTIxMTliZjNlNzRjNzgxOWRiYWQ5NWFk
15
+ ZGQyM2U4YmMzYjBjZGI3OGVmOWJiYmVjYTYwNTA1Yzk2ODM4YWM=
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical AND Operator
6
6
  class And < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical AND of the inputs
14
8
  def tick
15
9
  self[:out].set(inputs.map(&:get).inject(:&))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -10,17 +10,22 @@ module Circuits
10
10
  class Base
11
11
  # Creates the Component with inputs and outputs
12
12
  # @param opts [Hash] options to create the Component with
13
- # @option opts [Array<Input>, FixNum] :inputs The array of inputs to use,
14
- # or the number of inputs to create
13
+ # @option opts [FixNum] :input_count The number of inputs
14
+ # @option opts [FixNum] :ouput_count The number of outputs
15
+ # @option opts [Hash] :port_mappings The port_mappings to use
15
16
  def initialize(opts = {})
16
- create_inputs opts
17
- create_outputs opts
17
+ input_count = opts[:input_count] || default_input_count
18
+ output_count = opts[:output_count] || default_output_count
19
+ @inputs = input_count.times.collect { Circuits::Terminal::Input.new }
20
+ @outputs = output_count.times.collect { Circuits::Terminal::Output.new }
21
+ @port_mappings = opts[:port_mappings] || default_port_mappings
18
22
  end
19
23
 
20
- # Does the internal computation and sets the outputs
21
- def tick
22
- fail NotImplementedError
23
- end
24
+ # the inputs of this component
25
+ attr_reader :inputs
26
+
27
+ # the outputs of this component
28
+ attr_reader :outputs
24
29
 
25
30
  # Sets all the outputs expose what was set in #tick
26
31
  def tock
@@ -31,83 +36,46 @@ module Circuits
31
36
  # @param port [Symbol] The symbol that represents the terminal
32
37
  # @return [Input, Output] The terminal
33
38
  def [](port)
34
- p = port_mappings[port]
35
- case p[:type]
36
- when :input
37
- inputs[p[:number]]
38
- when :output
39
- outputs[p[:number]]
40
- end
41
- end
42
-
43
- # Assigns to an input or output
44
- # @param port [Symbol] The symbol that represents the terminal
45
- # @param terminal [Input, Output] The terminal to assign
46
- # @return [Input, Output] The terminal that was passed in
47
- def []=(port, terminal)
48
- p = port_mappings[port]
49
- case p[:type]
39
+ port_mapping = port_mappings[port]
40
+ port_number = port_mapping[:number]
41
+ case port_mapping[:type]
50
42
  when :input
51
- inputs[p[:number]] = terminal
43
+ inputs[port_number]
52
44
  when :output
53
- outputs[p[:number]] = terminal
45
+ outputs[port_number]
54
46
  end
55
47
  end
56
48
 
57
- # the inputs of this component
58
- attr_reader :inputs
59
-
60
- # the outputs of this component
61
- attr_reader :outputs
62
-
63
49
  private
64
50
 
65
- def create_inputs(opts)
66
- if opts[:inputs].class == Array
67
- @inputs = opts[:inputs]
68
- @input_count = @inputs.length
69
- elsif opts[:inputs].class == Fixnum
70
- @input_count = opts[:inputs]
71
- end
72
- @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
73
- end
51
+ attr_reader :port_mappings
74
52
 
75
- def create_outputs(opts)
76
- if opts[:outputs].class == Array
77
- @outputs = opts[:outputs]
78
- @output_count = @outputs.length
79
- elsif opts[:outputs].class == Fixnum
80
- @output_count = opts[:outputs]
81
- end
82
- @outputs ||= @output_count.times.collect do
83
- Circuits::Terminal::Output.new
53
+ def default_port_mappings
54
+ res = {}
55
+ (input_mappings + output_mappings).each do |mapping|
56
+ res.merge!(mapping)
84
57
  end
85
- end
86
-
87
- def port_mappings
88
- return @port_mappings unless @port_mappings.nil?
89
- @port_mappings = {}
90
- input_mappings.each { |x| @port_mappings.merge!(x) }
91
- output_mappings.each { |x| @port_mappings.merge!(x) }
92
- @port_mappings
58
+ res
93
59
  end
94
60
 
95
61
  def input_mappings
96
- return [{ in: { type: :input, number: 0 } }] if @input_count == 1
97
- @input_count.times.collect do |i|
98
- { num_to_port(i) => { type: :input, number: i } }
62
+ input_count = inputs.length
63
+ return [{ in: { type: :input, number: 0 } }] if input_count == 1
64
+ input_count.times.collect do |num|
65
+ { num_to_port(num) => { type: :input, number: num } }
99
66
  end
100
67
  end
101
68
 
102
69
  def output_mappings
103
- return[{ out: { type: :output, number: 0 } }] if @output_count == 1
104
- @output_count.times.collect do |i|
105
- { num_to_port(i + @input_count) => { type: :output, number: i } }
70
+ output_count = outputs.length
71
+ return[{ out: { type: :output, number: 0 } }] if output_count == 1
72
+ output_count.times.collect do |num|
73
+ { num_to_port(num + inputs.length) => { type: :output, number: num } }
106
74
  end
107
75
  end
108
76
 
109
- def num_to_port(i)
110
- (i + 'a'.ord).chr.to_sym
77
+ def num_to_port(num)
78
+ (num + 'a'.ord).chr.to_sym
111
79
  end
112
80
  end
113
81
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical NAND Operator
6
6
  class Nand < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical NAND of the inputs
14
8
  def tick
15
9
  self[:out].set(!inputs.map(&:get).inject(:&))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical NOR Operator
6
6
  class Nor < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical OR of the inputs
14
8
  def tick
15
9
  self[:out].set(!inputs.map(&:get).inject(:|))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical NOT Operator
6
6
  class Not < Base
7
- def initialize(opts = {})
8
- @input_count = 1
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical NOT of the inputs
14
8
  def tick
15
9
  self[:out].set(!self[:in].get)
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 1
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical OR Operator
6
6
  class Or < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical OR of the inputs
14
8
  def tick
15
9
  self[:out].set(inputs.map(&:get).inject(:|))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -5,9 +5,11 @@ module Circuits
5
5
  module Component
6
6
  # SR NAND Latch
7
7
  class SrNand < Base
8
- def initialize(opts = {})
9
- set_defaults
10
- super opts
8
+ def initialize
9
+ super(port_mappings: { not_s: { type: :input, number: 0 },
10
+ not_r: { type: :input, number: 1 },
11
+ q: { type: :output, number: 0 },
12
+ not_q: { type: :output, number: 1 } })
11
13
  create_sub_components
12
14
  link_inputs
13
15
  link_outputs
@@ -24,38 +26,35 @@ module Circuits
24
26
 
25
27
  private
26
28
 
27
- attr_reader :nand_1, :nand_2, :sub_components
29
+ attr_reader :nand_s, :nand_r, :sub_components
28
30
 
29
31
  def create_sub_components
30
- @nand_1 = Nand.new
31
- @nand_2 = Nand.new
32
- @sub_components = [@nand_1, @nand_2]
32
+ @nand_s = Nand.new
33
+ @nand_r = Nand.new
34
+ @sub_components = [@nand_s, @nand_r]
35
+ end
36
+
37
+ def default_input_count
38
+ 2
39
+ end
40
+
41
+ def default_output_count
42
+ 2
33
43
  end
34
44
 
35
45
  def link_inputs
36
- nand_1[:a].set self[:not_s]
37
- nand_2[:a].set self[:not_r]
46
+ nand_s[:a].set self[:not_s]
47
+ nand_r[:a].set self[:not_r]
38
48
  end
39
49
 
40
50
  def link_outputs
41
- self[:q].set nand_1[:out]
42
- self[:not_q].set nand_2[:out]
51
+ self[:q].set nand_s[:out]
52
+ self[:not_q].set nand_r[:out]
43
53
  end
44
54
 
45
55
  def link_sub_components
46
- nand_1[:b].set nand_2[:out]
47
- nand_2[:b].set nand_1[:out]
48
- end
49
-
50
- def set_defaults
51
- @input_count = 2
52
- @output_count = 2
53
- @port_mappings = {
54
- not_s: { type: :input, number: 0 },
55
- not_r: { type: :input, number: 1 },
56
- q: { type: :output, number: 0 },
57
- not_q: { type: :output, number: 1 }
58
- }
56
+ nand_s[:b].set nand_r[:out]
57
+ nand_r[:b].set nand_s[:out]
59
58
  end
60
59
  end
61
60
  end
@@ -5,9 +5,11 @@ module Circuits
5
5
  module Component
6
6
  # SR NOR Latch
7
7
  class SrNor < Base
8
- def initialize(opts = {})
9
- set_defaults
10
- super opts
8
+ def initialize
9
+ super(port_mappings: { r: { type: :input, number: 0 },
10
+ s: { type: :input, number: 1 },
11
+ q: { type: :output, number: 0 },
12
+ not_q: { type: :output, number: 1 } })
11
13
  create_sub_components
12
14
  link_inputs
13
15
  link_outputs
@@ -24,38 +26,35 @@ module Circuits
24
26
 
25
27
  private
26
28
 
27
- attr_reader :nor_1, :nor_2, :sub_components
29
+ attr_reader :nor_r, :nor_s, :sub_components
28
30
 
29
31
  def create_sub_components
30
- @nor_1 = Nor.new
31
- @nor_2 = Nor.new
32
- @sub_components = [@nor_1, @nor_2]
32
+ @nor_r = Nor.new
33
+ @nor_s = Nor.new
34
+ @sub_components = [@nor_r, @nor_s]
35
+ end
36
+
37
+ def default_input_count
38
+ 2
39
+ end
40
+
41
+ def default_output_count
42
+ 2
33
43
  end
34
44
 
35
45
  def link_inputs
36
- nor_1[:a].set self[:r]
37
- nor_2[:a].set self[:s]
46
+ nor_r[:a].set self[:r]
47
+ nor_s[:a].set self[:s]
38
48
  end
39
49
 
40
50
  def link_outputs
41
- self[:q].set nor_1[:out]
42
- self[:not_q].set nor_2[:out]
51
+ self[:q].set nor_r[:out]
52
+ self[:not_q].set nor_s[:out]
43
53
  end
44
54
 
45
55
  def link_sub_components
46
- nor_1[:b].set nor_2[:out]
47
- nor_2[:b].set nor_1[:out]
48
- end
49
-
50
- def set_defaults
51
- @input_count = 2
52
- @output_count = 2
53
- @port_mappings = {
54
- r: { type: :input, number: 0 },
55
- s: { type: :input, number: 1 },
56
- q: { type: :output, number: 0 },
57
- not_q: { type: :output, number: 1 }
58
- }
56
+ nor_r[:b].set nor_s[:out]
57
+ nor_s[:b].set nor_r[:out]
59
58
  end
60
59
  end
61
60
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical XNOR Operator
6
6
  class Xnor < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical XNOR of the inputs
14
8
  def tick
15
9
  self[:out].set(!inputs.map(&:get).inject(:^))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -4,16 +4,20 @@ module Circuits
4
4
  module Component
5
5
  # Logical XOR Operator
6
6
  class Xor < Base
7
- def initialize(opts = {})
8
- @input_count = 2
9
- @output_count = 1
10
- super opts
11
- end
12
-
13
7
  # Sets the output to be the result of a logical XOR of the inputs
14
8
  def tick
15
9
  self[:out].set(inputs.map(&:get).inject(:^))
16
10
  end
11
+
12
+ private
13
+
14
+ def default_input_count
15
+ 2
16
+ end
17
+
18
+ def default_output_count
19
+ 1
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -6,6 +6,7 @@ module Circuits
6
6
  # Creates the output
7
7
  # @param opts [Hash] Options to create the Output with
8
8
  # @option opts [Boolean] :state The initial state of the Output
9
+ # @option opts [Input, Output] :terminal The terminal to read from
9
10
  def initialize(opts = {})
10
11
  @next_state = opts[:terminal] || opts[:state] || false
11
12
  tock
@@ -18,7 +19,7 @@ module Circuits
18
19
  end
19
20
 
20
21
  # The next state
21
- # @param [Boolean, Terminal] terminal The terminal or state to output
22
+ # @param [Boolean, Terminal] state The terminal or state to output
22
23
  def set(state)
23
24
  @next_state = state
24
25
  end
@@ -1,5 +1,5 @@
1
1
  # Circuits allows you to express logical circuits in code
2
2
  module Circuits
3
3
  # The version of the Circuits gem
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,4 @@
1
1
  require 'coveralls'
2
- Coveralls.wear!
2
+ require 'simplecov'
3
+
4
+ SimpleCov.start
@@ -58,7 +58,7 @@ describe Circuits::Component::And do
58
58
 
59
59
  [3, 4, 8].each do |n|
60
60
  context "with #{n} inputs" do
61
- subject { Circuits::Component::And.new inputs: n }
61
+ subject { Circuits::Component::And.new input_count: n }
62
62
 
63
63
  before do
64
64
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -2,100 +2,52 @@ require 'spec_helper'
2
2
  require 'circuits/component/base'
3
3
 
4
4
  # Mock component to include Circuits::Component::Base
5
- class MockComponent < Circuits::Component::Base
6
- def initialize(opts = {})
7
- @input_count = 1
8
- @output_count = 1
9
- super opts
5
+ class MockComponent1 < Circuits::Component::Base
6
+ def default_input_count
7
+ 1
10
8
  end
11
- end
12
-
13
- describe Circuits::Component::Base do
14
- context 'when using defaults' do
15
- subject { MockComponent.new }
16
-
17
- it 'has one input' do
18
- expect(subject.inputs.count).to eq(1)
19
- end
20
-
21
- it 'has one output' do
22
- expect(subject.outputs.count).to eq(1)
23
- end
24
9
 
25
- describe '#[]=' do
26
- let(:new_input) { double('new_input') }
27
- let(:new_output) { double('new_output') }
28
-
29
- before do
30
- subject[:in] = new_input
31
- subject[:out] = new_output
32
- end
33
-
34
- it 'has the new input available as :in' do
35
- expect(subject[:in]).to eq(new_input)
36
- end
37
-
38
- it 'has the new output available as :out' do
39
- expect(subject[:out]).to eq(new_output)
40
- end
41
- end
10
+ def default_output_count
11
+ 1
42
12
  end
13
+ end
43
14
 
44
- context 'when specifying input and output count' do
45
- subject { MockComponent.new(inputs: 2, outputs: 2) }
46
-
47
- it 'has one input' do
48
- expect(subject.inputs.count).to eq(2)
49
- end
50
-
51
- it 'has one output' do
52
- expect(subject.outputs.count).to eq(2)
53
- end
15
+ # Mock component to include Circuits::Component::Base
16
+ class MockComponent2 < Circuits::Component::Base
17
+ def default_input_count
18
+ 2
54
19
  end
55
20
 
56
- context 'when supplying inputs and outputs' do
57
- let(:inputs) { [double('input')] }
58
- let(:outputs) { [double('output')] }
21
+ def default_output_count
22
+ 2
23
+ end
24
+ end
59
25
 
60
- subject { MockComponent.new(inputs: inputs, outputs: outputs) }
26
+ describe Circuits::Component::Base do
27
+ describe '#[]' do
28
+ context 'one input and one output' do
29
+ subject { MockComponent1.new }
61
30
 
62
- describe '#tick' do
63
- it 'raises NotImplementedError' do
64
- expect { subject.tick }.to raise_error(NotImplementedError)
31
+ it 'has the input available as :in' do
32
+ expect(subject[:in]).to eq(subject.inputs[0])
65
33
  end
66
- end
67
34
 
68
- describe '#tock' do
69
- it 'tocks the outputs' do
70
- expect(outputs[0]).to receive(:tock)
71
- subject.tock
35
+ it 'has the output available as :out' do
36
+ expect(subject[:out]).to eq(subject.outputs[0])
72
37
  end
73
38
  end
74
39
 
75
- describe '#[]' do
76
- context 'one input and one output' do
77
- it 'has the input available as :in' do
78
- expect(subject[:in]).to eq(inputs[0])
79
- end
40
+ context 'two inputs and two outputs' do
41
+ subject { MockComponent2.new }
80
42
 
81
- it 'has the output available as :out' do
82
- expect(subject[:out]).to eq(outputs[0])
83
- end
43
+ it 'has the inputs available as :a and :b' do
44
+ expect(subject[:a]).to eq(subject.inputs[0])
45
+ expect(subject[:b]).to eq(subject.inputs[1])
84
46
  end
85
47
 
86
- context 'two inputs and two outputs' do
87
- let(:inputs) { [double('input_1'), double('input_2')] }
88
- let(:outputs) { [double('output_1'), double('output_2')] }
89
-
90
- it 'has the inputs available as :a and :b' do
91
- expect(subject[:a]).to eq(inputs[0])
92
- expect(subject[:b]).to eq(inputs[1])
93
- end
94
-
95
- it 'has the outputs available as :c and :d' do
96
- expect(subject[:c]).to eq(outputs[0])
97
- expect(subject[:d]).to eq(outputs[1])
98
- end
48
+ it 'has the outputs available as :c and :d' do
49
+ expect(subject[:c]).to eq(subject.outputs[0])
50
+ expect(subject[:d]).to eq(subject.outputs[1])
99
51
  end
100
52
  end
101
53
  end
@@ -57,7 +57,7 @@ describe Circuits::Component::Nand do
57
57
  end
58
58
  [3, 4, 8].each do |n|
59
59
  context "with #{n} inputs" do
60
- subject { Circuits::Component::Nand.new inputs: n }
60
+ subject { Circuits::Component::Nand.new input_count: n }
61
61
 
62
62
  before do
63
63
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -57,7 +57,7 @@ describe Circuits::Component::Nor do
57
57
  end
58
58
  [3, 4, 8].each do |n|
59
59
  context "with #{n} inputs" do
60
- subject { Circuits::Component::Nor.new inputs: n }
60
+ subject { Circuits::Component::Nor.new input_count: n }
61
61
 
62
62
  before do
63
63
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -57,7 +57,7 @@ describe Circuits::Component::Or do
57
57
  end
58
58
  [3, 4, 8].each do |n|
59
59
  context "with #{n} inputs" do
60
- subject { Circuits::Component::Or.new inputs: n }
60
+ subject { Circuits::Component::Or.new input_count: n }
61
61
 
62
62
  before do
63
63
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -58,7 +58,7 @@ describe Circuits::Component::Xnor do
58
58
  context 'when the number of inputs is even' do
59
59
  [2, 4, 8].each do |n|
60
60
  context "with #{n} inputs" do
61
- subject { Circuits::Component::Xnor.new inputs: n }
61
+ subject { Circuits::Component::Xnor.new input_count: n }
62
62
 
63
63
  before do
64
64
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -107,7 +107,7 @@ describe Circuits::Component::Xnor do
107
107
  context 'when the number of inputs is odd' do
108
108
  [3, 5, 7].each do |n|
109
109
  context "with #{n} inputs" do
110
- subject { Circuits::Component::Xnor.new inputs: n }
110
+ subject { Circuits::Component::Xnor.new input_count: n }
111
111
 
112
112
  before do
113
113
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -58,7 +58,7 @@ describe Circuits::Component::Xor do
58
58
  context 'when the number of inputs is even' do
59
59
  [2, 4, 8].each do |n|
60
60
  context "with #{n} inputs" do
61
- subject { Circuits::Component::Xor.new inputs: n }
61
+ subject { Circuits::Component::Xor.new input_count: n }
62
62
 
63
63
  before do
64
64
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -107,7 +107,7 @@ describe Circuits::Component::Xor do
107
107
  context 'when the number of inputs is odd' do
108
108
  [3, 5, 7].each do |n|
109
109
  context "with #{n} inputs" do
110
- subject { Circuits::Component::Xor.new inputs: n }
110
+ subject { Circuits::Component::Xor.new input_count: n }
111
111
 
112
112
  before do
113
113
  n.times { |x| subject.inputs[x].set inputs[x] }
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'circuits/terminal/input'
3
+
4
+ describe Circuits::Terminal::Input do
5
+ describe '#get' do
6
+ context 'when given no state' do
7
+ subject { Circuits::Terminal::Input.new }
8
+
9
+ it 'is false' do
10
+ expect(subject.get).to eq(false)
11
+ end
12
+ end
13
+ end
14
+
15
+ describe '#set' do
16
+ let(:state) { double('state') }
17
+
18
+ context 'when given a state' do
19
+ subject { Circuits::Terminal::Input.new(state: state) }
20
+
21
+ it 'has that state' do
22
+ subject.set state
23
+ expect(subject.get).to eq(state)
24
+ end
25
+ end
26
+
27
+ context 'when given an input' do
28
+ let(:input) { Circuits::Terminal::Input.new(state: state) }
29
+
30
+ subject { Circuits::Terminal::Input.new }
31
+
32
+ it 'has the input state' do
33
+ subject.set input
34
+ expect(subject.get).to eq(state)
35
+ end
36
+ end
37
+
38
+ context 'when given an output' do
39
+ let(:output) { Circuits::Terminal::Output.new(state: state) }
40
+
41
+ subject { Circuits::Terminal::Input.new }
42
+
43
+ it 'has the output state' do
44
+ subject.set output
45
+ expect(subject.get).to eq(state)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+ require 'circuits/terminal/output'
3
+
4
+ describe Circuits::Terminal::Output do
5
+ describe '#get' do
6
+ let(:state) { double('state') }
7
+
8
+ context 'when given no state' do
9
+ subject { Circuits::Terminal::Output.new }
10
+
11
+ it 'is false' do
12
+ expect(subject.get).to eq(false)
13
+ end
14
+ end
15
+
16
+ context 'when given a state' do
17
+ subject { Circuits::Terminal::Output.new(state: state) }
18
+
19
+ it 'has that state' do
20
+ expect(subject.get).to eq(state)
21
+ end
22
+ end
23
+
24
+ context 'when given an input' do
25
+ let(:input) { Circuits::Terminal::Input.new(state: state) }
26
+
27
+ subject { Circuits::Terminal::Output.new(terminal: input) }
28
+
29
+ it 'has the input state' do
30
+ expect(subject.get).to eq(state)
31
+ end
32
+ end
33
+
34
+ context 'when given an output' do
35
+ let(:output) { Circuits::Terminal::Output.new(state: state) }
36
+
37
+ subject { Circuits::Terminal::Output.new(terminal: output) }
38
+
39
+ it 'has the output state' do
40
+ expect(subject.get).to eq(state)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#set' do
46
+ let(:state_1) { double('state_1') }
47
+ let(:state_2) { double('state_2') }
48
+
49
+ subject { Circuits::Terminal::Output.new(state: state_1) }
50
+
51
+ context 'when given a state' do
52
+ it 'gets does not get set immediately' do
53
+ subject.set state_2
54
+ expect(subject.get).to eq(state_1)
55
+ end
56
+ end
57
+
58
+ context 'when given an input' do
59
+ let(:input) { Circuits::Terminal::Input.new(state: state_2) }
60
+
61
+ it 'gets does not get set immediately' do
62
+ subject.set input
63
+ expect(subject.get).to eq(state_1)
64
+ end
65
+ end
66
+
67
+ context 'when given an output' do
68
+ let(:output) { Circuits::Terminal::Output.new(state: state_2) }
69
+
70
+ it 'gets does not get set immediately' do
71
+ subject.set output
72
+ expect(subject.get).to eq(state_1)
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#tock' do
78
+ let(:state_1) { double('state_1') }
79
+ let(:state_2) { double('state_2') }
80
+
81
+ subject { Circuits::Terminal::Output.new(state: state_1) }
82
+
83
+ context 'when given a state' do
84
+ it 'updates the state' do
85
+ subject.set state_2
86
+ subject.tock
87
+ expect(subject.get).to eq(state_2)
88
+ end
89
+ end
90
+
91
+ context 'when given an input' do
92
+ let(:input) { Circuits::Terminal::Input.new(state: state_2) }
93
+
94
+ it 'updates the state' do
95
+ subject.set input
96
+ subject.tock
97
+ expect(subject.get).to eq(state_2)
98
+ end
99
+ end
100
+
101
+ context 'when given an output' do
102
+ let(:output) { Circuits::Terminal::Output.new(state: state_2) }
103
+
104
+ it 'updates the state' do
105
+ subject.set output
106
+ subject.tock
107
+ expect(subject.get).to eq(state_2)
108
+ end
109
+ end
110
+ end
111
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Muru Paenga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,6 +148,8 @@ files:
148
148
  - spec/unit/circuits/component/sr_nor_spec.rb
149
149
  - spec/unit/circuits/component/xnor_spec.rb
150
150
  - spec/unit/circuits/component/xor_spec.rb
151
+ - spec/unit/circuits/terminal/input_spec.rb
152
+ - spec/unit/circuits/terminal/output_spec.rb
151
153
  homepage: https://github.com/meringu/circuits
152
154
  licenses:
153
155
  - MIT
@@ -184,3 +186,5 @@ test_files:
184
186
  - spec/unit/circuits/component/sr_nor_spec.rb
185
187
  - spec/unit/circuits/component/xnor_spec.rb
186
188
  - spec/unit/circuits/component/xor_spec.rb
189
+ - spec/unit/circuits/terminal/input_spec.rb
190
+ - spec/unit/circuits/terminal/output_spec.rb