# Path: groundstation/ue_encoder.py
# class UnambiguousEncoder(object):
#     def __init__(self, codes=DEFAULT_LOOKUP):
#         self.codes = codes
# 
#     def encode(self, cat, buf):
#         """ Encodes from LSB"""
# 
#         if cat not in self.codes:
#             raise UnknownCodeCategory("Unknown code category: %s" % cat)
#         code = self.codes[cat]
# 
#         out = []
#         bit = 1
#         for i in buf:
#             # Map the bits onto the binary code
#             for bit in range(8):
#                 f = i & (1 << bit)
#                 idx = 1 if f else 0
#                 val = code.code[idx]
#                 out.append(val)
# 
#         return out
# 
#     def decode(self, cat, buf):
#         if cat not in self.codes:
#             raise UnknownCodeCategory("Unknown code category: %s" % cat)
#         code = self.codes[cat]
# 
#         out = []
#         values = chunks(8, buf)
# 
#         # Slice off 8 byte chunks because we're encoding from LSB
#         for chunk in values:
#             bit = 0
#             this = 0
#             for byt in chunk:
#                 if code.lookup(byt):
#                     this |= (1 << bit)
#                 bit += 1
#             out.append(this)
# 
#         return out


import unittest
import random
from groundstation.ue_encoder import UnambiguousEncoder

        # Try encoding with the message code
        message = encoder.encode("message", self.test_bytes)
        header  = encoder.encode("header", self.test_bytes)

        # These offsets are totes legit. Trust me.
        self.assertEqual(message, self.test_message)
        self.assertEqual(header,  self.test_header)

    def test_successfully_decoded(self):
        encoder = UnambiguousEncoder()

        message = encoder.decode("message", self.test_message)
        header  = encoder.decode("header", self.test_header)

        self.assertEqual(message, self.test_bytes)
        self.assertEqual(header,  self.test_bytes)

    @staticmethod
    def flip_n_bits(n, msg):
        out = []
        for byte in msg:
            bits = list(range(8))
            for _ in range(n):
                bit = bits.pop(random.randint(0, len(bits) - 1))
                byte = byte ^ 1 << bit
            out.append(byte)
        return out

    def test_1_flipped_bit(self):

# Path: groundstation/ue_encoder.py
# class UnambiguousEncoder(object):
#     def __init__(self, codes=DEFAULT_LOOKUP):
#         self.codes = codes
# 
#     def encode(self, cat, buf):
#         """ Encodes from LSB"""
# 
#         if cat not in self.codes:
#             raise UnknownCodeCategory("Unknown code category: %s" % cat)
#         code = self.codes[cat]
# 
#         out = []
#         bit = 1
#         for i in buf:
#             # Map the bits onto the binary code
#             for bit in range(8):
#                 f = i & (1 << bit)
#                 idx = 1 if f else 0
#                 val = code.code[idx]
#                 out.append(val)
# 
#         return out
# 
#     def decode(self, cat, buf):
#         if cat not in self.codes:
#             raise UnknownCodeCategory("Unknown code category: %s" % cat)
#         code = self.codes[cat]
# 
#         out = []
#         values = chunks(8, buf)
# 
#         # Slice off 8 byte chunks because we're encoding from LSB
#         for chunk in values:
#             bit = 0
#             this = 0
#             for byt in chunk:
#                 if code.lookup(byt):
#                     this |= (1 << bit)
#                 bit += 1
#             out.append(this)
# 
#         return out


# Path: test/test_ue_encoder.py
import unittest
import random
from groundstation.ue_encoder import UnambiguousEncoder


        # Try encoding with the message code
        message = encoder.encode("message", self.test_bytes)
        header  = encoder.encode("header", self.test_bytes)

        # These offsets are totes legit. Trust me.
        self.assertEqual(message, self.test_message)
        self.assertEqual(header,  self.test_header)

    def test_successfully_decoded(self):
        encoder = UnambiguousEncoder()

        message = encoder.decode("message", self.test_message)
        header  = encoder.decode("header", self.test_header)

        self.assertEqual(message, self.test_bytes)
        self.assertEqual(header,  self.test_bytes)

    @staticmethod
    def flip_n_bits(n, msg):
        out = []
        for byte in msg:
            bits = list(range(8))
            for _ in range(n):
                bit = bits.pop(random.randint(0, len(bits) - 1))
                byte = byte ^ 1 << bit
            out.append(byte)
        return out

    def test_1_flipped_bit(self):

        encoder = UnambiguousEncoder()