# Path: pym2149/reg.py
# class Reg:
# 
#     undefined = object()
# 
#     def __init__(self, value = undefined, *, maxval = None, minval = None):
#         self.links = []
#         self.idle = True
#         self.maxval = maxval # Friendlier than a mask.
#         self.minval = minval # Typically to avoid 0 period.
#         if value is not self.undefined:
#             self.value = value
# 
#     def link(self, xform, *upstream):
#         link = Link(self, xform, upstream)
#         for r in upstream:
#             r.links.append(link)
#         return self
# 
#     def mlink(self, mask, xform, *upstream):
#         negmask = ~mask
#         self.link(lambda *args: (negmask & self.value) | (mask & xform(*args)), *upstream)
# 
#     def set(self, value):
#         if self.maxval is not None:
#             value = min(self.maxval, value)
#         if self.minval is not None:
#             value = max(self.minval, value)
#         self._value = value
#         self.idle = False
#         try:
#             for link in self.links:
#                 link.update()
#         finally:
#             self.idle = True
# 
#     value = property(lambda self: self._value, lambda self, value: self.set(value))
# 
# class VersionReg(Reg):
# 
#     version = 0
# 
#     def set(self, value):
#         super().set(value)
#         self.version += 1


from .reg import Reg, VersionReg
from unittest import TestCase
# Copyright 2014, 2018, 2019, 2020 Andrzej Cichocki

# This file is part of pym2149.
#
# pym2149 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pym2149 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pym2149.  If not, see <http://www.gnu.org/licenses/>.


class TestReg(TestCase):

    def test_bidi(self):
        high = Reg()
        low = Reg()
        value = Reg().link(lambda h, l: (h << 8) | l, high, low)
        high.link(lambda v: (v >> 8), value)
        low.link(lambda v: v & 0xff, value)

# Path: pym2149/reg.py
# class Reg:
# 
#     undefined = object()
# 
#     def __init__(self, value = undefined, *, maxval = None, minval = None):
#         self.links = []
#         self.idle = True
#         self.maxval = maxval # Friendlier than a mask.
#         self.minval = minval # Typically to avoid 0 period.
#         if value is not self.undefined:
#             self.value = value
# 
#     def link(self, xform, *upstream):
#         link = Link(self, xform, upstream)
#         for r in upstream:
#             r.links.append(link)
#         return self
# 
#     def mlink(self, mask, xform, *upstream):
#         negmask = ~mask
#         self.link(lambda *args: (negmask & self.value) | (mask & xform(*args)), *upstream)
# 
#     def set(self, value):
#         if self.maxval is not None:
#             value = min(self.maxval, value)
#         if self.minval is not None:
#             value = max(self.minval, value)
#         self._value = value
#         self.idle = False
#         try:
#             for link in self.links:
#                 link.update()
#         finally:
#             self.idle = True
# 
#     value = property(lambda self: self._value, lambda self, value: self.set(value))
# 
# class VersionReg(Reg):
# 
#     version = 0
# 
#     def set(self, value):
#         super().set(value)
#         self.version += 1


# Path: pym2149/test_reg.py
from .reg import Reg, VersionReg
from unittest import TestCase

# Copyright 2014, 2018, 2019, 2020 Andrzej Cichocki

# This file is part of pym2149.
#
# pym2149 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pym2149 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pym2149.  If not, see <http://www.gnu.org/licenses/>.


class TestReg(TestCase):

    def test_bidi(self):
        high = Reg()
        low = Reg()
        value = Reg().link(lambda h, l: (h << 8) | l, high, low)
        high.link(lambda v: (v >> 8), value)
        low.link(lambda v: v & 0xff, value)

        vr = VersionReg().link(lambda v: v, value)