Skip to content

Commit

Permalink
Merge pull request #12 from Zardoz89/feature/issue#11
Browse files Browse the repository at this point in the history
Resolves issue #11
  • Loading branch information
Zardoz89 committed Mar 27, 2021
2 parents 8f2a434 + bb127a5 commit 7a572b8
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
os: linux
dist: bionic
dist: focal
language: d

jobs:
include:
- d: dmd-nightly
- d: dmd
- d: dmd-2.090.1
- d: dmd-2.091.1
- d: ldc
allow_failures:
- d: dmd-nightly
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.3.4

* Update to DLang frontend 2.91
* Using std.math.isClose instead of .approxEquals
* Added .close as alias of .approxEquals

# v0.3.3

* .throw must be @trusted, to allow to catch Errors
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ assertion.
255.should.equal(10); // Throws an Exception "expected 255 to equal 10"
```

#### `T approxEqual(U)(U other, U maxRelDiff = 1e-2, U maxAbsDiff = 1e-05, string file = __FILE__, size_t line = __LINE__);`
#### `T approxEqual(U)(U other, U maxRelDiff = CommonDefaultFor!(T,U), U maxAbsDiff = 0.0, string file = __FILE__, size_t line = __LINE__);`

Asserts for aproximated equality of float types. Returns the value wrapped around the
assertion.
Asserts for approximated equality of float types. Returns the value wrapped around the
assertion. See Phobos std.math.isClose().
```d
(1.0f).should.be.approxEqual(1.00000001);
(1.0f).should.not.be.approxEqual(1.001);
(1.0f).should.not.be.approxEqual(1.01);
```
#### `T close(U)(U other, U maxRelDiff = CommonDefaultFor!(T,U), U maxAbsDiff = 0.0, string file = __FILE__, size_t line = __LINE__);`

Alias of approxEqual

#### `T exist(string file = __FILE__, size_t line = __LINE__);`

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
DVersion: stable
arch: x86
- DC: dmd
DVersion: 2.090.1
DVersion: 2.091.1
arch: x86
- DC: ldc
DVersion: stable
Expand Down
Binary file added docs/assets/img/logo-dub.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
layout: default
title: Changelog
---
# v0.3.4

* Update to DLang frontend 2.91
* Using std.math.isClose instead of .approxEquals
* Added .close as alias of .approxEquals

# v0.3.3

* .throw must be @trusted, to allow to catch Errors
Expand Down
11 changes: 7 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ assertion.
255.should.equal(10); // Throws an Exception "expected 255 to equal 10"
```

#### `T approxEqual(U)(U other, U maxRelDiff = 1e-2, U maxAbsDiff = 1e-05, string file = __FILE__, size_t line = __LINE__);`
#### `T approxEqual(U)(U other, U maxRelDiff = CommonDefaultFor!(T,U), U maxAbsDiff = 0.0, string file = __FILE__, size_t line = __LINE__);`

Asserts for aproximated equality of float types. Returns the value wrapped around the
assertion.
Asserts for approximated equality of float types. Returns the value wrapped around the
assertion. See Phobos std.math.isClose().
```d
(1.0f).should.be.approxEqual(1.00000001);
(1.0f).should.not.be.approxEqual(1.001);
(1.0f).should.not.be.approxEqual(1.01);
```
#### `T close(U)(U other, U maxRelDiff = CommonDefaultFor!(T,U), U maxAbsDiff = 0.0, string file = __FILE__, size_t line = __LINE__);`

Alias of approxEqual

#### `T exist(string file = __FILE__, size_t line = __LINE__);`

