| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package layout |
| |
| import ( |
| "testing" |
| |
| _ "github.com/bazelbuild/rsclient/internal/pkg/testsetup" |
| ) |
| |
| func TestGridMap(t *testing.T) { |
| // Setup: A 2x2 regular grid representing a total world area of 30x20. |
| // |
| // Columns (X-axis): |
| // Col 0: Width 10 (Span [0, 10)) |
| // Col 1: Width 20 (Span [10, 30)) |
| // |
| // Rows (Y-axis): |
| // Row 0: Height 5 (Span [0, 5)) |
| // Row 1: Height 15 (Span [5, 20)) |
| m := NewGridMap([]int{10, 20}, []int{5, 15}) |
| |
| t.Run("Cell geometry", func(t *testing.T) { |
| // Verify that we can retrieve the absolute rectangular coordinates |
| // and dimensions for a specific cell in the grid. |
| |
| // Target cell: Row 0, Col 1 |
| got := m.Cell(Point{X: 1, Y: 0}) |
| |
| // Expected: Starts at X=10 (after Col 0), Y=0. |
| // Dimensions: Width=20 (Col 1 size), Height=5 (Row 0 size). |
| want := Rect{ |
| X: Span{10, 20}, |
| Y: Span{0, 5}, |
| } |
| if got != want { |
| t.Errorf("Cell(1,0) = %+v, want %+v", got, want) |
| } |
| }) |
| |
| t.Run("Visible Range", func(t *testing.T) { |
| // Verify that a 2D viewport identifies the correct sub-rectangle (BoundingBox) |
| // of indices that actually exist within that view. |
| |
| // Viewport: Top-left at (15, 10), with size 5x5. |
| // World span: X in [15, 20), Y in [10, 15). |
| viewport := Rect{ |
| X: Span{15, 5}, |
| Y: Span{10, 5}, |
| } |
| got := m.VisibleRange(viewport) |
| |
| // Intersection Analysis: |
| // X-axis: [15, 20) falls entirely inside Col 1 [10, 30). -> Col range [1, 2) |
| // Y-axis: [10, 15) falls entirely inside Row 1 [5, 20). -> Row range [1, 2) |
| want := BoundingBox{ |
| Rows: Interval{1, 2}, |
| Cols: Interval{1, 2}, |
| } |
| if got != want { |
| t.Errorf("VisibleRange = %+v, want Row[1,2) Col[1,2)", got) |
| } |
| }) |
| } |