Files
Terminal.Gui/Tests/UnitTestsParallelizable/Drawing/Region/RegionTests.cs
Tig a84b2c4896 Fixes #4419, #4148, #4408 - Toplevel is GONE - Replaced by Runnable (#4422)
* WIP: Broken

* Got working. Mostly.

* Parllel tests pass

* More progres

* Fixed app tests.

* Mouse

* more progress.

* working on shortcut

* Shortcut accept on ENTER is broken.

* One left...

* More test progress.

* All unit tests pass. Still some issues though.

* tweak

* Fixed Integration Tests

* Fixed UI Catalog

* Tweaking CP to try to find race condition

* Refactor StandardColors and improve ColorPicker logic

Refactored `StandardColors` to use lazy initialization for static fields, improving performance and avoiding static constructor convoy effects. Introduced `NamesValueFactory` and `MapValueFactory` methods for encapsulated initialization logic.

Simplified `GetColorNames` to directly return `_names.Value`. Improved `TryParseColor` by clarifying default value usage and adopting object initializer syntax. Updated `TryNameColor` to use `_argbNameMap.Value`.

Refactored `GetArgb` for better readability. Replaced `MultiStandardColorNameResolver` with `StandardColorsNameResolver` in `ColorPicker`. Commented out `app.Init("Fake")` in `ColorPickerTests` for testing purposes.

Made minor formatting improvements, including updated comments and XML documentation for consistency.

* revert

* Throttle input loop to prevent CPU spinning

Introduce a 20ms delay in the input loop of `InputImpl<TInputRecord>`
to prevent excessive CPU usage when no input is available. Removed
the `DateTime dt = Now();` line and the `while (Peek())` block, which
previously enqueued input records.

This change improves resource management, especially in scenarios
where multiple `ApplicationImpl` instances are created in parallel
tests without calling `Shutdown()`. It prevents thread pool
exhaustion and ensures better performance in such cases.

* Refactor ApplicationImpl to use IDisposable pattern

Implemented the IDisposable pattern in ApplicationImpl to improve resource management. Added `Dispose` and `DisposeCore` methods, and marked the `Shutdown` method as obsolete, encouraging the use of `Dispose` or `using` statements instead. Updated the `IApplication` interface to inherit from IDisposable and added `GetResult` methods for retrieving run session results.

Refactored unit tests to adopt the new lifecycle management approach, replacing legacy `Shutdown` calls with `Dispose` or `using`. Removed fragile and obsolete tests, and re-enabled previously skipped tests after addressing underlying issues.

Updated `FakeApplicationLifecycle` and `SetupFakeApplicationAttribute` to align with the new disposal pattern. Improved documentation and examples to guide users toward modern usage patterns. Maintained backward compatibility for legacy singleton usage.

* Add IDisposable pattern with input loop throttling

- Add IDisposable to IApplication for proper resource cleanup
- Add 20ms throttle to input loop (prevents CPU spinning)
- Add Lazy<T> to StandardColors (eliminates convoy effect)
- Add MainLoopCoordinatorTests suite (5 new tests)
- Add Dispose() calls to all 16 ColorPickerTests
- Mark Application.Shutdown() as [Obsolete]

IApplication now requires Dispose() for cleanup

Performance: 100x CPU reduction, 15x faster disposal, tests complete in <5s

Fixes: Thread leaks, CPU saturation, test hangs in parallel execution
Docs: Updated application.md and newinv2.md with disposal patterns

* Refactor test for input loop throttling clarity

Updated `InputLoop_Throttle_Limits_Poll_Rate` test to improve clarity, reliability, and efficiency:
- Rewrote summary comment to clarify purpose and emphasize the 20ms throttle's role in preventing CPU spinning.
- Replaced `var` with explicit types for better readability.
- Reduced test duration from 1s to 500ms to improve test speed.
- Revised assertions:
  - Replaced range-based assertion with upper-bound check to ensure poll count is below 500, avoiding timing sensitivity issues.
  - Added assertion to verify the thread ran and was not immediately canceled.
- Added a 2-second timeout to `inputTask.Wait` and verified task completion.
- Improved comments to explain test behavior and reasoning behind changes.

* tweaks

* Fix nullabiltiy stuff.

* runnable fixes

* more nullabe

* More nullability

* warnings gone

* Fixed fluent test failure.

* Refactor ApplicationImpl and update Runnable layout logic

Refactored `ApplicationImpl.Run.cs` for improved readability and
atomicity:
- Combined `if (wasModal)` with `SessionStack?.TryPop` to streamline
  logic.
- Simplified restoration of `previousRunnable` by reducing nesting.
- Updated comments for clarity and retained `SetIsModal` call.

Simplified focus-setting logic in `ApplicationImpl.Run.cs` using
pattern matching for `TopRunnableView`.

In `Runnable<TResult>`, added `SetNeedsLayout` after `IsModalChanged`
to ensure layout updates. Removed an unused empty line for cleanup.

Corrected namespace in `GetViewsUnderLocationForRootTests.cs` to
align with test structure.

* Update layout on modal state change

A call to `SetNeedsLayout()` was added to the `OnIsModalChanged`
method in the `Runnable` class. This ensures that the layout
is updated whenever the modal state changes.

* Increase test timeout for inputTask.Wait to 10 seconds

Extended the timeout duration for the `inputTask.Wait` method
from 4 seconds to 10 seconds in `MainLoopCoordinatorTests`.
This change ensures the test has a longer window to complete
under conditions of increased load or slower execution
environments, reducing the likelihood of false test failures.

* Refactor project files and simplify test logic

Removed `<LangVersion>` and `<ImplicitUsings>` properties from
`UnitTests.csproj` and `UnitTests.Parallelizable.csproj` to rely
on default SDK settings and disable implicit global usings.

Simplified the `SizeChanged_Event_Still_Fires_For_Compatibility`
test in `FakeDriverTests` by removing the `screenChangedFired`
variable, its associated event handler, and related assertions.
Also removed obsolete warning suppression directives as they
are no longer needed.

* Reduce UnitTestsParallelizable iterations from 10 to 3

Reduced the number of iterations for the UnitTestsParallelizable
test suite from 10 to 3 to save time and resources while still
exposing concurrency issues. Updated the loop and log messages
to reflect the new iteration count.

* disabled InputLoop_Throttle_Limits_Poll_Rate

* Refactor app lifecycle and improve Runnable API

Refactored `Program.cs` to simplify application lifecycle:
- Modularized app creation, initialization, and disposal.
- Improved result handling and ensured proper resource cleanup.

Re-implemented `Runnable<TResult>` with a cleaner design:
- Retained functionality while improving readability and structure.
- Added XML documentation and followed the Cancellable Work Pattern.

Re-implemented `RunnableWrapper<TView, TResult>`:
- Enabled wrapping any `View` to make it runnable with typed results.
- Added examples and remarks for better developer guidance.