Expand Down
139 changes: 137 additions & 2 deletions docs/pijamas.html
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ <h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.approxEqual"></span>@trusted T <code class="code">approxEqual</code>(U = double)(U <code class="code">other</code>, U <code class="code">maxRelDiff</code> = 0.01, U <code class="code">maxAbsDiff</code> = 1e-05, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__) if (is(T : real) &amp;&amp; __traits(isFloating, T) &amp;&amp; is(U : real) &amp;&amp; __traits(isFloating, U));
<span class="ddoc_anchor" id="Assertion.approxEqual"></span>@trusted T <code class="code">approxEqual</code>(U = double)(U <code class="code">other</code>, U <code class="code">maxRelDiff</code> = CommonDefaultFor!(T, U), U <code class="code">maxAbsDiff</code> = 0.0, string <code class="code">file</code> = __FILE__, size_t <code class="code">line</code> = __LINE__) if (is(T : real) &amp;&amp; __traits(isFloating, T) &amp;&amp; is(U : real) &amp;&amp; __traits(isFloating, U));

</code>
</p>
Expand All @@ -771,6 +771,89 @@ <h4>Declaration</h4>

</p>
</div>
<div class="ddoc_params">
<h4>Parameters</h4>
<table cellspacing="0" cellpadding="5" border="0" class="graybox">
<tbody>
<tr class="ddoc_param_row">
<td scope="ddoc_param_id">
<code class="code">
<em class="term">U <code class="code">other</code></em>
</code>
</td>
<td>
<div class="ddoc_param_desc">
<p class="para">
Value to compare to compare.
</p>
</div>
</td>
</tr>
<tr class="ddoc_param_row">
<td scope="ddoc_param_id">
<code class="code">
<em class="term">U <code class="code">maxRelDiff</code></em>
</code>
</td>
<td>
<div class="ddoc_param_desc">
<p class="para">
Maximum allowable relative difference.
Setting to 0.0 disables this check. Default depends on the type of
<code class="code"><code class="code">other</code></code> and the original valie: It is approximately half the number of decimal digits of
precision of the smaller type.
</p>
</div>
</td>
</tr>
<tr class="ddoc_param_row">
<td scope="ddoc_param_id">
<code class="code">
<em class="term">U <code class="code">maxAbsDiff</code></em>
</code>
</td>
<td>
<div class="ddoc_param_desc">
<p class="para">
Maximum absolute difference. This is mainly usefull
for comparing values to zero. Setting to 0.0 disables this check.
Defaults to <code class="code">0.0</code>.
</p>
</div>
</td>
</tr>
<tr class="ddoc_param_row">
<td scope="ddoc_param_id">
<code class="code">
<em class="term">string <code class="code">file</code></em>
</code>
</td>
<td>
<div class="ddoc_param_desc">
<p class="para">
filename
</p>
</div>
</td>
</tr>
<tr class="ddoc_param_row">
<td scope="ddoc_param_id">
<code class="code">
<em class="term">size_t <code class="code">line</code></em>
</code>
</td>
<td>
<div class="ddoc_param_desc">
<p class="para">
<code class="code">line</code> number inside of <code class="code">file</code>
</p>
</div>
</td>
</tr>

</tbody>
</table>
</div>
<div class="ddoc_examples">
<h4>Examples</h4>
<p class="para">
Expand All @@ -780,7 +863,7 @@ <h4>Examples</h4>
<div class="dlang">
<ol class="code_lines">
<li><code class="code"><span class="keyword">double</span> d = 0.1;
<span class="keyword">double</span> d2 = d + 1e-05;
<span class="keyword">double</span> d2 = d + 1e-10;
d.should.not.be.equal(d2);
d.should.be.<span class="psymbol">approxEqual</span>(d2);
</code></li>
Expand All @@ -795,6 +878,58 @@ <h4>Examples</h4>

</div>

</li><li class="ddoc_member">
<div class="ddoc_member_header">
<div class="ddoc_header_anchor">
<a href="#Assertion.close" id="Assertion.close"><code class="code">close</code></a>
</div>
</div><div class="ddoc_decl">
<section class="section">
<div class="declaration">
<h4>Declaration</h4>
<div class="dlang">
<p class="para">
<code class="code">
<span class="ddoc_anchor" id="Assertion.close"></span>alias <code class="code">close</code> = approxEqual;

