def setUp(self): self.matrix_proto = sor_pb2.SparseMatrix(matrix_name="a", row_count=3, column_count=3) for i in range(0, 3): value = self.matrix_proto.values.add() value.row_index = i value.column_index = i value.value = (i + 1) * 3.9 self.negative_matrix_proto = sor_pb2.SparseMatrix(matrix_name="a-", row_count=3, column_count=3) for i in range(0, 3): value = self.negative_matrix_proto.values.add() value.row_index = i value.column_index = i value.value = -(i + 1) * 2.1 self.vector_proto = sor_pb2.Vector(vector_name="b", length=3) self.vector_proto.values.extend([1.0, 3.6, 9.1]) self.negative_vector_proto = sor_pb2.Vector(vector_name="b-", length=3) self.negative_vector_proto.values.extend([-1.0, -3.6, -9.1]) self.input_file = 'testdata/io_test.in' self.output_file = 'testdata/io_test.out'
def _process_string_vector_proto(string_vector_proto): string_segments = string_vector_proto.split('values: ') head_segment = string_segments[0] name = re.search(r'vector_name: "([^"]+)"', head_segment).group(1) length = re.search(r'length: (\d+)', head_segment).group(1) vector_proto = sor_pb2.Vector(vector_name=name, length=int(length)) for value in string_segments[1:]: vector_proto.values.append(float(value.strip())) return vector_proto
def to_proto(self): """Converts Vector object to sor_pb2.Vector object Returns: A sor_pb2.Vector proto object. """ vector_proto = sor_pb2.Vector(vector_name=self._name, length=self._length) vector_proto.values.extend(self._values) return vector_proto
def testVector_FromProto(self): vector_a_proto = sor_pb2.Vector(vector_name = "a", length = 5, values = [1, 2, 3, 4, 5]) vector_a = vector.Vector(vector_proto=vector_a_proto) expected = vector.Vector(number_list=[1, 2, 3, 4, 5]) self.assertEqual(vector_a.values, expected.values)
def testValidateVectorProto_Success(self): vector = sor_pb2.Vector(vector_name="test", length=3) vector.values.extend([i for i in range(3, 6)]) self.assertTrue(validation.ValidateVectorProto(vector))
def testValidateVectorProto_InvalidLength(self): vector = sor_pb2.Vector(vector_name="test", length=12) vector.values.extend([i for i in range(3, 6)]) self.assertRaises(validation.ValidationError, validation.ValidateVectorProto, vector)