Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchI / BubbleSort2 / BubbleSort2.cs
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 // See the LICENSE file in the project root for more information.
4 //
5
6 using Microsoft.Xunit.Performance;
7 using System;
8 using System.Runtime.CompilerServices;
9 using Xunit;
10
11 [assembly: OptimizeForBenchmarks]
12
13 namespace Benchstone.BenchI
14 {
15 public static class BubbleSort2
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20     public const int Bound = 5 * Iterations;
21 #else
22     public const int Iterations = 15;
23     public const int Bound = 500 * Iterations;
24 #endif
25
26     static void Inner(int[] x) {
27         int limit1 = Bound - 1;
28         for (int i = 1; i <= limit1; i++) {
29             for (int j = i; j <= Bound; j++) {
30                 if (x[i] > x[j]) {
31                     int temp = x[j];
32                     x[j] = x[i];
33                     x[i] = temp;
34                 }
35             }
36         }
37     }
38
39     [MethodImpl(MethodImplOptions.NoInlining)]
40     static bool Bench() {
41         int[] x = new int[Bound + 1];
42         int i, j;
43         int limit;
44         j = 99999;
45         limit = Bound - 2;
46         i = 1;
47         do {
48             x[i] = j & 32767;
49             x[i + 1] = (j + 11111) & 32767;
50             x[i + 2] = (j + 22222) & 32767;
51             j = j + 33333;
52             i = i + 3;
53         } while (i <= limit);
54         x[Bound - 1] = j;
55         x[Bound] = j;
56
57         Inner(x);
58
59         for (i = 0; i < Bound - 1; i++) {
60             if (x[i] > x[i + 1]) {
61                 return false;
62             }
63         }
64
65         return true;
66     }
67
68     [Benchmark]
69     public static void Test() {
70         foreach (var iteration in Benchmark.Iterations) {
71             using (iteration.StartMeasurement()) {
72                 for (int i = 0; i < Iterations; i++) {
73                     Bench();
74                 }
75             }
76         }
77     }
78
79     static bool TestBase() {
80         bool result = true;
81         for (int i = 0; i < Iterations; i++) {
82             result &= Bench();
83         }
84         return result;
85     }
86
87     public static int Main() {
88         bool result = TestBase();
89         return (result ? 100 : -1);
90     }
91 }
92 }