UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ T2/ q2-tests.js
test("basic operation",  (t)=>{ let r=new Reducer((a,x)=>a*x,1,[1,2,3,5]);
                                assert.strictEqual(r.run(),30); });

test("compare to reduceRight",
     (t)=>{ let array = [ 1, 2, 3 ]; let sub=(a,x)=> x-a;
            let r=new Reducer(sub,0,array);
            assert.strictEqual(r.run(), array.reduceRight(sub,0)); });

test("string operation",
     (t)=>{ let r=new Reducer((a,x)=>a+x,'', ['a','b','c' ]);
            assert.strictEqual(r.run(),"cba"); })

test("non-destructive",
     (t)=>{ let r=new Reducer((a,x)=>a*x,1,[ 1, 2, 3, 5 ]);
            assert.strictEqual(r.run(),r.run()); });

test("big array",
     (t)=>{ let array = Array.from({length: 1000}, (_, i) => i );
            let r=new Reducer((a,x)=>a+x,0,array);
            assert.strictEqual(r.run(),500*999); });

test("big array 2",
     (t)=>{ const array = Array.from({length: 1000}, (_, i) => i );
            let r=new Reducer((a,x)=>a.concat(x),[],array);
            assert.deepStrictEqual(r.run(),array.reverse()); });

test("basic iterator",
     (t)=>{
         let array = [ 1, 2, 3, 5 ];
         let sub=(a,x)=> x-a;
         let r=new Reducer(sub,0,array);
         let out=[];
         for (let rval of r) { out.push(rval); }
         assert.deepStrictEqual(out,[5,-2,4,-3]);
     });

test("big iterator",
     (t)=>{
         const m=1000;
         const array = Array.from({length: m}, (_, i) => i+1 );
         const expected = Array.from({length: m},
                                     (_, j) => (j+1)*(2*m-j)/2 );
         let r=new Reducer((a,x)=>a+x, 0, array);
         let out=[];
         for (let val of r) { out.push(val); }
         assert.deepStrictEqual(out,expected);
     });