Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / BubbleSort / BubbleSort.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 BubbleSort
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 55000;
22 #endif
23
24     static void SortArray(int[] tab, int last) {
25         bool swap;
26         int temp;
27         do {
28             swap = false;
29             for (int i = 0; i < last; i++) {
30                 if (tab[i] > tab[i + 1]) {
31                     temp = tab[i];
32                     tab[i] = tab[i + 1];
33                     tab[i + 1] = temp;
34                     swap = true;
35                 }
36             }
37         }
38         while (swap);
39     }
40
41     static bool VerifySort(int[] tab, int last) {
42         for (int i = 0; i < last; i++) {
43             if (tab[i] > tab[i + 1]) {
44                 return false;
45             }
46         }
47
48         return true;
49     }
50
51     [MethodImpl(MethodImplOptions.NoInlining)]
52     static public bool Bench() {
53         int[] tab = new int[100];
54         int k = 0;
55         for (int i = 9; i >= 0; i--) {
56             for (int j = i * 10; j < (i + 1) * 10; j++) {
57                 tab[k++] = ((j & 1) == 1) ? j + 1 : j - 1;
58             }
59         }
60         SortArray(tab, 99);
61         bool result = VerifySort(tab, 99);
62         return result;
63     }
64
65     [Benchmark]
66     public static void Test() {
67         foreach (var iteration in Benchmark.Iterations) {
68             using (iteration.StartMeasurement()) {
69                 for (int i = 0; i < Iterations; i++) {
70                     Bench();
71                 }
72             }
73         }
74     }
75
76     static bool TestBase() {
77         bool result = true;
78         for (int i = 0; i < Iterations; i++) {
79             result &= Bench();
80         }
81         return result;
82     }
83
84     public static int Main() {
85         bool result = TestBase();
86         return (result ? 100 : -1);
87     }
88 }
89 }