def test_matrix_interface(self): # Read and check a Matrix from a Parameters-Object tmp = Parameters("""{ "false_matrix_value": [[2, 1.5,3.3],[1,2]] }""") # Manually assign and check a Matrix mat = Matrix(3, 2) mat[0, 0] = 1.0 mat[0, 1] = 2.0 mat[1, 0] = 3.0 mat[1, 1] = 4.0 mat[2, 0] = 5.0 mat[2, 1] = 6.0 tmp.AddEmptyValue("matrix_value") tmp["matrix_value"].SetMatrix(mat) self.assertTrue(tmp["matrix_value"].IsMatrix()) A2 = tmp["matrix_value"].GetMatrix() self.assertEqual(A2[0, 0], 1.0) self.assertEqual(A2[0, 1], 2.0) self.assertEqual(A2[1, 0], 3.0) self.assertEqual(A2[1, 1], 4.0) self.assertEqual(A2[2, 0], 5.0) self.assertEqual(A2[2, 1], 6.0) # Check the IsMatrix Method self.assertFalse( tmp["false_matrix_value"].IsMatrix()) # Mis-sized Matrix # check that the errors of the GetMatrix method are thrown correctly with self.assertRaises(RuntimeError): tmp["false_matrix_value"].GetMatrix() # Mis-sized Matrix
def test_matrix_interface(self): # Read and check Matrices from a Parameters-Object tmp = Parameters("""{ "valid_matrices" : [ [[]], [[],[]], [[-9.81,8, 5.47]] ], "false_matrices" : [ [], [[[]]], [[3.3] , [1,2]], [[2,1.5,3.3] , [3,{"key":3},2]], [[2,1.5,3.3] , [5,false,2]], [[2,1.5,3.3] , [[2,3],1,2]], [[2,1.5,3.3] , ["string",2,9]] ] }""") # Check the IsMatrix Method for i in range(tmp["valid_matrices"].size()): valid_matrix = tmp["valid_matrices"][i] self.assertTrue(valid_matrix.IsMatrix()) for i in range(tmp["false_matrices"].size()): false_matrix = tmp["false_matrices"][i] self.assertFalse(false_matrix.IsMatrix()) # Check the GetMatrix Method also on the valid Matrices for i in range(tmp["valid_matrices"].size()): valid_matrix = tmp["valid_matrices"][i] valid_matrix.GetMatrix() # Check that the errors of the GetMatrix method are thrown correctly for i in range(tmp["false_matrices"].size()): false_matrix = tmp["false_matrices"][i] with self.assertRaises(RuntimeError): false_matrix.GetMatrix() # Manually assign and check a Matrix mat = Matrix(3, 2) mat[0, 0] = 1.0 mat[0, 1] = 2.0 mat[1, 0] = 3.0 mat[1, 1] = 4.0 mat[2, 0] = 5.0 mat[2, 1] = 6.0 tmp.AddEmptyValue("matrix_value") tmp["matrix_value"].SetMatrix(mat) self.assertTrue(tmp["matrix_value"].IsMatrix()) A2 = tmp["matrix_value"].GetMatrix() self.assertEqual(A2[0, 0], 1.0) self.assertEqual(A2[0, 1], 2.0) self.assertEqual(A2[1, 0], 3.0) self.assertEqual(A2[1, 1], 4.0) self.assertEqual(A2[2, 0], 5.0) self.assertEqual(A2[2, 1], 6.0)
def test_add_methods(self): # This method checks all the "GetXXX" Methods if they throw an error tmp = Parameters("""{}""") key = "int" tmp.AddInt(key, 10) self.assertEqual(tmp[key].GetInt(), 10) key = "double" tmp.AddDouble(key, 2.0) self.assertEqual(tmp[key].GetDouble(), 2.0) key = "bool" tmp.AddBool(key, True) self.assertEqual(tmp[key].GetBool(), True) key = "string" tmp.AddString(key, "hello") self.assertEqual(tmp[key].GetString(), "hello") key = "vector" vector = Vector(3) vector[0] = 5.2 vector[1] = -3.1 vector[2] = 4.33 tmp.AddVector(key, vector) V = tmp[key].GetVector() self.assertEqual(V[0], 5.2) self.assertEqual(V[1], -3.1) self.assertEqual(V[2], 4.33) key = "matrix" matrix = Matrix(3, 2) matrix[0, 0] = 1.0 matrix[0, 1] = 2.0 matrix[1, 0] = 3.0 matrix[1, 1] = 4.0 matrix[2, 0] = 5.0 matrix[2, 1] = 6.0 tmp.AddMatrix(key, matrix) A = tmp[key].GetMatrix() self.assertEqual(A[0, 0], 1.0) self.assertEqual(A[0, 1], 2.0) self.assertEqual(A[1, 0], 3.0) self.assertEqual(A[1, 1], 4.0) self.assertEqual(A[2, 0], 5.0) self.assertEqual(A[2, 1], 6.0)
def test_set_methods(self): # This method checks all the "GetXXX" Methods if they throw an error tmp = Parameters( """{ "int_value" : 0, "double_value": 0.0, "bool_value" : false, "string_value" : "", "vector_value" : [], "matrix_value" : [[0]] }""" ) # if you add more values to this, make sure to add the corresponding in the loop for key in tmp.keys(): val_type = key[:-6] # removing "_value" # Int and Double are checked tgth bcs both internally call "IsNumber" if val_type == "int" or val_type == "double": if val_type == "int": tmp[key].SetInt(10) self.assertEqual(tmp[key].GetInt(), 10) else: with self.assertRaises(RuntimeError): tmp[key].GetInt() if val_type == "double" or val_type == "int": if val_type == "double": tmp[key].SetDouble(2.0) self.assertEqual(tmp[key].GetDouble(), 2.0) else: with self.assertRaises(RuntimeError): tmp[key].GetDouble() if val_type == "bool": tmp[key].SetBool(True) self.assertEqual(tmp[key].GetBool(), True) else: with self.assertRaises(RuntimeError): tmp[key].GetBool() if val_type == "string": tmp[key].SetString("hello") self.assertEqual(tmp[key].GetString(), "hello") else: with self.assertRaises(RuntimeError): tmp[key].GetString() if val_type == "vector": vector = Vector(3) vector[0] = 5.2 vector[1] = -3.1 vector[2] = 4.33 tmp[key].SetVector(vector) V = tmp[key].GetVector() self.assertEqual(V[0], 5.2) self.assertEqual(V[1], -3.1) self.assertEqual(V[2], 4.33) else: with self.assertRaises(RuntimeError): tmp[key].GetVector() if val_type == "matrix": matrix = Matrix(3, 2) matrix[0, 0] = 1.0 matrix[0, 1] = 2.0 matrix[1, 0] = 3.0 matrix[1, 1] = 4.0 matrix[2, 0] = 5.0 matrix[2, 1] = 6.0 tmp[key].SetMatrix(matrix) A = tmp[key].GetMatrix() self.assertEqual(A[0, 0], 1.0) self.assertEqual(A[0, 1], 2.0) self.assertEqual(A[1, 0], 3.0) self.assertEqual(A[1, 1], 4.0) self.assertEqual(A[2, 0], 5.0) self.assertEqual(A[2, 1], 6.0) else: with self.assertRaises(RuntimeError): tmp[key].GetMatrix()