Skip to content

Commit

Permalink
Merge pull request #85 from MoeOrganization/prakashk/tests
Browse files Browse the repository at this point in the history
spec/examples/99-problems converted to tests
  • Loading branch information
Stevan Little committed Apr 24, 2013
2 parents 791afad + 6f6abf3 commit 0b1febb
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 34 deletions.
36 changes: 35 additions & 1 deletion lib/Test-More/lib/Test/More.mo
Expand Up @@ -37,6 +37,16 @@ package Test {
}
}

method is_deeply($got, $expected, $msg?) {
self.inc_count;
if (self.compare_deeply($got, $expected)) {
$!output.ok($!count, $msg);
} else {
$!output.not_ok($!count, $msg);
self.output_err($got, $expected, $msg);
}
}

method diag ($msg) { $!output.diag($msg) }

submethod inc_count { $!count = $!count + 1; }
Expand All @@ -60,6 +70,26 @@ package Test {
$!output.bailout("Can only compare Str, Int, Num and Bool objects");
}
}

submethod compare_deeply($got, $expected) {
if ($got.isa("Array")) {
self.compare_arrays($got, $expected);
} elsif ($got.isa("Hash")) {
self.compare_hashes($got, $expected);
} else {
$!output.bailout("Can only compare deeply Array and Hash objects");
}
}

submethod compare_arrays($got, $expected) {
# poor man's deep-compare of arrays
self.compare($got.join("|"), $expected.join("|"));
}

submethod compare_hashes($got, $expected) {
# poor man's deep-compare of hashes
self.compare($got.pairs.join("|"), $expected.pairs.join("|"));
}
}

package More {
Expand All @@ -85,10 +115,14 @@ package Test {
$builder.is($got, $expected, $msg);
}

sub is_deeply ($got, $expected, $msg?) is export {
$builder.is_deeply($got, $expected, $msg);
}

sub diag ($msg) is export {
$builder.diag($msg);
}

}

}
}
4 changes: 0 additions & 4 deletions spec/examples/99-problems/05.mo

This file was deleted.

4 changes: 0 additions & 4 deletions spec/examples/99-problems/07.mo

This file was deleted.

7 changes: 6 additions & 1 deletion spec/examples/99-problems/01.mo → t/099-problems/01.t
@@ -1,4 +1,9 @@
# P01 (*) Find the last element of a list.

use Test::More;

my @list = [1, 1, 2, 3, 5, 8];
say @list[-1];

is(@list[-1], 8, "... P01");

done_testing(1);
7 changes: 6 additions & 1 deletion spec/examples/99-problems/02.mo → t/099-problems/02.t
@@ -1,4 +1,9 @@
# P02 (*) Find the last but one element of a list.

use Test::More;

my @list = [1, 1, 2, 3, 5, 8];
say @list[-2];

is(@list[-2], 5, "... P02");

done_testing(1);
6 changes: 5 additions & 1 deletion spec/examples/99-problems/03.mo → t/099-problems/03.t
Expand Up @@ -6,6 +6,10 @@
# moe> nth(2, [1, 1, 2, 3, 5, 8)]
# 2

use Test::More;

sub nth($n, @list) { @list[$n] }

say nth(2, [1, 1, 2, 3, 5, 8]);
is (nth(2, [1, 1, 2, 3, 5, 8], 2, "... P03");

done_testing(1);
8 changes: 7 additions & 1 deletion spec/examples/99-problems/04.mo → t/099-problems/04.t
@@ -1,4 +1,10 @@
# P04 (*) Find the number of elements of a list.

use Test::More;

my @list = [1, 1, 2, 3, 5, 8];
say @list.length;

is (@list.length, 6, "... P04");

done_testing(1);

10 changes: 10 additions & 0 deletions t/099-problems/05.t
@@ -0,0 +1,10 @@
# P05 (*) Reverse a list.

use Test::More;

my @list = [1, 1, 2, 3, 5, 8];

is_deeply(@list.reverse, [8, 5, 3, 2, 1, 1], "... P05");

done_testing(1);

9 changes: 7 additions & 2 deletions spec/examples/99-problems/06.mo → t/099-problems/06.t
Expand Up @@ -4,9 +4,14 @@
# moe> is_palindrome([1, 2, 3, 2, 1)]
# true

use Test::More;

sub is_palindrome(@list) {
@list.reverse.eqv(@list);
}

my @list = [1, 2, 3, 2, 1];
say is_palindrome(@list);
is(is_palindrome([1, 2, 3, 2, 1]), true, "... P06 -- true") ;
is(is_palindrome([1, 2, 3, 4, 5]), false, "... P06 -- false") ;

done_testing(2);

10 changes: 10 additions & 0 deletions t/099-problems/07.t
@@ -0,0 +1,10 @@
# P07 (**) Flatten a nested list structure.

use Test::More;

my @list = [[1, 1], 2, [3, [5, 8]]];

is_deeply(@list.flatten, [1, 1, 2, 3, 5, 8], "... P07");

done_testing(1);

7 changes: 6 additions & 1 deletion spec/examples/99-problems/08.mo → t/099-problems/08.t
Expand Up @@ -8,6 +8,8 @@
# moe> compress(['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'])
# ['a', 'b', 'c', 'a', 'd', 'e']

use Test::More;

sub compress(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1] ne $b) {
Expand All @@ -17,4 +19,7 @@ sub compress(*@list) {
[])
}

