diff --git a/.editorconfig b/.editorconfig
index 1a2db97..951ce01 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,74 +1,37 @@
-root = true
-
-[*]
-charset = utf-8
-indent_size = 4
-indent_style = space
-insert_final_newline = true
-trim_trailing_whitespace = true
-
-[*.{yml,props,targets,csproj}]
-indent_size = 2
-
-# IDE0079: Remove unnecessary suppression
-dotnet_remove_unnecessary_suppression_exclusions = category:ReSharper
-
-# IDE0079: Remove unnecessary suppression
-# dotnet_diagnostic.IDE0079.severity = none
-
-# CA2255: The 'ModuleInitializer' attribute should not be used in libraries
-dotnet_diagnostic.CA2255.severity = none
-
-# CS8600: Converting null literal or possible null value to non-nullable type.
-dotnet_diagnostic.CS8600.severity = error
-
-# CS8601: Possible null reference assignment.
-dotnet_diagnostic.CS8601.severity = error
-
-# CS8602: Dereference of a possibly null reference.
-dotnet_diagnostic.CS8602.severity = error
-
-# CS8603: Possible null reference return.
-dotnet_diagnostic.CS8603.severity = error
-
-# CS8604: Possible null reference argument.
-dotnet_diagnostic.CS8604.severity = error
-
-# CS8610: Nullability of reference types in type of parameter doesn't match overridden member.
-dotnet_diagnostic.CS8610.severity = error
-
-# CS8613: Nullability of reference types in return type doesn't match implicitly implemented member.
-dotnet_diagnostic.CS8613.severity = error
-
-# CS8614: Nullability of reference types in type of parameter doesn't match implicitly implemented member.
-dotnet_diagnostic.CS8614.severity = error
-
-# CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.
-dotnet_diagnostic.CS8618.severity = error
-
-# CS8619: Nullability of reference types in value doesn't match target type.
-dotnet_diagnostic.CS8619.severity = error
-
-# CS8620: Argument cannot be used for parameter due to differences in the nullability of reference types.
-dotnet_diagnostic.CS8620.severity = error
-
-# CS8625: Cannot convert null literal to non-nullable reference type.
-dotnet_diagnostic.CS8625.severity = error
-
-# CS8634: The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'class' constraint.
-dotnet_diagnostic.CS8634.severity = error
-
-# CS8643: Nullability of reference types in explicit interface specifier doesn't match interface implemented by the type.
-dotnet_diagnostic.CS8643.severity = error
-
-# CS8653: A default expression introduces a null value for a type parameter.
-dotnet_diagnostic.CS8653.severity = error
-
-# CS8714: The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
-dotnet_diagnostic.CS8714.severity = error
-
-# IDE0059: Unnecessary assigment of a value
-dotnet_diagnostic.IDE0059.severity = none
-
-# IDE0060: Remove unused parameter
-dotnet_diagnostic.IDE0060.severity = none
+root = true
+
+[*]
+charset = utf-8
+indent_size = 4
+indent_style = space
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[*.{yml,yaml}]
+indent_size = 2
+
+[*.{slnx,csproj,proj,props,targets}]
+indent_size = 2
+
+[*.cs]
+resharper_csharp_max_line_length = 160
+
+# Using directive options
+dotnet_sort_system_directives_first = true
+dotnet_separate_import_directive_groups = true
+
+# Prefer var whenever possible
+csharp_style_var_for_built_in_types = true:suggestion
+csharp_style_var_when_type_is_apparent = true:suggestion
+csharp_style_var_elsewhere = true:suggestion
+
+# Indentation preferences
+csharp_indent_labels = no_change
+
+# IDE0079: Remove unnecessary suppression
+dotnet_remove_unnecessary_suppression_exclusions = category:ReSharper
+
+[Ramstack.Structures.Tests/**.cs]
+# CA1707: Identifiers should not contain underscores
+# Tests follow the Method_Condition_ExpectedResult naming convention.
+dotnet_diagnostic.CA1707.severity = none
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 7c3b52a..1c8c080 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -1,38 +1,38 @@
-name: Publish
-
-on:
- push:
- tags:
- - "[0-9]+.[0-9]+.[0-9]+"
- - "[0-9]+.[0-9]+.[0-9]+-[a-z]+.[0-9]+"
-
-permissions:
- contents: write
-
-jobs:
- publish:
- if: github.repository == 'rameel/ramstack.structures'
- runs-on: ubuntu-latest
- steps:
- - name: Install .NET
- uses: actions/setup-dotnet@v3
- with:
- dotnet-version: 10.0.x
-
- - name: Checkout
- uses: actions/checkout@v4
-
- - name: Build
- run: dotnet build -c Release
-
- - name: Create NuGet Packages
- run: dotnet pack -c Release -o ./nuget --no-build
-
- - name: Publish NuGet Packages
- run: dotnet nuget push ./nuget/*.nupkg --api-key ${{secrets.NUGET_API_KEY}} --skip-duplicate -s https://api.nuget.org/v3/index.json
-
- - name: Create GitHub Release
- uses: softprops/action-gh-release@v2
- with:
- draft: true
- files: nuget/*
+name: Publish
+
+on:
+ push:
+ tags:
+ - "[0-9]+.[0-9]+.[0-9]+"
+ - "[0-9]+.[0-9]+.[0-9]+-[a-z]+.[0-9]+"
+
+permissions:
+ contents: write
+
+jobs:
+ publish:
+ if: github.repository == 'rameel/ramstack.structures'
+ runs-on: ubuntu-latest
+ steps:
+ - name: Install .NET
+ uses: actions/setup-dotnet@v3
+ with:
+ dotnet-version: 10.0.x
+
+ - name: Checkout
+ uses: actions/checkout@v4
+
+ - name: Build
+ run: dotnet build -c Release
+
+ - name: Create NuGet Packages
+ run: dotnet pack -c Release -o ./nuget --no-build
+
+ - name: Publish NuGet Packages
+ run: dotnet nuget push ./nuget/*.nupkg --api-key ${{secrets.NUGET_API_KEY}} --skip-duplicate -s https://api.nuget.org/v3/index.json
+
+ - name: Create GitHub Release
+ uses: softprops/action-gh-release@v2
+ with:
+ draft: true
+ files: nuget/*
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 9f09599..d5cbee7 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -1,30 +1,30 @@
-name: Build & Test
-
-on:
- pull_request:
- branches: [main]
-
-jobs:
- build-and-test:
- name: "Test project"
- runs-on: ubuntu-latest
-
- steps:
- - name: Checkout
- uses: actions/checkout@v4
-
- - name: Setup .NET
- uses: actions/setup-dotnet@v3
- with:
- dotnet-version: |
- 6.x
- 7.x
- 8.x
- 9.x
- 10.x
-
- - name: Test (Debug)
- run: dotnet test -c Debug
-
- - name: Test (Release)
- run: dotnet test -c Release
+name: Build & Test
+
+on:
+ pull_request:
+ branches: [main]
+
+jobs:
+ build-and-test:
+ name: "Test project"
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v4
+
+ - name: Setup .NET
+ uses: actions/setup-dotnet@v3
+ with:
+ dotnet-version: |
+ 6.x
+ 7.x
+ 8.x
+ 9.x
+ 10.x
+
+ - name: Test (Debug)
+ run: dotnet test -c Debug
+
+ - name: Test (Release)
+ run: dotnet test -c Release
diff --git a/.gitignore b/.gitignore
index 4f81b1b..bd6adb1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,37 +1,37 @@
-#Ignore thumbnails created by windows
-[Tt]humbs.db
-
-#Ignore files build by Visual Studio
-*.obj
-*.exe
-*.pdb
-*.user
-*.patch
-*.aps
-*.pch
-*.vspscc
-*_i.c
-*_p.c
-*.ncb
-*.suo
-*.tlb
-*.tlh
-*.bak
-*.cache
-*.ilk
-*.log
-*.lib
-*.sbr
-*.cs.ide
-[Dd]ebug*/
-[Rr]elease*/
-bin/
-obj/
-.vs/
-.vscode/
-*.ReSharper
-*.ReSharper.user
-[Tt]est[Rr]esult*
-
-#Ignore Rider/Idea files
-.idea/
+#Ignore thumbnails created by windows
+[Tt]humbs.db
+
+#Ignore files build by Visual Studio
+*.obj
+*.exe
+*.pdb
+*.user
+*.patch
+*.aps
+*.pch
+*.vspscc
+*_i.c
+*_p.c
+*.ncb
+*.suo
+*.tlb
+*.tlh
+*.bak
+*.cache
+*.ilk
+*.log
+*.lib
+*.sbr
+*.cs.ide
+[Dd]ebug*/
+[Rr]elease*/
+bin/
+obj/
+.vs/
+.vscode/
+*.ReSharper
+*.ReSharper.user
+[Tt]est[Rr]esult*
+
+#Ignore Rider/Idea files
+.idea/
diff --git a/Directory.Build.props b/Directory.Build.props
index bf61882..9e6caca 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -1,5 +1,19 @@
-
-
- true
-
-
+
+
+ true
+
+
+
+ $(NoWarn);CS0649
+ true
+
+
+
+ true
+ latest
+ Recommended
+ Default
+ true
+ true
+
+
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 960898d..4137cad 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -1,13 +1,13 @@
-
-
-
- true
-
-
-
-
-
-
-
-
+
+
+
+ true
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
index 6666765..92e3431 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,9 +1,9 @@
-MIT License
-
-Copyright 2024 Rameel (https://github.com/rameel)
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+MIT License
+
+Copyright 2024 Rameel (https://github.com/rameel)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
index 815cac3..659dcec 100644
--- a/README.md
+++ b/README.md
@@ -1,64 +1,64 @@
-# Ramstack.Structures
-
-[](https://nuget.org/packages/Ramstack.Structures)
-[](https://github.com/rameel/ramstack.structures/blob/main/LICENSE)
-
-Ramstack.Structures is a .NET library providing various data structures and utilities.
-
-## Installation
-
-To install the `Ramstack.Structures` [NuGet package](https://www.nuget.org/packages/Ramstack.Structures) to your project, use the following command:
-
-```console
-dotnet add package Ramstack.Structures
-```
-
-## Features
-
-The current release includes primary types:
-
-* `Ramstack.Text.StringView`:
Represents a read-only view of a string, providing safe and efficient access to a subset of its characters.
-* `Ramstack.Collections.ArrayView`:
A generic read-only view of an array, allowing safe and efficient access to a subset of its elements.
-* `Ramstack.Collections.ReadOnlyArray`:
A generic read-only array wrapper, similar to `ImmutableArray`, but with some performance improvements and better code generation in certain cases.
-
-Additionally, the library provides the `StringViewComparer` class, which offers various comparators accessible through corresponding properties:
-
-| Property | Description |
-|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
-| **Ordinal** | Gets a **StringViewComparer** object that performs a case-sensitive ordinal string comparison. |
-| **OrdinalIgnoreCase** | Gets a **StringViewComparer** object that performs a case-insensitive ordinal string comparison. |
-| **CurrentCulture** | Gets a **StringViewComparer** object that performs case-sensitive string comparisons using the word comparison rules of the current culture. |
-| **CurrentCultureIgnoreCase** | Gets a **StringViewComparer** object that performs case-insensitive string comparisons using the word comparison rules of the current culture. |
-| **InvariantCulture** | Gets a **StringViewComparer** object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture. |
-| **InvariantCultureIgnoreCase** | Gets a **StringViewComparer** object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture. |
-
-## Notes
-
-The `ArrayView` and `ReadOnlyArray` types have a `ref readonly` indexer, which provides efficient access to elements of the underlying array,
-avoiding expensive copying in the case of large structures.
-
-This same optimization is also applied to the enumerators of these types. The `Current` property has a `ref readonly` signature,
-which enables the following pattern when necessary:
-
-```csharp
-foreach (ref readonly HeavyStruct s in view)
-{
- ...
-}
-
-```
-
-## Supported versions
-
-| | Version |
-|------|------------|
-| .NET | 6, 7, 8, 9 |
-
-## Contributions
-
-Bug reports and contributions are welcome.
-
-## License
-This package is released as open source under the **MIT License**.
-
-See the [LICENSE](LICENSE) file for more details.
+# Ramstack.Structures
+
+[](https://nuget.org/packages/Ramstack.Structures)
+[](https://github.com/rameel/ramstack.structures/blob/main/LICENSE)
+
+Ramstack.Structures is a .NET library providing various data structures and utilities.
+
+## Installation
+
+To install the `Ramstack.Structures` [NuGet package](https://www.nuget.org/packages/Ramstack.Structures) to your project, use the following command:
+
+```console
+dotnet add package Ramstack.Structures
+```
+
+## Features
+
+The current release includes primary types:
+
+* `Ramstack.Text.StringView`:
Represents a read-only view of a string, providing safe and efficient access to a subset of its characters.
+* `Ramstack.Collections.ArrayView`:
A generic read-only view of an array, allowing safe and efficient access to a subset of its elements.
+* `Ramstack.Collections.ReadOnlyArray`:
A generic read-only array wrapper, similar to `ImmutableArray`, but with some performance improvements and better code generation in certain cases.
+
+Additionally, the library provides the `StringViewComparer` class, which offers various comparators accessible through corresponding properties:
+
+| Property | Description |
+|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
+| **Ordinal** | Gets a **StringViewComparer** object that performs a case-sensitive ordinal string comparison. |
+| **OrdinalIgnoreCase** | Gets a **StringViewComparer** object that performs a case-insensitive ordinal string comparison. |
+| **CurrentCulture** | Gets a **StringViewComparer** object that performs case-sensitive string comparisons using the word comparison rules of the current culture. |
+| **CurrentCultureIgnoreCase** | Gets a **StringViewComparer** object that performs case-insensitive string comparisons using the word comparison rules of the current culture. |
+| **InvariantCulture** | Gets a **StringViewComparer** object that performs a case-sensitive string comparison using the word comparison rules of the invariant culture. |
+| **InvariantCultureIgnoreCase** | Gets a **StringViewComparer** object that performs a case-insensitive string comparison using the word comparison rules of the invariant culture. |
+
+## Notes
+
+The `ArrayView` and `ReadOnlyArray` types have a `ref readonly` indexer, which provides efficient access to elements of the underlying array,
+avoiding expensive copying in the case of large structures.
+
+This same optimization is also applied to the enumerators of these types. The `Current` property has a `ref readonly` signature,
+which enables the following pattern when necessary:
+
+```csharp
+foreach (ref readonly HeavyStruct s in view)
+{
+ ...
+}
+
+```
+
+## Supported versions
+
+| | Version |
+|------|------------|
+| .NET | 6, 7, 8, 9 |
+
+## Contributions
+
+Bug reports and contributions are welcome.
+
+## License
+This package is released as open source under the **MIT License**.
+
+See the [LICENSE](LICENSE) file for more details.
diff --git a/Ramstack.Structures.Tests/Collections/ArrayViewExtensionsTests.cs b/Ramstack.Structures.Tests/Collections/ArrayViewExtensionsTests.cs
index 76c0501..2c01f58 100644
--- a/Ramstack.Structures.Tests/Collections/ArrayViewExtensionsTests.cs
+++ b/Ramstack.Structures.Tests/Collections/ArrayViewExtensionsTests.cs
@@ -1,30 +1,30 @@
-namespace Ramstack.Collections;
-
-#if NET9_0_OR_GREATER
-
-[TestFixture]
-public class ArrayViewExtensionsTests
-{
- [Test]
- public void List_AsView_Empty()
- {
- var list = new List();
- var view = list.AsView();
-
- Assert.That(view.Length, Is.EqualTo(list.Count));
- Assert.That(view, Is.EquivalentTo(list));
- }
-
- [Test]
- public void List_AsView()
- {
- var list = new List { "apple", "banana", "cherry", "kiwi", "elderberry" };
- var view = list.AsView();
-
- Assert.That(view.Length, Is.EqualTo(list.Count));
- Assert.That(view, Is.EquivalentTo(list));
- }
-
-}
-
-#endif
+namespace Ramstack.Collections;
+
+#if NET9_0_OR_GREATER
+
+[TestFixture]
+public class ArrayViewExtensionsTests
+{
+ [Test]
+ public void List_AsView_Empty()
+ {
+ var list = new List();
+ var view = list.AsView();
+
+ Assert.That(view.Length, Is.EqualTo(list.Count));
+ Assert.That(view, Is.EquivalentTo(list));
+ }
+
+ [Test]
+ public void List_AsView()
+ {
+ var list = new List { "apple", "banana", "cherry", "kiwi", "elderberry" };
+ var view = list.AsView();
+
+ Assert.That(view.Length, Is.EqualTo(list.Count));
+ Assert.That(view, Is.EquivalentTo(list));
+ }
+
+}
+
+#endif
diff --git a/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs b/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs
index 8874997..3a3dec0 100644
--- a/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs
+++ b/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs
@@ -1,512 +1,512 @@
-using System.Collections.Immutable;
-
-namespace Ramstack.Collections;
-
-[TestFixture]
-[SuppressMessage("ReSharper", "ReplaceSliceWithRangeIndexer")]
-public class ArrayViewTests
-{
- [Test]
- public void Empty_HaveZeroLength()
- {
- Assert.That(ArrayView.Empty.Length, Is.Zero);
- }
-
- [Test]
- public void Default_DoesNotThrow()
- {
- var view = default(ArrayView);
-
- Assert.That(view.Length, Is.Zero);
- Assert.That(view.IsDefault, Is.True);
-
- for (var i = 0; i < view.Length; i++)
- Assert.Fail();
-
- foreach (var _ in view)
- Assert.Fail();
-
- foreach (var _ in view.AsEnumerable())
- Assert.Fail();
-
- foreach (var _ in view.AsSpan())
- Assert.Fail();
-
- Assert.That(view.ToArray(), Is.EquivalentTo(Array.Empty()));
- Assert.That(view.AsMemory().ToArray(), Is.EquivalentTo(Array.Empty()));
- Assert.That(view.Slice(0).Length, Is.Zero);
- Assert.That(view.Slice(0, 0).Length, Is.Zero);
- Assert.That(view, Is.EquivalentTo(Array.Empty()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("value", 1, 4)]
- [TestCase("value", 2, 3)]
- [TestCase("value", 3, 2)]
- [TestCase("value", 4, 1)]
- [TestCase("value", 5, 0)]
- public void Enumeration(string value, int index, int length)
- {
- var view = new ArrayView(value.ToCharArray(), index, length);
- var span = value.AsSpan(index, length);
-
- Assert.That(view.Length, Is.EqualTo(span.Length));
-
- for (var i = 0; i < view.Length; i++)
- Assert.That(view[i], Is.EqualTo(span[i]));
-
- var j = 0;
- foreach (var c in view)
- Assert.That(c, Is.EqualTo(span[j++]));
-
- j = 0;
- foreach (var c in view.AsEnumerable())
- Assert.That(c, Is.EqualTo(span[j++]));
-
- for (var i = 0; i < view.Length; i++)
- Assert.That(view[i], Is.EqualTo(span[i]));
- }
-
- [TestCase("")]
- [TestCase("value")]
- [TestCase("key=value")]
- public void Ctor(string value)
- {
- Assert.That(
- new ArrayView(value.ToCharArray()),
- Is.EquivalentTo(value));
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void Ctor_Index(string value, int index)
- {
- Assert.That(
- new ArrayView(value.ToCharArray(), index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Ctor_Range(string value, int index, int length)
- {
- Assert.That(
- new ArrayView(value.ToCharArray(), index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [Test]
- public void Ctor_NullInput_ShouldThrow()
- {
- Assert.That(() => new ArrayView(null!), Throws.TypeOf());
- Assert.That(() => new ArrayView(null!, 0), Throws.TypeOf());
- Assert.That(() => new ArrayView(null!, 0, 0), Throws.TypeOf());
- }
-
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void Ctor_InvalidIndex_ShouldThrow(string value, int index)
- {
- Assert.That(
- () => new ArrayView(value.ToCharArray(), index),
- Throws.TypeOf());
- }
-
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void Ctor_InvalidRange_ShouldThrow(string value, int index, int length)
- {
- Assert.That(
- () => new ArrayView(value.ToCharArray(), index, length),
- Throws.TypeOf());
- }
-
- [Test]
- public void Slice()
- {
- var view = default(ArrayView);
-
- Assert.That(view.Slice(0).Length, Is.Zero);
- Assert.That(view.Slice(0, 0).Length, Is.Zero);
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void Slice_Index(string value, int index)
- {
- Assert.That(
- new ArrayView(value.ToCharArray()).Slice(index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Slice_Range(string value, int index, int length)
- {
- Assert.That(
- new ArrayView(value.ToCharArray()).Slice(index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void Slice_InvalidIndex_ShouldThrow(string value, int index)
- {
- Assert.That(
- () => new ArrayView(value.ToCharArray()).Slice(index),
- Throws.TypeOf());
- }
-
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void Slice_InvalidRange_ShouldThrow(string value, int index, int length)
- {
- Assert.That(
- () => new ArrayView(value.ToCharArray()).Slice(index, length),
- Throws.TypeOf());
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Length(string? value, int index, int length)
- {
- Assert.That(
- (value?.ToCharArray()).AsView(index, length).Length,
- Is.EqualTo(length));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void ToArray(string? value, int index, int length)
- {
- Assert.That(
- (value?.ToCharArray()).AsView(index, length).ToArray(),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase("", 0, 0)]
- [TestCase("id;name;lastname", 3, 0)]
- [TestCase("id;name;lastname", 3, 4)]
- [TestCase("id;name;lastname", 0, 2)]
- [TestCase("id;name;lastname", 8, 8)]
- public void GetPinnableReference(string value, int index, int length)
- {
- var list = value.ToCharArray();
- var view = list.AsView(index, length);
- var span = list.AsSpan(index, length);
-
- Assert.That(
- Unsafe.AreSame(
- ref Unsafe.AsRef(in view.GetPinnableReference()),
- ref Unsafe.AsRef(in span.GetPinnableReference())),
- Is.True);
- }
-
- [Test]
- public void GetPinnableReference_Default()
- {
- var view = default(ArrayView);
- var span = default(Span);
-
- Assert.That(
- Unsafe.AreSame(
- ref Unsafe.AsRef(in view.GetPinnableReference()),
- ref Unsafe.AsRef(in span.GetPinnableReference())),
- Is.True);
- }
-
- [TestCase(null)]
- [TestCase("")]
- [TestCase("test")]
- public void Operator_Conversions(string? value)
- {
- ArrayView view = value?.ToCharArray();
- ReadOnlyMemory memory = view;
- ReadOnlySpan span = view;
-
- Assert.That(
- span.ToString() == value.AsSpan().ToString(),
- Is.True);
-
- Assert.That(
- memory.ToString() == value.AsMemory().ToString(),
- Is.True);
-
- for (var i = 0; i < view.Length; i++)
- {
- Assert.That(view[i], Is.EqualTo(span[i]));
- Assert.That(view[i], Is.EqualTo(memory.Span[i]));
- }
-
- var j = 0;
- foreach (var c in view)
- {
- Assert.That(c, Is.EqualTo(span[j]));
- Assert.That(c, Is.EqualTo(memory.Span[j]));
- j++;
- }
- }
-
- [TestCase("", 0, 0)]
- [TestCase(null, 0, 0)]
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- [TestCase("key=value", 5, 0)]
- [TestCase("key=value", 3, 0)]
- public void CopyTo(string? source, int index, int length)
- {
- var data = new char[length];
- var list = source?.ToCharArray();
- var view = list.AsView(index, length);
-
- view.CopyTo(data.AsSpan());
-
- Assert.That(view, Is.EquivalentTo(data));
- }
-
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void CopyTo_InsufficientRoom_ShouldThrow(string? source, int index, int length)
- {
- var data = new char[length - 1];
- var list = source?.ToCharArray();
- var view = list.AsView(index, length);
-
- var e = Assert.Throws(() => view.CopyTo(data.AsSpan()))!;
- Assert.That(e.Message, Is.EqualTo("Destination is too short. (Parameter 'destination')"));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(null, 0, 0)]
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- [TestCase("key=value", 5, 0)]
- public void TryCopyTo(string? source, int index, int length)
- {
- var data = new char[length];
- var list = source?.ToCharArray();
- var view = list.AsView(index, length);
-
- Assert.That(view.TryCopyTo(data.AsSpan()), Is.True);
- Assert.That(view, Is.EquivalentTo(data));
- }
-
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void TryCopyTo_InsufficientRoom_ShouldReturnFalse(string? source, int index, int length)
- {
- var data = new char[length - 1];
- var list = source?.ToCharArray();
- var view = list.AsView(index, length);
-
- Assert.That(view.TryCopyTo(data.AsSpan()), Is.False);
- Assert.That(data, Is.EquivalentTo(new char[length - 1]));
- }
-
- [TestCase("kiwi")]
- [TestCase("KIWI")]
- [TestCase("lemon")]
- public void IndexOf(string needle)
- {
- var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
-
- Assert.That(
- source.AsView().IndexOf(needle),
- Is.EqualTo(source.AsSpan().IndexOf(needle)));
-
- Assert.That(
- source.AsView().IndexOf(needle, null),
- Is.EqualTo(Array.IndexOf(source, needle)));
-
- Assert.That(
- source.AsView().IndexOf(needle, StringComparer.OrdinalIgnoreCase),
- Is.EqualTo(source.ToList().FindIndex(s => s.Equals(needle, StringComparison.OrdinalIgnoreCase))));
- }
-
- [TestCase("kiwi")]
- [TestCase("KIWI")]
- [TestCase("lemon")]
- public void LastIndexOf(string needle)
- {
- var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
-
- Assert.That(
- source.AsView().LastIndexOf(needle),
- Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
-
- Assert.That(
- source.AsView(1, 3).LastIndexOf(needle),
- Is.EqualTo(source.AsSpan(1, 3).LastIndexOf(needle)));
-
- Assert.That(
- source.AsView().LastIndexOf(needle, null),
- Is.EqualTo(Array.LastIndexOf(source, needle)));
-
- Assert.That(
- source.AsView().LastIndexOf(needle, StringComparer.OrdinalIgnoreCase),
- Is.EqualTo(source.ToList().FindLastIndex(s => s.Equals(needle, StringComparison.OrdinalIgnoreCase))));
- }
-
- [Test]
- public void IndexOf()
- {
- var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
-
- Assert.That(
- source.AsView().IndexOf("kiwi"),
- Is.EqualTo(source.AsSpan().IndexOf("kiwi")));
-
- Assert.That(
- source.AsView().IndexOf("kiwi", null),
- Is.EqualTo(Array.IndexOf(source, "kiwi")));
-
- Assert.That(
- source.AsView().IndexOf("KIWI", StringComparer.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().IndexOf("kiwi")));
- }
-
- [TestCase(null)]
- [TestCase("")]
- [TestCase("value")]
- [TestCase("key=value")]
- public void AsView(string? value)
- {
- var list = value?.ToCharArray();
- var immutable = value.AsSpan().ToArray().ToImmutableArray();
-
- Assert.That(
- list.AsView(),
- Is.EquivalentTo(value.AsSpan().ToArray()));
-
- Assert.That(
- immutable.AsView(),
- Is.EquivalentTo(value.AsSpan().ToArray()));
- }
-
- [TestCase(null, 0)]
- [TestCase("", 0)]
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 0)]
- [TestCase("key=value", 3)]
- [TestCase("key=value", 4)]
- public void AsView_Index(string? value, int index)
- {
- var list = value?.ToCharArray();
- var immutable = value.AsSpan().ToArray().ToImmutableArray();
-
- Assert.That(
- list.AsView(index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
-
- Assert.That(
- immutable.AsView(index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void AsView_Range(string? value, int index, int length)
- {
- var list = value?.ToCharArray();
- var immutable = value.AsSpan().ToArray().ToImmutableArray();
-
- Assert.That(
- list.AsView(index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
-
- Assert.That(
- immutable.AsView(index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase(null, 1)]
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
- {
- var list = value?.ToCharArray();
- var immutable = value.AsSpan().ToArray().ToImmutableArray();
-
- Assert.That(
- () => list.AsView(index),
- Throws.TypeOf());
-
- Assert.That(
- () => immutable.AsView(index),
- Throws.TypeOf());
- }
-
- [TestCase(null, 1, 0)]
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length)
- {
- var list = value?.ToCharArray();
- var immutable = value.AsSpan().ToArray().ToImmutableArray();
-
- Assert.That(
- () => list.AsView(index, length),
- Throws.TypeOf());
-
- Assert.That(
- () => immutable.AsView(index, length),
- Throws.TypeOf());
- }
-}
+using System.Collections.Immutable;
+
+namespace Ramstack.Collections;
+
+[TestFixture]
+[SuppressMessage("ReSharper", "ReplaceSliceWithRangeIndexer")]
+public class ArrayViewTests
+{
+ [Test]
+ public void Empty_HaveZeroLength()
+ {
+ Assert.That(ArrayView.Empty.Length, Is.Zero);
+ }
+
+ [Test]
+ public void Default_DoesNotThrow()
+ {
+ var view = default(ArrayView);
+
+ Assert.That(view.Length, Is.Zero);
+ Assert.That(view.IsDefault, Is.True);
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.Fail();
+
+ foreach (var _ in view)
+ Assert.Fail();
+
+ foreach (var _ in view.AsEnumerable())
+ Assert.Fail();
+
+ foreach (var _ in view.AsSpan())
+ Assert.Fail();
+
+ Assert.That(view.ToArray(), Is.EquivalentTo(Array.Empty()));
+ Assert.That(view.AsMemory().ToArray(), Is.EquivalentTo(Array.Empty()));
+ Assert.That(view.Slice(0).Length, Is.Zero);
+ Assert.That(view.Slice(0, 0).Length, Is.Zero);
+ Assert.That(view, Is.EquivalentTo(Array.Empty()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("value", 1, 4)]
+ [TestCase("value", 2, 3)]
+ [TestCase("value", 3, 2)]
+ [TestCase("value", 4, 1)]
+ [TestCase("value", 5, 0)]
+ public void Enumeration(string value, int index, int length)
+ {
+ var view = new ArrayView(value.ToCharArray(), index, length);
+ var span = value.AsSpan(index, length);
+
+ Assert.That(view.Length, Is.EqualTo(span.Length));
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.That(view[i], Is.EqualTo(span[i]));
+
+ var j = 0;
+ foreach (var c in view)
+ Assert.That(c, Is.EqualTo(span[j++]));
+
+ j = 0;
+ foreach (var c in view.AsEnumerable())
+ Assert.That(c, Is.EqualTo(span[j++]));
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.That(view[i], Is.EqualTo(span[i]));
+ }
+
+ [TestCase("")]
+ [TestCase("value")]
+ [TestCase("key=value")]
+ public void Ctor(string value)
+ {
+ Assert.That(
+ new ArrayView(value.ToCharArray()),
+ Is.EquivalentTo(value));
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void Ctor_Index(string value, int index)
+ {
+ Assert.That(
+ new ArrayView(value.ToCharArray(), index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Ctor_Range(string value, int index, int length)
+ {
+ Assert.That(
+ new ArrayView(value.ToCharArray(), index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [Test]
+ public void Ctor_NullInput_ShouldThrow()
+ {
+ Assert.That(() => new ArrayView(null!), Throws.TypeOf());
+ Assert.That(() => new ArrayView(null!, 0), Throws.TypeOf());
+ Assert.That(() => new ArrayView(null!, 0, 0), Throws.TypeOf());
+ }
+
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void Ctor_InvalidIndex_ShouldThrow(string value, int index)
+ {
+ Assert.That(
+ () => new ArrayView(value.ToCharArray(), index),
+ Throws.TypeOf());
+ }
+
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void Ctor_InvalidRange_ShouldThrow(string value, int index, int length)
+ {
+ Assert.That(
+ () => new ArrayView(value.ToCharArray(), index, length),
+ Throws.TypeOf());
+ }
+
+ [Test]
+ public void Slice()
+ {
+ var view = default(ArrayView);
+
+ Assert.That(view.Slice(0).Length, Is.Zero);
+ Assert.That(view.Slice(0, 0).Length, Is.Zero);
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void Slice_Index(string value, int index)
+ {
+ Assert.That(
+ new ArrayView(value.ToCharArray()).Slice(index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Slice_Range(string value, int index, int length)
+ {
+ Assert.That(
+ new ArrayView(value.ToCharArray()).Slice(index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void Slice_InvalidIndex_ShouldThrow(string value, int index)
+ {
+ Assert.That(
+ () => new ArrayView(value.ToCharArray()).Slice(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void Slice_InvalidRange_ShouldThrow(string value, int index, int length)
+ {
+ Assert.That(
+ () => new ArrayView(value.ToCharArray()).Slice(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Length(string? value, int index, int length)
+ {
+ Assert.That(
+ (value?.ToCharArray()).AsView(index, length).Length,
+ Is.EqualTo(length));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void ToArray(string? value, int index, int length)
+ {
+ Assert.That(
+ (value?.ToCharArray()).AsView(index, length).ToArray(),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase("id;name;lastname", 3, 0)]
+ [TestCase("id;name;lastname", 3, 4)]
+ [TestCase("id;name;lastname", 0, 2)]
+ [TestCase("id;name;lastname", 8, 8)]
+ public void GetPinnableReference(string value, int index, int length)
+ {
+ var list = value.ToCharArray();
+ var view = list.AsView(index, length);
+ var span = list.AsSpan(index, length);
+
+ Assert.That(
+ Unsafe.AreSame(
+ ref Unsafe.AsRef(in view.GetPinnableReference()),
+ ref Unsafe.AsRef(in span.GetPinnableReference())),
+ Is.True);
+ }
+
+ [Test]
+ public void GetPinnableReference_Default()
+ {
+ var view = default(ArrayView);
+ var span = default(Span);
+
+ Assert.That(
+ Unsafe.AreSame(
+ ref Unsafe.AsRef(in view.GetPinnableReference()),
+ ref Unsafe.AsRef(in span.GetPinnableReference())),
+ Is.True);
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("test")]
+ public void Operator_Conversions(string? value)
+ {
+ ArrayView view = value?.ToCharArray();
+ ReadOnlyMemory memory = view;
+ ReadOnlySpan span = view;
+
+ Assert.That(
+ span.ToString() == value.AsSpan().ToString(),
+ Is.True);
+
+ Assert.That(
+ memory.ToString() == value.AsMemory().ToString(),
+ Is.True);
+
+ for (var i = 0; i < view.Length; i++)
+ {
+ Assert.That(view[i], Is.EqualTo(span[i]));
+ Assert.That(view[i], Is.EqualTo(memory.Span[i]));
+ }
+
+ var j = 0;
+ foreach (var c in view)
+ {
+ Assert.That(c, Is.EqualTo(span[j]));
+ Assert.That(c, Is.EqualTo(memory.Span[j]));
+ j++;
+ }
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(null, 0, 0)]
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ [TestCase("key=value", 5, 0)]
+ [TestCase("key=value", 3, 0)]
+ public void CopyTo(string? source, int index, int length)
+ {
+ var data = new char[length];
+ var list = source?.ToCharArray();
+ var view = list.AsView(index, length);
+
+ view.CopyTo(data.AsSpan());
+
+ Assert.That(view, Is.EquivalentTo(data));
+ }
+
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void CopyTo_InsufficientRoom_ShouldThrow(string? source, int index, int length)
+ {
+ var data = new char[length - 1];
+ var list = source?.ToCharArray();
+ var view = list.AsView(index, length);
+
+ var e = Assert.Throws(() => view.CopyTo(data.AsSpan()))!;
+ Assert.That(e.Message, Is.EqualTo("Destination is too short. (Parameter 'destination')"));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(null, 0, 0)]
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ [TestCase("key=value", 5, 0)]
+ public void TryCopyTo(string? source, int index, int length)
+ {
+ var data = new char[length];
+ var list = source?.ToCharArray();
+ var view = list.AsView(index, length);
+
+ Assert.That(view.TryCopyTo(data.AsSpan()), Is.True);
+ Assert.That(view, Is.EquivalentTo(data));
+ }
+
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void TryCopyTo_InsufficientRoom_ShouldReturnFalse(string? source, int index, int length)
+ {
+ var data = new char[length - 1];
+ var list = source?.ToCharArray();
+ var view = list.AsView(index, length);
+
+ Assert.That(view.TryCopyTo(data.AsSpan()), Is.False);
+ Assert.That(data, Is.EquivalentTo(new char[length - 1]));
+ }
+
+ [TestCase("kiwi")]
+ [TestCase("KIWI")]
+ [TestCase("lemon")]
+ public void IndexOf(string needle)
+ {
+ var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
+
+ Assert.That(
+ source.AsView().IndexOf(needle),
+ Is.EqualTo(source.AsSpan().IndexOf(needle)));
+
+ Assert.That(
+ source.AsView().IndexOf(needle, null),
+ Is.EqualTo(Array.IndexOf(source, needle)));
+
+ Assert.That(
+ source.AsView().IndexOf(needle, StringComparer.OrdinalIgnoreCase),
+ Is.EqualTo(source.ToList().FindIndex(s => s.Equals(needle, StringComparison.OrdinalIgnoreCase))));
+ }
+
+ [TestCase("kiwi")]
+ [TestCase("KIWI")]
+ [TestCase("lemon")]
+ public void LastIndexOf(string needle)
+ {
+ var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
+
+ Assert.That(
+ source.AsView().LastIndexOf(needle),
+ Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
+
+ Assert.That(
+ source.AsView(1, 3).LastIndexOf(needle),
+ Is.EqualTo(source.AsSpan(1, 3).LastIndexOf(needle)));
+
+ Assert.That(
+ source.AsView().LastIndexOf(needle, null),
+ Is.EqualTo(Array.LastIndexOf(source, needle)));
+
+ Assert.That(
+ source.AsView().LastIndexOf(needle, StringComparer.OrdinalIgnoreCase),
+ Is.EqualTo(source.ToList().FindLastIndex(s => s.Equals(needle, StringComparison.OrdinalIgnoreCase))));
+ }
+
+ [Test]
+ public void IndexOf()
+ {
+ var source = new[] { "apple", "banana", "cherry", "kiwi", "elderberry" };
+
+ Assert.That(
+ source.AsView().IndexOf("kiwi"),
+ Is.EqualTo(source.AsSpan().IndexOf("kiwi")));
+
+ Assert.That(
+ source.AsView().IndexOf("kiwi", null),
+ Is.EqualTo(Array.IndexOf(source, "kiwi")));
+
+ Assert.That(
+ source.AsView().IndexOf("KIWI", StringComparer.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().IndexOf("kiwi")));
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("value")]
+ [TestCase("key=value")]
+ public void AsView(string? value)
+ {
+ var list = value?.ToCharArray();
+ var immutable = value.AsSpan().ToArray().ToImmutableArray();
+
+ Assert.That(
+ list.AsView(),
+ Is.EquivalentTo(value.AsSpan().ToArray()));
+
+ Assert.That(
+ immutable.AsView(),
+ Is.EquivalentTo(value.AsSpan().ToArray()));
+ }
+
+ [TestCase(null, 0)]
+ [TestCase("", 0)]
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 0)]
+ [TestCase("key=value", 3)]
+ [TestCase("key=value", 4)]
+ public void AsView_Index(string? value, int index)
+ {
+ var list = value?.ToCharArray();
+ var immutable = value.AsSpan().ToArray().ToImmutableArray();
+
+ Assert.That(
+ list.AsView(index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+
+ Assert.That(
+ immutable.AsView(index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void AsView_Range(string? value, int index, int length)
+ {
+ var list = value?.ToCharArray();
+ var immutable = value.AsSpan().ToArray().ToImmutableArray();
+
+ Assert.That(
+ list.AsView(index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+
+ Assert.That(
+ immutable.AsView(index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase(null, 1)]
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
+ {
+ var list = value?.ToCharArray();
+ var immutable = value.AsSpan().ToArray().ToImmutableArray();
+
+ Assert.That(
+ () => list.AsView(index),
+ Throws.TypeOf());
+
+ Assert.That(
+ () => immutable.AsView(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase(null, 1, 0)]
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length)
+ {
+ var list = value?.ToCharArray();
+ var immutable = value.AsSpan().ToArray().ToImmutableArray();
+
+ Assert.That(
+ () => list.AsView(index, length),
+ Throws.TypeOf());
+
+ Assert.That(
+ () => immutable.AsView(index, length),
+ Throws.TypeOf());
+ }
+}
diff --git a/Ramstack.Structures.Tests/Collections/ReadOnlyArrayTests.cs b/Ramstack.Structures.Tests/Collections/ReadOnlyArrayTests.cs
index f03ba21..f6ce978 100644
--- a/Ramstack.Structures.Tests/Collections/ReadOnlyArrayTests.cs
+++ b/Ramstack.Structures.Tests/Collections/ReadOnlyArrayTests.cs
@@ -1,601 +1,601 @@
-using System.Collections.Immutable;
-
-namespace Ramstack.Collections;
-
-[TestFixture]
-public class ReadOnlyArrayTests
-{
- [Test]
- [SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
- public void Default_BehavesAsUnderlyingArray()
- {
- var array = new ReadOnlyArray();
-
- Assert.That(array.IsDefault, Is.True);
-
- Assert.That(() => array.AsSpan(), Throws.Nothing);
- Assert.That(() => array.AsSpan(0), Throws.Nothing);
- Assert.That(() => array.AsSpan(0, 0), Throws.Nothing);
- Assert.That(() => array.AsView(), Throws.Nothing);
- Assert.That(() => array.AsView(0), Throws.Nothing);
- Assert.That(() => array.AsView(0, 0), Throws.Nothing);
- Assert.That(() => array.GetHashCode(), Throws.Nothing);
-
- Assert.Throws(() => _ = array.Length);
- Assert.Throws(() => _ = array[0]);
- Assert.Throws(() => array.GetEnumerator());
- Assert.Throws(() => array.GetPinnableReference());
- Assert.Throws(() => array.Any());
- Assert.Throws(() => array.Any(_ => true));
- Assert.Throws(() => array.All(_ => true));
- Assert.Throws(() => array.ToArray());
- Assert.Throws(() => array.Slice(0));
- Assert.Throws(() => array.Slice(0, 0));
-
- Assert.Throws(() => array.IndexOf(0));
- Assert.Throws(() => array.LastIndexOf(0));
- }
-
- [Test]
- [SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
- public void Default_Boxed()
- {
- var array = (IReadOnlyList)new ReadOnlyArray();
-
- Assert.Throws(() => _ = array.Count);
- Assert.Throws(() => _ = array[0]);
- Assert.Throws(() => array.GetEnumerator());
- }
-
- [Test]
- public void Ctor()
- {
- for (var count = 1; count < 10; count++)
- {
- var items = CreateArray(count);
- var array = count switch
- {
- 1 => new ReadOnlyArray(items[0]),
- 2 => new ReadOnlyArray(items[0], items[1]),
- 3 => new ReadOnlyArray(items[0], items[1], items[2]),
- 4 => new ReadOnlyArray(items[0], items[1], items[2], items[3]),
- _ => new ReadOnlyArray(items)
- };
-
- Assert.That(array.Length, Is.EqualTo(items.Length));
- Assert.That(array, Is.EquivalentTo(items));
- }
- }
-
- [TestCase(0)]
- [TestCase(4)]
- [TestCase(9)]
- public void Ctor_Span(int length)
- {
- ReadOnlySpan span = CreateArray(length);
- var array = ReadOnlyArray.Create(span);
-
- Assert.That(array.Length, Is.EqualTo(length));
- Assert.That(array, Is.EquivalentTo(span.ToArray()));
- }
-
- [Test]
- public void Create()
- {
- #pragma warning disable IDE0303
- for (var count = 1; count < 10; count++)
- {
- var items = Enumerable.Range(1, count).ToArray();
- var array = count switch
- {
- 1 => ReadOnlyArray.Create(items[0]),
- 2 => ReadOnlyArray.Create(items[0], items[1]),
- 3 => ReadOnlyArray.Create(items[0], items[1], items[2]),
- 4 => ReadOnlyArray.Create(items[0], items[1], items[2], items[3]),
- _ => ReadOnlyArray.Create(items)
- };
-
- Assert.That(array.Length, Is.EqualTo(items.Length));
- Assert.That(array, Is.EquivalentTo(items));
- }
- #pragma warning restore IDE0303
- }
-
- [TestCase(0)]
- [TestCase(4)]
- [TestCase(9)]
- public void Create_Span(int length)
- {
- ReadOnlySpan span = Enumerable.Range(1, length).ToArray();
- var array = ReadOnlyArray.Create(span);
-
- Assert.That(array.Length, Is.EqualTo(length));
- Assert.That(array, Is.EquivalentTo(span.ToArray()));
- }
-
- [TestCase(0)]
- [TestCase(4)]
- [TestCase(9)]
- [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
- public void Create_Enumerable(int length)
- {
- var enumerable = Enumerable.Range(1, length);
- var array = ReadOnlyArray.Create(enumerable);
-
- Assert.That(array.Length, Is.EqualTo(length));
- Assert.That(array, Is.EquivalentTo(enumerable));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void Indexer(int count)
- {
- var array = Enumerable.Range(1, count).ToReadOnlyArray();
-
- Assert.That(array.Length, Is.EqualTo(count));
-
- for (var i = 0; i < array.Length; i++)
- Assert.That(array[i], Is.EqualTo(i + 1));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void GetEnumerator(int count)
- {
- var array = Enumerable.Range(1, count).ToReadOnlyArray();
-
- var x = 1;
- foreach (var v in array)
- Assert.That(v, Is.EqualTo(x++));
-
- Assert.That(array.Length, Is.EqualTo(count));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void GetEnumerator_Boxed(int count)
- {
- var array = Enumerable.Range(1, count).ToReadOnlyArray();
-
- var x = 1;
- foreach (var v in (IEnumerable)array)
- Assert.That(v, Is.EqualTo(x++));
-
- Assert.That(array.Length, Is.EqualTo(count));
- }
-
- [Test]
- public void Indexer_Ref()
- {
- var array = ReadOnlyArray.Create((Key: 1, Value: 1), (Key: 2, Value: 2));
-
- Assert.That(array[0].Key, Is.EqualTo(1));
- Assert.That(array[1].Key, Is.EqualTo(2));
-
- Unsafe.AsRef(in array[0]).Key = 3;
- Unsafe.AsRef(in array[1]).Key = 4;
-
- Assert.That(array[0].Key, Is.EqualTo(3));
- Assert.That(array[1].Key, Is.EqualTo(4));
- }
-
- [Test]
- public void Indexer_OutOfBounds()
- {
- Assert.Throws(() => _ = ReadOnlyArray.Create(1, 2, 3, 4)[5]);
- Assert.Throws(() => _ = ReadOnlyArray.Empty[5]);
- Assert.Throws(() => _ = ReadOnlyArray.Empty()[5]);
- }
-
- [Test]
- public void Empty()
- {
- Assert.That(ReadOnlyArray.Empty.Length, Is.Zero);
- Assert.That(ReadOnlyArray.Empty().Length, Is.Zero);
-
- Assert.That(ReadOnlyArray.Empty.IsDefault, Is.False);
- Assert.That(ReadOnlyArray.Empty.IsDefaultOrEmpty, Is.True);
- Assert.That(ReadOnlyArray.Empty().IsDefault, Is.False);
- Assert.That(ReadOnlyArray.Empty().IsDefaultOrEmpty, Is.True);
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void Slice_Index(string value, int index)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().Slice(index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Slice_Range(string value, int index, int length)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().Slice(index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase(1)]
- [TestCase(6)]
- [TestCase(-1)]
- public void Slice_InvalidIndex_ShouldThrow(int index)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().Slice(index),
- Throws.TypeOf());
- }
-
- [TestCase(1, 0)]
- [TestCase(0, 1)]
- [TestCase(6, 0)]
- [TestCase(0, 6)]
- [TestCase(5, 1)]
- [TestCase(4, 2)]
- [TestCase(-1, 0)]
- [TestCase(4, -1)]
- public void Slice_InvalidRange_ShouldThrow(int index, int length)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().Slice(index, length),
- Throws.TypeOf());
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void AsView_Index(string value, int index)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().AsView(index),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void AsView_Range(string value, int index, int length)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().AsView(index, length),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase(1)]
- [TestCase(6)]
- [TestCase(-1)]
- public void AsView_InvalidIndex_ShouldThrow(int index)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().AsView(index),
- Throws.TypeOf());
- }
-
- [TestCase(1, 0)]
- [TestCase(0, 1)]
- [TestCase(6, 0)]
- [TestCase(0, 6)]
- [TestCase(5, 1)]
- [TestCase(4, 2)]
- [TestCase(-1, 0)]
- [TestCase(4, -1)]
- public void AsView_InvalidRange_ShouldThrow(int index, int length)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().AsView(index, length),
- Throws.TypeOf());
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void AsSpan_Index(string value, int index)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().AsSpan(index).ToArray(),
- Is.EquivalentTo(value.AsSpan(index).ToArray()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void AsSpan_Range(string value, int index, int length)
- {
- Assert.That(
- value.ToCharArray().ToReadOnlyArray().AsSpan(index, length).ToArray(),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase(1)]
- [TestCase(6)]
- [TestCase(-1)]
- public void AsSpan_InvalidIndex_ShouldThrow(int index)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().AsSpan(index),
- Throws.TypeOf());
- }
-
- [TestCase(1, 0)]
- [TestCase(0, 1)]
- [TestCase(6, 0)]
- [TestCase(0, 6)]
- [TestCase(5, 1)]
- [TestCase(4, 2)]
- [TestCase(-1, 0)]
- [TestCase(4, -1)]
- public void AsSpan_InvalidRange_ShouldThrow(int index, int length)
- {
- Assert.That(
- () => Array.Empty().ToReadOnlyArray().AsSpan(index, length),
- Throws.TypeOf());
- }
-
- [TestCase(0)]
- [TestCase(3)]
- [TestCase(9)]
- public void GetPinnableReference(int count)
- {
- var items = new int[count];
- var array = ReadOnlyArray.UnsafeWrap(items);
-
- Assert.That(
- Unsafe.AreSame(
- ref Unsafe.AsRef(in array.GetPinnableReference()),
- ref Unsafe.AsRef(in MemoryMarshal.GetArrayDataReference(items))),
- Is.True);
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void CopyTo(int length)
- {
- var source = CreateArray(length).ToReadOnlyArray();
- var destination = new int[length];
-
- source.CopyTo(destination.AsSpan());
-
- Assert.That(source, Is.EquivalentTo(destination));
- }
-
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void CopyTo_InsufficientRoom_ShouldThrow(int length)
- {
- var source = CreateArray(length).ToReadOnlyArray();
- var destination = new int[length - 1];
-
- Assert.Throws(() => source.CopyTo(destination.AsSpan()));
- Assert.That(destination, Is.EquivalentTo(new int[length - 1]));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void TryCopyTo(int length)
- {
- var source = CreateArray(length).ToReadOnlyArray();
- var destination = new int[length];
-
- Assert.That(source.TryCopyTo(destination.AsSpan()), Is.True);
- Assert.That(source, Is.EquivalentTo(destination));
- }
-
- [TestCase(1)]
- [TestCase(5)]
- [TestCase(9)]
- public void TryCopyTo_InsufficientRoom_ShouldThrow(int length)
- {
- var source = CreateArray(length).ToReadOnlyArray();
- var destination = new int[length - 1];
-
- Assert.That(source.TryCopyTo(destination.AsSpan()), Is.False);
- Assert.That(destination, Is.EquivalentTo(new int[length - 1]));
- }
-
- [Test]
- public void UnsafeWrap()
- {
- var array = new int[10];
- var readonlyArray = ReadOnlyArray.UnsafeWrap(array);
-
- array[5] = 0xABCD;
- Assert.That(readonlyArray[5], Is.EqualTo(0xABCD));
- }
-
- [Test]
- [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
- public void ToReadOnlyArray_Enumerable()
- {
- var enumerable = Enumerable.Range(1, 100);
-
- Assert.That(
- enumerable.ToReadOnlyArray(),
- Is.EquivalentTo(enumerable));
- }
-
- [Test]
- public void ToReadOnlyArray_ImmutableArray()
- {
- var immutable = Enumerable.Range(1, 100).ToImmutableArray();
- var array = immutable.ToReadOnlyArray();
-
- Assert.That(
- Unsafe.AreSame(
- ref Unsafe.AsRef(in array[0]),
- ref Unsafe.AsRef(in immutable.ItemRef(0))),
- Is.True);
- }
-
- [Test]
- public void ToReadOnlyArray_Span()
- {
- var source = Enumerable.Range(1, 100).ToArray();
-
- Span span1 = source;
- ReadOnlySpan span2 = span1;
-
- var array1 = span1.ToReadOnlyArray();
- var array2 = span2.ToReadOnlyArray();
-
- Assert.That(array1, Is.EquivalentTo(source));
- Assert.That(array2, Is.EquivalentTo(source));
- }
-
- [Test]
- public void ToReadOnlyArray_Array()
- {
- var items = Enumerable.Range(1, 100).ToArray();
- var array = items.ToReadOnlyArray();
-
- Assert.That(array, Is.EquivalentTo(items));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- public void IndexOf(int length)
- {
- var array = CreateArray(length);
- var readonlyArray = array.ToReadOnlyArray();
-
- for (var i = 0; i <= length; i++)
- Assert.That(
- readonlyArray.IndexOf(i),
- Is.EqualTo(Array.IndexOf(array, i)));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- public void LastIndexOf(int length)
- {
- var array = CreateArray(length);
- var readonlyArray = array.ToReadOnlyArray();
-
- for (var i = 0; i <= length; i++)
- Assert.That(
- readonlyArray.LastIndexOf(i),
- Is.EqualTo(Array.LastIndexOf(array, i)));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- public void Contains(int length)
- {
- var array = CreateArray(length);
- var readonlyArray = array.ToReadOnlyArray();
-
- for (var i = -1; i <= length; i++)
- Assert.That(
- readonlyArray.Contains(i),
- Is.EqualTo(Array.LastIndexOf(array, i) >= 0));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- public void Interface_IEnumerable(int count)
- {
- var array = CreateArray(count);
- var collection = (IEnumerable)array.ToReadOnlyArray();
-
- var value = 0;
- foreach (var item in collection)
- Assert.That(item, Is.EqualTo(value++));
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- [SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")]
- public void Interface_IReadOnlyList(int count)
- {
- var array = CreateArray(count);
- var collection = (IReadOnlyList)array.ToReadOnlyArray();
-
- Assert.That(collection.Count, Is.EqualTo(count));
-
- var value = 0;
- for (var i = 0; i < collection.Count; i++)
- Assert.That(collection[i], Is.EqualTo(value++));
- }
-
- [TestCase(null)]
- [TestCase("")]
- [TestCase("test")]
- public void Operator_Conversions(string? value)
- {
- var array = ReadOnlyArray.UnsafeWrap(value?.ToCharArray()!);
-
- ArrayView view = array;
- ReadOnlyMemory memory = array;
- ReadOnlySpan span = array;
- ImmutableArray immutable = array;
- ReadOnlyArray @readonly = immutable;
-
- Assert.That(view.Length, Is.EqualTo(value?.Length ?? 0));
- Assert.That(span.Length, Is.EqualTo(value?.Length ?? 0));
- Assert.That(memory.Length, Is.EqualTo(value?.Length ?? 0));
-
- Assert.That(array.IsDefault, Is.EqualTo(immutable.IsDefault));
- Assert.That(array.IsDefault, Is.EqualTo(@readonly.IsDefault));
- Assert.That(array.IsDefaultOrEmpty, Is.EqualTo(immutable.IsDefaultOrEmpty));
- Assert.That(array.IsDefaultOrEmpty, Is.EqualTo(@readonly.IsDefaultOrEmpty));
-
- if (!array.IsDefault)
- {
- Assert.That(array.Length, Is.EqualTo(value!.Length));
- Assert.That(immutable.Length, Is.EqualTo(value.Length));
- Assert.That(@readonly.Length, Is.EqualTo(value.Length));
- }
-
- if (view.Length != 0)
- {
- Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in view[0])), Is.True);
- Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in span[0])), Is.True);
- Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in memory.Span[0])), Is.True);
- Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in immutable.ItemRef(0))), Is.True);
- Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in @readonly[0])), Is.True);
- }
- }
-
- [TestCase(0)]
- [TestCase(1)]
- [TestCase(9)]
- public void Linq_ToList(int count)
- {
- var array = CreateArray(count).ToReadOnlyArray();
- var list = array.ToList();
-
- Assert.That(list.Count, Is.EqualTo(count));
- Assert.That(list.Capacity, Is.EqualTo(count));
- Assert.That(list, Is.EqualTo(array));
- }
-
- private static int[] CreateArray(int length) =>
- Enumerable.Range(0, length).ToArray();
-}
+using System.Collections.Immutable;
+
+namespace Ramstack.Collections;
+
+[TestFixture]
+public class ReadOnlyArrayTests
+{
+ [Test]
+ [SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
+ public void Default_BehavesAsUnderlyingArray()
+ {
+ var array = new ReadOnlyArray();
+
+ Assert.That(array.IsDefault, Is.True);
+
+ Assert.That(() => array.AsSpan(), Throws.Nothing);
+ Assert.That(() => array.AsSpan(0), Throws.Nothing);
+ Assert.That(() => array.AsSpan(0, 0), Throws.Nothing);
+ Assert.That(() => array.AsView(), Throws.Nothing);
+ Assert.That(() => array.AsView(0), Throws.Nothing);
+ Assert.That(() => array.AsView(0, 0), Throws.Nothing);
+ Assert.That(() => array.GetHashCode(), Throws.Nothing);
+
+ Assert.Throws(() => _ = array.Length);
+ Assert.Throws(() => _ = array[0]);
+ Assert.Throws(() => array.GetEnumerator());
+ Assert.Throws(() => array.GetPinnableReference());
+ Assert.Throws(() => array.Any());
+ Assert.Throws(() => array.Any(_ => true));
+ Assert.Throws(() => array.All(_ => true));
+ Assert.Throws(() => array.ToArray());
+ Assert.Throws(() => array.Slice(0));
+ Assert.Throws(() => array.Slice(0, 0));
+
+ Assert.Throws(() => array.IndexOf(0));
+ Assert.Throws(() => array.LastIndexOf(0));
+ }
+
+ [Test]
+ [SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")]
+ public void Default_Boxed()
+ {
+ var array = (IReadOnlyList)new ReadOnlyArray();
+
+ Assert.Throws(() => _ = array.Count);
+ Assert.Throws(() => _ = array[0]);
+ Assert.Throws(() => array.GetEnumerator());
+ }
+
+ [Test]
+ public void Ctor()
+ {
+ for (var count = 1; count < 10; count++)
+ {
+ var items = CreateArray(count);
+ var array = count switch
+ {
+ 1 => new ReadOnlyArray(items[0]),
+ 2 => new ReadOnlyArray(items[0], items[1]),
+ 3 => new ReadOnlyArray(items[0], items[1], items[2]),
+ 4 => new ReadOnlyArray(items[0], items[1], items[2], items[3]),
+ _ => new ReadOnlyArray(items)
+ };
+
+ Assert.That(array.Length, Is.EqualTo(items.Length));
+ Assert.That(array, Is.EquivalentTo(items));
+ }
+ }
+
+ [TestCase(0)]
+ [TestCase(4)]
+ [TestCase(9)]
+ public void Ctor_Span(int length)
+ {
+ ReadOnlySpan span = CreateArray(length);
+ var array = ReadOnlyArray.Create(span);
+
+ Assert.That(array.Length, Is.EqualTo(length));
+ Assert.That(array, Is.EquivalentTo(span.ToArray()));
+ }
+
+ [Test]
+ public void Create()
+ {
+ #pragma warning disable IDE0303
+ for (var count = 1; count < 10; count++)
+ {
+ var items = Enumerable.Range(1, count).ToArray();
+ var array = count switch
+ {
+ 1 => ReadOnlyArray.Create(items[0]),
+ 2 => ReadOnlyArray.Create(items[0], items[1]),
+ 3 => ReadOnlyArray.Create(items[0], items[1], items[2]),
+ 4 => ReadOnlyArray.Create(items[0], items[1], items[2], items[3]),
+ _ => ReadOnlyArray.Create(items)
+ };
+
+ Assert.That(array.Length, Is.EqualTo(items.Length));
+ Assert.That(array, Is.EquivalentTo(items));
+ }
+ #pragma warning restore IDE0303
+ }
+
+ [TestCase(0)]
+ [TestCase(4)]
+ [TestCase(9)]
+ public void Create_Span(int length)
+ {
+ ReadOnlySpan span = Enumerable.Range(1, length).ToArray();
+ var array = ReadOnlyArray.Create(span);
+
+ Assert.That(array.Length, Is.EqualTo(length));
+ Assert.That(array, Is.EquivalentTo(span.ToArray()));
+ }
+
+ [TestCase(0)]
+ [TestCase(4)]
+ [TestCase(9)]
+ [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
+ public void Create_Enumerable(int length)
+ {
+ var enumerable = Enumerable.Range(1, length);
+ var array = ReadOnlyArray.Create(enumerable);
+
+ Assert.That(array.Length, Is.EqualTo(length));
+ Assert.That(array, Is.EquivalentTo(enumerable));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void Indexer(int count)
+ {
+ var array = Enumerable.Range(1, count).ToReadOnlyArray();
+
+ Assert.That(array.Length, Is.EqualTo(count));
+
+ for (var i = 0; i < array.Length; i++)
+ Assert.That(array[i], Is.EqualTo(i + 1));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void GetEnumerator(int count)
+ {
+ var array = Enumerable.Range(1, count).ToReadOnlyArray();
+
+ var x = 1;
+ foreach (var v in array)
+ Assert.That(v, Is.EqualTo(x++));
+
+ Assert.That(array.Length, Is.EqualTo(count));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void GetEnumerator_Boxed(int count)
+ {
+ var array = Enumerable.Range(1, count).ToReadOnlyArray();
+
+ var x = 1;
+ foreach (var v in (IEnumerable)array)
+ Assert.That(v, Is.EqualTo(x++));
+
+ Assert.That(array.Length, Is.EqualTo(count));
+ }
+
+ [Test]
+ public void Indexer_Ref()
+ {
+ var array = ReadOnlyArray.Create((Key: 1, Value: 1), (Key: 2, Value: 2));
+
+ Assert.That(array[0].Key, Is.EqualTo(1));
+ Assert.That(array[1].Key, Is.EqualTo(2));
+
+ Unsafe.AsRef(in array[0]).Key = 3;
+ Unsafe.AsRef(in array[1]).Key = 4;
+
+ Assert.That(array[0].Key, Is.EqualTo(3));
+ Assert.That(array[1].Key, Is.EqualTo(4));
+ }
+
+ [Test]
+ public void Indexer_OutOfBounds()
+ {
+ Assert.Throws(() => _ = ReadOnlyArray.Create(1, 2, 3, 4)[5]);
+ Assert.Throws(() => _ = ReadOnlyArray.Empty[5]);
+ Assert.Throws(() => _ = ReadOnlyArray.Empty()[5]);
+ }
+
+ [Test]
+ public void Empty()
+ {
+ Assert.That(ReadOnlyArray.Empty.Length, Is.Zero);
+ Assert.That(ReadOnlyArray.Empty().Length, Is.Zero);
+
+ Assert.That(ReadOnlyArray.Empty.IsDefault, Is.False);
+ Assert.That(ReadOnlyArray.Empty.IsDefaultOrEmpty, Is.True);
+ Assert.That(ReadOnlyArray.Empty().IsDefault, Is.False);
+ Assert.That(ReadOnlyArray.Empty().IsDefaultOrEmpty, Is.True);
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void Slice_Index(string value, int index)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().Slice(index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Slice_Range(string value, int index, int length)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().Slice(index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase(1)]
+ [TestCase(6)]
+ [TestCase(-1)]
+ public void Slice_InvalidIndex_ShouldThrow(int index)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().Slice(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase(1, 0)]
+ [TestCase(0, 1)]
+ [TestCase(6, 0)]
+ [TestCase(0, 6)]
+ [TestCase(5, 1)]
+ [TestCase(4, 2)]
+ [TestCase(-1, 0)]
+ [TestCase(4, -1)]
+ public void Slice_InvalidRange_ShouldThrow(int index, int length)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().Slice(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void AsView_Index(string value, int index)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().AsView(index),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void AsView_Range(string value, int index, int length)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().AsView(index, length),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase(1)]
+ [TestCase(6)]
+ [TestCase(-1)]
+ public void AsView_InvalidIndex_ShouldThrow(int index)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().AsView(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase(1, 0)]
+ [TestCase(0, 1)]
+ [TestCase(6, 0)]
+ [TestCase(0, 6)]
+ [TestCase(5, 1)]
+ [TestCase(4, 2)]
+ [TestCase(-1, 0)]
+ [TestCase(4, -1)]
+ public void AsView_InvalidRange_ShouldThrow(int index, int length)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().AsView(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void AsSpan_Index(string value, int index)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().AsSpan(index).ToArray(),
+ Is.EquivalentTo(value.AsSpan(index).ToArray()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void AsSpan_Range(string value, int index, int length)
+ {
+ Assert.That(
+ value.ToCharArray().ToReadOnlyArray().AsSpan(index, length).ToArray(),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase(1)]
+ [TestCase(6)]
+ [TestCase(-1)]
+ public void AsSpan_InvalidIndex_ShouldThrow(int index)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().AsSpan(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase(1, 0)]
+ [TestCase(0, 1)]
+ [TestCase(6, 0)]
+ [TestCase(0, 6)]
+ [TestCase(5, 1)]
+ [TestCase(4, 2)]
+ [TestCase(-1, 0)]
+ [TestCase(4, -1)]
+ public void AsSpan_InvalidRange_ShouldThrow(int index, int length)
+ {
+ Assert.That(
+ () => Array.Empty().ToReadOnlyArray().AsSpan(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase(0)]
+ [TestCase(3)]
+ [TestCase(9)]
+ public void GetPinnableReference(int count)
+ {
+ var items = new int[count];
+ var array = ReadOnlyArray.UnsafeWrap(items);
+
+ Assert.That(
+ Unsafe.AreSame(
+ ref Unsafe.AsRef(in array.GetPinnableReference()),
+ ref Unsafe.AsRef(in MemoryMarshal.GetArrayDataReference(items))),
+ Is.True);
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void CopyTo(int length)
+ {
+ var source = CreateArray(length).ToReadOnlyArray();
+ var destination = new int[length];
+
+ source.CopyTo(destination.AsSpan());
+
+ Assert.That(source, Is.EquivalentTo(destination));
+ }
+
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void CopyTo_InsufficientRoom_ShouldThrow(int length)
+ {
+ var source = CreateArray(length).ToReadOnlyArray();
+ var destination = new int[length - 1];
+
+ Assert.Throws(() => source.CopyTo(destination.AsSpan()));
+ Assert.That(destination, Is.EquivalentTo(new int[length - 1]));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void TryCopyTo(int length)
+ {
+ var source = CreateArray(length).ToReadOnlyArray();
+ var destination = new int[length];
+
+ Assert.That(source.TryCopyTo(destination.AsSpan()), Is.True);
+ Assert.That(source, Is.EquivalentTo(destination));
+ }
+
+ [TestCase(1)]
+ [TestCase(5)]
+ [TestCase(9)]
+ public void TryCopyTo_InsufficientRoom_ShouldThrow(int length)
+ {
+ var source = CreateArray(length).ToReadOnlyArray();
+ var destination = new int[length - 1];
+
+ Assert.That(source.TryCopyTo(destination.AsSpan()), Is.False);
+ Assert.That(destination, Is.EquivalentTo(new int[length - 1]));
+ }
+
+ [Test]
+ public void UnsafeWrap()
+ {
+ var array = new int[10];
+ var readonlyArray = ReadOnlyArray.UnsafeWrap(array);
+
+ array[5] = 0xABCD;
+ Assert.That(readonlyArray[5], Is.EqualTo(0xABCD));
+ }
+
+ [Test]
+ [SuppressMessage("ReSharper", "PossibleMultipleEnumeration")]
+ public void ToReadOnlyArray_Enumerable()
+ {
+ var enumerable = Enumerable.Range(1, 100);
+
+ Assert.That(
+ enumerable.ToReadOnlyArray(),
+ Is.EquivalentTo(enumerable));
+ }
+
+ [Test]
+ public void ToReadOnlyArray_ImmutableArray()
+ {
+ var immutable = Enumerable.Range(1, 100).ToImmutableArray();
+ var array = immutable.ToReadOnlyArray();
+
+ Assert.That(
+ Unsafe.AreSame(
+ ref Unsafe.AsRef(in array[0]),
+ ref Unsafe.AsRef(in immutable.ItemRef(0))),
+ Is.True);
+ }
+
+ [Test]
+ public void ToReadOnlyArray_Span()
+ {
+ var source = Enumerable.Range(1, 100).ToArray();
+
+ Span span1 = source;
+ ReadOnlySpan span2 = span1;
+
+ var array1 = span1.ToReadOnlyArray();
+ var array2 = span2.ToReadOnlyArray();
+
+ Assert.That(array1, Is.EquivalentTo(source));
+ Assert.That(array2, Is.EquivalentTo(source));
+ }
+
+ [Test]
+ public void ToReadOnlyArray_Array()
+ {
+ var items = Enumerable.Range(1, 100).ToArray();
+ var array = items.ToReadOnlyArray();
+
+ Assert.That(array, Is.EquivalentTo(items));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ public void IndexOf(int length)
+ {
+ var array = CreateArray(length);
+ var readonlyArray = array.ToReadOnlyArray();
+
+ for (var i = 0; i <= length; i++)
+ Assert.That(
+ readonlyArray.IndexOf(i),
+ Is.EqualTo(Array.IndexOf(array, i)));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ public void LastIndexOf(int length)
+ {
+ var array = CreateArray(length);
+ var readonlyArray = array.ToReadOnlyArray();
+
+ for (var i = 0; i <= length; i++)
+ Assert.That(
+ readonlyArray.LastIndexOf(i),
+ Is.EqualTo(Array.LastIndexOf(array, i)));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ public void Contains(int length)
+ {
+ var array = CreateArray(length);
+ var readonlyArray = array.ToReadOnlyArray();
+
+ for (var i = -1; i <= length; i++)
+ Assert.That(
+ readonlyArray.Contains(i),
+ Is.EqualTo(Array.LastIndexOf(array, i) >= 0));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ public void Interface_IEnumerable(int count)
+ {
+ var array = CreateArray(count);
+ var collection = (IEnumerable)array.ToReadOnlyArray();
+
+ var value = 0;
+ foreach (var item in collection)
+ Assert.That(item, Is.EqualTo(value++));
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ [SuppressMessage("ReSharper", "ForCanBeConvertedToForeach")]
+ public void Interface_IReadOnlyList(int count)
+ {
+ var array = CreateArray(count);
+ var collection = (IReadOnlyList)array.ToReadOnlyArray();
+
+ Assert.That(collection.Count, Is.EqualTo(count));
+
+ var value = 0;
+ for (var i = 0; i < collection.Count; i++)
+ Assert.That(collection[i], Is.EqualTo(value++));
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("test")]
+ public void Operator_Conversions(string? value)
+ {
+ var array = ReadOnlyArray.UnsafeWrap(value?.ToCharArray()!);
+
+ ArrayView view = array;
+ ReadOnlyMemory memory = array;
+ ReadOnlySpan span = array;
+ ImmutableArray immutable = array;
+ ReadOnlyArray @readonly = immutable;
+
+ Assert.That(view.Length, Is.EqualTo(value?.Length ?? 0));
+ Assert.That(span.Length, Is.EqualTo(value?.Length ?? 0));
+ Assert.That(memory.Length, Is.EqualTo(value?.Length ?? 0));
+
+ Assert.That(array.IsDefault, Is.EqualTo(immutable.IsDefault));
+ Assert.That(array.IsDefault, Is.EqualTo(@readonly.IsDefault));
+ Assert.That(array.IsDefaultOrEmpty, Is.EqualTo(immutable.IsDefaultOrEmpty));
+ Assert.That(array.IsDefaultOrEmpty, Is.EqualTo(@readonly.IsDefaultOrEmpty));
+
+ if (!array.IsDefault)
+ {
+ Assert.That(array.Length, Is.EqualTo(value!.Length));
+ Assert.That(immutable.Length, Is.EqualTo(value.Length));
+ Assert.That(@readonly.Length, Is.EqualTo(value.Length));
+ }
+
+ if (view.Length != 0)
+ {
+ Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in view[0])), Is.True);
+ Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in span[0])), Is.True);
+ Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in memory.Span[0])), Is.True);
+ Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in immutable.ItemRef(0))), Is.True);
+ Assert.That(Unsafe.AreSame(ref Unsafe.AsRef(in array[0]), ref Unsafe.AsRef(in @readonly[0])), Is.True);
+ }
+ }
+
+ [TestCase(0)]
+ [TestCase(1)]
+ [TestCase(9)]
+ public void Linq_ToList(int count)
+ {
+ var array = CreateArray(count).ToReadOnlyArray();
+ var list = array.ToList();
+
+ Assert.That(list.Count, Is.EqualTo(count));
+ Assert.That(list.Capacity, Is.EqualTo(count));
+ Assert.That(list, Is.EqualTo(array));
+ }
+
+ private static int[] CreateArray(int length) =>
+ Enumerable.Range(0, length).ToArray();
+}
diff --git a/Ramstack.Structures.Tests/InteropServices/MemoryMarshalTests.cs b/Ramstack.Structures.Tests/InteropServices/MemoryMarshalTests.cs
index 88e2b72..97ca14f 100644
--- a/Ramstack.Structures.Tests/InteropServices/MemoryMarshalTests.cs
+++ b/Ramstack.Structures.Tests/InteropServices/MemoryMarshalTests.cs
@@ -1,142 +1,142 @@
-namespace Ramstack.InteropServices;
-
-[TestFixture]
-public class MemoryMarshalTests
-{
- [Test]
- public void CreateStringView()
- {
- Assert.That(
- MemoryMarshal.CreateStringView("Hello, World!", 7).ToString(),
- Is.EqualTo("World!"));
-
- Assert.That(
- MemoryMarshal.CreateStringView("Hello, World!", 7).Length,
- Is.EqualTo(6));
-
- Assert.That(
- MemoryMarshal.CreateStringView("Hello, World!", 0, 5).ToString(),
- Is.EqualTo("Hello"));
-
- Assert.That(
- MemoryMarshal.CreateStringView("Hello, World!", 0, 5).Length,
- Is.EqualTo(5));
- }
-
- [Test]
- public void CreateArrayView()
- {
- var array = "Hello, World!".ToArray();
-
- Assert.That(
- MemoryMarshal.CreateArrayView(array, 7).AsSpan().ToString(),
- Is.EqualTo("World!"));
-
- Assert.That(
- MemoryMarshal.CreateArrayView(array, 7).Length,
- Is.EqualTo(6));
-
- Assert.That(
- MemoryMarshal.CreateArrayView(array, 0, 5).AsSpan().ToString(),
- Is.EqualTo("Hello"));
-
- Assert.That(
- MemoryMarshal.CreateArrayView(array, 0, 5).Length,
- Is.EqualTo(5));
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetStringView()
- {
- const string Input = "0123456789";
-
- var memory = Input.AsMemory();
- Assert.That(memory.IsEmpty, Is.False);
-
- Assert.That(MemoryMarshal.TryGetStringView(memory, out var view), Is.True);
- Assert.That(view, Is.EqualTo(Input));
-
- memory = memory[1..];
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view, Is.EqualTo(Input[1..]));
- Assert.That(view.Length, Is.EqualTo(Input.Length - 1));
-
- memory = memory[1..];
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view, Is.EqualTo(Input[2..]));
- Assert.That(view.Length, Is.EqualTo(Input.Length - 2));
-
- memory = memory.Slice(3, 2);
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view, Is.EqualTo(Input[5..7]));
- Assert.That(view.Length, Is.EqualTo(2));
-
- memory = memory[memory.Length..];
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view.Length, Is.Zero);
-
- memory = memory[..];
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view.Length, Is.Zero);
-
- memory = memory[..0];
- Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
- Assert.That(view.Length, Is.Zero);
-
- Assert.That(memory.IsEmpty, Is.True);
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetStringView_Array_ReturnsFalse()
- {
- var memory = new char[10].AsMemory();
- Assert.That(MemoryMarshal.TryGetStringView(memory, out var view), Is.False);
- Assert.That(view.IsDefault, Is.True);
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetArrayView()
- {
- var buffer = Enumerable.Range(0, 10).ToArray();
- var memory = buffer.AsMemory();
-
- Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
- Assert.That(view, Is.EquivalentTo(buffer));
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetArrayView_EmptyArray()
- {
- var buffer = Array.Empty();
- var memory = buffer.AsMemory();
-
- Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
- Assert.That(view, Is.EquivalentTo(buffer));
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetArrayView_Default()
- {
- var memory = default(ReadOnlyMemory);
-
- Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
- Assert.That(view, Is.EquivalentTo(Array.Empty()));
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetArrayView_Empty()
- {
- var memory = ReadOnlyMemory.Empty;
-
- Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
- Assert.That(view, Is.EquivalentTo(Array.Empty()));
- }
-
- [Test]
- public static void ReadOnlyMemory_TryGetArrayView_String_ReturnsFalse()
- {
- var memory = "0123456789".AsMemory();
- Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.False);
- Assert.That(view.IsDefault, Is.True);
- }
-}
+namespace Ramstack.InteropServices;
+
+[TestFixture]
+public class MemoryMarshalTests
+{
+ [Test]
+ public void CreateStringView()
+ {
+ Assert.That(
+ MemoryMarshal.CreateStringView("Hello, World!", 7).ToString(),
+ Is.EqualTo("World!"));
+
+ Assert.That(
+ MemoryMarshal.CreateStringView("Hello, World!", 7).Length,
+ Is.EqualTo(6));
+
+ Assert.That(
+ MemoryMarshal.CreateStringView("Hello, World!", 0, 5).ToString(),
+ Is.EqualTo("Hello"));
+
+ Assert.That(
+ MemoryMarshal.CreateStringView("Hello, World!", 0, 5).Length,
+ Is.EqualTo(5));
+ }
+
+ [Test]
+ public void CreateArrayView()
+ {
+ var array = "Hello, World!".ToArray();
+
+ Assert.That(
+ MemoryMarshal.CreateArrayView(array, 7).AsSpan().ToString(),
+ Is.EqualTo("World!"));
+
+ Assert.That(
+ MemoryMarshal.CreateArrayView(array, 7).Length,
+ Is.EqualTo(6));
+
+ Assert.That(
+ MemoryMarshal.CreateArrayView(array, 0, 5).AsSpan().ToString(),
+ Is.EqualTo("Hello"));
+
+ Assert.That(
+ MemoryMarshal.CreateArrayView(array, 0, 5).Length,
+ Is.EqualTo(5));
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetStringView()
+ {
+ const string Input = "0123456789";
+
+ var memory = Input.AsMemory();
+ Assert.That(memory.IsEmpty, Is.False);
+
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out var view), Is.True);
+ Assert.That(view, Is.EqualTo(Input));
+
+ memory = memory[1..];
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view, Is.EqualTo(Input[1..]));
+ Assert.That(view.Length, Is.EqualTo(Input.Length - 1));
+
+ memory = memory[1..];
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view, Is.EqualTo(Input[2..]));
+ Assert.That(view.Length, Is.EqualTo(Input.Length - 2));
+
+ memory = memory.Slice(3, 2);
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view, Is.EqualTo(Input[5..7]));
+ Assert.That(view.Length, Is.EqualTo(2));
+
+ memory = memory[memory.Length..];
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view.Length, Is.Zero);
+
+ memory = memory[..];
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view.Length, Is.Zero);
+
+ memory = memory[..0];
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out view), Is.True);
+ Assert.That(view.Length, Is.Zero);
+
+ Assert.That(memory.IsEmpty, Is.True);
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetStringView_Array_ReturnsFalse()
+ {
+ var memory = new char[10].AsMemory();
+ Assert.That(MemoryMarshal.TryGetStringView(memory, out var view), Is.False);
+ Assert.That(view.IsDefault, Is.True);
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetArrayView()
+ {
+ var buffer = Enumerable.Range(0, 10).ToArray();
+ var memory = buffer.AsMemory();
+
+ Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
+ Assert.That(view, Is.EquivalentTo(buffer));
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetArrayView_EmptyArray()
+ {
+ var buffer = Array.Empty();
+ var memory = buffer.AsMemory();
+
+ Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
+ Assert.That(view, Is.EquivalentTo(buffer));
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetArrayView_Default()
+ {
+ var memory = default(ReadOnlyMemory);
+
+ Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
+ Assert.That(view, Is.EquivalentTo(Array.Empty()));
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetArrayView_Empty()
+ {
+ var memory = ReadOnlyMemory.Empty;
+
+ Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.True);
+ Assert.That(view, Is.EquivalentTo(Array.Empty()));
+ }
+
+ [Test]
+ public static void ReadOnlyMemory_TryGetArrayView_String_ReturnsFalse()
+ {
+ var memory = "0123456789".AsMemory();
+ Assert.That(MemoryMarshal.TryGetArrayView(memory, out var view), Is.False);
+ Assert.That(view.IsDefault, Is.True);
+ }
+}
diff --git a/Ramstack.Structures.Tests/Ramstack.Structures.Tests.csproj b/Ramstack.Structures.Tests/Ramstack.Structures.Tests.csproj
index 2835311..64da15d 100644
--- a/Ramstack.Structures.Tests/Ramstack.Structures.Tests.csproj
+++ b/Ramstack.Structures.Tests/Ramstack.Structures.Tests.csproj
@@ -1,32 +1,32 @@
-
-
- net6.0;net7.0;net8.0;net9.0
- enable
- enable
- preview
- true
- true
- Ramstack
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+ net6.0;net7.0;net8.0;net9.0
+ enable
+ enable
+ preview
+ true
+ true
+ Ramstack
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Ramstack.Structures.Tests/Text/StringViewComparerTests.cs b/Ramstack.Structures.Tests/Text/StringViewComparerTests.cs
index 97a95fa..a68cfbd 100644
--- a/Ramstack.Structures.Tests/Text/StringViewComparerTests.cs
+++ b/Ramstack.Structures.Tests/Text/StringViewComparerTests.cs
@@ -1,169 +1,169 @@
-namespace Ramstack.Text;
-
-[TestFixture]
-public class StringViewComparerTests
-{
- [Test]
- public void Equals_Ordinal()
- {
- var comparer = StringViewComparer.Ordinal;
-
- Assert.That(comparer.Equals("test", "test"), Is.True);
- Assert.That(comparer.Equals("test", "Test"), Is.False);
- }
-
- [Test]
- public void Equals_OrdinalIgnoreCase()
- {
- var comparer = StringViewComparer.OrdinalIgnoreCase;
- Assert.That(comparer.Equals("test", "Test"), Is.True);
- }
-
- [Test]
- public void Equals_CultureCulture()
- {
- var comparer = StringViewComparer.CurrentCulture;
- Assert.That(comparer.Equals("test", "test"), Is.True);
- Assert.That(comparer.Equals("test", "Test"), Is.False);
- }
-
- [Test]
- public void Equals_CultureCultureIgnoreCase()
- {
- var comparer = StringViewComparer.CurrentCultureIgnoreCase;
- Assert.That(comparer.Equals("test", "Test"), Is.True);
- }
-
- [Test]
- public void Equals_InvariantCulture()
- {
- var comparer = StringViewComparer.InvariantCulture;
- Assert.That(comparer.Equals("test", "test"), Is.True);
- Assert.That(comparer.Equals("test", "Test"), Is.False);
- }
-
- [Test]
- public void Equals_InvariantCultureIgnoreCase()
- {
- var comparer = StringViewComparer.CurrentCultureIgnoreCase;
- Assert.That(comparer.Equals("test", "Test"), Is.True);
- }
-
- [Test]
- public void Compare_Ordinal()
- {
- var comparer = StringViewComparer.Ordinal;
- Assert.That(comparer.Compare("test", "test"), Is.Zero);
- Assert.That(comparer.Compare("test", "Test"), Is.Not.Zero);
- }
-
- [Test]
- public void Compare_OrdinalIgnoreCase()
- {
- var comparer = StringViewComparer.OrdinalIgnoreCase;
- Assert.That(comparer.Compare("test", "Test"), Is.Zero);
- }
-
- [Test]
- public void FromComparison_Ordinal()
- {
- var comparer = StringViewComparer.FromComparison(StringComparison.Ordinal);
- Assert.That(comparer, Is.SameAs(StringViewComparer.Ordinal));
- }
-
- [Test]
- public void FromComparison_OrdinalIgnoreCase()
- {
- var comparer = StringViewComparer.FromComparison(StringComparison.OrdinalIgnoreCase);
- Assert.That(comparer, Is.SameAs(StringViewComparer.OrdinalIgnoreCase));
- }
-
- [Test]
- public void FromComparison_CurrentCulture()
- {
- var comparer = StringViewComparer.CurrentCulture;
- Assert.That(comparer.Equals("test", "test"), Is.True);
- Assert.That(comparer.Equals("test", "Test"), Is.False);
- }
-
- [Test]
- public void FromComparison_CurrentCultureIgnoreCase()
- {
- var comparer = StringViewComparer.CurrentCultureIgnoreCase;
- Assert.That(comparer.Equals("test", "Test"), Is.True);
- }
-
- [Test]
- public void FromComparison_InvariantCulture()
- {
- var comparer = StringViewComparer.FromComparison(StringComparison.InvariantCulture);
- Assert.That(comparer, Is.SameAs(StringViewComparer.InvariantCulture));
- }
-
- [Test]
- public void FromComparison_InvariantCultureIgnoreCase()
- {
- var comparer = StringViewComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase);
- Assert.That(comparer, Is.SameAs(StringViewComparer.InvariantCultureIgnoreCase));
- }
-
- [Test]
- public void FromComparison_InvalidComparison()
- {
- Assert.Throws(
- () => StringViewComparer.FromComparison((StringComparison)999));
- }
-
- [Test]
- public void Create()
- {
- var c1 = StringViewComparer.Create(CultureInfo.InvariantCulture, false);
- var c2 = StringViewComparer.Create(CultureInfo.InvariantCulture, CompareOptions.None);
-
- Assert.That(c1.Equals("test", "test"), Is.True);
- Assert.That(c2.Equals("test", "test"), Is.True);
-
- Assert.That(c1.Equals("test", "TEST"), Is.False);
- Assert.That(c2.Equals("test", "TEST"), Is.False);
- }
-
- [Test]
- public void Create_IgnoreCase()
- {
- var c1 = StringViewComparer.Create(CultureInfo.InvariantCulture, true);
- var c2 = StringViewComparer.Create(CultureInfo.InvariantCulture, CompareOptions.IgnoreCase);
-
- Assert.That(c1.Equals("test", "TEST"), Is.True);
- Assert.That(c2.Equals("test", "TEST"), Is.True);
- }
-
- #if NET9_0_OR_GREATER
- [Test]
- public void AlternateComparer_Ordinal()
- {
- var dictionary = new Dictionary(StringViewComparer.Ordinal)
- {
- ["test"] = 1
- };
-
- var lookup = dictionary.GetAlternateLookup>();
- Assert.That(lookup.TryGetValue("TEST", out _), Is.False);
- Assert.That(lookup.TryGetValue("test", out var v), Is.True);
- Assert.That(v, Is.EqualTo(1));
- }
-
- [Test]
- public void AlternateComparer_OrdinalIgnoreCase()
- {
- var dictionary = new Dictionary(StringViewComparer.OrdinalIgnoreCase)
- {
- ["TEST"] = 1
- };
-
- var lookup = dictionary.GetAlternateLookup>();
- Assert.That(lookup.TryGetValue("text", out _), Is.False);
- Assert.That(lookup.TryGetValue("test", out var v), Is.True);
- Assert.That(v, Is.EqualTo(1));
- }
- #endif
-}
+namespace Ramstack.Text;
+
+[TestFixture]
+public class StringViewComparerTests
+{
+ [Test]
+ public void Equals_Ordinal()
+ {
+ var comparer = StringViewComparer.Ordinal;
+
+ Assert.That(comparer.Equals("test", "test"), Is.True);
+ Assert.That(comparer.Equals("test", "Test"), Is.False);
+ }
+
+ [Test]
+ public void Equals_OrdinalIgnoreCase()
+ {
+ var comparer = StringViewComparer.OrdinalIgnoreCase;
+ Assert.That(comparer.Equals("test", "Test"), Is.True);
+ }
+
+ [Test]
+ public void Equals_CultureCulture()
+ {
+ var comparer = StringViewComparer.CurrentCulture;
+ Assert.That(comparer.Equals("test", "test"), Is.True);
+ Assert.That(comparer.Equals("test", "Test"), Is.False);
+ }
+
+ [Test]
+ public void Equals_CultureCultureIgnoreCase()
+ {
+ var comparer = StringViewComparer.CurrentCultureIgnoreCase;
+ Assert.That(comparer.Equals("test", "Test"), Is.True);
+ }
+
+ [Test]
+ public void Equals_InvariantCulture()
+ {
+ var comparer = StringViewComparer.InvariantCulture;
+ Assert.That(comparer.Equals("test", "test"), Is.True);
+ Assert.That(comparer.Equals("test", "Test"), Is.False);
+ }
+
+ [Test]
+ public void Equals_InvariantCultureIgnoreCase()
+ {
+ var comparer = StringViewComparer.CurrentCultureIgnoreCase;
+ Assert.That(comparer.Equals("test", "Test"), Is.True);
+ }
+
+ [Test]
+ public void Compare_Ordinal()
+ {
+ var comparer = StringViewComparer.Ordinal;
+ Assert.That(comparer.Compare("test", "test"), Is.Zero);
+ Assert.That(comparer.Compare("test", "Test"), Is.Not.Zero);
+ }
+
+ [Test]
+ public void Compare_OrdinalIgnoreCase()
+ {
+ var comparer = StringViewComparer.OrdinalIgnoreCase;
+ Assert.That(comparer.Compare("test", "Test"), Is.Zero);
+ }
+
+ [Test]
+ public void FromComparison_Ordinal()
+ {
+ var comparer = StringViewComparer.FromComparison(StringComparison.Ordinal);
+ Assert.That(comparer, Is.SameAs(StringViewComparer.Ordinal));
+ }
+
+ [Test]
+ public void FromComparison_OrdinalIgnoreCase()
+ {
+ var comparer = StringViewComparer.FromComparison(StringComparison.OrdinalIgnoreCase);
+ Assert.That(comparer, Is.SameAs(StringViewComparer.OrdinalIgnoreCase));
+ }
+
+ [Test]
+ public void FromComparison_CurrentCulture()
+ {
+ var comparer = StringViewComparer.CurrentCulture;
+ Assert.That(comparer.Equals("test", "test"), Is.True);
+ Assert.That(comparer.Equals("test", "Test"), Is.False);
+ }
+
+ [Test]
+ public void FromComparison_CurrentCultureIgnoreCase()
+ {
+ var comparer = StringViewComparer.CurrentCultureIgnoreCase;
+ Assert.That(comparer.Equals("test", "Test"), Is.True);
+ }
+
+ [Test]
+ public void FromComparison_InvariantCulture()
+ {
+ var comparer = StringViewComparer.FromComparison(StringComparison.InvariantCulture);
+ Assert.That(comparer, Is.SameAs(StringViewComparer.InvariantCulture));
+ }
+
+ [Test]
+ public void FromComparison_InvariantCultureIgnoreCase()
+ {
+ var comparer = StringViewComparer.FromComparison(StringComparison.InvariantCultureIgnoreCase);
+ Assert.That(comparer, Is.SameAs(StringViewComparer.InvariantCultureIgnoreCase));
+ }
+
+ [Test]
+ public void FromComparison_InvalidComparison()
+ {
+ Assert.Throws(
+ () => StringViewComparer.FromComparison((StringComparison)999));
+ }
+
+ [Test]
+ public void Create()
+ {
+ var c1 = StringViewComparer.Create(CultureInfo.InvariantCulture, false);
+ var c2 = StringViewComparer.Create(CultureInfo.InvariantCulture, CompareOptions.None);
+
+ Assert.That(c1.Equals("test", "test"), Is.True);
+ Assert.That(c2.Equals("test", "test"), Is.True);
+
+ Assert.That(c1.Equals("test", "TEST"), Is.False);
+ Assert.That(c2.Equals("test", "TEST"), Is.False);
+ }
+
+ [Test]
+ public void Create_IgnoreCase()
+ {
+ var c1 = StringViewComparer.Create(CultureInfo.InvariantCulture, true);
+ var c2 = StringViewComparer.Create(CultureInfo.InvariantCulture, CompareOptions.IgnoreCase);
+
+ Assert.That(c1.Equals("test", "TEST"), Is.True);
+ Assert.That(c2.Equals("test", "TEST"), Is.True);
+ }
+
+ #if NET9_0_OR_GREATER
+ [Test]
+ public void AlternateComparer_Ordinal()
+ {
+ var dictionary = new Dictionary(StringViewComparer.Ordinal)
+ {
+ ["test"] = 1
+ };
+
+ var lookup = dictionary.GetAlternateLookup>();
+ Assert.That(lookup.TryGetValue("TEST", out _), Is.False);
+ Assert.That(lookup.TryGetValue("test", out var v), Is.True);
+ Assert.That(v, Is.EqualTo(1));
+ }
+
+ [Test]
+ public void AlternateComparer_OrdinalIgnoreCase()
+ {
+ var dictionary = new Dictionary(StringViewComparer.OrdinalIgnoreCase)
+ {
+ ["TEST"] = 1
+ };
+
+ var lookup = dictionary.GetAlternateLookup>();
+ Assert.That(lookup.TryGetValue("text", out _), Is.False);
+ Assert.That(lookup.TryGetValue("test", out var v), Is.True);
+ Assert.That(v, Is.EqualTo(1));
+ }
+ #endif
+}
diff --git a/Ramstack.Structures.Tests/Text/StringViewTests.cs b/Ramstack.Structures.Tests/Text/StringViewTests.cs
index 9451e12..44bdac0 100644
--- a/Ramstack.Structures.Tests/Text/StringViewTests.cs
+++ b/Ramstack.Structures.Tests/Text/StringViewTests.cs
@@ -1,923 +1,923 @@
-namespace Ramstack.Text;
-
-[TestFixture]
-[SuppressMessage("ReSharper", "ReplaceSliceWithRangeIndexer")]
-public sealed class StringViewTests
-{
- [Test]
- public void Empty_HaveZeroLength()
- {
- Assert.That(StringView.Empty.Length, Is.Zero);
- }
-
- [Test]
- public void Default_DoesNotThrow()
- {
- var view = default(StringView);
-
- Assert.That(view.Length, Is.Zero);
- Assert.That(view.IsDefault, Is.True);
-
- for (var i = 0; i < view.Length; i++)
- Assert.Fail();
-
- foreach (var _ in view)
- Assert.Fail();
-
- foreach (var _ in view.AsEnumerable())
- Assert.Fail();
-
- foreach (var _ in view.AsSpan())
- Assert.Fail();
-
- Assert.That(view.ToArray(), Is.EquivalentTo(Array.Empty()));
- Assert.That(view.ToString(), Is.EqualTo(""));
- Assert.That(view.AsMemory().ToString(), Is.EqualTo(""));
- Assert.That(view.Slice(0).Length, Is.Zero);
- Assert.That(view.Slice(0, 0).Length, Is.Zero);
-
- Assert.That(view == "", Is.True);
- Assert.That(view == "".AsView(), Is.True);
- }
-
- [TestCase("", 0, 0)]
- [TestCase(" ", 0, 4)]
- [TestCase(" ", 1, 3)]
- [TestCase(" ", 3, 1)]
- [TestCase("value", 0, 5)]
- [TestCase(" value", 0, 8)]
- [TestCase(" value", 3, 5)]
- [TestCase(" value", 2, 6)]
- [TestCase("value ", 0, 6)]
- public void TrimStart(string value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).TrimStart().ToString(),
- Is.EqualTo(value.AsSpan(index, length).TrimStart().ToString()));
- }
-
- [TestCase("", '-')]
- [TestCase("--------", '-')]
- [TestCase("--++value", '-')]
- [TestCase("--++value", '+')]
- [TestCase(" value", '-')]
- [TestCase(" value", ' ')]
- [TestCase("value", '-')]
- public void TrimStart_TrimChar(string value, char trimChar)
- {
- Assert.That(
- value.AsView().TrimStart(trimChar).ToString(),
- Is.EqualTo(value.TrimStart(trimChar)));
- }
-
- [TestCase("", "-+")]
- [TestCase("--++--++-", "-+")]
- [TestCase("--++value", "-+")]
- [TestCase("--++value", "?=")]
- [TestCase(" value", "-+")]
- [TestCase(" value", "-+")]
- [TestCase(" value", "")]
- [TestCase(" value", null)]
- [TestCase("value", "+-")]
- [TestCase("value", null)]
- public void TrimStart_TrimChars(string value, string? trimChars)
- {
- Assert.That(
- value.AsView().TrimStart(trimChars?.ToCharArray()).ToString(),
- Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));
-
- Assert.That(
- value.AsView().TrimStart(trimChars.AsSpan()).ToString(),
- Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(" ", 0, 4)]
- [TestCase(" ", 1, 3)]
- [TestCase(" ", 3, 1)]
- [TestCase("value", 0, 5)]
- [TestCase("value ", 0, 8)]
- [TestCase("value ", 0, 5)]
- [TestCase("value ", 0, 6)]
- [TestCase(" value", 0, 6)]
- public void TrimEnd(string value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).TrimEnd().ToString(),
- Is.EqualTo(value.AsSpan(index, length).TrimEnd().ToString()));
- }
-
- [TestCase("", '-')]
- [TestCase("---------", '-')]
- [TestCase("value--++", '-')]
- [TestCase("value--++", '-')]
- [TestCase("value--++", '+')]
- [TestCase("value ", '-')]
- [TestCase("value ", ' ')]
- [TestCase("value", '-')]
- public void TrimEnd_TrimChar(string value, char trimChar)
- {
- Assert.That(
- value.AsView().TrimEnd(trimChar).ToString(),
- Is.EqualTo(value.TrimEnd(trimChar)));
- }
-
- [TestCase("", "-+")]
- [TestCase("++--++--+", "-+")]
- [TestCase("value++--", "-+")]
- [TestCase("value++--", "?=")]
- [TestCase("value ", "-+")]
- [TestCase("value ", "-+")]
- [TestCase("value ", "")]
- [TestCase("value ", null)]
- [TestCase("value", "+-")]
- [TestCase("value", null)]
- public void TrimEnd_TrimChars(string value, string? trimChars)
- {
- Assert.That(
- value.AsView().TrimEnd(trimChars?.ToCharArray()).ToString(),
- Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));
-
- Assert.That(
- value.AsView().TrimEnd(trimChars.AsSpan()).ToString(),
- Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(" ", 0, 4)]
- [TestCase(" ", 1, 3)]
- [TestCase(" ", 3, 1)]
- [TestCase("value", 0, 5)]
- [TestCase(" value", 0, 8)]
- [TestCase(" value", 3, 5)]
- [TestCase(" value", 2, 6)]
- [TestCase("value ", 0, 8)]
- [TestCase("value ", 0, 5)]
- [TestCase("value ", 0, 6)]
- [TestCase(" value ", 0, 11)]
- [TestCase(" value ", 1, 9)]
- public void Trim(string value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).Trim().ToString(),
- Is.EqualTo(value.AsSpan(index, length).Trim().ToString()));
- }
-
- [TestCase("", '-')]
- [TestCase("---------", '-')]
- [TestCase("++--value--++", '-')]
- [TestCase("++--value--++", '-')]
- [TestCase("++--value--++", '+')]
- [TestCase(" value ", '-')]
- [TestCase(" value ", ' ')]
- [TestCase("value", '-')]
- public void Trim_TrimChar(string value, char trimChar)
- {
- Assert.That(
- value.AsView().Trim(trimChar).ToString(),
- Is.EqualTo(value.Trim(trimChar)));
- }
-
- [TestCase("", "-+")]
- [TestCase("++--++--+", "-+")]
- [TestCase("--++value++--", "-+")]
- [TestCase("--++value++--", "?=")]
- [TestCase(" value ", "-+")]
- [TestCase(" value ", "-+")]
- [TestCase(" value ", "")]
- [TestCase(" value ", null)]
- [TestCase("value", "+-")]
- [TestCase("value", null)]
- public void Trim_TrimChars(string value, string? trimChars)
- {
- Assert.That(
- value.AsView().Trim(trimChars?.ToCharArray()).ToString(),
- Is.EqualTo(value.Trim(trimChars?.ToCharArray())));
-
- Assert.That(
- value.AsView().Trim(trimChars.AsSpan()).ToString(),
- Is.EqualTo(value.Trim(trimChars?.ToCharArray())));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("value", 1, 4)]
- [TestCase("value", 2, 3)]
- [TestCase("value", 3, 2)]
- [TestCase("value", 4, 1)]
- [TestCase("value", 5, 0)]
- public void Enumeration(string value, int index, int length)
- {
- var view = new StringView(value, index, length);
- var span = value.AsSpan(index, length);
-
- Assert.That(view.Length, Is.EqualTo(span.Length));
-
- for (var i = 0; i < view.Length; i++)
- Assert.That(view[i], Is.EqualTo(span[i]));
-
- var j = 0;
- foreach (var c in view)
- Assert.That(c, Is.EqualTo(span[j++]));
-
- j = 0;
- foreach (var c in view.AsEnumerable())
- Assert.That(c, Is.EqualTo(span[j++]));
-
- for (var i = 0; i < view.Length; i++)
- Assert.That(view[i], Is.EqualTo(span[i]));
- }
-
- [TestCase("")]
- [TestCase("value")]
- [TestCase("key=value")]
- public void Ctor(string value)
- {
- Assert.That(
- new StringView(value).ToString(),
- Is.EqualTo(value.AsSpan().ToString()));
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void Ctor_Index(string value, int index)
- {
- Assert.That(
- new StringView(value, index).ToString(),
- Is.EqualTo(value.AsSpan(index).ToString()));
- }
-
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Ctor_Range(string value, int index, int length)
- {
- Assert.That(
- new StringView(value, index, length).ToString(),
- Is.EqualTo(value.AsSpan(index, length).ToString()));
- }
-
- [Test]
- public void Ctor_NullInput_ShouldThrow()
- {
- Assert.That(() => new StringView(null!), Throws.TypeOf());
- Assert.That(() => new StringView(null!, 0), Throws.TypeOf());
- Assert.That(() => new StringView(null!, 0, 0), Throws.TypeOf());
- }
-
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void Ctor_InvalidIndex_ShouldThrow(string value, int index)
- {
- Assert.That(
- () => new StringView(value, index),
- Throws.TypeOf());
- }
-
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void Ctor_InvalidRange_ShouldThrow(string value, int index, int length)
- {
- Assert.That(
- () => new StringView(value, index, length),
- Throws.TypeOf());
- }
-
- [Test]
- public void Slice()
- {
- var view = default(StringView);
-
- Assert.That(view.Slice(0).Length, Is.Zero);
- Assert.That(view.Slice(0, 0).Length, Is.Zero);
- }
-
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 4)]
- public void Slice_Index(string value, int index)
- {
- Assert.That(
- new StringView(value).Slice(index).ToString(),
- Is.EqualTo(value.AsSpan(index).ToString()));
- }
-
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Slice_Range(string value, int index, int length)
- {
- Assert.That(
- new StringView(value).Slice(index, length).ToString(),
- Is.EqualTo(value.AsSpan(index, length).ToString()));
- }
-
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void Slice_InvalidIndex_ShouldThrow(string value, int index)
- {
- Assert.That(
- () => new StringView(value).Slice(index),
- Throws.TypeOf());
- }
-
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void Slice_InvalidRange_ShouldThrow(string value, int index, int length)
- {
- Assert.That(
- () => new StringView(value).Slice(index, length),
- Throws.TypeOf());
- }
-
- [TestCase("value", "value", 0, 5)]
- [TestCase("key=value", "value", 4, 5)]
- [TestCase("key=value", "key", 0, 3)]
- [TestCase("key=value", "=", 3, 1)]
- [TestCase("key=value", ">", 3, 1)]
- [TestCase("value", "final", 0, 5)]
- [TestCase("key=value", "final", 0, 5)]
- [TestCase("key=value", "source", 0, 5)]
- [TestCase(null, null, 0, 0)]
- [TestCase(null, "", 0, 0)]
- [TestCase("", null, 0, 0)]
- [TestCase("", "", 0, 0)]
- public void Equals(string? value, string? other, int index, int length)
- {
- var view1 = value.AsView(index, length);
- var view2 = other.AsView();
-
- var span1 = value.AsSpan(index, length);
- var span2 = other.AsSpan();
-
- Assert.That(view1.Equals(other), Is.EqualTo(span1.SequenceEqual(span2)));
-
- // ReSharper disable once SuspiciousTypeConversion.Global
- Assert.That(view1.Equals((object?)other), Is.EqualTo(span1.SequenceEqual(span2)));
- Assert.That(view1.Equals((object?)other.AsView()), Is.EqualTo(span1.SequenceEqual(span2)));
-
- Assert.That(StringView.Equals(view1, other), Is.EqualTo(span1.SequenceEqual(span2)));
- Assert.That(StringView.Equals(view1, view2), Is.EqualTo(span1.SequenceEqual(span2)));
- }
-
- [TestCase("value", "VALUE", 0, 5)]
- [TestCase("key=value", "VALUE", 4, 5)]
- [TestCase("key=value", "Key", 0, 3)]
- [TestCase("key=value", "=", 3, 1)]
- [TestCase("key=value", ">", 3, 1)]
- [TestCase("value", "final", 0, 5)]
- [TestCase("key=value", "final", 0, 5)]
- [TestCase("key=value", "source", 0, 5)]
- [TestCase(null, null, 0, 0)]
- [TestCase(null, "", 0, 0)]
- [TestCase("", null, 0, 0)]
- [TestCase("", "", 0, 0)]
- public void Equals_StringComparison(string? value, string? other, int index, int length)
- {
- var view1 = value.AsView(index, length);
- var view2 = other.AsView();
-
- var span1 = value.AsSpan(index, length);
- var span2 = other.AsSpan();
-
- Assert.That(
- view1.Equals(other, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
-
- Assert.That(
- view1.Equals(view2, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
-
- Assert.That(
- StringView.Equals(view1, other, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
-
- Assert.That(
- StringView.Equals(view1, view2, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("")]
- [TestCase("value")]
- [TestCase(null)]
- public void GetHashCode(string? value)
- {
- Assert.That(
- value.AsView().GetHashCode(),
- Is.EqualTo(string.GetHashCode(value.AsSpan())));
-
- Assert.That(
- (value?.ToUpper()).AsView().GetHashCode(StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(string.GetHashCode(value.AsSpan(), StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Abcd", "Abcd")]
- [TestCase("abcd", "abcd")]
- [TestCase("1234", "2345")]
- [TestCase("2345", "1234")]
- [TestCase("", "1234")]
- [TestCase("1234", "")]
- [TestCase("", null)]
- [TestCase(null, "")]
- [TestCase("ABCD", "abcd")]
- [TestCase("abcde", "abc")]
- [TestCase("abc", "abcde")]
- public void Compare(string? value, string? other)
- {
- CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
-
- var view1 = value.AsView();
- var view2 = other.AsView();
-
- var span1 = value.AsSpan();
- var span2 = other.AsSpan();
-
- Assert.That(
- StringView.Compare(view1, other, StringComparison.CurrentCulture),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
-
- Assert.That(
- StringView.Compare(view1, view2, StringComparison.CurrentCulture),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
-
- Assert.That(
- view1.CompareTo(view2),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
-
- Assert.That(
- view1.CompareTo(other),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
-
- Assert.That(
- view1.CompareTo(view2, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.OrdinalIgnoreCase)));
-
- Assert.That(
- view1.CompareTo(other, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(span1.CompareTo(span2, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Abcd", "Abcd")]
- [TestCase("abcd", "abcd")]
- [TestCase("1234", "2345")]
- [TestCase("2345", "1234")]
- [TestCase("", "1234")]
- [TestCase("1234", "")]
- [TestCase("", null)]
- [TestCase(null, "")]
- [TestCase("ABCD", "abcd")]
- [TestCase("abcde", "abc")]
- [TestCase("abc", "abcde")]
- public void CompareOrdinal(string? value, string? other)
- {
- var view1 = value.AsView();
- var view2 = other.AsView();
-
- var span1 = value.AsSpan();
- var span2 = other.AsSpan();
-
- Assert.That(StringView.CompareOrdinal(view1, other), Is.EqualTo(span1.CompareTo(span2, StringComparison.Ordinal)));
- Assert.That(StringView.CompareOrdinal(view1, view2), Is.EqualTo(span1.CompareTo(span2, StringComparison.Ordinal)));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(null, 0, 0)]
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- [TestCase("key=value", 5, 0)]
- [TestCase("key=value", 3, 0)]
- public void CopyTo(string? source, int index, int length)
- {
- var data = new char[length];
- var view = source.AsView(index, length);
-
- view.CopyTo(data.AsSpan());
-
- Assert.That(view, Is.EquivalentTo(data));
- }
-
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void CopyTo_InsufficientRoom_ShouldThrow(string? source, int index, int length)
- {
- var data = new char[length - 1];
- var view = source.AsView(index, length);
-
- var e = Assert.Throws(() => view.CopyTo(data.AsSpan()))!;
- Assert.That(e.Message, Is.EqualTo("Destination is too short. (Parameter 'destination')"));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(null, 0, 0)]
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- [TestCase("key=value", 5, 0)]
- public void TryCopyTo(string? source, int index, int length)
- {
- var data = new char[length];
- var view = source.AsView(index, length);
-
- Assert.That(view.TryCopyTo(data.AsSpan()), Is.True);
- Assert.That(view, Is.EquivalentTo(data));
- }
-
- [TestCase("text", 0, 4)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void TryCopyTo_InsufficientRoom_ShouldReturnFalse(string? source, int index, int length)
- {
- var data = new char[length - 1];
- var view = source.AsView(index, length);
-
- Assert.That(view.TryCopyTo(data.AsSpan()), Is.False);
- Assert.That(data, Is.EquivalentTo(new char[length - 1]));
- }
-
- [TestCase(null)]
- [TestCase("")]
- [TestCase("value")]
- [TestCase("key=value")]
- public void AsView(string? value)
- {
- Assert.That(
- value.AsView().ToString(),
- Is.EqualTo(value.AsSpan().ToString()));
- }
-
- [TestCase(null, 0)]
- [TestCase("", 0)]
- [TestCase("value", 0)]
- [TestCase("value", 5)]
- [TestCase("key=value", 0)]
- [TestCase("key=value", 3)]
- [TestCase("key=value", 4)]
- public void AsView_Index(string? value, int index)
- {
- Assert.That(
- value.AsView(index).ToString(),
- Is.EqualTo(value.AsSpan(index).ToString()));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void AsView_Range(string? value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).ToString(),
- Is.EqualTo(value.AsSpan(index, length).ToString()));
- }
-
- [TestCase(null, 1)]
- [TestCase("", 1)]
- [TestCase("value", -1)]
- [TestCase("value", 6)]
- public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
- {
- Assert.That(
- () => value.AsView(index),
- Throws.TypeOf());
- }
-
- [TestCase(null, 1, 0)]
- [TestCase("", 1, 0)]
- [TestCase("", 0, 1)]
- [TestCase("value", -1, 0)]
- [TestCase("value", 6, 0)]
- [TestCase("value", 0, 6)]
- [TestCase("value", 5, 1)]
- [TestCase("value", 4, 2)]
- public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length)
- {
- Assert.That(
- () => value.AsView(index, length),
- Throws.TypeOf());
- }
-
- [TestCase("Hello", 'H', true)]
- [TestCase("Hello", 'h', false)]
- [TestCase("", 'A', false)]
- [TestCase(null, 'A', false)]
- public void StartsWith_Char(string? source, char needle, bool expected)
- {
- Assert.That(source.AsView().StartsWith(needle), Is.EqualTo(expected));
- }
-
- [TestCase("Hello", "H")]
- [TestCase("Hello", "h")]
- [TestCase("Hello", "O")]
- [TestCase("", "A")]
- [TestCase(null, "A")]
- [TestCase("", "")]
- [TestCase(null, "")]
- [TestCase(null, null)]
- [TestCase("", null)]
- public void StartsWith_Text(string? source, string? needle)
- {
- Assert.That(
- source.AsView().StartsWith(needle),
- Is.EqualTo(source.AsSpan().StartsWith(needle)));
-
- Assert.That(
- source.AsView().StartsWith(needle, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().StartsWith(needle, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Hello", 'o', true)]
- [TestCase("Hello", 'h', false)]
- [TestCase("", 'a', false)]
- [TestCase(null, 'a', false)]
- public void EndsWith_Char(string? source, char needle, bool expected)
- {
- Assert.That(
- source.AsView().EndsWith(needle),
- Is.EqualTo(expected));
- }
-
- [TestCase("Hello", "o")]
- [TestCase("Hello", "O")]
- [TestCase("Hello", "h")]
- [TestCase("", "A")]
- [TestCase(null, "A")]
- [TestCase("", "")]
- [TestCase(null, "")]
- [TestCase(null, null)]
- [TestCase("", null)]
- public void EndsWith_Text(string? source, string? needle)
- {
- Assert.That(
- source.AsView().EndsWith(needle),
- Is.EqualTo(source.AsSpan().EndsWith(needle)));
-
- Assert.That(
- source.AsView().EndsWith(needle, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().EndsWith(needle, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Hello, world!", 'H')]
- [TestCase("Hello, world!", 'd')]
- [TestCase("Hello, world!", 'o')]
- [TestCase("Hello, world!", '!')]
- [TestCase("Hello, world!", ',')]
- [TestCase("Hello, world!", 'A')]
- [TestCase("", 'A')]
- [TestCase(null, 'A')]
- public void IndexOf_Char(string? source, char needle)
- {
- Assert.That(
- source.AsView().IndexOf(needle),
- Is.EqualTo(source.AsSpan().IndexOf(needle)));
- }
-
- [TestCase("Hello, world!", "world")]
- [TestCase("Hello, world!", "World")]
- [TestCase("Hello, world!", "hello")]
- [TestCase("Hello, world!", "Hello")]
- [TestCase("Hello, world!", "w")]
- [TestCase("Hello, world!", ",")]
- [TestCase("Hello, world!", "?")]
- [TestCase("Hello, world!", "")]
- [TestCase("", "")]
- [TestCase(null, "")]
- public void IndexOf_Text(string? source, string needle)
- {
- Assert.That(
- source.AsView().IndexOf(needle),
- Is.EqualTo(source.AsSpan().IndexOf(needle)));
-
- Assert.That(
- source.AsView().IndexOf(needle, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().IndexOf(needle, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Hello, world!", 'H')]
- [TestCase("Hello, world!", 'd')]
- [TestCase("Hello, world!", 'o')]
- [TestCase("Hello, world!", '!')]
- [TestCase("Hello, world!", ',')]
- [TestCase("Hello, world!", 'A')]
- [TestCase("", 'A')]
- [TestCase(null, 'A')]
- public void LastIndexOf_Char(string? source, char needle)
- {
- Assert.That(
- source.AsView().LastIndexOf(needle),
- Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
- }
-
- [TestCase("Hello, world!", "World")]
- [TestCase("Hello, world!", "hello")]
- [TestCase("Hello, world!", "?")]
- [TestCase("", "")]
- [TestCase(null, "")]
- public void LastIndexOf_Text(string? source, string needle)
- {
- Assert.That(
- source.AsView().LastIndexOf(needle),
- Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
-
- Assert.That(
- source.AsView().LastIndexOf(needle, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().LastIndexOf(needle, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase("Hello, world!", 'H')]
- [TestCase("Hello, world!", '!')]
- [TestCase("Hello, world!", ',')]
- [TestCase("Hello, world!", 'A')]
- [TestCase("", 'A')]
- [TestCase(null, 'A')]
- public void Contains_Char(string? source, char needle)
- {
- Assert.That(
- source.AsView().Contains(needle),
- Is.EqualTo(source.AsSpan().Contains(needle)));
- }
-
- [TestCase("Hello, world!", "world")]
- [TestCase("Hello, world!", "World")]
- [TestCase("Hello, world!", "w")]
- [TestCase("Hello, world!", ",")]
- [TestCase("Hello, world!", "?")]
- [TestCase("Hello, world!", "")]
- [TestCase("", "")]
- [TestCase(null, "")]
- public void Contains_Text(string? source, string needle)
- {
- Assert.That(
- source.AsView().Contains(needle),
- Is.EqualTo(source.AsSpan().Contains(needle, StringComparison.Ordinal)));
-
- Assert.That(
- source.AsView().Contains(needle, StringComparison.OrdinalIgnoreCase),
- Is.EqualTo(source.AsSpan().Contains(needle, StringComparison.OrdinalIgnoreCase)));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void Length(string? value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).Length,
- Is.EqualTo(length));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void ToString(string? value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).ToString(),
- Is.EqualTo(value.AsSpan(index, length).ToString()));
- }
-
- [TestCase(null, 0, 0)]
- [TestCase("", 0, 0)]
- [TestCase("value", 0, 0)]
- [TestCase("value", 5, 0)]
- [TestCase("value", 0, 5)]
- [TestCase("key=value", 0, 3)]
- [TestCase("key=value", 3, 1)]
- [TestCase("key=value", 4, 5)]
- public void ToArray(string? value, int index, int length)
- {
- Assert.That(
- value.AsView(index, length).ToArray(),
- Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
- }
-
- [TestCase("", 0, 0)]
- [TestCase(null, 0, 0)]
- [TestCase("id;name;lastname", 3, 0)]
- [TestCase("id;name;lastname", 3, 4)]
- [TestCase("id;name;lastname", 0, 2)]
- [TestCase("id;name;lastname", 8, 8)]
- public void GetPinnableReference(string? value, int index, int length)
- {
- var view = value.AsView(index, length);
- var span = value.AsSpan(index, length);
-
- Assert.That(
- Unsafe.AreSame(
- ref Unsafe.AsRef(in view.GetPinnableReference()),
- ref Unsafe.AsRef(in span.GetPinnableReference())),
- Is.True);
- }
-
- [Test]
- public void GetPinnableReference_Default()
- {
- var view = default(StringView);
- view.GetPinnableReference();
- }
-
- [TestCase("value", "value", 0, 5, true)]
- [TestCase("key=value", "value", 4, 5, true)]
- [TestCase("key=value", "key", 0, 3, true)]
- [TestCase("key=value", "=", 3, 1, true)]
- [TestCase("key=value", ">", 3, 1, false)]
- [TestCase("value", "final", 0, 5, false)]
- [TestCase("key=value", "final", 0, 5, false)]
- [TestCase("key=value", "source", 0, 5, false)]
- [TestCase("key=value", "", 0, 0, true)]
- [TestCase("key=value", null, 0, 0, true)]
- [TestCase(null, null, 0, 0, true)]
- [TestCase(null, "", 0, 0, true)]
- [TestCase("", null, 0, 0, true)]
- [TestCase("", "", 0, 0, true)]
- public void Operator_Equals(string? source, string? value, int index, int length, bool expected)
- {
- Assert.That(source.AsView(index, length) == value, Is.EqualTo(expected));
- Assert.That(source.AsView(index, length) == value.AsView(), Is.EqualTo(expected));
- }
-
- [TestCase("value", "key", true)]
- [TestCase("key", "value", true)]
- [TestCase("key", "Key", true)]
- [TestCase("key", "key", false)]
- [TestCase("key", "", true)]
- [TestCase("key", null, true)]
- [TestCase("", "key", true)]
- [TestCase(null, "key", true)]
- public void Operator_NotEquals(string? source, string? value, bool expected)
- {
- Assert.That(source.AsView() != value, Is.EqualTo(expected));
- Assert.That(source.AsView() != value.AsView(), Is.EqualTo(expected));
- }
-
- [TestCase(null)]
- [TestCase("")]
- [TestCase("test")]
- public void Operator_Conversions(string? value)
- {
- StringView view = value;
- ReadOnlyMemory memory = view;
- ReadOnlySpan span = view;
-
- Assert.That(
- span.ToString() == value.AsSpan().ToString(),
- Is.True);
-
- Assert.That(
- memory.ToString() == value.AsMemory().ToString(),
- Is.True);
-
- for (var i = 0; i < view.Length; i++)
- {
- Assert.That(view[i], Is.EqualTo(span[i]));
- Assert.That(view[i], Is.EqualTo(memory.Span[i]));
- }
-
- var j = 0;
- foreach (var c in view)
- {
- Assert.That(c, Is.EqualTo(span[j]));
- Assert.That(c, Is.EqualTo(memory.Span[j]));
- j++;
- }
- }
-}
+namespace Ramstack.Text;
+
+[TestFixture]
+[SuppressMessage("ReSharper", "ReplaceSliceWithRangeIndexer")]
+public sealed class StringViewTests
+{
+ [Test]
+ public void Empty_HaveZeroLength()
+ {
+ Assert.That(StringView.Empty.Length, Is.Zero);
+ }
+
+ [Test]
+ public void Default_DoesNotThrow()
+ {
+ var view = default(StringView);
+
+ Assert.That(view.Length, Is.Zero);
+ Assert.That(view.IsDefault, Is.True);
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.Fail();
+
+ foreach (var _ in view)
+ Assert.Fail();
+
+ foreach (var _ in view.AsEnumerable())
+ Assert.Fail();
+
+ foreach (var _ in view.AsSpan())
+ Assert.Fail();
+
+ Assert.That(view.ToArray(), Is.EquivalentTo(Array.Empty()));
+ Assert.That(view.ToString(), Is.EqualTo(""));
+ Assert.That(view.AsMemory().ToString(), Is.EqualTo(""));
+ Assert.That(view.Slice(0).Length, Is.Zero);
+ Assert.That(view.Slice(0, 0).Length, Is.Zero);
+
+ Assert.That(view == "", Is.True);
+ Assert.That(view == "".AsView(), Is.True);
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(" ", 0, 4)]
+ [TestCase(" ", 1, 3)]
+ [TestCase(" ", 3, 1)]
+ [TestCase("value", 0, 5)]
+ [TestCase(" value", 0, 8)]
+ [TestCase(" value", 3, 5)]
+ [TestCase(" value", 2, 6)]
+ [TestCase("value ", 0, 6)]
+ public void TrimStart(string value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).TrimStart().ToString(),
+ Is.EqualTo(value.AsSpan(index, length).TrimStart().ToString()));
+ }
+
+ [TestCase("", '-')]
+ [TestCase("--------", '-')]
+ [TestCase("--++value", '-')]
+ [TestCase("--++value", '+')]
+ [TestCase(" value", '-')]
+ [TestCase(" value", ' ')]
+ [TestCase("value", '-')]
+ public void TrimStart_TrimChar(string value, char trimChar)
+ {
+ Assert.That(
+ value.AsView().TrimStart(trimChar).ToString(),
+ Is.EqualTo(value.TrimStart(trimChar)));
+ }
+
+ [TestCase("", "-+")]
+ [TestCase("--++--++-", "-+")]
+ [TestCase("--++value", "-+")]
+ [TestCase("--++value", "?=")]
+ [TestCase(" value", "-+")]
+ [TestCase(" value", "-+")]
+ [TestCase(" value", "")]
+ [TestCase(" value", null)]
+ [TestCase("value", "+-")]
+ [TestCase("value", null)]
+ public void TrimStart_TrimChars(string value, string? trimChars)
+ {
+ Assert.That(
+ value.AsView().TrimStart(trimChars?.ToCharArray()).ToString(),
+ Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));
+
+ Assert.That(
+ value.AsView().TrimStart(trimChars.AsSpan()).ToString(),
+ Is.EqualTo(value.TrimStart(trimChars?.ToCharArray())));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(" ", 0, 4)]
+ [TestCase(" ", 1, 3)]
+ [TestCase(" ", 3, 1)]
+ [TestCase("value", 0, 5)]
+ [TestCase("value ", 0, 8)]
+ [TestCase("value ", 0, 5)]
+ [TestCase("value ", 0, 6)]
+ [TestCase(" value", 0, 6)]
+ public void TrimEnd(string value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).TrimEnd().ToString(),
+ Is.EqualTo(value.AsSpan(index, length).TrimEnd().ToString()));
+ }
+
+ [TestCase("", '-')]
+ [TestCase("---------", '-')]
+ [TestCase("value--++", '-')]
+ [TestCase("value--++", '-')]
+ [TestCase("value--++", '+')]
+ [TestCase("value ", '-')]
+ [TestCase("value ", ' ')]
+ [TestCase("value", '-')]
+ public void TrimEnd_TrimChar(string value, char trimChar)
+ {
+ Assert.That(
+ value.AsView().TrimEnd(trimChar).ToString(),
+ Is.EqualTo(value.TrimEnd(trimChar)));
+ }
+
+ [TestCase("", "-+")]
+ [TestCase("++--++--+", "-+")]
+ [TestCase("value++--", "-+")]
+ [TestCase("value++--", "?=")]
+ [TestCase("value ", "-+")]
+ [TestCase("value ", "-+")]
+ [TestCase("value ", "")]
+ [TestCase("value ", null)]
+ [TestCase("value", "+-")]
+ [TestCase("value", null)]
+ public void TrimEnd_TrimChars(string value, string? trimChars)
+ {
+ Assert.That(
+ value.AsView().TrimEnd(trimChars?.ToCharArray()).ToString(),
+ Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));
+
+ Assert.That(
+ value.AsView().TrimEnd(trimChars.AsSpan()).ToString(),
+ Is.EqualTo(value.TrimEnd(trimChars?.ToCharArray())));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(" ", 0, 4)]
+ [TestCase(" ", 1, 3)]
+ [TestCase(" ", 3, 1)]
+ [TestCase("value", 0, 5)]
+ [TestCase(" value", 0, 8)]
+ [TestCase(" value", 3, 5)]
+ [TestCase(" value", 2, 6)]
+ [TestCase("value ", 0, 8)]
+ [TestCase("value ", 0, 5)]
+ [TestCase("value ", 0, 6)]
+ [TestCase(" value ", 0, 11)]
+ [TestCase(" value ", 1, 9)]
+ public void Trim(string value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).Trim().ToString(),
+ Is.EqualTo(value.AsSpan(index, length).Trim().ToString()));
+ }
+
+ [TestCase("", '-')]
+ [TestCase("---------", '-')]
+ [TestCase("++--value--++", '-')]
+ [TestCase("++--value--++", '-')]
+ [TestCase("++--value--++", '+')]
+ [TestCase(" value ", '-')]
+ [TestCase(" value ", ' ')]
+ [TestCase("value", '-')]
+ public void Trim_TrimChar(string value, char trimChar)
+ {
+ Assert.That(
+ value.AsView().Trim(trimChar).ToString(),
+ Is.EqualTo(value.Trim(trimChar)));
+ }
+
+ [TestCase("", "-+")]
+ [TestCase("++--++--+", "-+")]
+ [TestCase("--++value++--", "-+")]
+ [TestCase("--++value++--", "?=")]
+ [TestCase(" value ", "-+")]
+ [TestCase(" value ", "-+")]
+ [TestCase(" value ", "")]
+ [TestCase(" value ", null)]
+ [TestCase("value", "+-")]
+ [TestCase("value", null)]
+ public void Trim_TrimChars(string value, string? trimChars)
+ {
+ Assert.That(
+ value.AsView().Trim(trimChars?.ToCharArray()).ToString(),
+ Is.EqualTo(value.Trim(trimChars?.ToCharArray())));
+
+ Assert.That(
+ value.AsView().Trim(trimChars.AsSpan()).ToString(),
+ Is.EqualTo(value.Trim(trimChars?.ToCharArray())));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("value", 1, 4)]
+ [TestCase("value", 2, 3)]
+ [TestCase("value", 3, 2)]
+ [TestCase("value", 4, 1)]
+ [TestCase("value", 5, 0)]
+ public void Enumeration(string value, int index, int length)
+ {
+ var view = new StringView(value, index, length);
+ var span = value.AsSpan(index, length);
+
+ Assert.That(view.Length, Is.EqualTo(span.Length));
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.That(view[i], Is.EqualTo(span[i]));
+
+ var j = 0;
+ foreach (var c in view)
+ Assert.That(c, Is.EqualTo(span[j++]));
+
+ j = 0;
+ foreach (var c in view.AsEnumerable())
+ Assert.That(c, Is.EqualTo(span[j++]));
+
+ for (var i = 0; i < view.Length; i++)
+ Assert.That(view[i], Is.EqualTo(span[i]));
+ }
+
+ [TestCase("")]
+ [TestCase("value")]
+ [TestCase("key=value")]
+ public void Ctor(string value)
+ {
+ Assert.That(
+ new StringView(value).ToString(),
+ Is.EqualTo(value.AsSpan().ToString()));
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void Ctor_Index(string value, int index)
+ {
+ Assert.That(
+ new StringView(value, index).ToString(),
+ Is.EqualTo(value.AsSpan(index).ToString()));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Ctor_Range(string value, int index, int length)
+ {
+ Assert.That(
+ new StringView(value, index, length).ToString(),
+ Is.EqualTo(value.AsSpan(index, length).ToString()));
+ }
+
+ [Test]
+ public void Ctor_NullInput_ShouldThrow()
+ {
+ Assert.That(() => new StringView(null!), Throws.TypeOf());
+ Assert.That(() => new StringView(null!, 0), Throws.TypeOf());
+ Assert.That(() => new StringView(null!, 0, 0), Throws.TypeOf());
+ }
+
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void Ctor_InvalidIndex_ShouldThrow(string value, int index)
+ {
+ Assert.That(
+ () => new StringView(value, index),
+ Throws.TypeOf());
+ }
+
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void Ctor_InvalidRange_ShouldThrow(string value, int index, int length)
+ {
+ Assert.That(
+ () => new StringView(value, index, length),
+ Throws.TypeOf());
+ }
+
+ [Test]
+ public void Slice()
+ {
+ var view = default(StringView);
+
+ Assert.That(view.Slice(0).Length, Is.Zero);
+ Assert.That(view.Slice(0, 0).Length, Is.Zero);
+ }
+
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 4)]
+ public void Slice_Index(string value, int index)
+ {
+ Assert.That(
+ new StringView(value).Slice(index).ToString(),
+ Is.EqualTo(value.AsSpan(index).ToString()));
+ }
+
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Slice_Range(string value, int index, int length)
+ {
+ Assert.That(
+ new StringView(value).Slice(index, length).ToString(),
+ Is.EqualTo(value.AsSpan(index, length).ToString()));
+ }
+
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void Slice_InvalidIndex_ShouldThrow(string value, int index)
+ {
+ Assert.That(
+ () => new StringView(value).Slice(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void Slice_InvalidRange_ShouldThrow(string value, int index, int length)
+ {
+ Assert.That(
+ () => new StringView(value).Slice(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase("value", "value", 0, 5)]
+ [TestCase("key=value", "value", 4, 5)]
+ [TestCase("key=value", "key", 0, 3)]
+ [TestCase("key=value", "=", 3, 1)]
+ [TestCase("key=value", ">", 3, 1)]
+ [TestCase("value", "final", 0, 5)]
+ [TestCase("key=value", "final", 0, 5)]
+ [TestCase("key=value", "source", 0, 5)]
+ [TestCase(null, null, 0, 0)]
+ [TestCase(null, "", 0, 0)]
+ [TestCase("", null, 0, 0)]
+ [TestCase("", "", 0, 0)]
+ public void Equals(string? value, string? other, int index, int length)
+ {
+ var view1 = value.AsView(index, length);
+ var view2 = other.AsView();
+
+ var span1 = value.AsSpan(index, length);
+ var span2 = other.AsSpan();
+
+ Assert.That(view1.Equals(other), Is.EqualTo(span1.SequenceEqual(span2)));
+
+ // ReSharper disable once SuspiciousTypeConversion.Global
+ Assert.That(view1.Equals((object?)other), Is.EqualTo(span1.SequenceEqual(span2)));
+ Assert.That(view1.Equals((object?)other.AsView()), Is.EqualTo(span1.SequenceEqual(span2)));
+
+ Assert.That(StringView.Equals(view1, other), Is.EqualTo(span1.SequenceEqual(span2)));
+ Assert.That(StringView.Equals(view1, view2), Is.EqualTo(span1.SequenceEqual(span2)));
+ }
+
+ [TestCase("value", "VALUE", 0, 5)]
+ [TestCase("key=value", "VALUE", 4, 5)]
+ [TestCase("key=value", "Key", 0, 3)]
+ [TestCase("key=value", "=", 3, 1)]
+ [TestCase("key=value", ">", 3, 1)]
+ [TestCase("value", "final", 0, 5)]
+ [TestCase("key=value", "final", 0, 5)]
+ [TestCase("key=value", "source", 0, 5)]
+ [TestCase(null, null, 0, 0)]
+ [TestCase(null, "", 0, 0)]
+ [TestCase("", null, 0, 0)]
+ [TestCase("", "", 0, 0)]
+ public void Equals_StringComparison(string? value, string? other, int index, int length)
+ {
+ var view1 = value.AsView(index, length);
+ var view2 = other.AsView();
+
+ var span1 = value.AsSpan(index, length);
+ var span2 = other.AsSpan();
+
+ Assert.That(
+ view1.Equals(other, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
+
+ Assert.That(
+ view1.Equals(view2, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
+
+ Assert.That(
+ StringView.Equals(view1, other, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
+
+ Assert.That(
+ StringView.Equals(view1, view2, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.Equals(span2, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("")]
+ [TestCase("value")]
+ [TestCase(null)]
+ public void GetHashCode(string? value)
+ {
+ Assert.That(
+ value.AsView().GetHashCode(),
+ Is.EqualTo(string.GetHashCode(value.AsSpan())));
+
+ Assert.That(
+ (value?.ToUpperInvariant()).AsView().GetHashCode(StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(string.GetHashCode(value.AsSpan(), StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Abcd", "Abcd")]
+ [TestCase("abcd", "abcd")]
+ [TestCase("1234", "2345")]
+ [TestCase("2345", "1234")]
+ [TestCase("", "1234")]
+ [TestCase("1234", "")]
+ [TestCase("", null)]
+ [TestCase(null, "")]
+ [TestCase("ABCD", "abcd")]
+ [TestCase("abcde", "abc")]
+ [TestCase("abc", "abcde")]
+ public void Compare(string? value, string? other)
+ {
+ CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
+
+ var view1 = value.AsView();
+ var view2 = other.AsView();
+
+ var span1 = value.AsSpan();
+ var span2 = other.AsSpan();
+
+ Assert.That(
+ StringView.Compare(view1, other, StringComparison.CurrentCulture),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
+
+ Assert.That(
+ StringView.Compare(view1, view2, StringComparison.CurrentCulture),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
+
+ Assert.That(
+ view1.CompareTo(view2),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
+
+ Assert.That(
+ view1.CompareTo(other),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.CurrentCulture)));
+
+ Assert.That(
+ view1.CompareTo(view2, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.OrdinalIgnoreCase)));
+
+ Assert.That(
+ view1.CompareTo(other, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(span1.CompareTo(span2, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Abcd", "Abcd")]
+ [TestCase("abcd", "abcd")]
+ [TestCase("1234", "2345")]
+ [TestCase("2345", "1234")]
+ [TestCase("", "1234")]
+ [TestCase("1234", "")]
+ [TestCase("", null)]
+ [TestCase(null, "")]
+ [TestCase("ABCD", "abcd")]
+ [TestCase("abcde", "abc")]
+ [TestCase("abc", "abcde")]
+ public void CompareOrdinal(string? value, string? other)
+ {
+ var view1 = value.AsView();
+ var view2 = other.AsView();
+
+ var span1 = value.AsSpan();
+ var span2 = other.AsSpan();
+
+ Assert.That(StringView.CompareOrdinal(view1, other), Is.EqualTo(span1.CompareTo(span2, StringComparison.Ordinal)));
+ Assert.That(StringView.CompareOrdinal(view1, view2), Is.EqualTo(span1.CompareTo(span2, StringComparison.Ordinal)));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(null, 0, 0)]
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ [TestCase("key=value", 5, 0)]
+ [TestCase("key=value", 3, 0)]
+ public void CopyTo(string? source, int index, int length)
+ {
+ var data = new char[length];
+ var view = source.AsView(index, length);
+
+ view.CopyTo(data.AsSpan());
+
+ Assert.That(view, Is.EquivalentTo(data));
+ }
+
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void CopyTo_InsufficientRoom_ShouldThrow(string? source, int index, int length)
+ {
+ var data = new char[length - 1];
+ var view = source.AsView(index, length);
+
+ var e = Assert.Throws(() => view.CopyTo(data.AsSpan()))!;
+ Assert.That(e.Message, Is.EqualTo("Destination is too short. (Parameter 'destination')"));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(null, 0, 0)]
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ [TestCase("key=value", 5, 0)]
+ public void TryCopyTo(string? source, int index, int length)
+ {
+ var data = new char[length];
+ var view = source.AsView(index, length);
+
+ Assert.That(view.TryCopyTo(data.AsSpan()), Is.True);
+ Assert.That(view, Is.EquivalentTo(data));
+ }
+
+ [TestCase("text", 0, 4)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void TryCopyTo_InsufficientRoom_ShouldReturnFalse(string? source, int index, int length)
+ {
+ var data = new char[length - 1];
+ var view = source.AsView(index, length);
+
+ Assert.That(view.TryCopyTo(data.AsSpan()), Is.False);
+ Assert.That(data, Is.EquivalentTo(new char[length - 1]));
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("value")]
+ [TestCase("key=value")]
+ public void AsView(string? value)
+ {
+ Assert.That(
+ value.AsView().ToString(),
+ Is.EqualTo(value.AsSpan().ToString()));
+ }
+
+ [TestCase(null, 0)]
+ [TestCase("", 0)]
+ [TestCase("value", 0)]
+ [TestCase("value", 5)]
+ [TestCase("key=value", 0)]
+ [TestCase("key=value", 3)]
+ [TestCase("key=value", 4)]
+ public void AsView_Index(string? value, int index)
+ {
+ Assert.That(
+ value.AsView(index).ToString(),
+ Is.EqualTo(value.AsSpan(index).ToString()));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void AsView_Range(string? value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).ToString(),
+ Is.EqualTo(value.AsSpan(index, length).ToString()));
+ }
+
+ [TestCase(null, 1)]
+ [TestCase("", 1)]
+ [TestCase("value", -1)]
+ [TestCase("value", 6)]
+ public void AsView_InvalidIndex_ShouldThrow(string? value, int index)
+ {
+ Assert.That(
+ () => value.AsView(index),
+ Throws.TypeOf());
+ }
+
+ [TestCase(null, 1, 0)]
+ [TestCase("", 1, 0)]
+ [TestCase("", 0, 1)]
+ [TestCase("value", -1, 0)]
+ [TestCase("value", 6, 0)]
+ [TestCase("value", 0, 6)]
+ [TestCase("value", 5, 1)]
+ [TestCase("value", 4, 2)]
+ public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length)
+ {
+ Assert.That(
+ () => value.AsView(index, length),
+ Throws.TypeOf());
+ }
+
+ [TestCase("Hello", 'H', true)]
+ [TestCase("Hello", 'h', false)]
+ [TestCase("", 'A', false)]
+ [TestCase(null, 'A', false)]
+ public void StartsWith_Char(string? source, char needle, bool expected)
+ {
+ Assert.That(source.AsView().StartsWith(needle), Is.EqualTo(expected));
+ }
+
+ [TestCase("Hello", "H")]
+ [TestCase("Hello", "h")]
+ [TestCase("Hello", "O")]
+ [TestCase("", "A")]
+ [TestCase(null, "A")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ [TestCase(null, null)]
+ [TestCase("", null)]
+ public void StartsWith_Text(string? source, string? needle)
+ {
+ Assert.That(
+ source.AsView().StartsWith(needle),
+ Is.EqualTo(source.AsSpan().StartsWith(needle)));
+
+ Assert.That(
+ source.AsView().StartsWith(needle, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().StartsWith(needle, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Hello", 'o', true)]
+ [TestCase("Hello", 'h', false)]
+ [TestCase("", 'a', false)]
+ [TestCase(null, 'a', false)]
+ public void EndsWith_Char(string? source, char needle, bool expected)
+ {
+ Assert.That(
+ source.AsView().EndsWith(needle),
+ Is.EqualTo(expected));
+ }
+
+ [TestCase("Hello", "o")]
+ [TestCase("Hello", "O")]
+ [TestCase("Hello", "h")]
+ [TestCase("", "A")]
+ [TestCase(null, "A")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ [TestCase(null, null)]
+ [TestCase("", null)]
+ public void EndsWith_Text(string? source, string? needle)
+ {
+ Assert.That(
+ source.AsView().EndsWith(needle),
+ Is.EqualTo(source.AsSpan().EndsWith(needle)));
+
+ Assert.That(
+ source.AsView().EndsWith(needle, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().EndsWith(needle, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Hello, world!", 'H')]
+ [TestCase("Hello, world!", 'd')]
+ [TestCase("Hello, world!", 'o')]
+ [TestCase("Hello, world!", '!')]
+ [TestCase("Hello, world!", ',')]
+ [TestCase("Hello, world!", 'A')]
+ [TestCase("", 'A')]
+ [TestCase(null, 'A')]
+ public void IndexOf_Char(string? source, char needle)
+ {
+ Assert.That(
+ source.AsView().IndexOf(needle),
+ Is.EqualTo(source.AsSpan().IndexOf(needle)));
+ }
+
+ [TestCase("Hello, world!", "world")]
+ [TestCase("Hello, world!", "World")]
+ [TestCase("Hello, world!", "hello")]
+ [TestCase("Hello, world!", "Hello")]
+ [TestCase("Hello, world!", "w")]
+ [TestCase("Hello, world!", ",")]
+ [TestCase("Hello, world!", "?")]
+ [TestCase("Hello, world!", "")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ public void IndexOf_Text(string? source, string needle)
+ {
+ Assert.That(
+ source.AsView().IndexOf(needle),
+ Is.EqualTo(source.AsSpan().IndexOf(needle)));
+
+ Assert.That(
+ source.AsView().IndexOf(needle, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().IndexOf(needle, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Hello, world!", 'H')]
+ [TestCase("Hello, world!", 'd')]
+ [TestCase("Hello, world!", 'o')]
+ [TestCase("Hello, world!", '!')]
+ [TestCase("Hello, world!", ',')]
+ [TestCase("Hello, world!", 'A')]
+ [TestCase("", 'A')]
+ [TestCase(null, 'A')]
+ public void LastIndexOf_Char(string? source, char needle)
+ {
+ Assert.That(
+ source.AsView().LastIndexOf(needle),
+ Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
+ }
+
+ [TestCase("Hello, world!", "World")]
+ [TestCase("Hello, world!", "hello")]
+ [TestCase("Hello, world!", "?")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ public void LastIndexOf_Text(string? source, string needle)
+ {
+ Assert.That(
+ source.AsView().LastIndexOf(needle),
+ Is.EqualTo(source.AsSpan().LastIndexOf(needle)));
+
+ Assert.That(
+ source.AsView().LastIndexOf(needle, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().LastIndexOf(needle, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase("Hello, world!", 'H')]
+ [TestCase("Hello, world!", '!')]
+ [TestCase("Hello, world!", ',')]
+ [TestCase("Hello, world!", 'A')]
+ [TestCase("", 'A')]
+ [TestCase(null, 'A')]
+ public void Contains_Char(string? source, char needle)
+ {
+ Assert.That(
+ source.AsView().Contains(needle),
+ Is.EqualTo(source.AsSpan().Contains(needle)));
+ }
+
+ [TestCase("Hello, world!", "world")]
+ [TestCase("Hello, world!", "World")]
+ [TestCase("Hello, world!", "w")]
+ [TestCase("Hello, world!", ",")]
+ [TestCase("Hello, world!", "?")]
+ [TestCase("Hello, world!", "")]
+ [TestCase("", "")]
+ [TestCase(null, "")]
+ public void Contains_Text(string? source, string needle)
+ {
+ Assert.That(
+ source.AsView().Contains(needle),
+ Is.EqualTo(source.AsSpan().Contains(needle, StringComparison.Ordinal)));
+
+ Assert.That(
+ source.AsView().Contains(needle, StringComparison.OrdinalIgnoreCase),
+ Is.EqualTo(source.AsSpan().Contains(needle, StringComparison.OrdinalIgnoreCase)));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void Length(string? value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).Length,
+ Is.EqualTo(length));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void ToString(string? value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).ToString(),
+ Is.EqualTo(value.AsSpan(index, length).ToString()));
+ }
+
+ [TestCase(null, 0, 0)]
+ [TestCase("", 0, 0)]
+ [TestCase("value", 0, 0)]
+ [TestCase("value", 5, 0)]
+ [TestCase("value", 0, 5)]
+ [TestCase("key=value", 0, 3)]
+ [TestCase("key=value", 3, 1)]
+ [TestCase("key=value", 4, 5)]
+ public void ToArray(string? value, int index, int length)
+ {
+ Assert.That(
+ value.AsView(index, length).ToArray(),
+ Is.EquivalentTo(value.AsSpan(index, length).ToArray()));
+ }
+
+ [TestCase("", 0, 0)]
+ [TestCase(null, 0, 0)]
+ [TestCase("id;name;lastname", 3, 0)]
+ [TestCase("id;name;lastname", 3, 4)]
+ [TestCase("id;name;lastname", 0, 2)]
+ [TestCase("id;name;lastname", 8, 8)]
+ public void GetPinnableReference(string? value, int index, int length)
+ {
+ var view = value.AsView(index, length);
+ var span = value.AsSpan(index, length);
+
+ Assert.That(
+ Unsafe.AreSame(
+ ref Unsafe.AsRef(in view.GetPinnableReference()),
+ ref Unsafe.AsRef(in span.GetPinnableReference())),
+ Is.True);
+ }
+
+ [Test]
+ public void GetPinnableReference_Default()
+ {
+ var view = default(StringView);
+ view.GetPinnableReference();
+ }
+
+ [TestCase("value", "value", 0, 5, true)]
+ [TestCase("key=value", "value", 4, 5, true)]
+ [TestCase("key=value", "key", 0, 3, true)]
+ [TestCase("key=value", "=", 3, 1, true)]
+ [TestCase("key=value", ">", 3, 1, false)]
+ [TestCase("value", "final", 0, 5, false)]
+ [TestCase("key=value", "final", 0, 5, false)]
+ [TestCase("key=value", "source", 0, 5, false)]
+ [TestCase("key=value", "", 0, 0, true)]
+ [TestCase("key=value", null, 0, 0, true)]
+ [TestCase(null, null, 0, 0, true)]
+ [TestCase(null, "", 0, 0, true)]
+ [TestCase("", null, 0, 0, true)]
+ [TestCase("", "", 0, 0, true)]
+ public void Operator_Equals(string? source, string? value, int index, int length, bool expected)
+ {
+ Assert.That(source.AsView(index, length) == value, Is.EqualTo(expected));
+ Assert.That(source.AsView(index, length) == value.AsView(), Is.EqualTo(expected));
+ }
+
+ [TestCase("value", "key", true)]
+ [TestCase("key", "value", true)]
+ [TestCase("key", "Key", true)]
+ [TestCase("key", "key", false)]
+ [TestCase("key", "", true)]
+ [TestCase("key", null, true)]
+ [TestCase("", "key", true)]
+ [TestCase(null, "key", true)]
+ public void Operator_NotEquals(string? source, string? value, bool expected)
+ {
+ Assert.That(source.AsView() != value, Is.EqualTo(expected));
+ Assert.That(source.AsView() != value.AsView(), Is.EqualTo(expected));
+ }
+
+ [TestCase(null)]
+ [TestCase("")]
+ [TestCase("test")]
+ public void Operator_Conversions(string? value)
+ {
+ StringView view = value;
+ ReadOnlyMemory memory = view;
+ ReadOnlySpan