A game about forced loneliness, made by TACStudios
1using System;
2using System.Runtime.CompilerServices;
3using NUnit.Framework;
4
5/// <summary>
6/// This class mirrors parts of the Assert API from NUnit in a sane way.
7/// The problem with NUnit is that stuff like Assert.AreEqual(15, 16) creates allocations behind the scenes, so tests
8/// that checks large collections will spend the vast majority of their time just checking their results.
9/// You can use this by writing
10/// using Assert = FastAssert;
11/// at the top of your file. There are some parts of the API that you may need to fix up manually, mainly because this
12/// class does not expose overloads like Assert.AreEqual(object a, object b) because that's just asking for pain.
13/// </summary>
14internal static class FastAssert
15{
16 [MethodImpl(MethodImplOptions.AggressiveInlining)]
17 public static void IsTrue(bool b)
18 {
19 if (!b)
20 {
21 Assert.IsTrue(b);
22 }
23 }
24
25 [MethodImpl(MethodImplOptions.AggressiveInlining)]
26 public static void IsTrue(bool b, string msg)
27 {
28 if (!b)
29 {
30 Assert.IsTrue(b, msg);
31 }
32 }
33
34 [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 public static void True(bool b)
36 {
37 if (!b)
38 {
39 Assert.IsTrue(b);
40 }
41 }
42
43 [MethodImpl(MethodImplOptions.AggressiveInlining)]
44 public static void True(bool b, string msg)
45 {
46 if (!b)
47 {
48 Assert.IsTrue(b, msg);
49 }
50 }
51
52 [MethodImpl(MethodImplOptions.AggressiveInlining)]
53 public static void IsFalse(bool b)
54 {
55 if (b)
56 {
57 Assert.IsFalse(b);
58 }
59 }
60
61 [MethodImpl(MethodImplOptions.AggressiveInlining)]
62 public static void IsFalse(bool b, string msg)
63 {
64 if (b)
65 {
66 Assert.IsFalse(b, msg);
67 }
68 }
69
70 [MethodImpl(MethodImplOptions.AggressiveInlining)]
71 public static void False(bool b)
72 {
73 if (b)
74 {
75 Assert.IsFalse(b);
76 }
77 }
78
79 [MethodImpl(MethodImplOptions.AggressiveInlining)]
80 public static void False(bool b, string msg)
81 {
82 if (b)
83 {
84 Assert.IsFalse(b, msg);
85 }
86 }
87
88 [MethodImpl(MethodImplOptions.AggressiveInlining)]
89 public static void AreEqual<T>(in T a, in T b) where T : IEquatable<T>
90 {
91 if (!a.Equals(b))
92 {
93 Assert.Fail($"{a} != {b}");
94 }
95 }
96
97 [MethodImpl(MethodImplOptions.AggressiveInlining)]
98 public static void AreEqual<T>(in T a, in T b, string msg) where T : IEquatable<T>
99 {
100 if (!a.Equals(b))
101 {
102 Assert.Fail($"{a} != {b}: {msg}");
103 }
104 }
105
106 [MethodImpl(MethodImplOptions.AggressiveInlining)]
107 public static void AreNotEqual<T>(in T a, in T b) where T : IEquatable<T>
108 {
109 if (a.Equals(b))
110 {
111 Assert.Fail($"{a} == {b}");
112 }
113 }
114
115 [MethodImpl(MethodImplOptions.AggressiveInlining)]
116 public static void AreNotEqual<T>(in T a, in T b, string msg) where T : IEquatable<T>
117 {
118 if (a.Equals(b))
119 {
120 Assert.Fail($"{a} == {b}: {msg}");
121 }
122 }
123
124 [MethodImpl(MethodImplOptions.AggressiveInlining)]
125 public static void LessOrEqual<T>(in T a, in T b) where T : IComparable<T>
126 {
127 if (a.CompareTo(b) > 0)
128 {
129 Assert.Fail($"{a} > {b}");
130 }
131 }
132
133 [MethodImpl(MethodImplOptions.AggressiveInlining)]
134 public static void LessOrEqual<T>(in T a, in T b, string msg) where T : IComparable<T>
135 {
136 if (a.CompareTo(b) > 0)
137 {
138 Assert.Fail($"{a} > {b}: {msg}");
139 }
140 }
141
142 [MethodImpl(MethodImplOptions.AggressiveInlining)]
143 public static void Less<T>(in T a, in T b) where T : IComparable<T>
144 {
145 if (a.CompareTo(b) >= 0)
146 {
147 Assert.Fail($"{a} >= {b}");
148 }
149 }
150
151 [MethodImpl(MethodImplOptions.AggressiveInlining)]
152 public static void Less<T>(in T a, in T b, string msg) where T : IComparable<T>
153 {
154 if (a.CompareTo(b) >= 0)
155 {
156 Assert.Fail($"{a} >= {b}: {msg}");
157 }
158 }
159
160 [MethodImpl(MethodImplOptions.AggressiveInlining)]
161 public static void GreaterOrEqual<T>(in T a, in T b) where T : IComparable<T>
162 {
163 if (a.CompareTo(b) < 0)
164 {
165 Assert.Fail($"{a} < {b}");
166 }
167 }
168
169 [MethodImpl(MethodImplOptions.AggressiveInlining)]
170 public static void GreaterOrEqual<T>(in T a, in T b, string msg) where T : IComparable<T>
171 {
172 if (a.CompareTo(b) < 0)
173 {
174 Assert.Fail($"{a} < {b}: {msg}");
175 }
176 }
177
178 [MethodImpl(MethodImplOptions.AggressiveInlining)]
179 public static void Greater<T>(in T a, in T b) where T : IComparable<T>
180 {
181 if (a.CompareTo(b) <= 0)
182 {
183 Assert.Fail($"{a} <= {b}");
184 }
185 }
186
187 [MethodImpl(MethodImplOptions.AggressiveInlining)]
188 public static void Greater<T>(in T a, in T b, string msg) where T : IComparable<T>
189 {
190 if (a.CompareTo(b) <= 0)
191 {
192 Assert.Fail($"{a} <= {b}: {msg}");
193 }
194 }
195
196 [MethodImpl(MethodImplOptions.AggressiveInlining)]
197 public static void DoesNotThrow(TestDelegate del)
198 {
199 Assert.DoesNotThrow(del);
200 }
201
202 [MethodImpl(MethodImplOptions.AggressiveInlining)]
203 public static void Throws<T>(TestDelegate del) where T : Exception
204 {
205 Assert.Throws<T>(del);
206 }
207}