| ... | @@ -168,6 +168,32 @@ test "expectEqual.union(enum)" { | ... | @@ -168,6 +168,32 @@ test "expectEqual.union(enum)" { |
| 168 | expectEqual(a10, a10); | 168 | expectEqual(a10, a10); |
| 169 | } | 169 | } |
| 170 | | 170 | |
| | 171 | /// This function is intended to be used only in tests. When the actual value is not |
| | 172 | /// within the margin of the expected value, |
| | 173 | /// prints diagnostics to stderr to show exactly how they are not equal, then aborts. |
| | 174 | /// The types must be floating point |
| | 175 | pub fn expectWithinMargin(expected: var, actual: @TypeOf(expected), margin: @TypeOf(expected)) void { |
| | 176 | std.debug.assert(margin >= 0.0); |
| | 177 | |
| | 178 | switch (@typeInfo(@TypeOf(actual))) { |
| | 179 | .Float, |
| | 180 | .ComptimeFloat, |
| | 181 | => { |
| | 182 | if (@fabs(expected - actual) > margin) { |
| | 183 | std.debug.panic("actual {}, not within margin {} of expected {}", .{ actual, margin, expected }); |
| | 184 | } |
| | 185 | }, |
| | 186 | else => @compileError("Unable to compare non floating point values"), |
| | 187 | } |
| | 188 | } |
| | 189 | |
| | 190 | test "expectWithinMargin.f32" { |
| | 191 | const x: f32 = 12.0; |
| | 192 | const y: f32 = 12.06; |
| | 193 | |
| | 194 | expectWithinMargin(x, y, 0.1); |
| | 195 | } |
| | 196 | |
| 171 | /// This function is intended to be used only in tests. When the two slices are not | 197 | /// This function is intended to be used only in tests. When the two slices are not |
| 172 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, | 198 | /// equal, prints diagnostics to stderr to show exactly how they are not equal, |
| 173 | /// then aborts. | 199 | /// then aborts. |