53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
|
#
|
||
|
# This file is part of LiteX.
|
||
|
#
|
||
|
# Copyright (c) 2022 Florent Kermarrec <florent@enjoy-digital.fr>
|
||
|
# SPDX-License-Identifier: BSD-2-Clause
|
||
|
|
||
|
import unittest
|
||
|
import random
|
||
|
|
||
|
from migen import *
|
||
|
|
||
|
from litex.gen import *
|
||
|
|
||
|
class TestReduce(unittest.TestCase):
|
||
|
def reduce_test(self, operator, value, reduced):
|
||
|
class DUT(Module):
|
||
|
def __init__(self):
|
||
|
self.errors = 0
|
||
|
self.reduced = Reduce(operator, value)
|
||
|
|
||
|
def checker(self):
|
||
|
yield
|
||
|
if (yield self.reduced) != reduced:
|
||
|
self.errors += 1
|
||
|
|
||
|
dut = DUT()
|
||
|
run_simulation(dut, [dut.checker()])
|
||
|
self.assertEqual(dut.errors, 0)
|
||
|
|
||
|
def test_reduced_and(self):
|
||
|
self.reduce_test(operator="AND", value=Constant(0b00, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="AND", value=Constant(0b01, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="AND", value=Constant(0b10, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="AND", value=Constant(0b11, 2), reduced=0b1)
|
||
|
|
||
|
def test_reduced_or(self):
|
||
|
self.reduce_test(operator="OR", value=Constant(0b00, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="OR", value=Constant(0b01, 2), reduced=0b1)
|
||
|
self.reduce_test(operator="OR", value=Constant(0b10, 2), reduced=0b1)
|
||
|
self.reduce_test(operator="OR", value=Constant(0b11, 2), reduced=0b1)
|
||
|
|
||
|
def test_reduced_nor(self):
|
||
|
self.reduce_test(operator="NOR", value=Constant(0b00, 2), reduced=0b1)
|
||
|
self.reduce_test(operator="NOR", value=Constant(0b01, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="NOR", value=Constant(0b10, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="NOR", value=Constant(0b11, 2), reduced=0b0)
|
||
|
|
||
|
def test_reduced_nor(self):
|
||
|
self.reduce_test(operator="XOR", value=Constant(0b00, 2), reduced=0b0)
|
||
|
self.reduce_test(operator="XOR", value=Constant(0b01, 2), reduced=0b1)
|
||
|
self.reduce_test(operator="XOR", value=Constant(0b10, 2), reduced=0b1)
|
||
|
self.reduce_test(operator="XOR", value=Constant(0b11, 2), reduced=0b0)
|