def test_add_cylinder_primitive(self): mesh_manager = MeshManager.get_instance() mesh_manager.reset() self.assertEqual(len(mesh_manager.tags), 0) tag = mesh_manager.add( tag='cyl_1', type='cylinder', radius=1, height=1) self.assertEqual(tag, 'cyl_1') tag = mesh_manager.add( tag='cyl_2', type='cylinder', radius=2, height=2) self.assertEqual(tag, 'cyl_2') # Add cylinder with same size of cyl_1 tag = mesh_manager.add(type='cylinder', radius=1, height=1) self.assertEqual(tag, 'cyl_1') tag = mesh_manager.find_by_parameters( type='cylinder', radius=2, height=2) self.assertEqual(tag, 'cyl_2') # Let the manager generate the tag self.assertIsNotNone(mesh_manager.add( type='cylinder', radius=random.rand(), height=random.rand()))
def test_ellipsoid_inertia(self): # Test invalid inputs inputs = [[0, 0, 0, 0], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 0, 1], [1, 1, 1, 0]] for test_input in inputs: with self.assertRaises(AssertionError): Inertial.create_ellipsoid_inertia(*test_input) mass = random.rand() axis_length_x = random.rand() axis_length_y = random.rand() axis_length_z = random.rand() ref_inertia = get_ellipsoid_inertia(mass, axis_length_x, axis_length_y, axis_length_z) inertia = Inertial.create_ellipsoid_inertia(mass, axis_length_x, axis_length_y, axis_length_z) for tag in ref_inertia: self.assertEqual( ref_inertia[tag], getattr(inertia, tag), 'Element {} set with wrong value, ' 'correct={}, value={}'.format(tag, ref_inertia[tag], getattr(inertia, tag)))
def test_add_model(self): generator = WorldGenerator() name = generate_random_string(5) obj = SimulationModel(name) obj.add_cuboid_link(link_name='link', size=[random.rand() for _ in range(3)], mass=random.rand()) pose = [random.rand() for _ in range(6)] generator.add_model(obj, [pose]) self.assertTrue(generator.assets.is_model(name)) self.assertTrue(generator.run_engines()) self.assertIn(name, generator.world.models)
def test_solid_sphere_inertia(self): # Test invalid inputs inputs = [[-1, 1], [1, -1], [0, 1], [1, 0]] for test_input in inputs: with self.assertRaises(AssertionError): Inertial.create_hollow_sphere_inertia(*test_input) mass = random.rand() radius = random.rand() ref_inertia = get_solid_sphere_inertia(mass, radius) inertia = Inertial.create_solid_sphere_inertia(mass, radius) for tag in ref_inertia: self.assertEqual( ref_inertia[tag], getattr(inertia, tag), 'Element {} set with wrong value, ' 'correct={}, value={}'.format(tag, ref_inertia[tag], getattr(inertia, tag)))
def test_centered_rod_inerita(self): # Test invalid inputs inputs = [ [0, 0, []], [1, 1, None], [1, 1, [1, 1]], [1, 1, [1, 1, 1]], ] for test_input in inputs: with self.assertRaises(AssertionError): Inertial.create_centered_rod_inertia(*test_input) inputs = [[random.rand(), random.rand(), [1, 0, 0]], [random.rand(), random.rand(), [0, 1, 0]], [random.rand(), random.rand(), [0, 0, 1]]] for test_input in inputs: ref_inertia = get_centered_rod_inertia(*test_input) inertia = Inertial.create_centered_rod_inertia(*test_input) for tag in ref_inertia: self.assertEqual( ref_inertia[tag], getattr(inertia, tag), 'Element {} set with wrong ' 'value, correct={}, value={}'.format( tag, ref_inertia[tag], getattr(inertia, tag)))
def test_seed(self): seeds = [random.randint(100) for _ in range(3)] for seed in seeds: random.init_random_state(seed) self.assertEqual(random.PCG_RANDOM_SEED, seed) # Test rand random.init_random_state(seed) ref = [random.rand() for _ in range(5)] for _ in range(3): random.init_random_state(seed) self.assertEqual(random.PCG_RANDOM_SEED, seed) self.assertEqual(ref, [random.rand() for _ in range(5)]) random.init_random_state(seed) self.assertEqual(random.PCG_RANDOM_SEED, seed) # Test randint ref = [random.randint(10) for _ in range(5)] for _ in range(3): random.init_random_state(seed) self.assertEqual(random.PCG_RANDOM_SEED, seed) self.assertEqual(ref, [random.randint(10) for _ in range(5)]) # Test choice values = [random.rand() for _ in range(10)] random.init_random_state(seed) ref = [random.choice(values) for _ in range(5)] for _ in range(3): random.init_random_state(seed) self.assertEqual(random.PCG_RANDOM_SEED, seed) self.assertEqual(ref, [random.choice(values) for _ in range(5)])
def test_add_box_primitive(self): mesh_manager = MeshManager.get_instance() mesh_manager.reset() self.assertEqual(len(mesh_manager.tags), 0) tag = mesh_manager.add(tag='box_1', type='box', size=[1, 1, 1]) self.assertEqual(tag, 'box_1') tag = mesh_manager.add(tag='box_2', type='box', size=[2, 2, 2]) self.assertEqual(tag, 'box_2') # Add box with same size of box_1 tag = mesh_manager.add(type='box', size=[1, 1, 1]) self.assertEqual(tag, 'box_1') tag = mesh_manager.find_by_parameters( type='box', size=[2, 2, 2]) self.assertEqual(tag, 'box_2') # Let the manager generate the tag self.assertIsNotNone(mesh_manager.add( type='box', size=random.rand(3)))
def test_add_sphere_primitive(self): mesh_manager = MeshManager.get_instance() mesh_manager.reset() self.assertEqual(len(mesh_manager.tags), 0) tag = mesh_manager.add( tag='sphere_1', type='sphere', radius=1) self.assertEqual(tag, 'sphere_1') tag = mesh_manager.add( tag='sphere_2', type='sphere', radius=2) self.assertEqual(tag, 'sphere_2') # Add sphere with same size of sphere_1 tag = mesh_manager.add(type='sphere', radius=1) self.assertEqual(tag, 'sphere_1') tag = mesh_manager.find_by_parameters(type='sphere', radius=2) self.assertEqual(tag, 'sphere_2') # Let the manager generate the tag self.assertIsNotNone(mesh_manager.add( type='sphere', radius=random.rand()))
def test_choice(self): # Test default output values = [random.rand() for _ in range(10)] output = random.choice(values) self.assertIn(output, values)
def test_rand(self): # Test default output output = random.rand() self.assertTrue(is_scalar(output)) self.assertGreaterEqual(output, 0) self.assertLessEqual(output, 1)