summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.cs
blob: 07c7c298772e160fd89afc967fb9e82817f6003b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]

namespace Benchstone.BenchI
{
public static class TreeSort
{

#if DEBUG
    public const int Iterations = 1;
#else
    public const int Iterations = 1200;
#endif

    const int SortElements = 5000;
    const int Modulus = 65536;

    sealed class Node
    {
        public Node Left;
        public Node Right;
        public int Val;
        public Node(int n)
        {
            Left = null;
            Right = null;
            Val = n;
        }
    }

    static int s_biggest;
    static int s_littlest;
    static int s_seed;

    static void InitRand() {
        s_seed = 33;
    }

    static int Rand(ref int seed) {
        int multiplier = 25173;
        int increment = 13849;
        seed = (multiplier * seed + increment) % Modulus;
        return seed;
    }

    static void InitArray(int[] sortList) {
        InitRand();
        s_biggest = 0;
        s_littlest = Modulus;
        for (int i = 1; i <= SortElements; i++) {
            sortList[i] = Rand(ref s_seed) - 1;
            if (sortList[i] > s_biggest) {
                s_biggest = sortList[i];
            }
            else if (sortList[i] < s_littlest) {
                s_littlest = sortList[i];
            }
        }
    }

    static void Insert(int n, Node t) {
        if (n > t.Val) {
            if (t.Left == null) {
                t.Left = new Node(n);
            }
            else {
                Insert(n, t.Left);
            }
        }
        else if (n < t.Val) {
            if (t.Right == null) {
                t.Right = new Node(n);
            }
            else {
                Insert(n, t.Right);
            }
        }
    }

    static bool CheckTree(Node p) {
        bool result = true;
        if (p.Left != null) {
            if (p.Left.Val <= p.Val) {
                result = false;
            }
            else {
                result &= CheckTree(p.Left);
            }
        }

        if (p.Right != null) {
            if (p.Right.Val >= p.Val) {
                result = false;
            }
            else {
                result &= CheckTree(p.Right);
            }
        }

        return result;
    }

    static bool Trees(int[] sortList) {
        InitArray(sortList);
        Node tree = new Node(sortList[1]);
        for (int i = 2; i <= SortElements; i++) {
            Insert(sortList[i], tree);
        }
        bool result = CheckTree(tree);
        return result;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Bench() {
        int[] sortList = new int[SortElements + 1];
        bool result = Trees(sortList);
        return result;
    }

    [Benchmark]
    public static void Test() {
        foreach (var iteration in Benchmark.Iterations) {
            using (iteration.StartMeasurement()) {
                for (int i = 0; i < Iterations; i++) {
                    Bench();
                }
            }
        }
    }

    static bool TestBase() {
        bool result = true;
        for (int i = 0; i < Iterations; i++) {
            result &= Bench();
        }
        return result;
    }

    public static int Main() {
        bool result = TestBase();
        return (result ? 100 : -1);
    }
}
}