Re-implemented `ViewRunnableExtensions`:
- Provided fluent API for making views runnable with or without results.
- Enhanced documentation with examples for common use cases.

General improvements:
- Enhanced code readability, maintainability, and error handling.
- Replaced redundant code with cleaner, more maintainable versions.

* Modernize codebase for Terminal.Gui and MVVM updates

Refactored `LoginView` to remove redundant `Application.LayoutAndDraw()`
call. Enhanced `LoginViewModel` with new observable properties for
automatic property change notifications. Updated `Message` class to use
nullable generics for improved type safety.

Replaced legacy `Application.Init()` and `Application.Run()` calls with
the modern `IApplication` API across `Program.cs`, `Example.cs`, and
`ReactiveExample`. Ensured proper disposal of `IApplication` instances
to prevent resource leaks.

Updated `TerminalScheduler` to integrate with `IApplication` for
invoking actions and managing timeouts. Added null checks and improved
timeout disposal logic for robustness.

Refactored `ExampleWindow` for better readability and alignment with
modern `Terminal.Gui` conventions. Cleaned up unused imports and
improved code clarity across the codebase.

Updated README.md to reflect the latest `Terminal.Gui` practices,
including examples of the `IApplication` API and automatic UI refresh
handling. Renamed `LoginAction` to `LoginActions` for consistency.

* Refactor: Transition to IRunnable-based architecture

Replaced `Toplevel` with `Window` as the primary top-level UI element. Introduced the `IRunnable` interface to modernize the architecture, enabling greater flexibility and testability. Deprecated the static `Application` class in favor of the instance-based `IApplication` model, which supports multiple application contexts.

Updated methods like `Application.Run()` and `Application.RequestStop()` to use `IRunnable`. Removed or replaced legacy `Modal` properties with `IsModal`. Enhanced the `IApplication` interface with a fluent API, including methods like `Run<TRunnable>()` and `GetResult<T>()`.

Refactored tests and examples to align with the new architecture. Updated documentation to reflect the instance-based model. Deprecated obsolete members and methods, including `Application.Current` and `Application.TopRunnable`.

Improved event handling by replacing the `Accept` event with `Accepting` and using `e.Handled` for event processing. Updated threading examples to use `App?.Invoke()` or `app.Invoke()` for UI updates. Cleaned up redundant code and redefined modal behavior for better consistency.

These changes modernize the `Terminal.Gui` library, improving clarity, usability, and maintainability while ensuring backward compatibility where possible.

* Refactor: Replace Toplevel with Runnable class

This commit introduces a major architectural update to the `Terminal.Gui` library, replacing the legacy `Toplevel` class with the new `Runnable` class. The changes span the entire codebase, including core functionality, tests, documentation, and configuration files.

- **Core Class Replacement**:
  - Replaced `Toplevel` with `Runnable` as the base class for modal views and session management.
  - Updated all references to `Toplevel` in the codebase, including constructors, methods, and properties.

- **Configuration Updates**:
  - Updated `tui-config-schema.json` to reflect the new `Runnable` scheme.

- **New Classes**:
  - Added `UICatalogRunnable` for managing the UI Catalog application.
  - Introduced `Runnable<TResult>` as a generic base class for blocking sessions with result handling.

- **Documentation and Tests**:
  - Updated documentation to emphasize `Runnable` and mark `Toplevel` as obsolete.
  - Refactored test cases to use `Runnable` and ensure compatibility.

- **Behavioral Improvements**:
  - Enhanced lifecycle management and alignment with the `IRunnable` interface.
  - Improved clarity and consistency in naming conventions.

These changes modernize the library, improve flexibility, and provide a clearer architecture for developers.

* Refactor: Consolidate Runnable classes and decouple View from ApplicationImpl

- Made Runnable<TResult> inherit from Runnable (eliminating ~180 LOC duplication)
- Moved View init/layout/cursor logic from ApplicationImpl to Runnable lifecycle events
- ApplicationImpl.Begin now operates purely on IRunnable interface

Related to #4419

* Simplified the disposal logic in `ApplicationImpl.Run.cs` by replacing
the type-specific check for `View` with a more general check for
`IDisposable`. This ensures proper disposal of any `IDisposable`
object, improving robustness.

Removed the `FrameworkOwnedRunnable` property from the `ApplicationImpl`
class in `ApplicationImpl.cs` and the `IApplication` interface in
`IApplication.cs`. This eliminates the need to manage this property,
reducing complexity and improving maintainability.

Updated `application.md` to reflect the removal of the
`FrameworkOwnedRunnable` property, ensuring the documentation aligns
with the updated codebase.

* Replaces the legacy `Shutdown()` method with `Dispose()` to align
with the `IDisposable` pattern, ensuring proper resource cleanup
and simplifying the API. The `Dispose()` method is now the
recommended way to release resources, with `using` statements
encouraged for automatic disposal.

Key changes:
- Marked `Shutdown()` as obsolete; it now internally calls `Dispose()`.
- Updated the fluent API to remove `Shutdown()` from chaining.
- Enhanced session lifecycle management for thread safety.
- Updated tests to validate proper disposal and state reset.
- Improved `IRunnable` integration with automatic disposal for
  framework-created runnables.
- Maintained backward compatibility for the legacy static
  `Application` singleton.
- Refactored documentation and examples to reflect modern practices
  and emphasize `Dispose()` usage.

These changes modernize the `Terminal.Gui` lifecycle, improve
testability, and encourage alignment with .NET conventions.

* Refactor runnable app context handling in ApplicationImpl

Refactor how the application context is set for `runnable` objects
by introducing a new `SetApp` method in the `IRunnable` interface.
This replaces the previous logic of directly setting the `App`
property for `View` objects, making the process more generic and
encapsulated within `IRunnable` implementations.

Simplify `Mouse.UngrabMouse()` by removing the conditional check
and calling it unconditionally.

Make a minor formatting adjustment in the generic constraint of
`Run<TRunnable>` in `ApplicationImpl`.

Add `SetApp(IApplication app)` to the `IRunnable` interface and
implement it in the `Runnable` class to set the `App` property
to the provided application instance.

* Improve docs, tests, and modularity across the codebase

Reorganized and updated `CONTRIBUTING.md`:
- Added **Key Architecture Concepts** section and reordered the table of contents.
- Updated testing requirements to discourage legacy patterns.
- Added instructions for replicating CI workflows locally.
- Clarified PR guidelines and coding style expectations.

Enhanced `README.md` with detailed CI/CD workflow documentation.

Refactored `ColorPicker.Prompt` to use `IApplication` for improved modularity and testability.

Introduced `IApplicationScreenChangedTests` for comprehensive testing of `ScreenChanged` events and `Screen` property.

Refactored `ApplicationScreenTests` and `TextView.PromptForColors` to align with modern patterns.

