Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Nov 8, 2020
1 parent caddc42 commit 66df589
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bin/ODiffBin.re
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let main =
</Pastel>,
);

Images.Rgba32(diff) |> Png.save(diffPath, []);
Odiff.ImageIO.saveImage(diffPath, diff)
exit(1);
} else {
Console.log(
Expand Down
3 changes: 2 additions & 1 deletion esy.lock/index.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"buildEnv": { "ODOC_SYNTAX": "re" }
},
"scripts": {
"test": "esy x ODiffBin",
"run": "esy x ODiffBin",
"test": "esy x RunTests.exe",
"format": "esy dune build @fmt --auto-promote",
"doc": "esy dune build @doc"
},
Expand Down
9 changes: 6 additions & 3 deletions src/Diff.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ let redPixel: Rgba32.elt = {
},
};

let maxYIQPossibleDelta = 35215.

let compare = (a, b, diff, ~threshold=0.1, ()) => {
let diffCount = ref(0);
let (base, comp) = calcSize(a) > calcSize(b) ? (a, b) : (b, a);
let maxDelta = maxYIQPossibleDelta *. threshold *. threshold

for (x in 0 to base.width - 1) {
for (y in 0 to base.height - 1) {
Expand All @@ -21,11 +24,11 @@ let compare = (a, b, diff, ~threshold=0.1, ()) => {
if (r != r1 || g != g1 || b != b1 || a != a1) {
let delta =
ColorDelta.calculatePixelColorDelta(
(r, b, g, a),
(r1, b1, g1, a1),
(r, g, b, a),
(r1, g1, b1, a1),
);

if (delta > threshold) {
if (delta > maxDelta) {
diffCount := diffCount^ + 1;
Rgba32.set(diff, x, y, redPixel);
};
Expand Down
3 changes: 3 additions & 0 deletions src/ImageIO.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ let loadImage = filename => {
};
};

let saveImage = (filename, imageBuffer) =>
Images.Rgba32(imageBuffer) |> Png.save(filename, []);

let readImgColor = (x, y, img) => {
let (bytes, position) = Rgba32.unsafe_access(img, x, y);

Expand Down
42 changes: 42 additions & 0 deletions test/PngTests.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
open TestFramework;

describe("Png comparing", ({test, _}) => {
test("finds different between 2 images", ({expect, _}) => {
let img1 = Odiff.ImageIO.loadImage("test/test-images/orange.png");
let img2 = Odiff.ImageIO.loadImage("test/test-images/orange_changed.png");

let diffOutput = Rgba32.copy(img1);

let diffPixels = Odiff.Diff.compare(img1, img2, diffOutput, ());
expect.int(diffPixels).toBe(1430);
});

test("uses provided threshold", ({expect, _}) => {
let img1 = Odiff.ImageIO.loadImage("test/test-images/orange.png");
let img2 = Odiff.ImageIO.loadImage("test/test-images/orange_changed.png");

let diffOutput = Rgba32.copy(img1);

let diffPixels =
Odiff.Diff.compare(img1, img2, diffOutput, ~threshold=1.0, ());
expect.int(diffPixels).toBe(184);
});

test("create right diff mask", ({expect, _}) => {
let img1 = Odiff.ImageIO.loadImage("test/test-images/orange.png");
let img2 = Odiff.ImageIO.loadImage("test/test-images/orange_changed.png");

let diffOutput = Rgba32.create(img1.width, img1.height);
Odiff.Diff.compare(img1, img2, diffOutput, ()) |> ignore;

let originalDiff =
Odiff.ImageIO.loadImage("test/test-images/orange-diff.png");
let diffMaskOfDiff =
Rgba32.create(originalDiff.width, originalDiff.height);

let diffOfDiffPixels =
Odiff.Diff.compare(originalDiff, diffOutput, diffMaskOfDiff, ());

expect.int(diffOfDiffPixels).toBe(0);
});
});
1 change: 1 addition & 0 deletions test/RunTests.re
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OdiffTests.TestFramework.cli()
7 changes: 7 additions & 0 deletions test/TestFramework.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include Rely.Make({
let config =
Rely.TestFrameworkConfig.initialize({
snapshotDir: "test/__snapshots__",
projectDir: "."
});
});
21 changes: 21 additions & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(library
(name OdiffTests)
; the linkall flag ensures that all of our tests are compiled and the
; -g flag emits debugging information
(ocamlopt_flags -linkall -g)
; you will want to depend on the library you are testing as well, however for
; the purposes of this example we are only depending on the test runner itself
(libraries rely.lib odiff)
(modules (:standard \ RunTests))
)
(executable
; the for the library is automatically detected because of the name, but we
; need to explicitly specify the package here
(package odiff)
(name RunTests)
(public_name RunTests.exe)
(libraries
OdiffTests
)
(modules RunTests)
)
Binary file added test/test-images/orange-diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test-images/orange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/test-images/orange_changed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 66df589

Please sign in to comment.