Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91577

How to map a test onto a list of numbers

I have a function with a bug:

user> (-> 42 int-to-bytes bytes-to-int)
42
user> (-> 128 int-to-bytes bytes-to-int)
-128
user> 

looks like I need to handle overflow when converting back...

Better write a test to make sure this never happens again. This project is using clojure.contrib.test-is so i write:

(deftest int-to-bytes-to-int
  (let [lots-of-big-numbers (big-test-numbers)]
    (map #(is (= (-> %
                     int-to-bytes
                     bytes-to-int)
                 %))
         lots-of-big-numbers)))

This should be testing converting to a seq of bytes and back again produces the origional result on a list of 10000 random numbers. Looks OK in theory? except none of the tests ever run.

Testing com.cryptovide.miscTest

Ran 23 tests containing 34 assertions.
0 failures, 0 errors.

Upvotes: 4

Views: 129

Answers (3)

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44142

Avoid the need to write the map (or doseq) expression altogether by using are to write the test.

Upvotes: 1

Brian Carper
Brian Carper

Reputation: 72946

dorun + map => doseq

(doseq [x (big-test-numbers)]
  (is (= x (-> x int-to-bytes bytes-to-int))))

Upvotes: 5

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91577

bitten by the lazy-bug again. needed a (dorun around the map :) * blush *

Upvotes: 0

Related Questions