A Clojure library for approximate comparison of floating point types in tests
Перейти к файлу
Emlyn Corrin addef375c5 Update changelog 2023-01-23 17:09:10 +00:00
.github/workflows Try without base-path 2022-08-03 23:51:30 +02:00
doc Don't use deprecated ns in docs 2022-07-17 23:00:38 +01:00
src require macros 2023-01-21 12:27:20 -07:00
test update tess 2023-01-21 12:56:08 -07:00
.gitignore Figwheel stuff 2022-07-30 13:29:32 +01:00
CHANGELOG.md Update changelog 2023-01-23 17:09:10 +00:00
CONTRIBUTING.md Initial commit 2017-10-19 12:35:09 +01:00
LICENSE Initial commit 2017-10-19 12:35:09 +01:00
README.md Add another reference 2022-08-04 00:27:01 +02:00
SECURITY.md Microsoft mandatory file 2022-08-03 22:03:47 +00:00
dev.cljs.edn Figwheel stuff 2022-07-30 13:29:32 +01:00
figwheel-main.edn Figwheel stuff 2022-07-30 13:29:32 +01:00
project.clj Version 0.1.7-SNAPSHOT 2023-01-23 17:07:39 +00:00
test.cljs.edn Fix Chrome path after MacOS update 2022-07-30 17:25:40 +01:00

README.md

same/ish - floating point comparisons for tests.

Equality may perhaps be a right, but no power on earth can ever turn it into a fact.
        — Honore de Balzac

cljdoc Build status Coverage Dependencies Status Downloads

You have some functions that return doubles or floats and you want to write some tests. How do you compare your results to the expected values? You can't use equality because of rounding errors, so you have to check if the values are near each other. But how near is near enough? And what if your numbers are buried inside data structures?

same/ish is designed to help with these situations.

Usage

See the full API docs are available on cljdoc for more detailed instructions and examples. A brief summary is provided below.

In Leiningen, add the following to your :dependencies in project.clj:

Clojars Project

Then in your test namespace(s), require same.core:

(ns foo-test
  (:requre [clojure.test :refer :all]
           [same.core :refer [ish? zeroish?]]))

To compare two numbers, instead of using = or ==, use ish?:

(defn f [x]
  (* (/ 1. x) x)))

(deftest f-test
  (is (ish? 1.0 (f 49.0))))

You can also compare data structures, and they will be compared element-wise, using ish? for floating point types, == for other numbers, and = for anything else:

(defn g [x]
  {:a x
   :b [(* x x) (Math/sqrt x)]))

(deftest g-test
  (is (ish? {:a 23 :b [529 4.7958315233]}
            (g 23.0))))

Requirements

Clojure 1.7.0 or higher is required due to the reader conditionals required for ClojureScript support.

References

Changelog

See CHANGELOG.md.

Contributing

Contributions are welcomed, see CONTRIBUTING.md for details.

License

Copyright © Microsoft Corporation. All rights reserved.
Licensed under the MIT License.