Permalink
Browse files

Added tests. (A church encoding test by Seo Sanghyun.)

  • Loading branch information...
1 parent b54baed commit c37a2be1d77f1d4412c0813149ca57c149c8eb20 @dahlia dahlia committed Aug 23, 2010
Showing with 54 additions and 0 deletions.
  1. +38 −0 test.php
  2. +15 −0 tests/church-encoding.lisphp
  3. +1 −0 tests/church-encoding.out
View
@@ -0,0 +1,38 @@
+<?php
+require dirname(__FILE__) . '/Lisphp.php';
+
+function displayStrings() {
+ global $result;
+ $args = func_get_args();
+ $result .= join('', array_map('strval', $args));
+}
+
+$testFiles = glob(dirname(__FILE__) . '/tests/*.lisphp');
+$fails = array();
+
+foreach ($testFiles as $file) {
+ $program = Lisphp_Program::load($file);
+ $result = '';
+ $scope = Lisphp_Environment::full();
+ $scope['echo'] = new Lisphp_Runtime_PHPFunction('displayStrings');
+ $program->execute($scope);
+ $expected = file_get_contents(preg_replace('/\.lisphp$/', '.out', $file));
+ if ($result == $expected) {
+ echo '.';
+ } else {
+ echo 'F';
+ $fails[] = $file;
+ }
+}
+
+if ($fails) {
+ echo "\nFailed ";
+} else {
+ echo "\nOK ";
+}
+echo '(', count($testFiles), ' tests';
+if ($fails) {
+ echo ', ', count($fails), ' failed';
+}
+echo ")\n";
+
@@ -0,0 +1,15 @@
+"Church numerals by Seo Sanghyun. http://bit.ly/bSlqTK"
+
+(define zero (lambda (f) (lambda (x) x)))
+(define one (lambda (f) (lambda (x) (f x))))
+(define plus (lambda (m n) (lambda (f) (lambda (x) ((n f) ((m f) x))))))
+(define mult (lambda (m n) (lambda (f) (lambda (x) ((n (m f)) x)))))
+(define xp (lambda (m n) (lambda (f) (lambda (x) (((n m) f) x)))))
+(define pr (lambda (x) (do (echo x) x)))
+(define prn (lambda (n) (do ((n pr) ".") (echo "\n"))))
+(define two (plus one one))
+(define three (plus two one))
+(define six (mult two three))
+(define sixty-four (xp two six))
+(prn sixty-four)
+
@@ -0,0 +1 @@
+................................................................

0 comments on commit c37a2be

Please sign in to comment.