</code>
</p>
</div>
</div>
</section>
</div>
<div class="ddoc_decl">
<section class="section ddoc_sections">
<div class="ddoc_summary">
<p class="para">
Alias to approxEqual

</p>
</div>
<div class="ddoc_examples">
<h4>Examples</h4>
<p class="para">

<section class="code_listing">
<div class="code_sample">
<div class="dlang">
<ol class="code_lines">
<li><code class="code"><span class="keyword">double</span> d = 0.1;
<span class="keyword">double</span> d2 = d + 1e-10;
d.should.not.be.<span class="psymbol">close</span>(d2);
d.should.be.<span class="psymbol">close</span>(d2);
</code></li>
</ol>
</div>
</div>
</section>

</p>
</div>
</section>

</div>

</li><li class="ddoc_member">
<div class="ddoc_member_header">
<div class="ddoc_header_anchor">
Expand Down
4 changes: 2 additions & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"toolchainRequirements": {
"dub": ">=1.19.0",
"frontend": ">=2.090"
"dub": ">=1.20.0",
"frontend": ">=2.091"
}
}
63 changes: 59 additions & 4 deletions source/pijamas.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,82 @@ class Assertion(T)
return context;
}

// Ripped from std.math
private template CommonDefaultFor(T,U)
{
import std.traits : CommonType;
import std.algorithm.comparison : min;

alias baseT = FloatingPointBaseType!T;
alias baseU = FloatingPointBaseType!U;

enum CommonType!(baseT, baseU) CommonDefaultFor = 10.0L ^^ -((min(baseT.dig, baseU.dig) + 1) / 2 + 1);
}

private template FloatingPointBaseType(T)
{
import std.traits : isFloatingPoint;
import std.range.primitives : ElementType;
static if (isFloatingPoint!T)
{
alias FloatingPointBaseType = Unqual!T;
}
else static if (isFloatingPoint!(ElementType!(Unqual!T)))
{
alias FloatingPointBaseType = Unqual!(ElementType!(Unqual!T));
}
else
{
alias FloatingPointBaseType = real;
}
}

/**
* Asserts that a float type is aproximated equal. Returns the valued wrapped around the assertion
*
* Params:
* other = Value to compare to compare.
* maxRelDiff = Maximum allowable relative difference.
* Setting to 0.0 disables this check. Default depends on the type of
* `other` and the original valie: It is approximately half the number of decimal digits of
* precision of the smaller type.
* maxAbsDiff = Maximum absolute difference. This is mainly usefull
* for comparing values to zero. Setting to 0.0 disables this check.
* Defaults to `0.0`.
* file = filename
* line = line number inside of file
*
* Examples:
* ```
* double d = 0.1;
* double d2 = d + 1e-05;
* double d2 = d + 1e-10;
* d.should.not.be.equal(d2);
* d.should.be.approxEqual(d2);
* ```
*/
T approxEqual(U = double)(U other, U maxRelDiff = 1e-2, U maxAbsDiff = 1e-05,
T approxEqual(U = double)(U other, U maxRelDiff = CommonDefaultFor!(T,U), U maxAbsDiff = 0.0,
string file = __FILE__, size_t line = __LINE__) @trusted
if (is(T : real) && __traits(isFloating, T) && is(U : real) && __traits(isFloating, U))
{
import std.math : approxEqual;
import std.math : isClose;
operator = "be approximated equal than";
this.ok(approxEqual(context, other, maxRelDiff, maxAbsDiff), this.message(other), file, line);
this.ok(isClose(context, other, maxRelDiff, maxAbsDiff), this.message(other), file, line);
return context;
}

/**
* Alias to approxEqual
*
* Examples:
* ```
* double d = 0.1;
* double d2 = d + 1e-10;
* d.should.not.be.close(d2);
* d.should.be.close(d2);
* ```
*/
alias close = approxEqual;

/**
* Asserts whether a value exists - currently simply compares it with null, if it is a pointer, a class or a string.
* Returns the value wrapped around the assertion.
Expand Down
Loading

0 comments on commit 7a572b8

Please sign in to comment.