Created
February 22, 2010 13:42
-
-
Save alandipert/311077 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns org.dipert.alan.utils.test.math | |
(:use [org.dipert.alan.utils.math]) | |
(:use [clojure.test])) | |
(defmacro with-private-fns [[ns fns] & tests] | |
"Refers private fns from ns and runs tests in context." | |
`(let ~(reduce #(conj %1 %2 `(ns-resolve '~ns '~%2)) [] fns) | |
~@tests)) | |
(with-private-fns [org.dipert.alan.utils.math [powers-of syms-vals char-range prep-str]] | |
(deftest test-powers-of | |
(let [powers-2 (powers-of 2) | |
powers-8 (powers-of 8) | |
powers-10 (powers-of 10)] | |
(is (= 1024 (nth powers-2 10)) "The 10th power of 2 should be 1024") | |
(is (= 73 (reduce + (take 3 powers-8))) "The sum of the first 3 powers of 8 should be 73") | |
(is (= 100000 (nth powers-10 5)))) "The sum of the first 5 powers of 10 should be 100000") | |
(deftest test-syms-vals | |
(let [base-10-syms (syms-vals 10)] | |
(is (= 10 (count base-10-syms)) "The number of keys should be 10") | |
(is (= 2 (base-10-syms \2)) "The key \\2 should map to the number 2") | |
(is (= 45 (reduce + (vals base-10-syms))) "The sum of the decimal values should be 45") | |
(is (thrown? AssertionError (syms-vals 123)) "A base larger than 36 should throw AssertionError") | |
(is (thrown? AssertionError (syms-vals 0)) "A zero base should throw AssertionError") | |
(is (thrown? AssertionError (syms-vals -34)) "A negative base should throw AssertionError"))) | |
(deftest test-prep-str | |
(is (= 3 (count (prep-str "abc" 16))) "The number of values in the hex string 'abc' should be 3") | |
(is (= 12 (first (prep-str "10c" 16))) "The first value of the hex string '10c' should be 12 (c=12)") | |
(is (= 1 (last (prep-str "10c" 16))) "The last value of the hex string '10c' should be 1 (1=1)") | |
(is (= 3 (count (prep-str "azbxc" 16))) "Non-hex chars in input should be ignored"))) | |
(deftest test-strton | |
(is (= 3764 (strton "0111010110100" 2)) "The binary string should equal 3764 in decimal") | |
(is (= 3764 (strton "7264" 8)) "The octal string should equal 3764 in decimal") | |
(is (= 3764 (strton "EB4" 16)) "The hex string EB4 should equal 3764 in decimal") | |
(is (= 3764 (strton "eb4" 16)) "The hex string eb4 should equal 3764 in decimal") | |
(is (= 3764 (strton "zeb4q" 16)) "The corrupt hex string should equal 3764 in decimal") | |
(is (= 3764 (strton "0xEB4" 16)) "The hex string with a preceding 0x should equal 3764 in decimal") | |
(is (= 717 (strton "5a2" 11))) "The undecimal string should equal 717 in decimal") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment