summaryrefslogtreecommitdiff
path: root/src/tests/efl_mono/TestUtils.cs
blob: d6f00b0b46b4e19e760c61d5c3d3c4c86350f191 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright 2019 by its authors. See AUTHORS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;


/// <summary>Exception for assertion failures.</summary>
[Serializable]
public class AssertionException : Exception
{
    /// <summary> Default constructor.</summary>
    public AssertionException() : base () { }
    /// <summary> Most commonly used contructor.</summary>
    public AssertionException(string msg) : base(msg) { }
    /// <summary> Wraps an inner exception.</summary>
    public AssertionException(string msg, Exception inner) : base(msg, inner) { }
    /// <summary> Serializable constructor.</summary>
    protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}

/// <summary> Helper class for Mono EFL bindings tests.</summary>
public static class Test
{
    /// <summary> Asserts a boolean condition.</summary>
    public static void Assert(bool res, String msg = "Assertion failed",
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (msg == null)
            msg = "Assertion failed.";
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        if (!res)
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
    }

    /// <summary> Asserts if lhs is equal to rhs, using lhs.Equals(rhs).</summary>
    public static void AssertEquals<T>(T lhs, T rhs, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (lhs == null && rhs == null)
            return;
        if (lhs == null || !lhs.Equals(rhs))
        {
            if (file == null)
                file = "(unknown file)";
            if (member == null)
                member = "(unknown member)";
            if (msg == null || msg.Length == 0)
                msg = $"Left hand side \"{lhs}\", right hand side \"{rhs}\"";
            throw new AssertionException($"{file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if lhs is not equal to rhs, using !lhs.Equals(rhs).</summary>
    public static void AssertNotEquals<T>(T lhs, T rhs, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (lhs == null ? rhs == null : lhs.Equals(rhs))
        {
            if (file == null)
                file = "(unknown file)";
            if (member == null)
                member = "(unknown member)";
            if (msg == null || msg.Length == 0)
                msg = $"Left hand side \"{lhs}\" shouldn't be equal to right hand side \"{rhs}\"";
            throw new AssertionException($"{file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if rhs is near enough of lhs, using the optional tolerance (default 0.00001).</summary>
    public static void AssertAlmostEquals(double lhs, double rhs, double tolerance=0.00001,
                              String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        double difference = Math.Abs(lhs - rhs);
        if (difference > tolerance) {
            if (msg == null || msg.Length == 0)
                msg = $"Left hand side \"{lhs}\". Difference: \"{difference}\"";
            throw new AssertionException($"{file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if greater is greater than smaller , using greater.CompareTo(smaller) > 0.</summary>
    public static void AssertGreaterThan<T>(T greater, T smaller, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null) where T : System.IComparable<T>
    {
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        if (greater == null || smaller == null)
            throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull.");
        if (greater.CompareTo(smaller) <= 0) {
            if (msg == null || msg.Length == 0)
                msg = $"Greater \"{greater}\" is not greater than smaller \"{smaller}\"";
            throw new AssertionException($"{file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if smaller is smaller than greater, using greater.CompareTo(smaller) &lt; 0.</summary>
    public static void AssertLessThan<T>(T smaller, T greater, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null) where T : System.IComparable<T>
    {
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        if (greater == null || smaller == null)
            throw new AssertionException($"{file}:{line} ({member}) Null input value. Use AssertNull.");
        if (smaller.CompareTo(greater) >= 0) {
            if (msg == null || msg.Length == 0)
                msg = $"Smaller \"{smaller}\" is not smaller than greater \"{greater}\"";
            throw new AssertionException($"{file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if op, when called, raises the exception T.</summary>
    [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")]
    public static void AssertRaises<T>(Action op, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null) where T: Exception
    {
        if (msg == null)
            msg = "Exception not raised.";
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        if (op == null)
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation.");
        try {
            op();
        } catch (T) {
            return;
        }
        throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
    }

    /// <summary> Asserts if op, when called, does not raise the exception T.</summary>
    [SuppressMessage("Gendarme.Rules.Design.Generic", "AvoidMethodWithUnusedGenericTypeRule")]
    public static void AssertNotRaises<T>(Action op, String msg = null,
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null) where T: Exception
    {
        if (msg == null)
            msg = "Exception raised.";
        if (file == null)
            file = "(unknown file)";
        if (member == null)
            member = "(unknown member)";
        if (op == null)
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) Null operation.");
        try {
            op();
        } catch (T) {
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
        }
    }

    /// <summary> Asserts if the given reference is null.</summary>
    public static void AssertNull(object reference, String msg = "Reference not null",
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (reference != null)
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
    }

    /// <summary> Asserts if the given reference is not null.</summary>
    public static void AssertNotNull(object reference, String msg = "Reference is null",
                              [CallerLineNumber] int line = 0,
                              [CallerFilePath] string file = null,
                              [CallerMemberName] string member = null)
    {
        if (reference == null)
            throw new AssertionException($"Assertion failed: {file}:{line} ({member}) {msg}");
    }

    /// <summary>Runs a number of garbage collections and iterate the main loop.
    /// The iteration is needed to make sure objects collected in the GC thread
    /// are efl_unref'd in the main thread.</summary>
    public static void CollectAndIterate(int iterations=1000, int global_iterations=1)
    {
        for (int g = 0; g < global_iterations; ++g)
        {
            for (int i = 0; i < iterations; ++i)
            {
                System.GC.Collect();
            }
            System.GC.WaitForPendingFinalizers();
            Efl.App.AppMain.Iterate();
        }
    }

}