say compress('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
is_deeply(compress('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'), ['a', 'b', 'c', 'a', 'd', 'e'], "... P08");

done_testing(1);

11 changes: 9 additions & 2 deletions spec/examples/99-problems/09.mo → t/099-problems/09.t
Expand Up @@ -4,7 +4,9 @@

# Example:
# moe> pack(['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e')]
# [['a', 'a', 'a', 'a']', ['b']', ['c', 'c']', ['a', 'a']', ['d']', ['e', 'e', 'e', 'e']]
# [['a', 'a', 'a', 'a'], ['b'], ['c', 'c'], ['a', 'a'], ['d'], ['e', 'e', 'e', 'e']]

use Test::More;

sub pack(*@list) {
@list.reduce(-> (@a, $b) {
Expand All @@ -19,4 +21,9 @@ sub pack(*@list) {
[])
}

say pack('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
is_deeply(pack('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'),
[['a', 'a', 'a', 'a'], ['b'], ['c', 'c'], ['a', 'a'], ['d'], ['e', 'e', 'e', 'e']],
"... P09");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/10.mo → t/099-problems/10.t
Expand Up @@ -8,6 +8,8 @@
# moe> encode(["a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"])
# [[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]]

use Test::More;

sub pack(@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
Expand All @@ -25,4 +27,9 @@ sub encode(*@list) {
pack(@list).map(-> (@a) {[@a.length, @a[0]]})
}

say encode("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e");
is_deeply(encode("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"),
[[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]],
"... P10");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/11.mo → t/099-problems/11.t
Expand Up @@ -9,6 +9,8 @@
# moe> encodeModified("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")
# [[4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"]]

use Test::More;

sub encodeModified(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
Expand All @@ -23,4 +25,9 @@ sub encodeModified(*@list) {
.map(-> (@a) {@a.length == 1 ? @a[0] : [@a.length, @a[0]]})
}

say encodeModified('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e');
is_deeply(encodeModified('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'),
[[4, "a"], "b", [2, "c"], [2, "a"], "d", [4, "e"]],
"... P11");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/12.mo → t/099-problems/12.t
Expand Up @@ -7,8 +7,15 @@
# moe> decode([[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]])
# ["a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"]

use Test::More;

sub decode(@list) {
@list.map(-> (@rlc) {[@rlc[1]] x @rlc[0]}).flatten
}

say decode([[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]])
is_deeply(decode([[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]]),
["a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"],
"... P12");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/13.mo → t/099-problems/13.t
Expand Up @@ -7,6 +7,8 @@
# moe> encodeDirect(["a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"])
# [[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]]

use Test::More;

sub encodeDirect(*@list) {
@list.reduce(-> (@a, $b) {
if (@a.length == 0 || @a[-1].at_pos(0) ne $b) {
Expand All @@ -21,4 +23,9 @@ sub encodeDirect(*@list) {
.map(-> (@a) {[@a.length, @a[0]]})
}

say encodeDirect("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e");
is_deeply(encodeDirect("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e"),
[[4, "a"], [1, "b"], [2, "c"], [2, "a"], [1, "d"], [4, "e"]],
"... P13");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/14.mo → t/099-problems/14.t
Expand Up @@ -4,8 +4,15 @@
# moe> duplicate(["a", "b", "c", "c", "d"])
# ["a", "a", "b", "b", "c", "c", "c", "c", "d", "d"]

use Test::More;

sub duplicate(@list) {
@list.map(-> ($x) {[$x, $x]}).flatten
}

say duplicate(["a", "b", "c", "c", "d"]);
is_deeply(duplicate(["a", "b", "c", "c", "d"]),
["a", "a", "b", "b", "c", "c", "c", "c", "d", "d"],
"... P14");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/15.mo → t/099-problems/15.t
Expand Up @@ -4,8 +4,15 @@
# moe> duplicateN(3, ["a", "b", "c", "c", "d"])
# ["a", "a", "a", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d"]

use Test::More;

sub duplicateN($n, @list) {
@list.map(-> ($x) {[$x] x $n}).flatten
}

say duplicateN(3, ["a", "b", "c", "c", "d"]);
is_deeply(duplicateN(3, ["a", "b", "c", "c", "d"]),
["a", "a", "a", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d"],
"... P15");

done_testing(1);

9 changes: 8 additions & 1 deletion spec/examples/99-problems/16.mo → t/099-problems/16.t
Expand Up @@ -4,6 +4,8 @@
# moe> drop(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"])
# ["a", "b", "d", "e", "g", "h", "j", "k"]

use Test::More;

sub drop($n, @list) {
# need prefix -- (pre-decrement) operator for this to work
# @list.grep(-> ($a) { --$n % 3 != 0 })
Expand All @@ -12,4 +14,9 @@ sub drop($n, @list) {
@list.grep(-> ($a) { $n = $n - 1; $n % 3 != 0 })
}

say drop(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
is_deeply(drop(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]),
["a", "b", "d", "e", "g", "h", "j", "k"],
"... P16");

done_testing(1);

8 changes: 7 additions & 1 deletion spec/examples/99-problems/17.mo → t/099-problems/17.t
Expand Up @@ -6,10 +6,16 @@
# moe> split_list(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"])
# [["a", "b", "c"], ["d", "e", "f", "g", "h", "i", "j", "k"]]

use Test::More;

sub split_list($n, @list) {
# XXXX: the range expressions should work without enclosing parens too!
[@list[0..($n-1)], @list[$n..(@list.length-1)]]
}

say split_list(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
is_deeply(split_list(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]),
[["a", "b", "c"], ["d", "e", "f", "g", "h", "i", "j", "k"]],
"... P17");

done_testing(1);

10 changes: 9 additions & 1 deletion spec/examples/99-problems/18.mo → t/099-problems/18.t
Expand Up @@ -9,8 +9,16 @@
# moe> slice(3, 7, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"])
# ["d", "e", "f", "g"]

use Test::More;

sub slice_list($s, $e, @list) {
# XXXX: the range operand without enclosing parens should work too!
@list[$s .. ($e-1)]
}
say slice_list(3, 7, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);

is_deeply(slice_list(3, 7, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]),
["d", "e", "f", "g"],
"... P18");

done_testing(1);

16 changes: 13 additions & 3 deletions spec/examples/99-problems/19.mo → t/099-problems/19.t
Expand Up @@ -7,12 +7,22 @@
# moe> rotate(-2, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"])
# ["j", "k", "a", "b", "c", "d", "e", "f", "g", "h", "i"]

use Test::More;

sub rotate($n, @list) {
my $len = @list.length;
my @r = @list[($n < 0 ? $len + $n : $n) .. ($len - 1)];
@r.push(@list[0 .. ($n < 0 ? $len + $n : $n-1)]);
@r.push(@list[0 .. ($n < 0 ? $len + $n-1 : $n-1)]);
@r.flatten
}

say rotate(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
say rotate(-2, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]);
is_deeply(rotate(3, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]),
["d", "e", "f", "g", "h", "i", "j", "k", "a", "b", "c"],
"... P19 -- positive arg");

is_deeply(rotate(-2, ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]),
["j", "k", "a", "b", "c", "d", "e", "f", "g", "h", "i"],
"... P19 -- negative arg");

done_testing(2);

0 comments on commit 0b1febb

Please sign in to comment.