import cocotb
from cocotb.triggers import Timer
from cocotb.result import TestFailure
#from adder_model import adder_model
#import random


@cocotb.test()
def mux2_test(dut):
    """Test for MUX2 options"""
    opts = [(x,y,z, x&~z | y&z) for x in [0,1] for y in [0,1] for z in [0,1]]

    yield Timer(2)
    for I0, I1, S0, _ in opts:
      dut.I0 = I0
      dut.I1 = I1
      dut.S0 = S0
      if S0:
        expected = I1
      else:
        expected = I0
      yield Timer(2)

      if dut.O != expected:
        raise TestFailure(
            'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
      else:
        dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))

import cocotb
from cocotb.triggers import Timer
from cocotb.result import TestFailure
from cocotb.regression import TestFactory

@cocotb.coroutine
def mux2_basic_test(dut, inputs=(1,0,0)):
    """Test for MUX2 options"""

    yield Timer(2)
    I0, I1, S0 = inputs
    dut.I0 = I0
    dut.I1 = I1
    dut.S0 = S0
    if S0:
      expected = I1
    else:
      expected = I0
    yield Timer(2)

    if dut.O != expected:
      raise TestFailure(
          'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
    else:
      dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))


factory = TestFactory(mux2_basic_test)
input_permutations = [(x, y, z) for x in [0,1] for y in [0,1] for z in [0,1]]
factory.add_option("inputs", input_permutations)
factory.generate_tests()

Use factory to iterate over permutations
Use factory to iterate over permutations

Signed-off-by: Jeffrey Elms <23ce84ca7a7de9dc17ad8e8b0bbd717d4f9f9884@freshred.net>

  import cocotb
  from cocotb.triggers import Timer
  from cocotb.result import TestFailure
- #from adder_model import adder_model
- #import random
+ from cocotb.regression import TestFactory
+ 
+ @cocotb.coroutine
+ def mux2_basic_test(dut, inputs=(1,0,0)):
+     """Test for MUX2 options"""
+ 
+     yield Timer(2)
+     I0, I1, S0 = inputs
+     dut.I0 = I0
+     dut.I1 = I1
+     dut.S0 = S0
+     if S0:
+       expected = I1
+     else:
+       expected = I0
+     yield Timer(2)
+ 
+     if dut.O != expected:
+       raise TestFailure(
+           'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
+     else:
+       dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))
  
  
+ factory = TestFactory(mux2_basic_test)
- @cocotb.test()
- def mux2_test(dut):
-     """Test for MUX2 options"""
-     opts = [(x,y,z, x&~z | y&z) for x in [0,1] for y in [0,1] for z in [0,1]]
+ input_permutations = [(x, y, z) for x in [0,1] for y in [0,1] for z in [0,1]]
+ factory.add_option("inputs", input_permutations)
+ factory.generate_tests()
  
-     yield Timer(2)
-     for I0, I1, S0, _ in opts:
-       dut.I0 = I0
-       dut.I1 = I1
-       dut.S0 = S0
-       if S0:
-         expected = I1
-       else:
-         expected = I0
-       yield Timer(2)
- 
-       if dut.O != expected:
-         raise TestFailure(
-             'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
-       else:
-         dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))
- 
Use factory to iterate over permutations
## Code Before:
import cocotb
from cocotb.triggers import Timer
from cocotb.result import TestFailure
#from adder_model import adder_model
#import random


@cocotb.test()
def mux2_test(dut):
    """Test for MUX2 options"""
    opts = [(x,y,z, x&~z | y&z) for x in [0,1] for y in [0,1] for z in [0,1]]

    yield Timer(2)
    for I0, I1, S0, _ in opts:
      dut.I0 = I0
      dut.I1 = I1
      dut.S0 = S0
      if S0:
        expected = I1
      else:
        expected = I0
      yield Timer(2)

      if dut.O != expected:
        raise TestFailure(
            'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
      else:
        dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))

## Instruction:
Use factory to iterate over permutations
## Code After:
import cocotb
from cocotb.triggers import Timer
from cocotb.result import TestFailure
from cocotb.regression import TestFactory

@cocotb.coroutine
def mux2_basic_test(dut, inputs=(1,0,0)):
    """Test for MUX2 options"""

    yield Timer(2)
    I0, I1, S0 = inputs
    dut.I0 = I0
    dut.I1 = I1
    dut.S0 = S0
    if S0:
      expected = I1
    else:
      expected = I0
    yield Timer(2)

    if dut.O != expected:
      raise TestFailure(
          'Result is incorrect for I0(%d) I1(%d) S0(%d): %s(O) != %s (expected)' % (I0, I1, S0, dut.O, expected))
    else:
      dut._log.info('I0(%d) I1(%d) S0(%d) output(%d) Ok!'%(I0, I1, S0, dut.O))


factory = TestFactory(mux2_basic_test)
input_permutations = [(x, y, z) for x in [0,1] for y in [0,1] for z in [0,1]]
factory.add_option("inputs", input_permutations)
factory.generate_tests()
