| """Tests for symbolic/solver.py -- deterministic VSI-Bench question answering.""" | |
| import pytest | |
| import symbolic_solver_tests as solver | |
| def test_unit_parsers_accept_strings_and_numbers(): | |
| assert solver._parse_meters("-1.25 meters") == -1.25 | |
| assert solver._parse_square_meters("12.5 square meters") == 12.5 | |
| assert solver._parse_meters(3) == 3.0 | |
| with pytest.raises(ValueError, match="could not parse"): | |
| solver._parse_meters("unknown") | |
| def test_direct_numeric_answers(spatial_code): | |
| assert ( | |
| solver.answer( | |
| "object_counting", "How many chair(s) are in this room?", None, spatial_code | |
| ) | |
| == 2 | |
| ) | |
| assert ( | |
| solver.answer( | |
| "object_size_estimation", | |
| "What is the longest dimension of the table, measured in centimeters?", | |
| None, | |
| spatial_code, | |
| ) | |
| == 120.0 | |
| ) | |
| assert ( | |
| solver.answer( | |
| "room_size_estimation", "What is the size of this room?", None, spatial_code | |
| ) | |
| == 12.5 | |
| ) | |
| assert ( | |
| solver.answer( | |
| "object_abs_distance", | |
| "What is the distance between the chair and the table (in meters)?", | |
| None, | |
| spatial_code, | |
| ) | |
| == 1.0 | |
| ) | |
| def test_multiple_choice_distance_and_order_answers(spatial_code): | |
| assert ( | |
| solver.answer( | |
| "object_rel_distance", | |
| "Which of these objects is closest to the table?", | |
| ["A. sofa", "B. lamp", "C. chair"], | |
| spatial_code, | |
| ) | |
| == "B" | |
| ) | |
| assert ( | |
| solver.answer( | |
| "obj_appearance_order", | |
| "What is the first-time appearance order of the categories?", | |
| ["A. table, chair, lamp", "B. chair, table, lamp"], | |
| spatial_code, | |
| ) | |
| == "B" | |
| ) | |
| def test_direction_answers_use_floor_coordinates(spatial_code): | |
| question = ( | |
| "If I am standing by the chair and facing the table, is the lamp to my left?" | |
| ) | |
| assert ( | |
| solver.answer( | |
| "object_rel_direction_hard", | |
| question, | |
| ["A. front-left", "B. front-right", "C. back-left", "D. back-right"], | |
| spatial_code, | |
| ) | |
| == "A" | |
| ) | |
| assert ( | |
| solver.answer( | |
| "object_rel_direction_easy", | |
| question, | |
| ["A. left", "B. right"], | |
| spatial_code, | |
| ) | |
| == "A" | |
| ) | |
| def test_route_planning_chains_turns(spatial_code): | |
| question = ( | |
| "You are a robot beginning at the chair facing the table. Actions: " | |
| "1. Go forward until the table 2. [please fill in] " | |
| "3. Go forward until the lamp." | |
| ) | |
| assert ( | |
| solver.answer( | |
| "route_planning", | |
| question, | |
| ["A. Turn Left", "B. Turn Right", "C. Turn Back"], | |
| spatial_code, | |
| ) | |
| == "A" | |
| ) | |
| def test_dispatch_returns_none_for_unknown_or_missing_data(spatial_code): | |
| assert solver.answer("unknown", "question", None, spatial_code) is None | |
| assert ( | |
| solver.answer( | |
| "object_counting", | |
| "How many cabinet(s) are in this room?", | |
| None, | |
| spatial_code, | |
| ) | |
| == 0 | |
| ) | |
| assert solver.pairwise_swap_distance(["a", "b", "c"], ["b", "a", "c"]) == 1 | |
| assert solver.pairwise_swap_distance(["a"], ["b"]) is None | |
| def test_answer_snapshots_operation_counts_per_question(): | |
| """H25 instrumentation: LAST_ANSWER_OPS reflects only the LAST question, and a | |
| multi-step distance question costs strictly more operations than a pure count | |
| lookup. Counting must never change any answer (every other test in this file | |
| still passing is the guarantee).""" | |
| from symbolic import adapters, solver | |
| code = adapters.adapt_spatial_code( | |
| { | |
| "spatial code schema": {}, | |
| "objects": { | |
| "chair": [ | |
| { | |
| "3D oriented bounding box": { | |
| "3D oriented bounding box center coordinates": [ | |
| 0.0, | |
| 0.0, | |
| 0.5, | |
| ], | |
| "3D oriented bounding box dimensions": [1.0, 1.0, 1.0], | |
| "3D oriented bounding box orientation unit vectors": [ | |
| [1.0, 0.0, 0.0], | |
| [0.0, 0.0, 1.0], | |
| [0.0, -1.0, 0.0], | |
| ], | |
| }, | |
| "first visible time": 0.0, | |
| } | |
| ], | |
| "table": [ | |
| { | |
| "3D oriented bounding box": { | |
| "3D oriented bounding box center coordinates": [ | |
| 3.0, | |
| 0.0, | |
| 0.5, | |
| ], | |
| "3D oriented bounding box dimensions": [2.0, 1.0, 1.0], | |
| "3D oriented bounding box orientation unit vectors": [ | |
| [1.0, 0.0, 0.0], | |
| [0.0, 0.0, 1.0], | |
| [0.0, -1.0, 0.0], | |
| ], | |
| }, | |
| "first visible time": 1.0, | |
| } | |
| ], | |
| }, | |
| "room": {"floor boundary polygons": []}, | |
| } | |
| ) | |
| solver.answer("object_counting", "How many chair(s) are in this room?", None, code) | |
| counting_ops = dict(solver.LAST_ANSWER_OPS) | |
| assert counting_ops["total"] >= 1 | |
| solver.answer( | |
| "object_abs_distance", | |
| "Measuring from the closest point of each object, what is the direct distance " | |
| "between the chair and the table (in meters)?", | |
| None, | |
| code, | |
| ) | |
| distance_ops = dict(solver.LAST_ANSWER_OPS) | |
| assert distance_ops["total"] > counting_ops["total"] | |