Source: tests.js

/**
 * @file Some tests.
 * @version March 26, 2017
 *
 * @author Olivier Pirson --- http://www.opimedia.be/
 * @license GPLv3 --- Copyright (C) 2017 Olivier Pirson
 */

(function () {
    "use strict";

    function add2Matchings() {
        const points = [new Point(10, 10),
                        new Point(50, 200),
                        new Point(100, 150),
                        new Point(150, 250),
                        new Point(200, 170),
                        new Point(300, 150),
                        new Point(400, 100),
                        new Point(410, 290)];

        for (let point of points) {
            globalController.leftController.view.matching.pointAdd(point);
        }

        if (true) {
            for (let i = 0; i < points.length - 1; i += 2) {
                const leftSegment = new Segment(points[i], points[i + 1]);

                globalController.leftController.view.matching.segmentAdd(leftSegment);

                const rightSegment = new Segment(points[i + 1], points[(i + 2)%points.length]);

                globalController.rightController.view.matching.segmentAdd(rightSegment);
            }
        }

        globalController.globalView.update();
    }


    function testCompare() {
        assert(compare(42, 42) === 0);
        assert(compare(42, 666) === -1);
        assert(compare(-42, 666) === -1);
        assert(compare(666, 42) === 1);
        assert(compare(-666, 42) === -1);

        assert(compare("foo", "foo") === 0);
        assert(compare("foo", "bar") === 1);
        assert(compare("bar", "foo") === -1);
        assert(compare("bar", "FOO") === 1);
    }


    function testGetRandom() {
        for (let i = 0; i < 100; ++i) {
            const x = getRandom(-5, 17);

            assert(-5 <= x, x);
            assert(x < 17, x);
        }

        for (let i = 0; i < 100; ++i) {
            const x = getRandom(42.5, 42.8);

            assert(42.5 <= x, x);
            assert(x < 42.8, x);
        }
    }


    function testPoint() {
        {
            let p = new Point(0, 0);

            assert(p.x === 0, p);
            assert(p.y === 0, p);
            assert(p.distanceTo0Sqr() === 0, p);
            assert(p.distanceTo0() === 0, p);


            p = new Point(42, 0);
            assert(p.x === 42, p);
            assert(p.y === 0, p);
            assert(p.distanceTo0Sqr() === 42*42, p);
            assert(p.distanceTo0() === 42, p);

            p = new Point(0, 42);
            assert(p.x === 0, p);
            assert(p.y === 42, p);
            assert(p.distanceTo0Sqr() === 42*42, p);
            assert(p.distanceTo0() === 42, p);

            p = new Point(-42, 0);
            assert(p.x === -42, p);
            assert(p.y === 0, p);
            assert(p.distanceTo0Sqr() === 42*42, p);
            assert(p.distanceTo0() === 42, p);

            p = new Point(0, -42);
            assert(p.x === 0, p);
            assert(p.y === -42, p);
            assert(p.distanceTo0Sqr() === 42*42, p);
            assert(p.distanceTo0() === 42, p);
        }

        {
            const sqr = 2*42*42;
            const sqrt = Math.sqrt(sqr);

            assert(isFloatEqual(sqrt, 59,39696962), sqrt);

            let p = new Point(42, 42);

            assert(p.x === 42, p);
            assert(p.y === 42, p);
            assert(p.distanceTo0Sqr() === sqr, p);
            assert(p.distanceTo0() === sqrt, p);

            p = new Point(-42, 42);
            assert(p.x === -42, p);
            assert(p.y === 42, p);
            assert(p.distanceTo0Sqr() === sqr, p);
            assert(p.distanceTo0() === sqrt, p);

            p = new Point(-42, -42);
            assert(p.x === -42, p);
            assert(p.y === -42, p);
            assert(p.distanceTo0Sqr() === sqr, p);
            assert(p.distanceTo0() === sqrt, p);

            p = new Point(42, -42);
            assert(p.x === 42, p);
            assert(p.y === -42, p);
            assert(p.distanceTo0Sqr() === sqr, p);
            assert(p.distanceTo0() === sqrt, p);
        }

        {
            const p = new Point(-666, 365);

            assert(p.x === -666, p);
            assert(p.y === 365, p);
            assert(p.distanceTo0Sqr() === 666*666 + 365*365, p);
            assert(p.distanceTo0() === Math.sqrt(666*666 + 365*365), p);
        }

        for (let i = 0; i < 100; ++i) {
            const x = getRandom(-100, 100);
            const y = getRandom(-100, 100);
            const p = new Point(x, y);

            assert(p.x === x, x, p);
            assert(p.y === y, y, p);
            assert(isFloatEqual(p.distanceTo0Sqr(), x*x + y*y), x, y, p);
            assert(isFloatEqual(p.distanceTo0(), Math.sqrt(x*x + y*y)), x, y, p);
        }
    }


    function testSortedArrayBisectionLeft() {
        {
            const a = [];

            assert(sortedArrayBisectionLeft(a, 666) === 0);
        }

        {
            const a = [42];

            assert(sortedArrayBisectionLeft(a, 36) === 0);
            assert(sortedArrayBisectionLeft(a, 42) === 0);
            assert(sortedArrayBisectionLeft(a, 666) === 1);
        }

        {
            const a = [42, 666];

            assert(sortedArrayBisectionLeft(a, 36) === 0);
            assert(sortedArrayBisectionLeft(a, 42) === 0);
            assert(sortedArrayBisectionLeft(a, 666) === 1);
            assert(sortedArrayBisectionLeft(a, 1000) === 2);
        }

        {
            const a = [42, 666, 1000];

            assert(sortedArrayBisectionLeft(a, 36) === 0);
            assert(sortedArrayBisectionLeft(a, 42) === 0);
            assert(sortedArrayBisectionLeft(a, 666) === 1);
            assert(sortedArrayBisectionLeft(a, 1000) === 2);
            assert(sortedArrayBisectionLeft(a, 2000) === 3);
        }
    }


    function testSortedArrayInsert() {
        const a = [];

        sortedArrayInsert(a, 42);
        assert(arrayIsEquals(a, [42]));

        sortedArrayInsert(a, 36);
        assert(arrayIsEquals(a, [36, 42]));

        sortedArrayInsert(a, 666);
        assert(arrayIsEquals(a, [36, 42, 666]));

        sortedArrayInsert(a, 42);
        assert(arrayIsEquals(a, [36, 42, 42, 666]));

        sortedArrayInsert(a, 1000);
        assert(arrayIsEquals(a, [36, 42, 42, 666, 1000]));

        sortedArrayInsert(a, 500);
        assert(arrayIsEquals(a, [36, 42, 42, 500, 666, 1000]));

        sortedArrayInsert(a, 0);
        assert(arrayIsEquals(a, [0, 36, 42, 42, 500, 666, 1000]));
    }


    function runTest(fct, name) {
        console.log("--- BEGIN test", name, "---");
        fct();
        console.log("--- END test", name, "---");
    }


    window.addEventListener("load", function () {
        if (!isAssert()) {
            return;
        }

        console.log("===== BEGIN tests ======");

        runTest(testCompare, "compare");
        runTest(testGetRandom, "getRandom");
        runTest(testSortedArrayBisectionLeft, "sortedArrayBisectionLeft");
        runTest(testSortedArrayInsert, "sortedArrayInsert");

        runTest(testPoint, "class Point");

        console.log("===== END tests ======");

        add2Matchings();
    });
}());