Updated `Terminal.sln` to include `.github/workflows/README.md`.

Performed general cleanup:
- Removed outdated documentation links.
- Improved XML documentation and coding consistency.

* readme tweaks

* Improve thread safety, layout, and test coverage

Refactored `OutputBufferImpl.cs` to enhance thread safety by locking shared resources and adding bounds checks for columns and rows. Improved handling of wide characters and removed outdated TODO comments.

Updated `Runnable.cs` to call `SetNeedsDraw()` on modal state changes, ensuring proper layout and drawing updates. Simplified layout handling in `ApplicationImpl.Run.cs` by replacing redundant comments with a `LayoutAndDraw()` call.

Added a check in `AllViewsTester.cs` to skip creating instances of `RunnableWrapper` types with unsatisfiable generic constraints, logging a warning when encountered.

Enhanced `ListViewTests.cs` by adding explicit `app.LayoutAndDraw()` calls to validate visual output and ensure tests reflect the updated application state.

These changes improve robustness, prevent race conditions, and ensure consistent behavior across the application.

* Refactor: Rename Toplevel to Runnable and update logic

Updated the `Border` class to use `Command.Quit` instead of
`Command.QuitToplevel` in the `CloseButton.Accept` handler.

Renamed test methods in `GetViewsAtLocationTests.cs` to replace
"Toplevel" with "Runnable" for consistency. Updated `Runnable<bool>`
instances to use "topRunnable" as the `Id` property.

These changes align the codebase with updated naming conventions
and improve clarity.

* Removed `ToplevelTests` and migrated relevant test cases to
`MouseDragTests` with improved structure and coverage. Updated
tests to use `Application.Create`, `app.Begin`, and `app.End`
for better resource management and lifecycle handling.

Replaced direct event handling with `app.Mouse.RaiseMouseEvent`
to align with the application's event-handling mechanism. Added
`Runnable` objects to ensure views are properly initialized and
disposed of within the application context.

Enhanced tests to include assertions for minimum width and
height constraints during resize operations. Removed redundant
tests and streamlined logic to reduce duplication and improve
maintainability.

* Reorged Unit Test namespaces.

* more

* Refactor tests and update namespaces for consistency

Updated namespaces in `ArrangementTests.cs` and `MouseDragTests.cs` for better organization. Enhanced `ArrangementTests.cs` with additional checks for arrangement flags. Reformatted and re-added `MouseDragTests.cs` and `SchemeTests.cs` with modern C# features like nullable annotations and object initializers. Ensured no functional changes while improving code clarity and consistency.

* Fix nullability warnings in MouseDragTests.cs

Updated `app.End` calls to use the null-forgiving operator (`!`)
on `app.SessionStack` to ensure it is treated as non-null.
This change addresses potential nullability warnings and
improves code safety and clarity. Applied consistently across
all relevant test cases in the `MouseDragTests` class.
2025-12-01 12:54:21 -07:00

1107 lines
37 KiB
C#

