Class: MachineTester
- Inherits:
-
Object
- Object
- MachineTester
- Defined in:
- lib/urm/machine_tester.rb
Overview
The MachineTester class provides methods for testing the functionality of an Unbounded Register Machine (URM). It includes methods to assert that the machine produces expected outputs for given inputs and to validate the machine’s outputs over a range of input values using a provided lambda function.
Methods:
-
self.assert(machine, expected_output, *inputs): Checks that for the given input values, the machine produces the expected output. If the output does not match the expected value, it prints an error message. Returns true if the output matches, false otherwise.
-
self.assertRange(machine, a, b, func): Validates that for all combinations of input values within the range [a, b], the machine’s output matches the output of the provided lambda function. It handles any number of input parameters for the machine. Returns true if all outputs match, false otherwise.
Example usage:
machine = Urm::Machine.new(2)
multiplication_lambda = ->(x, y) { x * y }
MachineTester.assert(machine, 6, 2, 3) # Asserts that machine.run(2, 3) == 6
MachineTester.assertRange(machine, 0, 10, multiplication_lambda) # Validates for range [0, 10]
Class Method Summary collapse
-
.assert(machine, expected_output, *inputs) ⇒ Boolean
Проверяет, что на входе х2…
-
.assert_range(machine, lower_bound, upper_bound, func) ⇒ Boolean
Проверяет, что на всех наборах входных в диапазоне [a,b] machine.run(эти наборы) = lambda(эти наборы).
Class Method Details
.assert(machine, expected_output, *inputs) ⇒ Boolean
Проверяет, что на входе х2… х(n+1) машина дает х1
38 39 40 41 42 43 44 45 46 |
# File 'lib/urm/machine_tester.rb', line 38 def self.assert(machine, expected_output, *inputs) output = machine.run(*inputs) if output == expected_output true else puts "Test failed for input #{inputs}: expected #{expected_output}, got #{output}" false end end |
.assert_range(machine, lower_bound, upper_bound, func) ⇒ Boolean
Проверяет, что на всех наборах входных в диапазоне [a,b] machine.run(эти наборы) = lambda(эти наборы)
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/urm/machine_tester.rb', line 55 def self.assert_range(machine, lower_bound, upper_bound, func) input_size = machine.registers.size - 1 # учитываем x1, который не является входным ranges = Array.new(input_size) { (lower_bound..upper_bound).to_a } inputs_list = ranges.first.product(*ranges[1..]) inputs_list.each do |inputs| expected_output = func.call(*inputs) return false unless assert(machine, expected_output, *inputs) end true end |