From 68917ff2ef6a1ca7775eefaac20ed1f42f4a665c Mon Sep 17 00:00:00 2001 From: Quajak Date: Wed, 3 Jun 2020 22:14:17 +0200 Subject: [PATCH] Add additional tests Added index out of range tests for when setting elements and for object or struct arrays. --- .../System/ArrayTests.cs | 130 +++++++++++++++--- 1 file changed, 111 insertions(+), 19 deletions(-) diff --git a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/ArrayTests.cs b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/ArrayTests.cs index e78691211c..a18a6f8730 100644 --- a/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/ArrayTests.cs +++ b/Tests/Kernels/Cosmos.Compiler.Tests.Bcl.System/System/ArrayTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using Cosmos.TestRunner; @@ -56,30 +56,122 @@ public static void Execute() Assert.IsTrue(xDoubleResult[3] == xDoubleExpectedResult[3], "Assinging values to double array elements doesn't work: xResult[1] = " + (uint)xDoubleResult[3] + " != " + (uint)xDoubleExpectedResult[3]); //Test array indexes - int y = 0; - int[] x = new int[5] { 1, 2, 3, 4, 5 }; - bool error = false; - try { - y = x[1]; - y = x[7]; + int y = 0; + int[] x = new int[5] { 1, 2, 3, 4, 5 }; + bool error = false; + try + { + y = x[1]; + y = x[7]; + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error && y == 2, "Index out of range exception works correctly for too large positions."); + error = false; + try + { + #pragma warning disable CS0251 // Indexing an array with a negative index + y = x[-1]; + #pragma warning restore CS0251 // Indexing an array with a negative index + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error && y == 2, "Index out of range exception works correctly for too small positions."); + error = false; + try + { + x[7] = 9; + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly when setting elements."); } - catch (IndexOutOfRangeException) { - error = true; + object y; + object[] x = new object[5] { new object(), new object(), new object(), new object(), new object() }; + bool error = false; + try + { + y = x[28987]; + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly for too large positions."); + error = false; + try + { +#pragma warning disable CS0251 // Indexing an array with a negative index + y = x[-1]; +#pragma warning restore CS0251 // Indexing an array with a negative index + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly for too small positions."); + error = false; + try + { + x[7] = new object(); + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly when setting elements."); } - Assert.IsTrue(error && y == 2, "Index out of range exception works correctly for too large positions."); - error = false; - try { - y = x[-1]; + Test y; + Test[] x = new Test[5] { new Test(), new Test(), new Test(), new Test(), new Test() }; + bool error = false; + try + { + y = x[28987]; + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly for too large positions."); + error = false; + try + { +#pragma warning disable CS0251 // Indexing an array with a negative index + y = x[-1]; +#pragma warning restore CS0251 // Indexing an array with a negative index + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly for too small positions."); + error = false; + try + { + x[7] = new Test(); + } + catch (IndexOutOfRangeException) + { + error = true; + } + Assert.IsTrue(error, "Index out of range exception works correctly when setting elements."); } - catch (IndexOutOfRangeException) - { - error = true; - } - Assert.IsTrue(error && y == 2, "Index out of range exception works correctly for too small positions."); - } } + + struct Test + { + int x; + int y; + int z; + } }