Exemplo n.º 1
0
  def testReadWrite(self):
    """Test ScalarEncoder Cap'n Proto serialization implementation."""
    originalValue = self._l.encode(1)

    proto1 = ScalarEncoderProto.new_message()
    self._l.write(proto1)

    # Write the proto to a temp file and read it back into a new proto
    with tempfile.TemporaryFile() as f:
      proto1.write(f)
      f.seek(0)
      proto2 = ScalarEncoderProto.read(f)

    encoder = ScalarEncoder.read(proto2)

    self.assertIsInstance(encoder, ScalarEncoder)
    self.assertEqual(encoder.w, self._l.w)
    self.assertEqual(encoder.minval, self._l.minval)
    self.assertEqual(encoder.maxval, self._l.maxval)
    self.assertEqual(encoder.periodic, self._l.periodic)
    self.assertEqual(encoder.n, self._l.n)
    self.assertEqual(encoder.radius, self._l.radius)
    self.assertEqual(encoder.resolution, self._l.resolution)
    self.assertEqual(encoder.name, self._l.name)
    self.assertEqual(encoder.verbosity, self._l.verbosity)
    self.assertEqual(encoder.clipInput, self._l.clipInput)
    self.assertTrue(numpy.array_equal(encoder.encode(1), originalValue))
    self.assertEqual(self._l.decode(encoder.encode(1)),
                     encoder.decode(self._l.encode(1)))

    # Feed in a new value and ensure the encodings match
    result1 = self._l.encode(7)
    result2 = encoder.encode(7)
    self.assertTrue(numpy.array_equal(result1, result2))
Exemplo n.º 2
0
    def testReadWrite(self):
        """Test ScalarEncoder Cap'n Proto serialization implementation."""
        originalValue = self._l.encode(1)

        proto1 = ScalarEncoderProto.new_message()
        self._l.write(proto1)

        # Write the proto to a temp file and read it back into a new proto
        with tempfile.TemporaryFile() as f:
            proto1.write(f)
            f.seek(0)
            proto2 = ScalarEncoderProto.read(f)

        encoder = ScalarEncoder.read(proto2)

        self.assertIsInstance(encoder, ScalarEncoder)
        self.assertEqual(encoder.w, self._l.w)
        self.assertEqual(encoder.minval, self._l.minval)
        self.assertEqual(encoder.maxval, self._l.maxval)
        self.assertEqual(encoder.periodic, self._l.periodic)
        self.assertEqual(encoder.n, self._l.n)
        self.assertEqual(encoder.radius, self._l.radius)
        self.assertEqual(encoder.resolution, self._l.resolution)
        self.assertEqual(encoder.name, self._l.name)
        self.assertEqual(encoder.verbosity, self._l.verbosity)
        self.assertEqual(encoder.clipInput, self._l.clipInput)
        self.assertTrue(numpy.array_equal(encoder.encode(1), originalValue))
        self.assertEqual(self._l.decode(encoder.encode(1)),
                         encoder.decode(self._l.encode(1)))

        # Feed in a new value and ensure the encodings match
        result1 = self._l.encode(7)
        result2 = encoder.encode(7)
        self.assertTrue(numpy.array_equal(result1, result2))