From 610a070aab7ab83ffbce53879be3eb4e58c664d0 Mon Sep 17 00:00:00 2001 From: Matthew Coleman Date: Thu, 7 Mar 2024 17:26:16 -0500 Subject: [PATCH] Add test for HSplitCellsReversed --- private/area/area_test.go | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/private/area/area_test.go b/private/area/area_test.go index f4a46eb..1105b26 100644 --- a/private/area/area_test.go +++ b/private/area/area_test.go @@ -452,6 +452,91 @@ func TestHSplitCells(t *testing.T) { } } +func TestHSplitCellsReversed(t *testing.T) { + tests := []struct { + desc string + area image.Rectangle + cells int + wantTop image.Rectangle + wantBottom image.Rectangle + wantErr bool + }{ + { + desc: "fails on negative cells", + area: image.Rect(1, 1, 2, 2), + cells: -1, + wantErr: true, + }, + { + desc: "returns area as bottom on cells too large", + area: image.Rect(1, 1, 2, 2), + cells: 2, + wantTop: image.ZR, + wantBottom: image.Rect(1, 1, 2, 2), + }, + { + desc: "returns area as bottom on cells equal area width", + area: image.Rect(1, 1, 2, 2), + cells: 1, + wantTop: image.ZR, + wantBottom: image.Rect(1, 1, 2, 2), + }, + { + desc: "returns area as top on zero cells", + area: image.Rect(1, 1, 2, 2), + cells: 0, + wantBottom: image.ZR, + wantTop: image.Rect(1, 1, 2, 2), + }, + { + desc: "zero area to begin with", + area: image.ZR, + cells: 0, + wantTop: image.ZR, + wantBottom: image.ZR, + }, + { + desc: "splits area with even height", + area: image.Rect(1, 1, 3, 3), + cells: 1, + wantTop: image.Rect(1, 1, 3, 2), + wantBottom: image.Rect(1, 2, 3, 3), + }, + { + desc: "splits area with odd width", + area: image.Rect(1, 1, 4, 4), + cells: 1, + wantTop: image.Rect(1, 1, 4, 3), + wantBottom: image.Rect(1, 3, 4, 4), + }, + { + desc: "splits to unequal areas", + area: image.Rect(0, 0, 4, 4), + cells: 3, + wantTop: image.Rect(0, 0, 4, 1), + wantBottom: image.Rect(0, 1, 4, 4), + }, + } + + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + gotTop, gotBottom, err := HSplitCellsReversed(tc.area, tc.cells) + if (err != nil) != tc.wantErr { + t.Errorf("HSplitCells => unexpected error:%v, wantErr:%v", err, tc.wantErr) + } + if err != nil { + return + } + if diff := pretty.Compare(tc.wantTop, gotTop); diff != "" { + t.Errorf("HSplitCells => left value unexpected diff (-want, +got):\n%s", diff) + } + if diff := pretty.Compare(tc.wantBottom, gotBottom); diff != "" { + t.Errorf("HSplitCells => right value unexpected diff (-want, +got):\n%s", diff) + } + }) + } +} + func TestExcludeBorder(t *testing.T) { tests := []struct { desc string