diff --git a/Gemfile b/Gemfile index 00c7933..0468dba 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source "https://rubygems.org" gem "yard" gem "pry" +gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock index eb0675d..634fbac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,10 +2,24 @@ GEM remote: https://rubygems.org/ specs: coderay (1.1.2) + diff-lcs (1.3) method_source (0.9.0) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.2) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.3) yard (0.9.14) PLATFORMS @@ -13,7 +27,8 @@ PLATFORMS DEPENDENCIES pry + rspec yard BUNDLED WITH - 1.16.1 + 1.17.2 diff --git a/lib/rcircuit/gate.rb b/lib/rcircuit/gate.rb index e1bc01f..9124f91 100644 --- a/lib/rcircuit/gate.rb +++ b/lib/rcircuit/gate.rb @@ -43,6 +43,7 @@ class Gate # @param table [Array>] The truth table, # which is an array of entries. The last value in the entry is the expected output, # the rest are the inputs to the ports. + # @return [Boolean] Whether the truth table matches the gate's behavior def self.test_table(table) ports=[] (table[0].length-1).times do diff --git a/lib/rcircuit/not.rb b/lib/rcircuit/not.rb index 3eb23c8..c828f49 100644 --- a/lib/rcircuit/not.rb +++ b/lib/rcircuit/not.rb @@ -1,13 +1,6 @@ # Represents a NOT gate class NotGate < Gate - # (see Gate#initialize) - def initialize(*args) - @outmask=0 - super - @outmask=(2**@width)-1 - end - # Add a port to the gate. As this is a NOT gate, there may only be one port. # @param (see Gate#add_input) # @return [void] @@ -22,6 +15,6 @@ class NotGate < Gate # Calculates NOT of input and sets output port to that value. # @param vals [Array] List of values for connected ports. def inputs_changed(vals) - out.setval((~vals[0]) & @outmask) + out.setval((~vals[0]) & ((2**@width)-1)) end end diff --git a/lib/rcircuit/vcd.rb b/lib/rcircuit/vcd.rb index aaf6989..eaaa04c 100644 --- a/lib/rcircuit/vcd.rb +++ b/lib/rcircuit/vcd.rb @@ -2,7 +2,8 @@ class VCD # @!attribute [w] fd - # @api private + # @api private + # Used for testing to set the file to an RSpec double. attr_writer :fd # @param filename [String] The filename to output to # @param timescale [String] The timescale diff --git a/spec/gate_spec.rb b/spec/gate_spec.rb index 416071c..6e43a94 100644 --- a/spec/gate_spec.rb +++ b/spec/gate_spec.rb @@ -10,6 +10,21 @@ describe Gate do end end end + + it "makes sure that all ports have the same width on creation" do + a=Port.new(8) + b=Port.new(7) + expect{klass.new(a,b)}.to raise_error ArgumentError,"Incorrect width 7, expected 8" + end + + it "makes sure that all ports have the same width on adding a port" do + a=Port.new(8) + b=Port.new(8) + gate=klass.new(a,b) + port=Port.new(7) + expect{gate.add_input(port)}.to raise_error ArgumentError,"Incorrect width 7, expected 8" + end + it "calls #inputs_changed when input values change or an input is added" do a=Port.new(8) b=Port.new(8)