Class: TestEspresso
- Inherits:
-
Object
- Object
- TestEspresso
- Defined in:
- lib/logic_tools/test_logic_tools.rb
Overview
Class for testing the implementation of the ESPRESSO algorithm.
Instance Method Summary collapse
-
#initialize(seed = 0, deadline = Float::INFINITY, volume = Float::INFINITY) ⇒ TestEspresso
constructor
Creates the tester with a
seedfor random generation, adeadlinefor the simplify steps and avolumebefore splitting the cover. -
#same_truth_table?(cover0, cover1) ⇒ Boolean
Checks if covers have the same truth table.
-
#test_espresso(cover) ⇒ Object
Tests espresso on a given
cover. -
#test_espresso_all(test = nil) ⇒ Object
Tests the implementation of the espresso algorithm on each possible 1-cube cover of 4 variables.
-
#test_espresso_random(generator) ⇒ Object
Tests ESPRESSO on a cover generated by a random
generator. -
#test_espressos_random(number = 1024, dimensions = 4, max = nil) ⇒ Object
Tests ESPRESSO on randomly
numbercover cases on adimensionsboolean space, including at mostmaxcubes. -
#test_tautologies_random(number = 1024, dimensions = 4) ⇒ Object
Tests randomly
numbertautology cases on adimensionsboolean space. -
#test_tautology_random(generator) ⇒ Object
Tests the tautology check on covers generated by a random
generator. -
#truth_tautology(cover) ⇒ Object
Checks if a
coveris a tautology by generating its truth table.
Constructor Details
#initialize(seed = 0, deadline = Float::INFINITY, volume = Float::INFINITY) ⇒ TestEspresso
Creates the tester with a seed for random generation, a
+deadline+ for the simplify steps and a +volume+ before splitting
the cover.
19 20 21 22 23 24 |
# File 'lib/logic_tools/test_logic_tools.rb', line 19 def initialize(seed = 0, deadline = Float::INFINITY, volume = Float::INFINITY) @seed = seed @deadline = deadline @volume = volume end |
Instance Method Details
#same_truth_table?(cover0, cover1) ⇒ Boolean
Checks if covers have the same truth table.
78 79 80 81 82 83 84 85 |
# File 'lib/logic_tools/test_logic_tools.rb', line 78 def same_truth_table?(cover0,cover1) return false unless cover0.width == cover1.width # Check for each entry. (2**cover0.width).times do |i| return false unless cover0.eval(i) == cover1.eval(i) end return true end |
#test_espresso(cover) ⇒ Object
Tests espresso on a given cover.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/logic_tools/test_logic_tools.rb', line 89 def test_espresso(cover) print "ESPRESSO on cover=[#{cover.to_s}]...\n" simple = cover.simplify(@deadline,@volume) print "result: [#{simple}]\n" check0 = (cover + simple.complement).is_tautology? # check0 = same_truth_table?(cover,simple) # assert_equal(true,check0) print "check 0 = #{check0}\n" raise "Test failure" unless check0 check1 = (cover.complement + simple).is_tautology? # assert_equal(true,check1) print "check 1 = #{check1}\n" raise "Test failure" unless check1 return true end |
#test_espresso_all(test = nil) ⇒ Object
Tests the implementation of the espresso algorithm on each
possible 1-cube cover of 4 variables.
Test only on cover if a +test+ number is given.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/logic_tools/test_logic_tools.rb', line 109 def test_espresso_all(test = nil) generator = Generator.new("a","b","c","d") generator.seed = @seed if test then test = test.to_i print "Test #{test}: " return test_espresso(generator.make_1cover(test)) else generator.each_1cover.with_index do |cover,i| print "Test #{i}: " return false unless test_espresso(cover) end return true end end |
#test_espresso_random(generator) ⇒ Object
Tests ESPRESSO on a cover generated by a random generator.
126 127 128 129 130 131 132 |
# File 'lib/logic_tools/test_logic_tools.rb', line 126 def test_espresso_random(generator) # Genrate a random cover. cover = generator.random_cover # Test it. return false unless test_espresso(cover) return true end |
#test_espressos_random(number = 1024, dimensions = 4, max = nil) ⇒ Object
Tests ESPRESSO on randomly number cover cases on a dimensions boolean
space, including at most +max+ cubes.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/logic_tools/test_logic_tools.rb', line 136 def test_espressos_random(number = 1024, dimensions = 4, max = nil) # Create the variables. base = "`" variables = dimensions.times.map { |i| base.next!.clone } # Create the generator. generator = Generator.new(*variables) generator.seed = @seed generator.max = max if max # Ensures a rate of "-" large enough to have a high probability of # an interesting cover (i.e., which is actually simplifiable). # This rate +r+ is obainted as the solution of the followings: # max/2 * 2**(r*dimensions) >= 2**dimensions # NOTE: max/2 is the average size of the cover. generator.rate = Math::log2(2**(dimensions+1)/max.to_f)/dimensions # Ensures the rate is not too small though. generator.rate = 0.3 unless generator.rate >= 0.3 print "rate=#{generator.rate}\n" # Performs the tests. number.times do |i| print "Test #{i}: " return false unless test_espresso_random(generator) end return true end |
#test_tautologies_random(number = 1024, dimensions = 4) ⇒ Object
Tests randomly number tautology cases on a dimensions boolean
space.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/logic_tools/test_logic_tools.rb', line 61 def test_tautologies_random(number = 1024, dimensions = 4) # Create the variables. base = "`" variables = dimensions.times.map { |i| base.next!.clone } # Create the generator. generator = Generator.new(*variables) generator.seed = @seed # Performs the tests. number.times do |i| print "Test #{i}: " return false unless test_tautology_random(generator) end return true end |
#test_tautology_random(generator) ⇒ Object
Tests the tautology check on covers generated by a random generator.
NOTE: ends when a tautology is actually found.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/logic_tools/test_logic_tools.rb', line 38 def test_tautology_random(generator) # Create the cover. cover = Cover.new(*generator.each_variable) # Tautology results. taut0, taut1 = false, false # Add random cubes to the cover until a tautology is met while. begin # Add a random cube. cover << generator.random_cube print "Tautology check on cover=[#{cover.to_s}]...\n" # Check if the cover is a tautology using the standard approach. taut0 = cover.is_tautology? print "Through is_tautology?: #{taut0}; " # Check it again with a truth table. taut1 = truth_tautology(cover) print "through truth table: #{taut1}\n" raise "Test failure" unless taut0 == taut1 end while (!taut0) return true end |
#truth_tautology(cover) ⇒ Object
Checks if a cover is a tautology by generating its truth table.
27 28 29 30 31 32 33 |
# File 'lib/logic_tools/test_logic_tools.rb', line 27 def truth_tautology(cover) ## Generate each possible input and test it on the cover. (2**(cover.width)).times do |i| return false unless cover.eval(i) end return true end |