RectTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. namespace Terminal.Gui.TypeTests {
  5. public class RectTests {
  6. [Fact]
  7. public void Rect_New ()
  8. {
  9. var rect = new Rect ();
  10. Assert.True (rect.IsEmpty);
  11. rect = new Rect (new Point (), new Size ());
  12. Assert.True (rect.IsEmpty);
  13. rect = new Rect (1, 2, 3, 4);
  14. Assert.False (rect.IsEmpty);
  15. rect = new Rect (-1, -2, 3, 4);
  16. Assert.False (rect.IsEmpty);
  17. Action action = () => new Rect (1, 2, -3, 4);
  18. var ex = Assert.Throws<ArgumentException> (action);
  19. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  20. action = () => new Rect (1, 2, 3, -4);
  21. ex = Assert.Throws<ArgumentException> (action);
  22. Assert.Equal ("Height must be greater or equal to 0.", ex.Message);
  23. action = () => new Rect (1, 2, -3, -4);
  24. ex = Assert.Throws<ArgumentException> (action);
  25. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  26. }
  27. [Fact]
  28. public void Rect_SetsValue ()
  29. {
  30. var rect = new Rect () {
  31. X = 0,
  32. Y = 0
  33. };
  34. Assert.True (rect.IsEmpty);
  35. rect = new Rect () {
  36. X = -1,
  37. Y = -2
  38. };
  39. Assert.False (rect.IsEmpty);
  40. rect = new Rect () {
  41. Width = 3,
  42. Height = 4
  43. };
  44. Assert.False (rect.IsEmpty);
  45. rect = new Rect () {
  46. X = -1,
  47. Y = -2,
  48. Width = 3,
  49. Height = 4
  50. };
  51. Assert.False (rect.IsEmpty);
  52. Action action = () => {
  53. rect = new Rect () {
  54. X = -1,
  55. Y = -2,
  56. Width = -3,
  57. Height = 4
  58. };
  59. };
  60. var ex = Assert.Throws<ArgumentException> (action);
  61. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  62. action = () => {
  63. rect = new Rect () {
  64. X = -1,
  65. Y = -2,
  66. Width = 3,
  67. Height = -4
  68. };
  69. };
  70. ex = Assert.Throws<ArgumentException> (action);
  71. Assert.Equal ("Height must be greater or equal to 0.", ex.Message);
  72. action = () => {
  73. rect = new Rect () {
  74. X = -1,
  75. Y = -2,
  76. Width = -3,
  77. Height = -4
  78. };
  79. };
  80. ex = Assert.Throws<ArgumentException> (action);
  81. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  82. }
  83. [Fact]
  84. public void Rect_Equals ()
  85. {
  86. var rect1 = new Rect ();
  87. var rect2 = new Rect ();
  88. Assert.Equal (rect1, rect2);
  89. rect1 = new Rect (1, 2, 3, 4);
  90. rect2 = new Rect (1, 2, 3, 4);
  91. Assert.Equal (rect1, rect2);
  92. rect1 = new Rect (1, 2, 3, 4);
  93. rect2 = new Rect (-1, 2, 3, 4);
  94. Assert.NotEqual (rect1, rect2);
  95. }
  96. [Fact]
  97. public void Positive_X_Y_Positions ()
  98. {
  99. var rect = new Rect (10, 5, 100, 50);
  100. int yCount = 0, xCount = 0, yxCount = 0;
  101. for (int line = rect.Y; line < rect.Y + rect.Height; line++) {
  102. yCount++;
  103. xCount = 0;
  104. for (int col = rect.X; col < rect.X + rect.Width; col++) {
  105. xCount++;
  106. yxCount++;
  107. }
  108. }
  109. Assert.Equal (yCount, rect.Height);
  110. Assert.Equal (xCount, rect.Width);
  111. Assert.Equal (yxCount, rect.Height * rect.Width);
  112. }
  113. [Fact]
  114. public void Negative_X_Y_Positions ()
  115. {
  116. var rect = new Rect (-10, -5, 100, 50);
  117. int yCount = 0, xCount = 0, yxCount = 0;
  118. for (int line = rect.Y; line < rect.Y + rect.Height; line++) {
  119. yCount++;
  120. xCount = 0;
  121. for (int col = rect.X; col < rect.X + rect.Width; col++) {
  122. xCount++;
  123. yxCount++;
  124. }
  125. }
  126. Assert.Equal (yCount, rect.Height);
  127. Assert.Equal (xCount, rect.Width);
  128. Assert.Equal (yxCount, rect.Height * rect.Width);
  129. }
  130. }
  131. }