
var Heap = require("../heap");
var permute = require("./permute");

describe("Heap", function () {

    describe("always tracks the max value", function () {

        var commonNumbers = [1, 2, 3, 4, 5];
        permute(commonNumbers).forEach(function (numbers) {
            it(JSON.stringify(numbers), function () {

                var heap = Heap(numbers);
                var maxes = commonNumbers.slice();

                while (maxes.length > 0) {
                    var max = maxes.pop();
                    var top = heap.pop();
                    expect(top).toEqual(max);
                    expect(heap.length).toBe(maxes.length);
                }

                expect(heap.length).toBe(0);

            });
        });

        it("[5, 4, 3, 2, 1]", function () {
            var stack = [5, 4, 3, 2, 1];
            var heap = Heap(stack);
            expect(heap.content).toEqual([5, 4, 3, 2, 1]);
            expect(heap.length).toBe(5);
            expect(heap.pop()).toBe(5);
            expect(heap.content).toEqual([4, 2, 3, 1]);
            expect(heap.length).toBe(4);
            expect(heap.pop()).toBe(4);
            expect(heap.content).toEqual([3, 2, 1]);
            expect(heap.length).toBe(3);
            expect(heap.pop()).toBe(3);
            expect(heap.content).toEqual([2, 1]);
            expect(heap.length).toBe(2);
            expect(heap.pop()).toBe(2);
            expect(heap.content).toEqual([1]);
            expect(heap.length).toBe(1);
            expect(heap.pop()).toBe(1);
            expect(heap.content).toEqual([]);
            expect(heap.length).toBe(0);
        });

    });

    it("should be observable", function () {

        var heap = new Heap([1,2,3,4,5]);
        var top;
        heap.observeMapChange(function (plus, minus, key, type) {
            if (key === 0) {
                top = plus;
            }
        });

        heap.push(7);
        expect(top).toBe(7);

        heap.pop();
        expect(top).toBe(5);

        heap.pop();
        expect(top).toBe(4);

        heap.pop();
        expect(top).toBe(3);

    });

    it("should delete properly", function () {

        var heap = new Heap([1, 2, 3, 4, 5, 6]);
        heap.delete(3);
        expect(heap.sorted()).toEqual([1, 2, 4, 5, 6]);
        heap.delete(6);
        expect(heap.sorted()).toEqual([1, 2, 4, 5]);
        heap.delete(1);
        expect(heap.sorted()).toEqual([2, 4, 5]);
        heap.delete(4);
        expect(heap.sorted()).toEqual([2, 5]);
        heap.delete(2);
        expect(heap.sorted()).toEqual([5]);
        heap.delete(5);
        expect(heap.sorted()).toEqual([]);
        expect(heap.delete(null)).toBe(false);
        expect(heap.sorted()).toEqual([]);

    });

});


@@ -2190,32 +2190,72 @@
 , 3, 4, 5, 6%5D);%0A
+        expect(heap.length).toEqual(6);%0A
         heap.del
@@ -2258,24 +2258,24 @@
 .delete(3);%0A
-
         expe
@@ -2310,32 +2310,72 @@
 , 2, 4, 5, 6%5D);%0A
+        expect(heap.length).toEqual(5);%0A
         heap.del
@@ -2427,32 +2427,72 @@
 (%5B1, 2, 4, 5%5D);%0A
+        expect(heap.length).toEqual(4);%0A
         heap.del
@@ -2541,32 +2541,72 @@
 ual(%5B2, 4, 5%5D);%0A
+        expect(heap.length).toEqual(3);%0A
         heap.del
@@ -2656,24 +2656,64 @@
 al(%5B2, 5%5D);%0A
+        expect(heap.length).toEqual(2);%0A
         heap
@@ -2764,24 +2764,64 @@
 Equal(%5B5%5D);%0A
+        expect(heap.length).toEqual(1);%0A
         heap
@@ -2867,32 +2867,72 @@
 )).toEqual(%5B%5D);%0A
+        expect(heap.length).toEqual(0);%0A
         expect(h
@@ -3004,16 +3004,56 @@
 ual(%5B%5D);
+%0A        expect(heap.length).toEqual(0);
 %0A%0A    %7D)
@@ -3055,13 +3055,12 @@
    %7D);%0A%0A
+
 %7D);%0A
-%0A

Add test case for #66