Example #1
0
def test_basic_instances():
    for u in [uint8, byte, uint16, uint32, uint64, uint128, uint256]:
        v = u(123)
        assert isinstance(v, int)
        assert isinstance(v, BasicView)
        assert isinstance(v, View)

    assert isinstance(boolean(True), BasicView)
    assert isinstance(boolean(False), BasicView)
    assert isinstance(bit(True), boolean)
    assert isinstance(bit(False), boolean)
Example #2
0
    def pop(self):
        ll = self.length()
        if ll == 0:
            raise Exception("list is empty, cannot pop")
        i = ll - 1
        chunk_i = i // 256
        target: Gindex = to_gindex(chunk_i, self.__class__.tree_depth())
        if i & 0xff == 0:
            set_last = self.get_backing().setter(target)
            next_backing = set_last(zero_node(0))
        else:
            set_last = self.get_backing().setter(target)
            chunk = self.get_backing().getter(target)
            next_backing = set_last(_new_chunk_with_bit(chunk, ll & 0xff, boolean(False)))

        # if possible, summarize
        can_summarize = (target & 1) == 0
        if can_summarize:
            # summarize to the highest node possible.
            # I.e. the resulting target must be a right-hand, unless it's the only content node.
            while (target & 1) == 0 and target != 0b10:
                target >>= 1
            summary_fn = next_backing.summarize_into(target)
            next_backing = summary_fn()

        set_length = next_backing.rebind_right
        new_length = uint256(ll - 1).get_backing()
        next_backing = set_length(new_length)
        self.set_backing(next_backing)
Example #3
0
 def get(self, i: int) -> boolean:
     ll = self.length()
     if i >= ll:
         raise NavigationError(f"cannot get bit {i} in bits of length {ll}")
     chunk_i = i >> 8
     chunk = self.get_backing().getter(to_gindex(chunk_i, self.__class__.tree_depth()))
     chunk_byte = chunk.root[(i & 0xff) >> 3]
     return boolean((chunk_byte >> (i & 0x7)) & 1)
Example #4
0

def merge(a: str, branch: Iterable[str]) -> str:
    """
    Merge (out on left, branch on right) leaf a with branch items, branch is from bottom to top.
    """
    out = a
    for b in branch:
        out = h(out, b)
    return out


test_data = [
    ("bit F", bit, bit(False), "00", chunk("00"), False),
    ("bit T", bit, bit(True), "01", chunk("01"), True),
    ("boolean F", boolean, boolean(False), "00", chunk("00"), False),
    ("boolean T", boolean, boolean(True), "01", chunk("01"), True),
    ("bitlist empty", Bitlist[8], Bitlist[8](), "01",
     h(chunk(""), chunk("00")), "0x01"),
    ("bitvector TTFTFTFF", Bitvector[8], Bitvector[8](1, 1, 0, 1, 0,
                                                      1, 0, 0), "2b",
     chunk("2b"), "0x2b"),
    ("bitlist TTFTFTFF", Bitlist[8], Bitlist[8](1, 1, 0, 1, 0, 1, 0,
                                                0), "2b01",
     h(chunk("2b"), chunk("08")), "0x2b01"),
    ("bitvector FTFT", Bitvector[4], Bitvector[4](0, 1, 0, 1), "0a",
     chunk("0a"), "0x0a"),
    ("bitlist FTFT", Bitlist[4], Bitlist[4](0, 1, 0, 1), "1a",
     h(chunk("0a"), chunk("04")), "0x1a"),
    ("bitvector FTF", Bitvector[3], Bitvector[3](0, 1, 0), "02", chunk("02"),
     "0x02"),
Example #5
0
 def navigate_view(self, key: Any) -> View:
     return boolean(self.__getitem__(key))