|
| 1 | +using System; |
| 2 | +using System.Drawing; |
| 3 | +using System.Globalization; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace AdvancedSharpAdbClient.Tests |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Tests the <see cref="Area"/> class. |
| 10 | + /// </summary> |
| 11 | + public class AreaTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public void DefaultConstructorTest() |
| 15 | + { |
| 16 | + Assert.Equal(Area.Empty, new Area()); |
| 17 | + } |
| 18 | + |
| 19 | + [Theory] |
| 20 | + [InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)] |
| 21 | + [InlineData(int.MaxValue, 0, int.MinValue, 0)] |
| 22 | + [InlineData(0, 0, 0, 0)] |
| 23 | + [InlineData(0, int.MinValue, 0, int.MaxValue)] |
| 24 | + public void NonDefaultConstructorTest(int x, int y, int width, int height) |
| 25 | + { |
| 26 | + Area rect1 = new(x, y, width, height); |
| 27 | + Area rect2 = new(new Cords(x, y), new Size(width, height)); |
| 28 | + |
| 29 | + Assert.Equal(rect1, rect2); |
| 30 | + } |
| 31 | + |
| 32 | + [Theory] |
| 33 | + [InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)] |
| 34 | + [InlineData(int.MaxValue, 0, int.MinValue, 0)] |
| 35 | + [InlineData(0, 0, 0, 0)] |
| 36 | + [InlineData(0, int.MinValue, 0, int.MaxValue)] |
| 37 | + public void FromLTRBTest(int left, int top, int right, int bottom) |
| 38 | + { |
| 39 | + Area rect1 = new(left, top, unchecked(right - left), unchecked(bottom - top)); |
| 40 | + Area rect2 = Area.FromLTRB(left, top, right, bottom); |
| 41 | + |
| 42 | + Assert.Equal(rect1, rect2); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void EmptyTest() |
| 47 | + { |
| 48 | + Assert.True(Area.Empty.IsEmpty); |
| 49 | + Assert.True(new Area(0, 0, 0, 0).IsEmpty); |
| 50 | + Assert.True(new Area().IsEmpty); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory] |
| 54 | + [InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)] |
| 55 | + [InlineData(int.MaxValue, 0, int.MinValue, 0)] |
| 56 | + [InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)] |
| 57 | + [InlineData(0, int.MinValue, 0, int.MaxValue)] |
| 58 | + public void NonEmptyTest(int x, int y, int width, int height) |
| 59 | + { |
| 60 | + Assert.False(new Area(x, y, width, height).IsEmpty); |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)] |
| 65 | + [InlineData(int.MaxValue, 0, int.MinValue, 0)] |
| 66 | + [InlineData(0, 0, 0, 0)] |
| 67 | + [InlineData(0, int.MinValue, 0, int.MaxValue)] |
| 68 | + [InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)] |
| 69 | + public void DimensionsTest(int x, int y, int width, int height) |
| 70 | + { |
| 71 | + Area rect = new(x, y, width, height); |
| 72 | + Assert.Equal(new Cords(x, y), rect.Location); |
| 73 | + Assert.Equal(new Size(width, height), rect.Size); |
| 74 | + |
| 75 | + Assert.Equal(x, rect.X); |
| 76 | + Assert.Equal(y, rect.Y); |
| 77 | + Assert.Equal(width, rect.Width); |
| 78 | + Assert.Equal(height, rect.Height); |
| 79 | + Assert.Equal(x, rect.Left); |
| 80 | + Assert.Equal(y, rect.Top); |
| 81 | + Assert.Equal(unchecked(x + width), rect.Right); |
| 82 | + Assert.Equal(unchecked(y + height), rect.Bottom); |
| 83 | + |
| 84 | + Cords p = new(width, height); |
| 85 | + Size s = new(x, y); |
| 86 | + rect.Location = p; |
| 87 | + rect.Size = s; |
| 88 | + |
| 89 | + Assert.Equal(p, rect.Location); |
| 90 | + Assert.Equal(s, rect.Size); |
| 91 | + |
| 92 | + Assert.Equal(width, rect.X); |
| 93 | + Assert.Equal(height, rect.Y); |
| 94 | + Assert.Equal(x, rect.Width); |
| 95 | + Assert.Equal(y, rect.Height); |
| 96 | + Assert.Equal(width, rect.Left); |
| 97 | + Assert.Equal(height, rect.Top); |
| 98 | + Assert.Equal(unchecked(x + width), rect.Right); |
| 99 | + Assert.Equal(unchecked(y + height), rect.Bottom); |
| 100 | + } |
| 101 | + |
| 102 | + [Theory] |
| 103 | + [InlineData(0, 0)] |
| 104 | + [InlineData(int.MaxValue, int.MinValue)] |
| 105 | + public static void LocationSetTest(int x, int y) |
| 106 | + { |
| 107 | + Cords Cords = new(x, y); |
| 108 | + Area rect = new(10, 10, 10, 10) |
| 109 | + { |
| 110 | + Location = Cords |
| 111 | + }; |
| 112 | + Assert.Equal(Cords, rect.Location); |
| 113 | + Assert.Equal(Cords.X, rect.X); |
| 114 | + Assert.Equal(Cords.Y, rect.Y); |
| 115 | + } |
| 116 | + |
| 117 | + [Theory] |
| 118 | + [InlineData(0, 0)] |
| 119 | + [InlineData(int.MaxValue, int.MinValue)] |
| 120 | + public static void SizeSetTest(int x, int y) |
| 121 | + { |
| 122 | + Size size = new(x, y); |
| 123 | + Area rect = new(10, 10, 10, 10) |
| 124 | + { |
| 125 | + Size = size |
| 126 | + }; |
| 127 | + Assert.Equal(size, rect.Size); |
| 128 | + Assert.Equal(size.Width, rect.Width); |
| 129 | + Assert.Equal(size.Height, rect.Height); |
| 130 | + } |
| 131 | + |
| 132 | + [Theory] |
| 133 | + [InlineData(int.MaxValue, int.MinValue, int.MaxValue, int.MinValue)] |
| 134 | + [InlineData(int.MaxValue, 0, int.MinValue, 0)] |
| 135 | + [InlineData(0, int.MinValue, 0, int.MaxValue)] |
| 136 | + [InlineData(int.MinValue, int.MaxValue, int.MinValue, int.MaxValue)] |
| 137 | + public void EqualityTest(int x, int y, int width, int height) |
| 138 | + { |
| 139 | + Area rect1 = new(x, y, width, height); |
| 140 | + Area rect2 = new(width / 2, height / 2, x, y); |
| 141 | + |
| 142 | + Assert.True(rect1 != rect2); |
| 143 | + Assert.False(rect1 == rect2); |
| 144 | + Assert.False(rect1.Equals(rect2)); |
| 145 | + Assert.False(rect1.Equals((object)rect2)); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public static void EqualityTest_NotArea() |
| 150 | + { |
| 151 | + Area Area = new(0, 0, 0, 0); |
| 152 | + Assert.False(Area.Equals(null)); |
| 153 | + Assert.False(Area.Equals(0)); |
| 154 | + Assert.False(Area.Equals(new RectangleF(0, 0, 0, 0))); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public static void GetHashCodeTest() |
| 159 | + { |
| 160 | + Area rect1 = new(10, 10, 10, 10); |
| 161 | + Area rect2 = new(10, 10, 10, 10); |
| 162 | + Assert.Equal(rect1.GetHashCode(), rect2.GetHashCode()); |
| 163 | + Assert.NotEqual(rect1.GetHashCode(), new Area(20, 10, 10, 10).GetHashCode()); |
| 164 | + Assert.NotEqual(rect1.GetHashCode(), new Area(10, 20, 10, 10).GetHashCode()); |
| 165 | + Assert.NotEqual(rect1.GetHashCode(), new Area(10, 10, 20, 10).GetHashCode()); |
| 166 | + Assert.NotEqual(rect1.GetHashCode(), new Area(10, 10, 10, 20).GetHashCode()); |
| 167 | + } |
| 168 | + |
| 169 | + [Theory] |
| 170 | + [InlineData(float.MaxValue, float.MinValue, float.MaxValue, float.MinValue)] |
| 171 | + [InlineData(float.MinValue, float.MaxValue, float.MinValue, float.MaxValue)] |
| 172 | + [InlineData(0, 0, 0, 0)] |
| 173 | + public void AreaFConversionTest(float x, float y, float width, float height) |
| 174 | + { |
| 175 | + RectangleF rect = new(x, y, width, height); |
| 176 | + Area rCeiling, rTruncate, rRound; |
| 177 | + |
| 178 | + unchecked |
| 179 | + { |
| 180 | + rCeiling = new Area((int)Math.Ceiling(x), (int)Math.Ceiling(y), |
| 181 | + (int)Math.Ceiling(width), (int)Math.Ceiling(height)); |
| 182 | + rTruncate = new Area((int)x, (int)y, (int)width, (int)height); |
| 183 | + rRound = new Area((int)Math.Round(x), (int)Math.Round(y), |
| 184 | + (int)Math.Round(width), (int)Math.Round(height)); |
| 185 | + } |
| 186 | + |
| 187 | + Assert.Equal(rCeiling, Area.Ceiling(rect)); |
| 188 | + Assert.Equal(rTruncate, Area.Truncate(rect)); |
| 189 | + Assert.Equal(rRound, Area.Round(rect)); |
| 190 | + } |
| 191 | + |
| 192 | + [Theory] |
| 193 | + [InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)] |
| 194 | + [InlineData(0, int.MinValue, int.MaxValue, 0)] |
| 195 | + public void ContainsTest(int x, int y, int width, int height) |
| 196 | + { |
| 197 | + Area rect = new(unchecked(2 * x - width), unchecked(2 * y - height), width, height); |
| 198 | + Cords p = new(x, y); |
| 199 | + Area r = new(x, y, width / 2, height / 2); |
| 200 | + |
| 201 | + Assert.False(rect.Contains(x, y)); |
| 202 | + Assert.False(rect.Contains(p)); |
| 203 | + Assert.False(rect.Contains(r)); |
| 204 | + } |
| 205 | + |
| 206 | + [Theory] |
| 207 | + [InlineData(0, 0, 0, 0)] |
| 208 | + [InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)] |
| 209 | + [InlineData(0, int.MinValue, int.MaxValue, 0)] |
| 210 | + public void InflateTest(int x, int y, int width, int height) |
| 211 | + { |
| 212 | + Area inflatedRect, rect = new(x, y, width, height); |
| 213 | + unchecked |
| 214 | + { |
| 215 | + inflatedRect = new Area(x - width, y - height, width + 2 * width, height + 2 * height); |
| 216 | + } |
| 217 | + |
| 218 | + Assert.Equal(inflatedRect, Area.Inflate(rect, width, height)); |
| 219 | + |
| 220 | + rect.Inflate(width, height); |
| 221 | + Assert.Equal(inflatedRect, rect); |
| 222 | + |
| 223 | + Size s = new(x, y); |
| 224 | + unchecked |
| 225 | + { |
| 226 | + inflatedRect = new Area(rect.X - x, rect.Y - y, rect.Width + 2 * x, rect.Height + 2 * y); |
| 227 | + } |
| 228 | + |
| 229 | + rect.Inflate(s); |
| 230 | + Assert.Equal(inflatedRect, rect); |
| 231 | + } |
| 232 | + |
| 233 | + [Theory] |
| 234 | + [InlineData(0, 0, 0, 0)] |
| 235 | + [InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)] |
| 236 | + [InlineData(0, int.MinValue, int.MaxValue, 0)] |
| 237 | + public void IntersectTest(int x, int y, int width, int height) |
| 238 | + { |
| 239 | + Area rect = new(x, y, width, height); |
| 240 | + Area expectedRect = Area.Intersect(rect, rect); |
| 241 | + rect.Intersect(rect); |
| 242 | + Assert.Equal(expectedRect, rect); |
| 243 | + Assert.False(rect.IntersectsWith(expectedRect)); |
| 244 | + } |
| 245 | + |
| 246 | + [Fact] |
| 247 | + public static void Intersect_IntersectingAreas_Test() |
| 248 | + { |
| 249 | + Area rect1 = new(0, 0, 5, 5); |
| 250 | + Area rect2 = new(1, 1, 3, 3); |
| 251 | + Area expected = new(1, 1, 3, 3); |
| 252 | + |
| 253 | + Assert.Equal(expected, Area.Intersect(rect1, rect2)); |
| 254 | + } |
| 255 | + |
| 256 | + [Theory] |
| 257 | + [InlineData(0, 0, 0, 0)] |
| 258 | + [InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)] |
| 259 | + [InlineData(int.MaxValue, 0, 0, int.MaxValue)] |
| 260 | + [InlineData(0, int.MinValue, int.MaxValue, 0)] |
| 261 | + public void UnionTest(int x, int y, int width, int height) |
| 262 | + { |
| 263 | + Area a = new(x, y, width, height); |
| 264 | + Area b = new(width, height, x, y); |
| 265 | + |
| 266 | + int x1 = Math.Min(a.X, b.X); |
| 267 | + int x2 = Math.Max(a.X + a.Width, b.X + b.Width); |
| 268 | + int y1 = Math.Min(a.Y, b.Y); |
| 269 | + int y2 = Math.Max(a.Y + a.Height, b.Y + b.Height); |
| 270 | + |
| 271 | + Area expectedArea = new(x1, y1, x2 - x1, y2 - y1); |
| 272 | + |
| 273 | + Assert.Equal(expectedArea, Area.Union(a, b)); |
| 274 | + } |
| 275 | + |
| 276 | + [Theory] |
| 277 | + [InlineData(0, 0, 0, 0)] |
| 278 | + [InlineData(int.MaxValue, int.MinValue, int.MinValue, int.MaxValue)] |
| 279 | + [InlineData(int.MaxValue, 0, 0, int.MaxValue)] |
| 280 | + [InlineData(0, int.MinValue, int.MaxValue, 0)] |
| 281 | + public void OffsetTest(int x, int y, int width, int height) |
| 282 | + { |
| 283 | + Area r1 = new(x, y, width, height); |
| 284 | + Area expectedRect = new(x + width, y + height, width, height); |
| 285 | + Cords p = new(width, height); |
| 286 | + |
| 287 | + r1.Offset(p); |
| 288 | + Assert.Equal(expectedRect, r1); |
| 289 | + |
| 290 | + expectedRect.Offset(p); |
| 291 | + r1.Offset(width, height); |
| 292 | + Assert.Equal(expectedRect, r1); |
| 293 | + } |
| 294 | + |
| 295 | + [Theory] |
| 296 | + [InlineData(0, 0, 0, 0)] |
| 297 | + [InlineData(5, -5, 0, 1)] |
| 298 | + public void ToStringTest(int x, int y, int width, int height) |
| 299 | + { |
| 300 | + Area r = new(x, y, width, height); |
| 301 | + Assert.Equal(string.Format(CultureInfo.CurrentCulture, "{{X={0},Y={1},Width={2},Height={3}}}", r.X, r.Y, r.Width, r.Height), r.ToString()); |
| 302 | + } |
| 303 | + } |
| 304 | +} |
0 commit comments