From be80534affb691295fce8d76dad21e99d801424c Mon Sep 17 00:00:00 2001 From: Fogus Date: Fri, 4 Jun 2010 10:47:25 -0400 Subject: [PATCH] added tests for defconstrainedfn --- test/fogus/me/defcnstrfn_tests.clj | 84 ++++++++++++++++++++++++++++++ test/fogus/me/kontract_tests.clj | 42 +++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 test/fogus/me/defcnstrfn_tests.clj create mode 100644 test/fogus/me/kontract_tests.clj diff --git a/test/fogus/me/defcnstrfn_tests.clj b/test/fogus/me/defcnstrfn_tests.clj new file mode 100644 index 0000000..50ed464 --- /dev/null +++ b/test/fogus/me/defcnstrfn_tests.clj @@ -0,0 +1,84 @@ +;;; defcnstrfn-tests.clj -- Contracts programming library for Clojure + +;; by Michael Fogus - http://fogus.me/fun/ +;; June 04, 2010 + +;; Copyright (c) Michael Fogus, 2010. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file COPYING the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + +(ns fogus.me.defcnstrfn-tests + (:use [fogus.me.trammel :only [defconstrainedfn]]) + (:use [clojure.test :only [deftest is]])) + +(defconstrainedfn positive-nums + "test" + ([x] + :requires + (number? x) + (pos? x) + + :ensures + (float? %) + + :body + (float x)) + ([x y] + :requires + (every? number? [x y]) + (every? pos? [x y]) + + :ensures + (every? float? %) + + :body + (str "this is a noop, meant to ensure that multi-expr bodies work") + [(float x) (float y)])) + +(deftest multibody-cnstr-fn-test + (is (= 1.0 (positive-nums 1))) + (is (= [1.0 2.0] (positive-nums 1 2))) + (is (= 42 + (try (positive-nums -1) + (catch Error e 42)))) + (is (= 42 + (try (positive-nums -1 2) + (catch Error e 42)))) + (is (= 42 + (try (positive-nums 2 -1) + (catch Error e 42)))) + (is (= 42 + (try (positive-nums -1 -2) + (catch Error e 42))))) + + +(defconstrainedfn sqr + "Calculates the square of a number." + ([n] + :requires + (number? n) + (not (zero? n)) + + :ensures + (pos? %) + + :body + (* n n))) + +(deftest cnstr-sqr-test + (is (= 36 (sqr 6))) + (is (= 36 (sqr -6))) + (is (= 42 + (try (sqr 0) + (catch Error e 42)))) + (is (= 42 + (try (sqr :a) + (catch Error e 42))))) + +(deftest defconstrainedfn-test + (multibody-cnstr-fn-test) + (cnstr-sqr-test)) diff --git a/test/fogus/me/kontract_tests.clj b/test/fogus/me/kontract_tests.clj new file mode 100644 index 0000000..37204b3 --- /dev/null +++ b/test/fogus/me/kontract_tests.clj @@ -0,0 +1,42 @@ +;;; defcontract_tests.clj -- Contracts programming library for Clojure + +;; by Michael Fogus - http://fogus.me/fun/ +;; May 26, 2010 + +;; Copyright (c) Michael Fogus, 2010. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file COPYING the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + +(ns fogus.me.kontract-tests + (:use [fogus.me.trammel :only [contract kontract]]) + (:use [clojure.test :only [deftest is]])) + +(def doubler-kontract + (kontract doubler + [x] + (requires + (pos? x)) + (ensures + (= (* 2 x) %)) + [x y] + (requires + (pos? x) + (pos? y)) + (ensures + (= (* 2 (+ x y)) %)))) + +(deftest doubler-kontract-test + (is (= 10 ((partial doubler-kontract #(* 2 (+ %1 %2))) 2 3))) + (is (= 10 ((partial doubler-kontract #(+ %1 %1 %2 %2)) 2 3))) + (is (= 10 ((partial doubler-kontract #(* 2 %)) 5))) + (is (= 42 + (try ((partial doubler-kontract #(* 3 (+ %1 %2))) 2 3) + (catch Error e 42))))) + +(deftest kontract-test + (doubler-kontract-test)) +