Skip to content

Commit

Permalink
Tests for map, reduce, foldr and foldl
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Fairhurst committed Jan 31, 2015
1 parent a251533 commit 4401093
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/wake/stdlib/myobj/std.o
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,29 @@ function$ $$A(a, b) {

this.`sort(Num--(T,T))` = function(b) {
a.sort(b);
};

this.`map(R--(T))` = function(b) {
return$ a.map(b);
};

this.`reduce(T--(T,T))` = function(b) {
var$ s = $$A(a,0);
for(var$ i = 1; i < a.length; ++i)
s = b(a[i], s);
return$ s;
};

this.`foldr(R--(T,R),R)` = function(b,c) {
for(var$ i = 0; i < a.length; ++i)
c = b(a[i], c);
return$ c;
};

this.`foldl(R--(T,R),R)` = function(b,c) {
for(var$ i = a.length - 1; i >= 0; --i)
c = b(a[i], c);
return$ c;
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/wake/stdlib/tables/List.wk
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ every List{T} is:
Bool -- any(Bool -- fn(T) checker) { return true; }
sort(Num -- fn(T,T) sorter) {}
push(T) { }
{R} R[] -- map(R -- fn(T) mapper) { return []; }
{R} R -- foldl(R -- fn(T, R) mapper, R acc) { return [][0]; }
{R} R -- foldr(R -- fn(T, R) mapper, R acc) { return [][0]; }
T -- reduce(T -- fn(T, T) reducer) { return [][0]; }
43 changes: 40 additions & 3 deletions src/wake/test/LambdaTest.wk
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ every LambdaTest is:
}

@Test
~[test array filter]~(Asserts) {
~[test list.filter]~(Asserts) {
Asserts.that(["hey", "hey", "man"].filter({ t -> return true; }).getSize())Equals(3);
Asserts.that(["hey", "hey", "man"].filter({ t -> return false; }).getSize())Equals(0);
Asserts.that(["hey", "hello", "man"].filter({ t -> return t == "hey"; }).getSize())Equals(1);
Expand All @@ -119,7 +119,7 @@ every LambdaTest is:
}

@Test
~[test array any]~(Asserts) {
~[test list.any]~(Asserts) {
var runtimes Num = 0;
Asserts.that(["hey", "hey", "man"].any({ t -> runtimes += 1; return true; }))Equals(true);
Asserts.that(runtimes)Equals(1);
Expand All @@ -134,7 +134,7 @@ every LambdaTest is:
}

@Test
~[test array sort]~(Asserts) {
~[test list.sort]~(Asserts) {
var Text[] = ["zap", "goop", "pickle", "aardvark"];

Text[].sort({ a, b -> return 1 if a.getSize() < b.getSize() else -1; });
Expand All @@ -158,3 +158,40 @@ every LambdaTest is:
Asserts.that(Text[1])Equals("goop");
Asserts.that(Text[0])Equals("zap");
}

@Test
~[test list.map]~(Asserts) {
var Text[] = ["blah", "happy", "blappy"];

var Text[] res = Text[].map({ t -> return t.toUpper(); });
var Num[] = Text[].map({ t -> return t.getSize(); });

Asserts.that(res.getSize())Equals(3);
Asserts.that(res[0])Equals("BLAH");
Asserts.that(res[1])Equals("HAPPY");
Asserts.that(res[2])Equals("BLAPPY");

Asserts.that(Num[].getSize())Equals(3);
Asserts.that(Num[0])Equals(4);
Asserts.that(Num[1])Equals(5);
Asserts.that(Num[2])Equals(6);
}

@Test
~[test list.reduce]~(Asserts) {
var Num[] = [1, 2, 3];

Asserts.that(Num[].reduce({ n, acc -> return acc + n; }))Equals(6);
Asserts.that(Num[].reduce({ n, acc -> return acc - n; }))Equals(-4);
Asserts.that(Num[].reduce({ n, acc -> return (acc + 1) * n; }))Equals(15);
}

@Test
~[test list.foldr and foldl]~(Asserts) {
var Num[] = [1, 2, 3];

Asserts.that(Num[].foldr({ n, acc -> return acc + n.toString(); }, ""))Equals("123");
Asserts.that(Num[].foldl({ n, acc -> return acc + n.toString(); }, ""))Equals("321");
Asserts.that(Num[].foldr({ n, acc -> return acc + n.toString(); }, "start"))Equals("start123");
Asserts.that(Num[].foldl({ n, acc -> return acc + n.toString(); }, "start"))Equals("start321");
}

0 comments on commit 4401093

Please sign in to comment.