代码之家  ›  专栏  ›  技术社区  ›  Arthur Ulfeldt

如何将测试映射到数字列表

  •  4
  • Arthur Ulfeldt  · 技术社区  · 16 年前

    我有一个带bug的函数:

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

    最好写一个测试来确保这种情况不再发生。 这个项目使用clojure.contrib.test-is,所以我写:

    (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)))
    

    这应该是测试转换成一个字节序列,然后再转换回来,在一个由10000个随机数组成的列表上产生原始结果。

    Testing com.cryptovide.miscTest
    
    Ran 23 tests containing 34 assertions.
    0 failures, 0 errors.
    
    • 为什么不进行测试?
    • 我能做些什么让他们跑?
    3 回复  |  直到 16 年前
        1
  •  5
  •   Brian Carper    16 年前

    dorun map => doseq

    (doseq [x (big-test-numbers)]
      (is (= x (-> x int-to-bytes bytes-to-int))))
    
        2
  •  1
  •   Jouni K. Seppänen    16 年前

    避免使用 are 写测试。

        3
  •  0
  •   Arthur Ulfeldt    16 年前

    (dorun 在地图周围:) *脸红*