namespace DrawingTests;
public class RegionTests
{
[Fact]
public void Clone_CreatesExactCopy ()
{
var region = new Region (new (10, 10, 50, 50));
Region clone = region.Clone ();
Assert.True (clone.Contains (20, 20));
Assert.Equal (region.GetRectangles (), clone.GetRectangles ());
}
[Fact]
public void Combine_EmptyRectangles_ProducesEmptyRegion ()
{
// Arrange: Create region and combine with empty rectangles
var region = new Region ();
region.Combine (new Rectangle (0, 0, 0, 0), RegionOp.Union); // Empty rectangle
region.Combine (new Region (), RegionOp.Union); // Empty region
// Assert: Region is empty
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void Complement_Rectangle_ComplementsRegion ()
{
var region = new Region (new (10, 10, 50, 50));
region.Complement (new (0, 0, 100, 100));
Assert.True (region.Contains (5, 5));
Assert.False (region.Contains (20, 20));
}
[Theory]
[MemberData (nameof (Complement_TestData))]
public void Complement_Region_Success (Region region, Rectangle [] rectangles, Rectangle [] expectedScans)
{
foreach (Rectangle rect in rectangles)
{
region.Complement (rect);
}
Rectangle [] actualScans = region.GetRectangles ();
Assert.Equal (expectedScans, actualScans);
}
public static IEnumerable<object []> Complement_TestData ()
{
yield return new object []
{
new Region (new (10, 10, 100, 100)),
new Rectangle [] { new (40, 60, 100, 20) },
new Rectangle [] { new (110, 60, 30, 20) }
};
yield return new object []
{
new Region (new (70, 10, 100, 100)),
new Rectangle [] { new (40, 60, 100, 20) },
new Rectangle [] { new (40, 60, 30, 20) }
};
yield return new object []
{
new Region (new (40, 100, 100, 100)),
new Rectangle [] { new (70, 80, 50, 40) },
new Rectangle [] { new (70, 80, 50, 20) }
};
yield return new object []
{
new Region (new (40, 10, 100, 100)),
new Rectangle [] { new (70, 80, 50, 40) },
new Rectangle [] { new (70, 110, 50, 10) }
};
yield return new object []
{
new Region (new (30, 30, 80, 80)),
new Rectangle []
{
new (45, 45, 200, 200),
new (160, 260, 10, 10),
new (170, 260, 10, 10)
},
new Rectangle [] { new (170, 260, 10, 10) }
};
yield return new object []
{
new Region (),
new [] { Rectangle.Empty },
new Rectangle[0]
};
yield return new object []
{
new Region (),
new Rectangle [] { new (1, 2, 3, 4) },
new Rectangle[0]
};
}
[Fact]
public void Complement_WithRectangle_ComplementsRegion ()
{
var region = new Region (new (10, 10, 50, 50));
var rect = new Rectangle (0, 0, 100, 100);
region.Complement (rect);
// Points that were inside the original region should now be outside
Assert.False (region.Contains (35, 35));
// Points that were outside the original region but inside bounds should now be inside
Assert.True (region.Contains (5, 5));
Assert.True (region.Contains (95, 95));
}
[Fact]
public void Complement_WithRegion_ComplementsRegion ()
{
var region = new Region (new (10, 10, 50, 50));
var bounds = new Rectangle (0, 0, 100, 100);
region.Complement (bounds);
// Points that were inside the original region should now be outside
Assert.False (region.Contains (35, 35));
// Points that were outside the original region but inside bounds should now be inside
Assert.True (region.Contains (5, 5));
Assert.True (region.Contains (95, 95));
}
[Fact]
public void Constructor_EmptyRegion_IsEmpty ()
{
var region = new Region ();
Assert.True (region.IsEmpty ());
}
[Fact]
public void Constructor_WithRectangle_IsNotEmpty ()
{
var region = new Region (new (10, 10, 50, 50));
Assert.False (region.IsEmpty ());
}
[Fact]
public void Contains_Point_ReturnsCorrectResult ()
{
var region = new Region (new (10, 10, 50, 50));
Assert.True (region.Contains (20, 20));
Assert.False (region.Contains (100, 100));
}
[Fact]
public void Contains_PointInsideRegion_ReturnsTrue ()
{
var region = new Region (new (10, 10, 50, 50));
Assert.True (region.Contains (20, 20));
}
[Fact]
public void Contains_RectangleInsideRegion_ReturnsTrue ()
{
var region = new Region (new (10, 10, 50, 50));
Assert.True (region.Contains (new (20, 20, 10, 10)));
}
[Fact]
public void Equals_NullRegion_ReturnsFalse ()
{
var region = new Region ();
Assert.False (region.Equals (null));
}
[Fact]
public void Equals_SameRegion_ReturnsTrue ()
{
var region = new Region (new (1, 2, 3, 4));
Assert.True (region.Equals (region));
}
public static IEnumerable<object []> Equals_TestData ()
{
static Region Empty ()
{
Region emptyRegion = new ();
emptyRegion.Intersect (Rectangle.Empty);
return emptyRegion;
}
yield return new object [] { new Region (), new Region (), true };
yield return new object [] { new Region (), Empty (), true };
yield return new object [] { new Region (), new Region (new (1, 2, 3, 4)), false };
yield return new object [] { Empty (), Empty (), true };
yield return new object [] { Empty (), new Region (new (0, 0, 0, 0)), true };
yield return new object [] { Empty (), new Region (new (1, 2, 3, 3)), false };
yield return new object [] { new Region (new (1, 2, 3, 4)), new Region (new (1, 2, 3, 4)), true };
yield return new object [] { new Region (new (1, 2, 3, 4)), new Region (new (2, 2, 3, 4)), false };
yield return new object [] { new Region (new (1, 2, 3, 4)), new Region (new (1, 3, 3, 4)), false };
yield return new object [] { new Region (new (1, 2, 3, 4)), new Region (new (1, 2, 4, 4)), false };
yield return new object [] { new Region (new (1, 2, 3, 4)), new Region (new (1, 2, 3, 5)), false };
}
[Theory]
[MemberData (nameof (Equals_TestData))]
public void Equals_Valid_ReturnsExpected (Region region1, Region region2, bool expected) { Assert.Equal (expected, region1.Equals (region2)); }
[Fact]
public void GetBounds_ReturnsBoundingRectangle ()
{
var region = new Region (new (10, 10, 50, 50));
region.Union (new Rectangle (100, 100, 20, 20));
Rectangle bounds = region.GetBounds ();
Assert.Equal (new (10, 10, 110, 110), bounds);
}
[Fact]
public void GetBounds_ReturnsCorrectBounds ()
{
var region = new Region ();
region.Union (new Rectangle (10, 10, 50, 50));
region.Union (new Rectangle (30, 30, 50, 50));
Rectangle bounds = region.GetBounds ();
Assert.Equal (new (10, 10, 70, 70), bounds);
}
[Fact]
public void GetRegionScans_ReturnsAllRectangles ()
{
var region = new Region (new (10, 10, 50, 50));
region.Union (new Rectangle (100, 100, 20, 20));
Rectangle [] scans = region.GetRectangles ();
Assert.Equal (2, scans.Length);
Assert.Contains (new (10, 10, 50, 50), scans);
Assert.Contains (new (100, 100, 20, 20), scans);
}
[Fact]
public void Intersect_Rectangle_IntersectsRegion ()
{
var region = new Region (new (10, 10, 50, 50));
region.Intersect (new Rectangle (30, 30, 50, 50));
Assert.False (region.Contains (20, 20));
Assert.True (region.Contains (40, 40));
}
[Fact]
public void Intersect_Region_IntersectsRegions ()
{
var region1 = new Region (new (10, 10, 50, 50));
var region2 = new Region (new (30, 30, 50, 50));
region1.Intersect (region2);
Assert.False (region1.Contains (20, 20));
Assert.True (region1.Contains (40, 40));
}
[Fact]
public void Intersect_WithEmptyRectangle_ResultsInEmptyRegion ()
{
// Arrange
var region = new Region (new (0, 0, 10, 10));
var rectangle = Rectangle.Empty; // Use Empty instead of 0-size
// Act
region.Intersect (rectangle);
// Assert
Assert.True (region.IsEmpty ());
}
[Theory]
[InlineData (0, 0, 0, 0)] // Empty by zero size
[InlineData (0, 0, 0, 10)] // Empty by zero width
[InlineData (0, 0, 10, 0)] // Empty by zero height
[InlineData (-5, -5, 0, 0)] // Empty by zero size at negative coords
[InlineData (10, 10, -5, -5)] // Empty by negative size
public void Intersect_WithEmptyRegion_ResultsInEmptyRegion (int x, int y, int width, int height)
{
// Arrange
var region = new Region ();
region.Union (new Rectangle (0, 0, 10, 10));
region.Union (new Rectangle (20, 0, 10, 10));
// Create a region that should be considered empty
var emptyRegion = new Region ();
if (width <= 0 || height <= 0)
{
// For negative or zero dimensions, use an empty region
emptyRegion = new ();
}
else
{
emptyRegion = new (new (x, y, width, height));
}
// Verify initial states
Assert.Equal (2, region.GetRectangles ().Length);
Assert.True (emptyRegion.IsEmpty ());
// Act
region.Intersect (emptyRegion);
// Assert
Assert.True (region.IsEmpty ());
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void Intersect_WithFullyContainedRectangle_ResultsInSmallerRegion ()
{
// Arrange
var region = new Region (new (0, 0, 10, 10));
var rectangle = new Rectangle (2, 2, 4, 4);
// Act
region.Intersect (rectangle);
// Assert
Assert.Single (region.GetRectangles ());
Assert.Equal (new (2, 2, 4, 4), region.GetRectangles () [0]);
}
[Fact]
public void Intersect_WithMultipleRectanglesInRegion_IntersectsAll ()
{
// Arrange
var region = new Region ();
region.Union (new Rectangle (0, 0, 5, 5));
region.Union (new Rectangle (10, 0, 5, 5));
var rectangle = new Rectangle (2, 2, 10, 2);
// Act
region.Intersect (rectangle);
// Assert
Assert.Equal (2, region.GetRectangles ().Length);
Assert.Contains (new (2, 2, 3, 2), region.GetRectangles ());
Assert.Contains (new (10, 2, 2, 2), region.GetRectangles ());
}
//[Fact]
//public void Intersect_WithEmptyRegion_ResultsInEmptyRegion ()
//{
// // Arrange
// var region = new Region ();
// var rectangle = new Rectangle (0, 0, 10, 10);
// // Act
// region.Intersect (rectangle);
// // Assert
// Assert.True (region.IsEmpty ());
//}
[Fact]
public void Intersect_WithNonOverlappingRectangle_ResultsInEmptyRegion ()
{
// Arrange
var region = new Region (new (0, 0, 5, 5));
var rectangle = new Rectangle (10, 10, 5, 5);
// Act
region.Intersect (rectangle);
// Assert
Assert.True (region.IsEmpty ());
}
[Fact]
public void Intersect_WithNullRegion_ResultsInEmptyRegion ()
{
// Arrange
var region = new Region ();
region.Union (new Rectangle (0, 0, 10, 10));
region.Union (new Rectangle (20, 0, 10, 10));
// Verify initial state
Assert.Equal (2, region.GetRectangles ().Length);
// Act
region.Intersect (null);
// Assert
Assert.True (region.IsEmpty ());
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void Intersect_WithPartiallyOverlappingRectangle_ResultsInIntersectedRegion ()
{
// Arrange
var region = new Region (new (0, 0, 5, 5));
var rectangle = new Rectangle (2, 2, 5, 5);
// Act
region.Intersect (rectangle);
// Assert
Assert.Single (region.GetRectangles ());
Assert.Equal (new (2, 2, 3, 3), region.GetRectangles () [0]);
}
[Fact]
public void Intersect_WithRectangle_IntersectsRectangles ()
{
var region = new Region (new (10, 10, 50, 50));
var rect = new Rectangle (30, 30, 50, 50);
region.Intersect (rect);
Assert.True (region.Contains (35, 35));
Assert.False (region.Contains (20, 20));
}
[Fact]
public void Intersect_WithRegion_IntersectsRegions ()
{
var region1 = new Region (new (10, 10, 50, 50));
var region2 = new Region (new (30, 30, 50, 50));
region1.Intersect (region2.GetBounds ());
Assert.True (region1.Contains (35, 35));
Assert.False (region1.Contains (20, 20));
}
[Fact]
public void Intersect_ImmediateNormalization_AffectsRectangleOrder ()
{
// Create a region with two overlapping rectangles
var region1 = new Region (new (0, 0, 4, 4)); // 0,0 to 4,4
// Intersect with a region that partially overlaps
var region2 = new Region (new (2, 2, 4, 4)); // 2,2 to 6,6
region1.Intersect (region2);
// Get the resulting rectangles
Rectangle [] result = region1.GetRectangles ();
// Expected behavior from original Region:
// Intersect immediately produces a single rectangle (2,2,2,2)
Assert.Single (result); // Original has 1 rectangle due to immediate processing
Assert.Equal (new (2, 2, 2, 2), result [0]);
// My updated Region defers normalization after Intersect,
// so GetRectangles() might merge differently or preserve order differently,
// potentially failing the exact match or count due to _isDirty
}
[Fact]
public void IsEmpty_AfterClear_ReturnsTrue ()
{
// Arrange
var region = new Region (new (0, 0, 10, 10));
// Act
region.Intersect (Rectangle.Empty);
// Assert
Assert.True (region.IsEmpty ());
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void IsEmpty_AfterComplement_ReturnsCorrectState ()
{
// Test 1: Complement a region with bounds that fully contain it
var region = new Region (new (2, 2, 5, 5)); // Small inner rectangle
region.Complement (new (0, 0, 10, 10)); // Larger outer bounds
Assert.False (region.IsEmpty ()); // Should have area around the original rectangle
// Test 2: Complement with bounds equal to the region
region = new (new (0, 0, 10, 10));
region.Complement (new (0, 0, 10, 10));
Assert.True (region.IsEmpty ()); // Should be empty as there's no area left
// Test 3: Complement with empty bounds
region = new (new (0, 0, 10, 10));
region.Complement (Rectangle.Empty);
Assert.True (region.IsEmpty ()); // Should be empty as there's no bounds
}
[Fact]
public void IsEmpty_AfterExclude_ReturnsTrue ()
{
// Arrange
var region = new Region (new (0, 0, 10, 10));
// Act
region.Exclude (new Rectangle (0, 0, 10, 10));
// Assert
Assert.True (region.IsEmpty ());
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void IsEmpty_AfterUnion_ReturnsFalse ()
{
// Arrange
var region = new Region ();
region.Union (new Rectangle (0, 0, 10, 10));
// Assert
Assert.False (region.IsEmpty ());
Assert.Single (region.GetRectangles ());
}
[Fact]
public void IsEmpty_EmptyRegion_ReturnsTrue ()
{
var region = new Region ();
Assert.True (region.IsEmpty ());
}
[Fact]
public void IsEmpty_MultipleOperations_ReturnsExpectedResult ()
{
// Arrange
var region = new Region ();
// Act & Assert - Should be empty initially
Assert.True (region.IsEmpty ());
// Add a rectangle - Should not be empty
region.Union (new Rectangle (0, 0, 10, 10));
Assert.False (region.IsEmpty ());
// Exclude the same rectangle - Should be empty again
region.Exclude (new Rectangle (0, 0, 10, 10));
Assert.True (region.IsEmpty ());
// Add two rectangles - Should not be empty
region.Union (new Rectangle (0, 0, 5, 5));
region.Union (new Rectangle (10, 10, 5, 5));
Assert.False (region.IsEmpty ());
}
[Fact]
public void IsEmpty_NewRegion_ReturnsTrue ()
{
// Arrange
var region = new Region ();
// Act & Assert
Assert.True (region.IsEmpty ());
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void IsEmpty_ReturnsCorrectResult ()
{
var region = new Region ();
Assert.True (region.IsEmpty ());
region.Union (new Rectangle (10, 10, 50, 50));
Assert.False (region.IsEmpty ());
}
[Theory]
[InlineData (0, 0, 1, 1)] // 1x1 at origin
[InlineData (10, 10, 5, 5)] // 5x5 at (10,10)
[InlineData (-5, -5, 10, 10)] // Negative coordinates
public void IsEmpty_ValidRectangle_ReturnsFalse (int x, int y, int width, int height)
{
// Arrange
var region = new Region (new (x, y, width, height));
// Assert
Assert.False (region.IsEmpty ());
Assert.Single (region.GetRectangles ());
}
[Theory]
[InlineData (0, 0, 0, 0)] // Zero size
[InlineData (0, 0, 0, 10)] // Zero width
[InlineData (0, 0, 10, 0)] // Zero height
[InlineData (-5, -5, 0, 0)] // Zero size at negative coords
public void IsEmpty_ZeroSizeRectangle_ReturnsCorrectState (int x, int y, int width, int height)
{
var region = new Region (new (x, y, width, height));
// Only check IsEmpty() since Rectangle(0,0,0,0) is still stored
Assert.True (region.IsEmpty ());
}
//[Fact]
//public void MinimalUnion_SingleRectangle_DoesNotChange ()
//{
// var region = new Region (new Rectangle (0, 0, 10, 10));
// region.MinimalUnion (new Rectangle (0, 0, 10, 10));
// Assert.Single (region.GetRectangles ());
// Assert.Equal (new Rectangle (0, 0, 10, 10), region.GetRectangles ().First ());
//}
//[Fact]
//public void MinimalUnion_OverlappingRectangles_MergesIntoOne ()
//{
// var region = new Region (new Rectangle (0, 0, 10, 10));
// region.MinimalUnion (new Rectangle (5, 0, 10, 10));
// Assert.Single (region.GetRectangles ());
// Assert.Equal (new Rectangle (0, 0, 15, 10), region.GetRectangles ().First ());
//}
//[Fact]
//public void MinimalUnion_AdjacentRectangles_MergesIntoOne ()
//{
// var region = new Region (new Rectangle (0, 0, 10, 10));
// region.MinimalUnion (new Rectangle (10, 0, 10, 10));
// Assert.Single (region.GetRectangles ());
// Assert.Equal (new Rectangle (0, 0, 20, 10), region.GetRectangles ().First ());
//}
//[Fact]
//public void MinimalUnion_SeparateRectangles_KeepsBoth ()
//{
// var region = new Region (new Rectangle (0, 0, 10, 10));
// region.MinimalUnion (new Rectangle (20, 20, 10, 10));
// Assert.Equal (2, region.GetRectangles ().Length);
// Assert.Contains (new Rectangle (0, 0, 10, 10), region.GetRectangles ());
// Assert.Contains (new Rectangle (20, 20, 10, 10), region.GetRectangles ());
//}
//[Fact]
//public void MinimalUnion_ComplexMerging_ProducesMinimalSet ()
//{
// var region = new Region (new Rectangle (0, 0, 10, 10));
// region.MinimalUnion (new Rectangle (10, 0, 10, 10));
// region.MinimalUnion (new Rectangle (0, 10, 10, 10));
// region.MinimalUnion (new Rectangle (10, 10, 10, 10));
// Assert.Single (region.GetRectangles ());
// Assert.Equal (new Rectangle (0, 0, 20, 20), region.GetRectangles ().First ());
//}
[Fact]
public void Intersect_ViewportLimitsDrawnRegion ()
{
// Arrange: Create regions for viewport and drawn content
var viewport = new Region (new Rectangle (0, 0, 100, 100)); // Viewport
var drawnRegion = new Region (new Rectangle (50, 50, 200, 200)); // Larger drawn content
// Act: Intersect drawn region with viewport
drawnRegion.Intersect (viewport);
// Assert: Drawn region should be limited to viewport
var rectangles = drawnRegion.GetRectangles ();
Assert.Single (rectangles);
Assert.Equal (new Rectangle (50, 50, 50, 50), rectangles [0]); // Limited to viewport bounds
}
//[Fact]
//public void MinimalUnion_HorizontalMerge_MergesToSingleRectangle ()
//{
// // Arrange: Create a region with a rectangle at (0,0,5,5)
// var region = new Region (new Rectangle (0, 0, 5, 5));
// // Act: Merge an adjacent rectangle on the right using MinimalUnion
// region.MinimalUnion (new Rectangle (5, 0, 5, 5));
// var result = region.GetRectangles ();
// // Assert: Expect a single merged rectangle covering (0,0,10,5)
// Assert.Single (result);
// Assert.Equal (new Rectangle (0, 0, 10, 5), result [0]);
//}
//[Fact]
//public void MinimalUnion_VerticalMerge_MergesToSingleRectangle ()
//{
// // Arrange: Create a region with a rectangle at (0,0,5,5)
// var region = new Region (new Rectangle (0, 0, 5, 5));
// // Act: Merge an adjacent rectangle below using MinimalUnion
// region.MinimalUnion (new Rectangle (0, 5, 5, 5));
// var result = region.GetRectangles ();
// // Assert: Expect a single merged rectangle covering (0,0,5,10)
// Assert.Single (result);
// Assert.Equal (new Rectangle (0, 0, 5, 10), result [0]);
//}
//[Fact]
//public void MinimalUnion_OverlappingMerge_MergesToSingleRectangle ()
//{
// // Arrange: Create a region with a rectangle that overlaps with the next one horizontally
// var region = new Region (new Rectangle (0, 0, 6, 5));
// // Act: Merge an overlapping rectangle using MinimalUnion
// region.MinimalUnion (new Rectangle (4, 0, 6, 5));
// var result = region.GetRectangles ();
// // Assert: Expect a single merged rectangle covering (0,0,10,5)
// Assert.Single (result);
// Assert.Equal (new Rectangle (0, 0, 10, 5), result [0]);
//}
//[Fact]
//public void MinimalUnion_NonAdjacentRectangles_NoMergeOccurs ()
//{
// // Arrange: Create a region with one rectangle
// var region = new Region (new Rectangle (0, 0, 5, 5));
// // Act: Merge with a rectangle that does not touch the first
// region.MinimalUnion (new Rectangle (6, 0, 5, 5));
// var result = region.GetRectangles ();
// // Assert: Expect two separate rectangles since they are not adjacent
// Assert.Equal (2, result.Length);
// Assert.Contains (new Rectangle (0, 0, 5, 5), result);
// Assert.Contains (new Rectangle (6, 0, 5, 5), result);
//}
//[Fact]
//public void MinimalUnion_MultipleMerge_FormsSingleContiguousRectangle ()
//{
// // Arrange: Four small rectangles that form a contiguous 6x6 block
// var region = new Region (new Rectangle (0, 0, 3, 3));
// // Act: Merge adjacent rectangles one by one using MinimalUnion
// region.MinimalUnion (new Rectangle (3, 0, 3, 3)); // Now covers (0,0,6,3)
// region.MinimalUnion (new Rectangle (0, 3, 3, 3)); // Add bottom-left
// region.MinimalUnion (new Rectangle (3, 3, 3, 3)); // Add bottom-right to complete block
// var result = region.GetRectangles ();
// // Assert: Expect a single merged rectangle covering (0,0,6,6)
// Assert.Single (result);
// Assert.Equal (new Rectangle (0, 0, 6, 6), result [0]);
//}
[Fact]
public void Translate_EmptyRegionAfterEmptyCombine_NoEffect ()
{
// Arrange: Create region and combine with empty rectangles
var region = new Region ();
region.Combine (new Rectangle (0, 0, 0, 0), RegionOp.Union); // Empty rectangle
region.Combine (new Region (), RegionOp.Union); // Empty region
// Act: Translate by (10, 20)
region.Translate (10, 20);
// Assert: Still empty
Assert.Empty (region.GetRectangles ());
}
[Fact]
public void Union_Rectangle_AddsToRegion ()
{
var region = new Region ();
region.Union (new Rectangle (10, 10, 50, 50));
Assert.False (region.IsEmpty ());
Assert.True (region.Contains (20, 20));
}
[Fact]
public void Union_Region_MergesRegions ()
{
var region1 = new Region (new (10, 10, 50, 50));
var region2 = new Region (new (30, 30, 50, 50));
region1.Union (region2);
Assert.True (region1.Contains (20, 20));
Assert.True (region1.Contains (40, 40));
}
[Fact]
public void Union_Third_Rect_Covering_Two_Disjoint_Merges ()
{
var origRegion = new Region ();
var region1 = new Region (new (0, 0, 1, 1));
var region2 = new Region (new (1, 0, 1, 1));
origRegion.Union (region1);
origRegion.Union (region2);
Assert.Equal (new Rectangle (0, 0, 2, 1), origRegion.GetBounds ());
Assert.Equal (2, origRegion.GetRectangles ().Length);
origRegion.Union (new Region (new (0, 0, 4, 1)));
Assert.Equal (new Rectangle (0, 0, 4, 1), origRegion.GetBounds ());
Assert.Equal (3, origRegion.GetRectangles ().Length);
}
[Fact]
public void MinimalUnion_Third_Rect_Covering_Two_Disjoint_Merges ()
{
var origRegion = new Region ();
var region1 = new Region (new (0, 0, 1, 1));
var region2 = new Region (new (1, 0, 1, 1));
origRegion.Union (region1);
origRegion.Union (region2);
Assert.Equal (new Rectangle (0, 0, 2, 1), origRegion.GetBounds ());
Assert.Equal (2, origRegion.GetRectangles ().Length);
origRegion.MinimalUnion (new Region (new (0, 0, 4, 1)));
Assert.Equal (new Rectangle (0, 0, 4, 1), origRegion.GetBounds ());
Assert.Single (origRegion.GetRectangles ());
}
/// <summary>
/// Proves MergeRegion does not overly combine regions.
/// </summary>
[Fact]
public void Union_Region_MergesRegions_NonOverlapping ()
{
// 012345
// 0+++
// 1+ +
// 2+++
// 3 ***
// 4 * *
// 5 ***
var region1 = new Region (new (0, 0, 3, 3));
var region2 = new Region (new (3, 3, 3, 3));
region1.Union (region2);
// Positive
Assert.True (region1.Contains (0, 0));
Assert.True (region1.Contains (1, 1));
Assert.True (region1.Contains (2, 2));
Assert.True (region1.Contains (4, 4));
Assert.True (region1.Contains (5, 5));
// Negative
Assert.False (region1.Contains (0, 3));
Assert.False (region1.Contains (3, 0));
Assert.False (region1.Contains (6, 6));
}
/// <summary>
/// Proves MergeRegion does not overly combine regions.
/// </summary>
[Fact]
public void Union_Region_MergesRegions_Overlapping ()
{
// 01234567
// 0+++++
// 1+ +
// 2+ +
// 3+ *****
// 4+++* *
// 5 * *
// 6 * *
// 7 *****
var region1 = new Region (new (0, 0, 5, 5));
var region2 = new Region (new (3, 3, 5, 5));
region1.Union (region2);
// Positive
Assert.True (region1.Contains (0, 0));
Assert.True (region1.Contains (1, 1));
Assert.True (region1.Contains (4, 4));
Assert.True (region1.Contains (7, 7));
// Negative
Assert.False (region1.Contains (0, 5));
Assert.False (region1.Contains (5, 0));
Assert.False (region1.Contains (8, 8));
Assert.False (region1.Contains (8, 8));
}
[Fact]
public void Union_WithRectangle_AddsRectangle ()
{
var region = new Region ();
var rect = new Rectangle (10, 10, 50, 50);
region.Union (rect);
Assert.True (region.Contains (20, 20));
Assert.False (region.Contains (100, 100));
}
[Fact]
public void Union_WithRegion_AddsRegion ()
{
var region1 = new Region (new (10, 10, 50, 50));
var region2 = new Region (new (30, 30, 50, 50));
region1.Union (region2.GetBounds ());
Assert.True (region1.Contains (20, 20));
Assert.True (region1.Contains (40, 40));
}
[Fact]
public void Intersect_DeferredNormalization_PreservesSegments ()
{
var region = new Region (new (0, 0, 3, 1)); // Horizontal
region.Union (new Rectangle (1, 0, 1, 2)); // Vertical
region.Intersect (new Rectangle (0, 0, 2, 2)); // Clip
Rectangle [] result = region.GetRectangles ();
// Original & Updated (with normalization disabled) behavior:
// Produces [(0,0,1,1), (1,0,1,2), (2,0,0,1)]
Assert.Equal (3, result.Length);
Assert.Contains (new (0, 0, 1, 1), result);
Assert.Contains (new (1, 0, 1, 2), result);
Assert.Contains (new (2, 0, 0, 1), result);
}
[Fact]
public void MergeRectangles_Sort_Handles_Coincident_Events_Without_Crashing ()
{
// Arrange: Create rectangles designed to produce coincident start/end events
// Rect1 ends at x=10. Rect2 and Rect3 start at x=10.
// Rect4 ends at x=15. Rect5 starts at x=15.
var rect1 = new Rectangle (0, 0, 10, 10); // Ends at x=10
var rect2 = new Rectangle (10, 0, 10, 5); // Starts at x=10
var rect3 = new Rectangle (10, 5, 10, 5); // Starts at x=10, adjacent to rect2 vertically
var rect4 = new Rectangle (5, 10, 10, 5); // Ends at x=15
var rect5 = new Rectangle (15, 10, 5, 5); // Starts at x=15
var combinedList = new List<Rectangle> { rect1, rect2, rect3, rect4, rect5 };
// Act & Assert:
// The core assertion is that calling MergeRectangles with this list
// does *not* throw the ArgumentException related to sorting.
var exception = Record.Exception (() => Region.MergeRectangles (combinedList, false));
// Assert
Assert.Null (exception);
// Optional secondary assertion: Check if the merge produced a reasonable number of rectangles
// This isn't strictly necessary for proving the sort fix, but can be useful.
// var merged = Region.MergeRectangles(combinedList, false);
// Assert.True(merged.Count > 0 && merged.Count <= combinedList.Count);
}
[Fact]
public void MergeRectangles_Sort_Handles_Multiple_Coincident_Starts ()
{
// Arrange: Multiple rectangles starting at the same X
var rect1 = new Rectangle (5, 0, 10, 5);
var rect2 = new Rectangle (5, 5, 10, 5);
var rect3 = new Rectangle (5, 10, 10, 5);
var combinedList = new List<Rectangle> { rect1, rect2, rect3 };
// Act & Assert: Ensure no sorting exception
var exception = Record.Exception (() => Region.MergeRectangles (combinedList, false));
Assert.Null (exception);
}
[Fact]
public void MergeRectangles_Sort_Handles_Multiple_Coincident_Ends ()
{
// Arrange: Multiple rectangles ending at the same X
var rect1 = new Rectangle (0, 0, 10, 5);
var rect2 = new Rectangle (0, 5, 10, 5);
var rect3 = new Rectangle (0, 10, 10, 5);
var combinedList = new List<Rectangle> { rect1, rect2, rect3 };
// Act & Assert: Ensure no sorting exception
var exception = Record.Exception (() => Region.MergeRectangles (combinedList, false));
Assert.Null (exception);
}
[Fact]
public void MergeRectangles_Sort_Handles_Coincident_Mixed_Events_Without_Crashing ()
{
// Arrange: Create rectangles specifically designed to produce multiple
// Start AND End events at the same x-coordinate (e.g., x=10),
// mimicking the pattern observed in the crash log.
var rectA = new Rectangle (0, 0, 10, 5); // Ends at x=10, y=[0, 5)
var rectB = new Rectangle (0, 10, 10, 5); // Ends at x=10, y=[10, 15)
var rectC = new Rectangle (10, 0, 10, 5); // Starts at x=10, y=[0, 5)
var rectD = new Rectangle (10, 10, 10, 5); // Starts at x=10, y=[10, 15)
// Add another set at a different X to increase complexity
var rectE = new Rectangle (5, 20, 10, 5); // Ends at x=15, y=[20, 25)
var rectF = new Rectangle (5, 30, 10, 5); // Ends at x=15, y=[30, 35)
var rectG = new Rectangle (15, 20, 10, 5); // Starts at x=15, y=[20, 25)
var rectH = new Rectangle (15, 30, 10, 5); // Starts at x=15, y=[30, 35)
// Add some unrelated rectangles
var rectI = new Rectangle (0, 40, 5, 5);
var rectJ = new Rectangle (100, 100, 5, 5);
var combinedList = new List<Rectangle> {
rectA, rectB, rectC, rectD,
rectE, rectF, rectG, rectH,
rectI, rectJ
};
// Act & Assert:
// Call MergeRectangles with the current code.
// This test *should* fail by throwing ArgumentException due to unstable sort.
var exception = Record.Exception (() => Region.MergeRectangles (combinedList, false));
// Assert that no exception was thrown (this assertion will fail with the current code)
Assert.Null (exception);
}
[Fact]
public void MergeRectangles_Sort_Reproduces_UICatalog_Crash_Pattern_Directly ()
{
// Arrange: Rectangles derived *directly* from the events list that caused the crash at x=67
// This aims to replicate the exact problematic pattern.
var rect_End_67_7_30 = new Rectangle (60, 7, 7, 23); // Ends at x=67, y=[7, 30) -> Event [33]
var rect_Start_67_2_30 = new Rectangle (67, 2, 10, 28); // Starts at x=67, y=[2, 30) -> Event [34]
var rect_Start_67_1_1 = new Rectangle (67, 1, 10, 0); // Starts at x=67, y=[1, 1) -> Event [49] (Height 0)
var rect_End_67_1_1 = new Rectangle (60, 1, 7, 0); // Ends at x=67, y=[1, 1) -> Event [64] (Height 0)
// Add rectangles for x=94/95 pattern
var rect_End_94_1_30 = new Rectangle (90, 1, 4, 29); // Ends at x=94, y=[1, 30) -> Event [55]
var rect_Start_94_1_1 = new Rectangle (94, 1, 10, 0); // Starts at x=94, y=[1, 1) -> Event [56]
var rect_Start_94_7_30 = new Rectangle (94, 7, 10, 23); // Starts at x=94, y=[7, 30) -> Event [58]
var rect_End_95_1_1 = new Rectangle (90, 1, 5, 0); // Ends at x=95, y=[1, 1) -> Event [57]
var rect_End_95_7_30 = new Rectangle (90, 7, 5, 23); // Ends at x=95, y=[7, 30) -> Event [59]
var rect_Start_95_0_30 = new Rectangle (95, 0, 10, 30); // Starts at x=95, y=[0, 30) -> Event [60]
var combinedList = new List<Rectangle> {
rect_End_67_7_30, rect_Start_67_2_30, rect_Start_67_1_1, rect_End_67_1_1,
rect_End_94_1_30, rect_Start_94_1_1, rect_Start_94_7_30,
rect_End_95_1_1, rect_End_95_7_30, rect_Start_95_0_30
};
// Act & Assert:
// Call MergeRectangles. This test is specifically designed to fail with the current code.
var exception = Record.Exception (() => Region.MergeRectangles (combinedList, false));
// Assert that no exception was thrown (this assertion *should* fail with the current code)
Assert.Null (exception);
}
[Fact]
public void MergeRectangles_Sort_Reproduces_UICatalog_Crash_From_Captured_Data ()
{
// Arrange: The exact list of rectangles captured during the UICatalog crash
var rectanglesFromCrash = new List<Rectangle> {
new Rectangle(38, 7, 1, 11),
new Rectangle(39, 7, 5, 23),
new Rectangle(44, 7, 1, 23),
new Rectangle(45, 7, 6, 23),
new Rectangle(51, 7, 1, 23),
new Rectangle(52, 7, 1, 23),
new Rectangle(53, 7, 1, 23),
new Rectangle(54, 7, 1, 23),
new Rectangle(55, 7, 1, 23),
new Rectangle(56, 7, 1, 23),
new Rectangle(57, 7, 1, 23),
new Rectangle(58, 7, 1, 23),
new Rectangle(59, 7, 1, 23),
new Rectangle(60, 7, 1, 23),
new Rectangle(61, 7, 3, 23),
new Rectangle(64, 7, 1, 23),
new Rectangle(65, 7, 2, 23),
new Rectangle(67, 2, 2, 28),
new Rectangle(69, 2, 3, 28),
new Rectangle(72, 2, 3, 28),
new Rectangle(75, 2, 1, 28),
new Rectangle(76, 2, 2, 28),
new Rectangle(78, 2, 2, 28),
new Rectangle(80, 7, 1, 23),
new Rectangle(81, 1, 7, 29),
new Rectangle(88, 1, 1, 29),
new Rectangle(89, 1, 2, 29),
new Rectangle(91, 1, 3, 29),
new Rectangle(94, 1, 1, 0), // Note: Zero height
new Rectangle(94, 7, 1, 23),
new Rectangle(95, 0, 1, 30),
new Rectangle(96, 0, 23, 30),
new Rectangle(67, 1, 0, 0) // Note: Zero width and height
};
// Act & Assert:
// Call MergeRectangles with the current code.
// This test *should* fail by throwing ArgumentException due to unstable sort.
var exception = Record.Exception (() => Region.MergeRectangles (rectanglesFromCrash, false));
// Assert that no exception was thrown (this assertion will fail with the current code)
Assert.Null (exception);
}
}