|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Numerics; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +// LLM-Fuzz Mutation 5 |
| 11 | + |
| 12 | +public class Runtime_115495 |
| 13 | +{ |
| 14 | + private const int Pass = 100; |
| 15 | + private const int Fail = -1; |
| 16 | + |
| 17 | + public class Assert |
| 18 | + { |
| 19 | + public static void Equal(float a, float b) |
| 20 | + { |
| 21 | + if (a != b) |
| 22 | + throw new Exception("Assert.Equal failed"); |
| 23 | + } |
| 24 | + |
| 25 | + public static void NotEqual(float a, float b) |
| 26 | + { |
| 27 | + if (a == b) |
| 28 | + throw new Exception("Assert.NotEqual failed"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public struct SimpleVector3 |
| 33 | + { |
| 34 | + // Introduce lambda expressions to process array values. |
| 35 | + static Func<int, int> multiply = x => x * 2; |
| 36 | + |
| 37 | + static void Vector3GetHashCodeTest(ref int checksum) |
| 38 | + { |
| 39 | + Vector3 one = new Vector3(2.0f, 3.0f, 3.3f); |
| 40 | + Vector3 two = new Vector3(2.0f, 3.0f, 3.3f); |
| 41 | + Vector3 three = new Vector3(3.0f, 2.0f, 3.3f); |
| 42 | + Assert.Equal(one.GetHashCode(), two.GetHashCode()); |
| 43 | + Assert.NotEqual(one.GetHashCode(), three.GetHashCode()); |
| 44 | + Vector3 zero = new Vector3(0.0f, 0.0f, 0.0f); |
| 45 | + Vector3 oneAxis = new Vector3(1.0f, 0.0f, 0.0f); |
| 46 | + Assert.NotEqual(zero.GetHashCode(), oneAxis.GetHashCode()); |
| 47 | + |
| 48 | + // Create an array and then use a for-loop with invariant bounds. |
| 49 | + float[] items = new float[] { one.X, one.Y, one.Z, three.X, three.Y }; |
| 50 | + for (int i = 0, cnt = items.Length; i < cnt; i++) |
| 51 | + { |
| 52 | + checksum += (int)(items[i] * 1.0f); |
| 53 | + } |
| 54 | + // Use a for-each loop conditionally. |
| 55 | + if (checksum % 4 == 0) |
| 56 | + { |
| 57 | + foreach (float f in items) |
| 58 | + { |
| 59 | + checksum += multiply((int)f); |
| 60 | + } |
| 61 | + } |
| 62 | + Console.WriteLine("Intermediate Checksum (Mutation 5): " + checksum); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public static int Problem() |
| 67 | + { |
| 68 | + int checksum = 19; |
| 69 | + int returnVal = Pass; |
| 70 | + try |
| 71 | + { |
| 72 | + Vector3GetHashCodeTest(ref checksum); |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + Console.WriteLine("FAILED: " + ex.Message); |
| 77 | + returnVal = Fail; |
| 78 | + } |
| 79 | + // Additional clonable loop using same array in a different context. |
| 80 | + int[] reusedArray = { 2, 4, 6, 8, 10 }; |
| 81 | + for (int i = 0, length = reusedArray.Length; i < length; i++) |
| 82 | + { |
| 83 | + checksum += (i % 2 == 0) ? reusedArray[i] : -reusedArray[i]; |
| 84 | + } |
| 85 | + Console.WriteLine("Final Checksum (Mutation 5): " + checksum); |
| 86 | + return returnVal; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments