| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/wcharczuk/go-chart/v2/testutil" |
| 8 |
) |
| 9 |
|
| 10 |
func TestBoxClone(t *testing.T) { |
| 11 |
|
| 12 |
a := Box{Top: 5, Left: 5, Right: 15, Bottom: 15} |
| 13 |
b := a.Clone() |
| 14 |
testutil.AssertTrue(t, a.Equals(b)) |
| 15 |
testutil.AssertTrue(t, b.Equals(a)) |
| 16 |
} |
| 17 |
|
| 18 |
func TestBoxEquals(t *testing.T) { |
| 19 |
|
| 20 |
|
| 21 |
a := Box{Top: 5, Left: 5, Right: 15, Bottom: 15} |
| 22 |
b := Box{Top: 10, Left: 10, Right: 30, Bottom: 30} |
| 23 |
c := Box{Top: 5, Left: 5, Right: 15, Bottom: 15} |
| 24 |
testutil.AssertTrue(t, a.Equals(a)) |
| 25 |
testutil.AssertTrue(t, a.Equals(c)) |
| 26 |
testutil.AssertTrue(t, c.Equals(a)) |
| 27 |
testutil.AssertFalse(t, a.Equals(b)) |
| 28 |
testutil.AssertFalse(t, c.Equals(b)) |
| 29 |
testutil.AssertFalse(t, b.Equals(a)) |
| 30 |
testutil.AssertFalse(t, b.Equals(c)) |
| 31 |
} |
| 32 |
|
| 33 |
func TestBoxIsBiggerThan(t *testing.T) { |
| 34 |
|
| 35 |
|
| 36 |
a := Box{Top: 5, Left: 5, Right: 25, Bottom: 25} |
| 37 |
b := Box{Top: 10, Left: 10, Right: 20, Bottom: 20} |
| 38 |
c := Box{Top: 1, Left: 1, Right: 30, Bottom: 30} |
| 39 |
testutil.AssertTrue(t, a.IsBiggerThan(b)) |
| 40 |
testutil.AssertFalse(t, a.IsBiggerThan(c)) |
| 41 |
testutil.AssertTrue(t, c.IsBiggerThan(a)) |
| 42 |
} |
| 43 |
|
| 44 |
func TestBoxIsSmallerThan(t *testing.T) { |
| 45 |
|
| 46 |
|
| 47 |
a := Box{Top: 5, Left: 5, Right: 25, Bottom: 25} |
| 48 |
b := Box{Top: 10, Left: 10, Right: 20, Bottom: 20} |
| 49 |
c := Box{Top: 1, Left: 1, Right: 30, Bottom: 30} |
| 50 |
testutil.AssertFalse(t, a.IsSmallerThan(b)) |
| 51 |
testutil.AssertTrue(t, a.IsSmallerThan(c)) |
| 52 |
testutil.AssertFalse(t, c.IsSmallerThan(a)) |
| 53 |
} |
| 54 |
|
| 55 |
func TestBoxGrow(t *testing.T) { |
| 56 |
|
| 57 |
|
| 58 |
a := Box{Top: 1, Left: 2, Right: 15, Bottom: 15} |
| 59 |
b := Box{Top: 4, Left: 5, Right: 30, Bottom: 35} |
| 60 |
c := a.Grow(b) |
| 61 |
testutil.AssertFalse(t, c.Equals(b)) |
| 62 |
testutil.AssertFalse(t, c.Equals(a)) |
| 63 |
testutil.AssertEqual(t, 1, c.Top) |
| 64 |
testutil.AssertEqual(t, 2, c.Left) |
| 65 |
testutil.AssertEqual(t, 30, c.Right) |
| 66 |
testutil.AssertEqual(t, 35, c.Bottom) |
| 67 |
} |
| 68 |
|
| 69 |
func TestBoxFit(t *testing.T) { |
| 70 |
|
| 71 |
|
| 72 |
a := Box{Top: 64, Left: 64, Right: 192, Bottom: 192} |
| 73 |
b := Box{Top: 16, Left: 16, Right: 256, Bottom: 170} |
| 74 |
c := Box{Top: 16, Left: 16, Right: 170, Bottom: 256} |
| 75 |
|
| 76 |
fab := a.Fit(b) |
| 77 |
testutil.AssertEqual(t, a.Left, fab.Left) |
| 78 |
testutil.AssertEqual(t, a.Right, fab.Right) |
| 79 |
testutil.AssertTrue(t, fab.Top < fab.Bottom) |
| 80 |
testutil.AssertTrue(t, fab.Left < fab.Right) |
| 81 |
testutil.AssertTrue(t, math.Abs(b.Aspect()-fab.Aspect()) < 0.02) |
| 82 |
|
| 83 |
fac := a.Fit(c) |
| 84 |
testutil.AssertEqual(t, a.Top, fac.Top) |
| 85 |
testutil.AssertEqual(t, a.Bottom, fac.Bottom) |
| 86 |
testutil.AssertTrue(t, math.Abs(c.Aspect()-fac.Aspect()) < 0.02) |
| 87 |
} |
| 88 |
|
| 89 |
func TestBoxConstrain(t *testing.T) { |
| 90 |
|
| 91 |
|
| 92 |
a := Box{Top: 64, Left: 64, Right: 192, Bottom: 192} |
| 93 |
b := Box{Top: 16, Left: 16, Right: 256, Bottom: 170} |
| 94 |
c := Box{Top: 16, Left: 16, Right: 170, Bottom: 256} |
| 95 |
|
| 96 |
cab := a.Constrain(b) |
| 97 |
testutil.AssertEqual(t, 64, cab.Top) |
| 98 |
testutil.AssertEqual(t, 64, cab.Left) |
| 99 |
testutil.AssertEqual(t, 192, cab.Right) |
| 100 |
testutil.AssertEqual(t, 170, cab.Bottom) |
| 101 |
|
| 102 |
cac := a.Constrain(c) |
| 103 |
testutil.AssertEqual(t, 64, cac.Top) |
| 104 |
testutil.AssertEqual(t, 64, cac.Left) |
| 105 |
testutil.AssertEqual(t, 170, cac.Right) |
| 106 |
testutil.AssertEqual(t, 192, cac.Bottom) |
| 107 |
} |
| 108 |
|
| 109 |
func TestBoxOuterConstrain(t *testing.T) { |
| 110 |
|
| 111 |
|
| 112 |
box := NewBox(0, 0, 100, 100) |
| 113 |
canvas := NewBox(5, 5, 95, 95) |
| 114 |
taller := NewBox(-10, 5, 50, 50) |
| 115 |
|
| 116 |
c := canvas.OuterConstrain(box, taller) |
| 117 |
testutil.AssertEqual(t, 15, c.Top, c.String()) |
| 118 |
testutil.AssertEqual(t, 5, c.Left, c.String()) |
| 119 |
testutil.AssertEqual(t, 95, c.Right, c.String()) |
| 120 |
testutil.AssertEqual(t, 95, c.Bottom, c.String()) |
| 121 |
|
| 122 |
wider := NewBox(5, 5, 110, 50) |
| 123 |
d := canvas.OuterConstrain(box, wider) |
| 124 |
testutil.AssertEqual(t, 5, d.Top, d.String()) |
| 125 |
testutil.AssertEqual(t, 5, d.Left, d.String()) |
| 126 |
testutil.AssertEqual(t, 85, d.Right, d.String()) |
| 127 |
testutil.AssertEqual(t, 95, d.Bottom, d.String()) |
| 128 |
} |
| 129 |
|
| 130 |
func TestBoxShift(t *testing.T) { |
| 131 |
|
| 132 |
|
| 133 |
b := Box{ |
| 134 |
Top: 5, |
| 135 |
Left: 5, |
| 136 |
Right: 10, |
| 137 |
Bottom: 10, |
| 138 |
} |
| 139 |
|
| 140 |
shifted := b.Shift(1, 2) |
| 141 |
testutil.AssertEqual(t, 7, shifted.Top) |
| 142 |
testutil.AssertEqual(t, 6, shifted.Left) |
| 143 |
testutil.AssertEqual(t, 11, shifted.Right) |
| 144 |
testutil.AssertEqual(t, 12, shifted.Bottom) |
| 145 |
} |
| 146 |
|
| 147 |
func TestBoxCenter(t *testing.T) { |
| 148 |
|
| 149 |
|
| 150 |
b := Box{ |
| 151 |
Top: 10, |
| 152 |
Left: 10, |
| 153 |
Right: 20, |
| 154 |
Bottom: 30, |
| 155 |
} |
| 156 |
cx, cy := b.Center() |
| 157 |
testutil.AssertEqual(t, 15, cx) |
| 158 |
testutil.AssertEqual(t, 20, cy) |
| 159 |
} |
| 160 |
|
| 161 |
func TestBoxCornersCenter(t *testing.T) { |
| 162 |
|
| 163 |
|
| 164 |
bc := BoxCorners{ |
| 165 |
TopLeft: Point{5, 5}, |
| 166 |
TopRight: Point{15, 5}, |
| 167 |
BottomRight: Point{15, 15}, |
| 168 |
BottomLeft: Point{5, 15}, |
| 169 |
} |
| 170 |
|
| 171 |
cx, cy := bc.Center() |
| 172 |
testutil.AssertEqual(t, 10, cx) |
| 173 |
testutil.AssertEqual(t, 10, cy) |
| 174 |
} |
| 175 |
|
| 176 |
func TestBoxCornersRotate(t *testing.T) { |
| 177 |
|
| 178 |
|
| 179 |
bc := BoxCorners{ |
| 180 |
TopLeft: Point{5, 5}, |
| 181 |
TopRight: Point{15, 5}, |
| 182 |
BottomRight: Point{15, 15}, |
| 183 |
BottomLeft: Point{5, 15}, |
| 184 |
} |
| 185 |
|
| 186 |
rotated := bc.Rotate(45) |
| 187 |
testutil.AssertTrue(t, rotated.TopLeft.Equals(Point{10, 3}), rotated.String()) |
| 188 |
} |