Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use test.generative's runner, add a few generative tests

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
commit 7f69246f17a559119fbad8747100bb7586ff6e92 1 parent a61a703
@stuarthalloway stuarthalloway authored
Showing with 79 additions and 64 deletions.
  1. +2 −63 src/script/run_tests.clj
  2. +77 −1 test/clojure/test_clojure/numbers.clj
View
65 src/script/run_tests.clj
@@ -1,63 +1,2 @@
-(ns clojure.test-clojure (:require clojure.test))
-
-(def test-namespaces '[
-clojure.test-clojure.agents
-clojure.test-clojure.annotations
-clojure.test-clojure.atoms
-clojure.test-clojure.clojure-set
-clojure.test-clojure.clojure-walk
-clojure.test-clojure.clojure-xml
-clojure.test-clojure.clojure-zip
-clojure.test-clojure.compilation
-clojure.test-clojure.control
-clojure.test-clojure.data
-clojure.test-clojure.data-structures
-clojure.test-clojure.def
-clojure.test-clojure.errors
-clojure.test-clojure.evaluation
-clojure.test-clojure.fn
-clojure.test-clojure.for
-clojure.test-clojure.genclass.examples
-clojure.test-clojure.genclass
-clojure.test-clojure.java.io
-clojure.test-clojure.java.javadoc
-clojure.test-clojure.java.shell
-clojure.test-clojure.java-interop
-clojure.test-clojure.keywords
-clojure.test-clojure.load
-clojure.test-clojure.logic
-clojure.test-clojure.macros
-clojure.test-clojure.main
-clojure.test-clojure.metadata
-clojure.test-clojure.multimethods
-clojure.test-clojure.ns-libs
-clojure.test-clojure.numbers
-clojure.test-clojure.other-functions
-clojure.test-clojure.parallel
-clojure.test-clojure.pprint
-clojure.test-clojure.predicates
-clojure.test-clojure.printer
-clojure.test-clojure.protocols
-clojure.test-clojure.protocols.hash-collisions
-clojure.test-clojure.reader
-clojure.test-clojure.reducers
-clojure.test-clojure.reflect
-clojure.test-clojure.refs
-clojure.test-clojure.repl
-clojure.test-clojure.rt
-clojure.test-clojure.sequences
-clojure.test-clojure.serialization
-clojure.test-clojure.special
-clojure.test-clojure.string
-clojure.test-clojure.test
-clojure.test-clojure.test-fixtures
-clojure.test-clojure.transients
-clojure.test-clojure.try-catch
-clojure.test-clojure.vars
-clojure.test-clojure.vectors
-])
-
-(apply require test-namespaces)
-
-(let [results (apply clojure.test/run-tests test-namespaces)]
- (System/exit (+ (:error results) (:fail results))))
+(require '[clojure.test.generative.runner :as runner])
+(runner/-main "test")
View
78 test/clojure/test_clojure/numbers.clj
@@ -13,7 +13,9 @@
(ns clojure.test-clojure.numbers
(:use clojure.test
- clojure.template))
+ [clojure.test.generative :exclude (is)]
+ clojure.template)
+ (:require [clojure.test.generative.generators :as gen]))
; TODO:
@@ -563,3 +565,77 @@ Math/pow overflows to Infinity."
min
max))))
+(defn integer
+ "Distribution of integers biased towards the small, but
+ including all longs."
+ []
+ (gen/one-of #(gen/uniform -1 32) gen/byte gen/short gen/int gen/long))
+
+(defn longable?
+ [n]
+ (try
+ (long n)
+ true
+ (catch Exception _)))
+
+(defspec integer-commutative-laws
+ (partial map identity)
+ [^{:tag `integer} a ^{:tag `integer} b]
+ (if (longable? (+' a b))
+ (assert (= (+ a b) (+ b a)
+ (+' a b) (+' b a)
+ (unchecked-add a b) (unchecked-add b a)))
+ (assert (= (+' a b) (+' b a))))
+ (if (longable? (*' a b))
+ (assert (= (* a b) (* b a)
+ (*' a b) (*' b a)
+ (unchecked-multiply a b) (unchecked-multiply b a)))
+ (assert (= (*' a b) (*' b a)))))
+
+(defspec integer-associative-laws
+ (partial map identity)
+ [^{:tag `integer} a ^{:tag `integer} b ^{:tag `integer} c]
+ (if (every? longable? [(+' a b) (+' b c) (+' a b c)])
+ (assert (= (+ (+ a b) c) (+ a (+ b c))
+ (+' (+' a b) c) (+' a (+' b c))
+ (unchecked-add (unchecked-add a b) c) (unchecked-add a (unchecked-add b c))))
+ (assert (= (+' (+' a b) c) (+' a (+' b c))
+ (+ (+ (bigint a) b) c) (+ a (+ (bigint b) c)))))
+ (if (every? longable? [(*' a b) (*' b c) (*' a b c)])
+ (assert (= (* (* a b) c) (* a (* b c))
+ (*' (*' a b) c) (*' a (*' b c))
+ (unchecked-multiply (unchecked-multiply a b) c) (unchecked-multiply a (unchecked-multiply b c))))
+ (assert (= (*' (*' a b) c) (*' a (*' b c))
+ (* (* (bigint a) b) c) (* a (* (bigint b) c))))))
+
+(defspec integer-distributive-laws
+ (partial map identity)
+ [^{:tag `integer} a ^{:tag `integer} b ^{:tag `integer} c]
+ (if (every? longable? [(*' a (+' b c)) (+' (*' a b) (*' a c))
+ (*' a b) (*' a c) (+' b c)])
+ (assert (= (* a (+ b c)) (+ (* a b) (* a c))
+ (*' a (+' b c)) (+' (*' a b) (*' a c))
+ (unchecked-multiply a (+' b c)) (+' (unchecked-multiply a b) (unchecked-multiply a c))))
+ (assert (= (*' a (+' b c)) (+' (*' a b) (*' a c))
+ (* a (+ (bigint b) c)) (+ (* (bigint a) b) (* (bigint a) c))))))
+
+(defspec addition-undoes-subtraction
+ (partial map identity)
+ [^{:tag `integer} a ^{:tag `integer} b]
+ (if (longable? (-' a b))
+ (assert (= a
+ (-> a (- b) (+ b))
+ (-> a (unchecked-subtract b) (unchecked-add b)))))
+ (assert (= a
+ (-> a (-' b) (+' b)))))
+
+(defspec quotient-and-remainder
+ (fn [a b] (sort [a b]))
+ [^{:tag `integer} a ^{:tag `integer} b]
+ (when-not (zero? (second %))
+ (let [[a d] %
+ q (quot a d)
+ r (rem a d)]
+ (assert (= a
+ (+ (* q d) r)
+ (unchecked-add (unchecked-multiply q d) r))))))
Please sign in to comment.
Something went wrong with that request. Please try again.