"use strict";
exports.__esModule = true;
require("../index");
var assert = require('assert');
describe('LinqArray', function () {
    it('distinct', function () {
        assert.deepEqual([1, 1, 2, 3, 2, 3, 3, 2, 3, 1, 3, 4, 2, 5, 3, 4, 3, 1, 4, 5, 1, 2, 3, 4, 5, 1, 4, 3, 1, 3, 2, 4, 1, 5, 3].distinct(), [1, 2, 3, 4, 5]);
        assert.deepEqual([1, '2', true, null, undefined, {}, [1, 2, 3], 1, '2', true, null, undefined, {}, [1, 2, 3]].distinct(), [1, '2', true, null, undefined, {}, [1, 2, 3], {}, [1, 2, 3]]);
    });
    it('max', function () {
        assert.equal([4, 9, 5, 1, 3].max(), 9);
        assert.equal([4, 9, 5, 1, 3].max(function (v) { return v * 2; }), 18);
        assert.equal([999999, 1, 2, 3, 6].max(function (v, i) { return v * i; }), 24);
        assert.equal([4, 9, 5, 1, 3].max(function (v, i, arr) { return arr[i] + 1; }), 10);
    });
    it('min', function () {
        assert.equal([4, 9, 5, 1, 3].min(), 1);
        assert.equal([4, 9, 5, 1, 3].min(function (v) { return v * 2; }), 2);
        assert.equal([999999, 1, 2, 3, 6].min(function (v, i) { return v * i; }), 0);
        assert.equal([4, 9, 5, 1, 3].min(function (v, i, arr) { return arr[i] + 1; }), 2);
    });
    it('filterIndex', function () {
        assert.deepEqual([10, 20, 30, 40, 50].filterIndex(function (v) { return v > 30; }), [3, 4]);
        assert.deepEqual([13, 14, 16, 18, 20].filterIndex(function (v, i, arr) { return (v + i * arr.length) % 2 == 1; }), [0, 1, 3]);
    });
    it('count', function () {
        assert.equal([1, 3, 5, 7, 9, 2, 4, 6, 8, 0].count(function (v) { return v % 2 > 0; }), 5);
        assert.equal([0, 1, 2, 3, 4, 555, 555, 555, 555].count(function (v, i) { return v === i; }), 5);
    });
    it('sum', function () {
        assert.equal([1, 2, 3, 4].sum(), 10);
        assert.equal([{ v: 1 }, { v: 2 }, { v: 3 }, { v: 4 }].sum(function (v) { return v.v; }), 10);
    });
    it('average', function () {
        assert.equal([1, 2, 3, 4].average(), 2.5);
        assert.equal([{ v: 1 }, { v: 2 }, { v: 3 }, { v: 4 }].average(function (v) { return v.v; }), 2.5);
    });
    it('orderBy', function () {
        var a = [
            { a: 6, b: 5 },
            { a: 4, b: 3 },
            { a: 6, b: 1 },
            { a: 2, b: 9 },
            { a: 4, b: 7 }
        ];
        assert.deepEqual(a.orderBy(function (v) { return v.a; }), [
            { a: 2, b: 9 },
            { a: 4, b: 3 },
            { a: 4, b: 7 },
            { a: 6, b: 5 },
            { a: 6, b: 1 }
        ]);
        assert.deepEqual(a.orderBy(function (v) { return v.a; }, function (v) { return v.b; }), [
            { a: 2, b: 9 },
            { a: 4, b: 3 },
            { a: 4, b: 7 },
            { a: 6, b: 1 },
            { a: 6, b: 5 }
        ]);
        assert.deepEqual(a.orderByDesc(function (v) { return v.b; }), [
            { a: 2, b: 9 },
            { a: 4, b: 7 },
            { a: 6, b: 5 },
            { a: 4, b: 3 },
            { a: 6, b: 1 }
        ]);
        assert.deepEqual(a.orderByDesc(function (v) { return v.a; }, function (v) { return v.b; }), [
            { a: 6, b: 5 },
            { a: 6, b: 1 },
            { a: 4, b: 7 },
            { a: 4, b: 3 },
            { a: 2, b: 9 }
        ]);
    });
    describe('binarySearch', function () {
        it('normal', function () {
            var arr = [1, 1, 20, 20, 20, 21, 30, 30, 40, 50, 50, 50];
            assert.equal(arr.binarySearch(30), 6);
            assert.equal(arr.binarySearch(31), -1);
            assert.equal(arr.binarySearch(1), 0);
            assert.equal(arr.binarySearch(50), 10);
            assert.equal(arr.binarySearch(20), 2);
        });
        it('keyMapper', function () {
            var arr = [1, 1, 20, 20, 20, 21, 30, 30, 40, 50, 50, 50].map(function (v) { return ({ a: v }); });
            assert.equal(arr.binarySearch(30, function (v) { return v.a; }), 6);
            assert.equal(arr.binarySearch(31, function (v) { return v.a; }), -1);
            assert.equal(arr.binarySearch(1, function (v) { return v.a; }), 0);
            assert.equal(arr.binarySearch(50, function (v) { return v.a; }), 10);
            assert.equal(arr.binarySearch(20, function (v) { return v.a; }), 2);
        });
    });
    describe('binaryInsert', function () {
        describe('unique', function () {
            it('normal', function () {
                var arr = [10, 20, 30, 40, 50];
                assert.equal(arr.binaryInsert(20, true), 1);
                assert.deepEqual(arr, [10, 20, 30, 40, 50]);
                assert.equal(arr.binaryInsert(21, true), 2);
                assert.deepEqual(arr, [10, 20, 21, 30, 40, 50]);
                assert.equal(arr.binaryInsert(22, true), 3);
                assert.deepEqual(arr, [10, 20, 21, 22, 30, 40, 50]);
                assert.equal(arr.binaryInsert(2, true), 0);
                assert.deepEqual(arr, [2, 10, 20, 21, 22, 30, 40, 50]);
                assert.equal(arr.binaryInsert(52, true), 8);
                assert.deepEqual(arr, [2, 10, 20, 21, 22, 30, 40, 50, 52]);
            });
            it('keyMapper', function () {
                var arr = [10, 20, 30, 40, 50].map(function (v) { return ({ value: v }); });
                assert.equal(arr.binaryInsert({ value: 20 }, function (v) { return v.value; }, true), 1);
                assert.deepEqual(arr, [10, 20, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 21 }, function (v) { return v.value; }, true), 2);
                assert.deepEqual(arr, [10, 20, 21, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 22 }, function (v) { return v.value; }, true), 3);
                assert.deepEqual(arr, [10, 20, 21, 22, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 2 }, function (v) { return v.value; }, true), 0);
                assert.deepEqual(arr, [2, 10, 20, 21, 22, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 52 }, function (v) { return v.value; }, true), 8);
                assert.deepEqual(arr, [2, 10, 20, 21, 22, 30, 40, 50, 52].map(function (v) { return ({ value: v }); }));
            });
        });
        describe('not unique', function () {
            it('normal', function () {
                var arr = [10, 20, 30, 40, 50];
                assert.equal(arr.binaryInsert(20), 1);
                assert.deepEqual(arr, [10, 20, 20, 30, 40, 50]);
                assert.equal(arr.binaryInsert(21), 3);
                assert.deepEqual(arr, [10, 20, 20, 21, 30, 40, 50]);
                assert.equal(arr.binaryInsert(22), 4);
                assert.deepEqual(arr, [10, 20, 20, 21, 22, 30, 40, 50]);
                assert.equal(arr.binaryInsert(2), 0);
                assert.deepEqual(arr, [2, 10, 20, 20, 21, 22, 30, 40, 50]);
                assert.equal(arr.binaryInsert(52), 9);
                assert.deepEqual(arr, [2, 10, 20, 20, 21, 22, 30, 40, 50, 52]);
            });
            it('keyMapper', function () {
                var arr = [10, 20, 30, 40, 50].map(function (v) { return ({ value: v }); });
                assert.equal(arr.binaryInsert({ value: 20 }, function (v) { return v.value; }), 1);
                assert.deepEqual(arr, [10, 20, 20, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 21 }, function (v) { return v.value; }), 3);
                assert.deepEqual(arr, [10, 20, 20, 21, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 22 }, function (v) { return v.value; }), 4);
                assert.deepEqual(arr, [10, 20, 20, 21, 22, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 2 }, function (v) { return v.value; }), 0);
                assert.deepEqual(arr, [2, 10, 20, 20, 21, 22, 30, 40, 50].map(function (v) { return ({ value: v }); }));
                assert.equal(arr.binaryInsert({ value: 52 }, function (v) { return v.value; }), 9);
                assert.deepEqual(arr, [2, 10, 20, 20, 21, 22, 30, 40, 50, 52].map(function (v) { return ({ value: v }); }));
            });
        });
    });
    it('binaryDistinct', function () {
        var arr = [3, 2, 3, 2, 1];
        assert.notDeepEqual(arr.binaryDistinct(), [1, 2, 3]);
        assert.deepEqual(arr.orderBy(function (v) { return v; }).binaryDistinct(), [1, 2, 3]);
    });
    it('findLast', function () {
        assert.deepEqual([0, 0, 0, 0, 1, 2, 3, 4, 5].findLast(function (v) { return v % 2 == 0; }), 4);
        assert.deepEqual([0, 0, 0, 0, 1, 2, 3, 4, 5].map(function (v) { return ({ value: v }); }).findLast(function (v) { return v.value % 2 == 0; }), { value: 4 });
    });
    it('findLastIndex', function () {
        assert.deepEqual([0, 0, 0, 0, 1, 2, 3, 4, 5].findLastIndex(function (v) { return v % 2 == 0; }), 7);
        assert.deepEqual([0, 0, 0, 0, 1, 2, 3, 4, 5].map(function (v) { return ({ value: v }); }).findLastIndex(function (v) { return v.value % 2 == 0; }), 7);
    });
    it('groupBy', function () {
        assert.deepEqual(['a1', 'a3', 'a2', 'a3', 'a2', 'a3'].groupBy(function (v) { return v; }).map(function (v) { return [v.key, v.length]; }), [['a1', 1], ['a3', 3], ['a2', 2]]);
        assert.deepEqual(['a1', 'a3', 'a2', 'a3', 'a2', 'a3'].map(function (v) { return ({ value: v }); }).groupBy(function (v) { return v.value; }).map(function (v) { return [v.key, v.length]; }), [['a1', 1], ['a3', 3], ['a2', 2]]);
        //数字Key顺序按数字
        assert.deepEqual(['1', '3', '2', '3', '2', '3'].groupBy(function (v) { return v; }).map(function (v) { return [v.key, v.length]; }), [['1', 1], ['2', 2], ['3', 3]]);
    });
});
