|
2 | 2 | // we can't use uchar as a number format directly since it'll become 'a' or |
3 | 3 | // whatever and arithmetic ops will start failing |
4 | 4 | fmts = [ |
5 | | - ["uchar", cast_unsigned_char, cast_unsigned_int @ cast_unsigned_char], |
6 | | - ["char", cast_signed_char, cast_signed_int @ cast_signed_char], |
7 | | - ["ushort", cast_unsigned_short, cast_unsigned_short], |
8 | | - ["short", cast_signed_short, cast_signed_short], |
9 | | - ["uint", cast_unsigned_int, cast_unsigned_int], |
10 | | - ["int", cast_signed_int, cast_signed_int], |
11 | | - ["float", cast_float, cast_float], |
12 | | - ["double", cast_double, cast_double], |
13 | | - ["complex", cast_complex, cast_complex], |
14 | | - ["dcomplex", cast_double_complex, cast_double_complex] |
| 5 | + ["uchar", cast_unsigned_char, cast_unsigned_int @ cast_unsigned_char], |
| 6 | + ["char", cast_signed_char, cast_signed_int @ cast_signed_char], |
| 7 | + ["ushort", cast_unsigned_short, cast_unsigned_short], |
| 8 | + ["short", cast_signed_short, cast_signed_short], |
| 9 | + ["uint", cast_unsigned_int, cast_unsigned_int], |
| 10 | + ["int", cast_signed_int, cast_signed_int], |
| 11 | + ["float", cast_float, cast_float], |
| 12 | + ["double", cast_double, cast_double], |
| 13 | + ["complex", cast_complex, cast_complex], |
| 14 | + ["dcomplex", cast_double_complex, cast_double_complex] |
15 | 15 | ]; |
16 | 16 |
|
17 | 17 | // we need a to_real that does images as well |
18 | 18 | to_real x |
19 | | - = abs x, is_complex x |
20 | | - = mean x, is_Image x |
21 | | - = x; |
| 19 | + = abs x, is_complex x |
| 20 | + = mean x, is_Image x |
| 21 | + = x; |
22 | 22 |
|
23 | 23 | // numbers we test |
24 | 24 | numbers = [-10, 0, 1, 10, 3.1415927]; |
25 | 25 |
|
26 | 26 | test_unary op_name fn |
27 | | - = foldr1 logical_and |
28 | | - [test fname ifmt nfmt x :: [fname, ifmt, nfmt] <- fmts; x <- numbers] |
| 27 | + = [test fname ifmt nfmt x :: [fname, ifmt, nfmt] <- fmts; x <- numbers] |
29 | 28 | { |
30 | | - // image == number can fail due to rounding differences |
31 | | - test fname ifmt nfmt x |
32 | | - = true, abs (image - number) < 0.001 |
33 | | - = error (join_sep " " (map print |
34 | | - ["unary", fname, op_name, x, "==", image, number])) |
35 | | - { |
36 | | - image = (to_real @ fn @ ifmt @ to_image) x; |
37 | | - number = (to_real @ fn @ nfmt) x; |
38 | | - } |
| 29 | + test fname ifmt nfmt x |
| 30 | + = [status, message] |
| 31 | + { |
| 32 | + status = abs (image - number) < 0.001 && abs (matrix - number) < 0.001; |
| 33 | + message = join_sep " " (map print [ |
| 34 | + "unary", fname, op_name, x, "==", image, number, matrix |
| 35 | + ]); |
| 36 | + image = (to_real @ fn @ ifmt @ to_image) x; |
| 37 | + number = (to_real @ fn @ nfmt) x; |
| 38 | + matrix = (to_real @ fn @ ifmt @ to_matrix) x; |
| 39 | + } |
39 | 40 | } |
40 | 41 |
|
41 | 42 | test_binary op_name fn |
42 | | - = foldr1 logical_and |
43 | | - [test fname ifmt nfmt x y :: |
44 | | - [fname, ifmt, nfmt] <- fmts; x <- numbers; y <- numbers] |
| 43 | + = [test fname ifmt nfmt x y :: |
| 44 | + [fname, ifmt, nfmt] <- fmts; x <- numbers; y <- numbers] |
45 | 45 | { |
46 | | - // image == number can fail due to rounding differences |
47 | | - test fname ifmt nfmt x y |
48 | | - = true, abs (image - number) < 0.001 |
49 | | - = error (join_sep " " (map print |
50 | | - ["binary", fname, x, op_name, y, "==", image, number])) |
51 | | - { |
52 | | - image = to_real (fn ((ifmt @ to_image) x) ((ifmt @ to_image) y)); |
53 | | - number = to_real (fn (nfmt x) (nfmt y)); |
54 | | - } |
| 46 | + // image == number can fail due to rounding differences |
| 47 | + test fname ifmt nfmt x y |
| 48 | + = [status, message] |
| 49 | + { |
| 50 | + status = abs (image - number) < 0.001 && abs (matrix - number) < 0.001; |
| 51 | + message = join_sep " " (map print [ |
| 52 | + "binary", fname, x, op_name, y, "==", image, number, matrix |
| 53 | + ]); |
| 54 | + image = to_real (fn ((ifmt @ to_image) x) ((ifmt @ to_image) y)); |
| 55 | + number = to_real (fn (nfmt x) (nfmt y)); |
| 56 | + matrix = to_real (fn ((ifmt @ to_matrix) x) ((ifmt @ to_matrix) y)); |
| 57 | + } |
55 | 58 | } |
56 | 59 |
|
57 | 60 | tests = [ |
58 | | - test_binary "add" add, |
59 | | - test_binary "subtract" subtract, |
60 | | - test_binary "multiply" multiply, |
61 | | - test_binary "divide" test_div, |
62 | | - test_unary "square" square, |
63 | | - test_unary "constant plus" (add 12), |
64 | | - test_unary "plus constant" (converse add 12), |
65 | | - test_unary "divided by constant" (converse test_div 3), |
66 | | - test_unary "multiply constant" (multiply 7), |
67 | | - test_unary "constant multiplied by" (converse multiply 7), |
68 | | - test_unary "constant subtracted from" (subtract 4), |
69 | | - test_unary "subtract constant" (converse subtract 4), |
70 | | - "" ++ "a" == "a", |
71 | | - hd [1, error "nope"] == 1 |
| 61 | + test_binary "add" add, |
| 62 | + test_binary "subtract" subtract, |
| 63 | + test_binary "multiply" multiply, |
| 64 | + test_binary "divide" test_div, |
| 65 | + test_unary "square" square, |
| 66 | + test_unary "constant plus" (add 12), |
| 67 | + test_unary "plus constant" (converse add 12), |
| 68 | + test_unary "divided by constant" (converse test_div 3), |
| 69 | + test_unary "multiply constant" (multiply 7), |
| 70 | + test_unary "constant multiplied by" (converse multiply 7), |
| 71 | + test_unary "constant subtracted from" (subtract 4), |
| 72 | + test_unary "subtract constant" (converse subtract 4), |
| 73 | + ["" ++ "a" == "a", "concat"], |
| 74 | + [hd [1, error "nope"] == 1, "lazy hd"] |
72 | 75 | ] |
73 | 76 | { |
74 | | - // libvips divide returns 0 for divide by zero |
75 | | - test_div a b |
76 | | - = 0, is_real b && b == 0 |
77 | | - = 0, is_complex b && re b == 0 && im b == 0 |
78 | | - = divide a b; |
| 77 | + // libvips divide returns 0 for divide by zero |
| 78 | + test_div a b |
| 79 | + = 0, is_real b && b == 0 |
| 80 | + = 0, is_complex b && re b == 0 && im b == 0 |
| 81 | + = divide a b; |
79 | 82 | } |
80 | 83 |
|
81 | 84 | main |
82 | | - = "all tests pass", fail == [] |
83 | | - = "failed: " ++ join_sep ", " (map print fail_numbers) |
| 85 | + = concat (map print_result tests) ++ "\n" |
84 | 86 | { |
85 | | - numbered = zip2 tests [1..]; |
86 | | - fail = filter (not @ extract 0) numbered; |
87 | | - fail_numbers = map (extract 1) fail; |
| 87 | + print_result result |
| 88 | + = ".", result?0 |
| 89 | + = error result?1; |
88 | 90 | } |
0 commit comments