Example #1
0
    def test_typed_array(self) -> None:
        encoder = Serializer()
        val = TypedArray("i", [0, 1, 2, 3, 4, 5])
        rep = encoder.encode(val)

        assert len(encoder.buffers) == 1

        [buf] = encoder.buffers
        assert bytes(buf.data) == val.tobytes()

        assert rep == TypedArrayRep(
            type="typed_array",
            array=BytesRep(type="bytes", data=buf),
            order=sys.byteorder,
            dtype="int32",
        )
Example #2
0
    def add_buffer(self, buffer: array.array):
        '''
        Add a binary buffer. Builds glTF "JSON metadata" and saves buffer reference.
        Buffer will be copied into BIN chunk during "pack".
        Currently encodes buffers as glTF accessors, but this could be optimized.

        :param buffer: flattened array
        :param size: XXX
        :param component_type: XXX
        :param count: XXX
        :return: accessor_index: Index of added buffer in "accessors" list
        '''
        buffer_view_index = self.add_buffer_view(buffer.tobytes())
        return self.add_accessor(
            buffer_view_index,
            size=buffer.itemsize,
            component_type=component_type_d[buffer.typecode],
            count=len(buffer))
Example #3
0
def encode_py_array(data: array.array) -> bytes:
    typecode = data.typecode
    count = data.buffer_info()[1]
    byte_count = count * data.itemsize
    buffer = encode_string(typecode) + encode_int(byte_count) + data.tobytes()
    return buffer
Example #4
0
def tostring(x: array) -> str: return x.tobytes().decode('utf-8')


class Strand(CNObject):
Example #5
0
 def test_flashdevice_4_long_rw(self):
     """Long R/W test
     """
     # Max size to perform the test on
     size = 1 << 20
     # Whether to test with random value, or contiguous values to ease debug
     randomize = True
     # Fill in the whole flash with a monotonic increasing value, that is
     # the current flash 32-bit address, then verify the sequence has been
     # properly read back
     # limit the test to 1MiB to keep the test duration short, but performs
     # test at the end of the flash to verify that high addresses may be
     # reached
     length = min(len(self.flash), size)
     start = len(self.flash)-length
     print("Erase %s from flash @ 0x%06x (may take a while...)" %
           (pretty_size(length), start))
     delta = now()
     self.flash.unlock()
     self.flash.erase(start, length, True)
     delta = now()-delta
     self._report_bw('Erased', length, delta)
     if str(self.flash).startswith('SST'):
         # SST25 flash devices are tremendously slow at writing (one or two
         # bytes per SPI request MAX...). So keep the test sequence short
         # enough
         length = 16 << 10
     print("Build test sequence")
     if not randomize:
         buf = Array('I')
         back = Array('I')
         for address in range(0, length, 4):
             buf.append(address)
         # Expect to run on x86 or ARM (little endian), so swap the values
         # to ease debugging
         # A cleaner test would verify the host endianess, or use struct
         # module
         buf.byteswap()
         # Cannot use buf directly, as it's an I-array,
         # and SPI expects a B-array
     else:
         seed(0)
         buf = Array('B')
         back = Array('B')
         buf.extend((randint(0, 255) for _ in range(0, length)))
     bufstr = buf.tobytes()
     print("Writing %s to flash (may take a while...)" %
           pretty_size(len(bufstr)))
     delta = now()
     self.flash.write(start, bufstr)
     delta = now()-delta
     length = len(bufstr)
     self._report_bw('Wrote', length, delta)
     wmd = sha1()
     wmd.update(buf.tobytes())
     refdigest = wmd.hexdigest()
     print("Reading %s from flash" % pretty_size(length))
     delta = now()
     data = self.flash.read(start, length)
     delta = now()-delta
     self._report_bw('Read', length, delta)
     # print "Dump flash"
     # print hexdump(data.tobytes())
     print("Verify flash")
     rmd = sha1()
     rmd.update(data.tobytes())
     newdigest = rmd.hexdigest()
     print("Reference:", refdigest)
     print("Retrieved:", newdigest)
     if refdigest != newdigest:
         errcount = 0
         back.fromstring(data)
         for pos in range(len(buf)):
             if buf[pos] != data[pos]:
                 print('Invalid byte @ offset 0x%06x: 0x%02x / 0x%02x' %
                       (pos, buf[pos], back[pos]))
                 errcount += 1
                 # Stop report after 16 errors
                 if errcount >= 32:
                     break
         raise self.fail('Data comparison mismatch')
Example #6
0
def tostring(x: array) -> str:
    return x.tobytes().decode('utf-8')
Example #7
0
 def _make_buffer(self, regaddr, out=None):
     data = Array('B')
     data.extend(spack('%s%s' % (self._endian, self._format), regaddr))
     if out:
         data.extend(out)
     return data.tobytes()