|
| 1 | +""" |
| 2 | +Special cases tests for __add__. |
| 3 | +
|
| 4 | +These tests are generated from the special cases listed in the spec. |
| 5 | +
|
| 6 | +NOTE: This file is generated automatically by the generate_stubs.py script. Do |
| 7 | +not modify it directly. |
| 8 | +""" |
| 9 | + |
| 10 | +from ..array_helpers import (NaN, assert_exactly_equal, exactly_equal, infinity, isfinite, |
| 11 | + logical_and, logical_or, non_zero, zero) |
| 12 | +from ..hypothesis_helpers import numeric_arrays |
| 13 | + |
| 14 | +from hypothesis import given |
| 15 | + |
| 16 | + |
| 17 | +@given(numeric_arrays, numeric_arrays) |
| 18 | +def test_add_special_cases_two_args_either(arg1, arg2): |
| 19 | + """ |
| 20 | + Special case test for `__add__(self, other, /)`: |
| 21 | +
|
| 22 | + - If either `x1_i` or `x2_i` is `NaN`, the result is `NaN`. |
| 23 | +
|
| 24 | + """ |
| 25 | + res = arg1.__add__(arg2) |
| 26 | + mask = logical_or(exactly_equal(arg1, NaN(arg1.shape, arg1.dtype)), exactly_equal(arg2, NaN(arg1.shape, arg1.dtype))) |
| 27 | + assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask]) |
| 28 | + |
| 29 | + |
| 30 | +@given(numeric_arrays, numeric_arrays) |
| 31 | +def test_add_special_cases_two_args_equal__equal_1(arg1, arg2): |
| 32 | + """ |
| 33 | + Special case test for `__add__(self, other, /)`: |
| 34 | +
|
| 35 | + - If `x1_i` is `+infinity` and `x2_i` is `-infinity`, the result is `NaN`. |
| 36 | +
|
| 37 | + """ |
| 38 | + res = arg1.__add__(arg2) |
| 39 | + mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))) |
| 40 | + assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask]) |
| 41 | + |
| 42 | + |
| 43 | +@given(numeric_arrays, numeric_arrays) |
| 44 | +def test_add_special_cases_two_args_equal__equal_2(arg1, arg2): |
| 45 | + """ |
| 46 | + Special case test for `__add__(self, other, /)`: |
| 47 | +
|
| 48 | + - If `x1_i` is `-infinity` and `x2_i` is `+infinity`, the result is `NaN`. |
| 49 | +
|
| 50 | + """ |
| 51 | + res = arg1.__add__(arg2) |
| 52 | + mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype))) |
| 53 | + assert_exactly_equal(res[mask], (NaN(arg1.shape, arg1.dtype))[mask]) |
| 54 | + |
| 55 | + |
| 56 | +@given(numeric_arrays, numeric_arrays) |
| 57 | +def test_add_special_cases_two_args_equal__equal_3(arg1, arg2): |
| 58 | + """ |
| 59 | + Special case test for `__add__(self, other, /)`: |
| 60 | +
|
| 61 | + - If `x1_i` is `+infinity` and `x2_i` is `+infinity`, the result is `+infinity`. |
| 62 | +
|
| 63 | + """ |
| 64 | + res = arg1.__add__(arg2) |
| 65 | + mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype))) |
| 66 | + assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask]) |
| 67 | + |
| 68 | + |
| 69 | +@given(numeric_arrays, numeric_arrays) |
| 70 | +def test_add_special_cases_two_args_equal__equal_4(arg1, arg2): |
| 71 | + """ |
| 72 | + Special case test for `__add__(self, other, /)`: |
| 73 | +
|
| 74 | + - If `x1_i` is `-infinity` and `x2_i` is `-infinity`, the result is `-infinity`. |
| 75 | +
|
| 76 | + """ |
| 77 | + res = arg1.__add__(arg2) |
| 78 | + mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))) |
| 79 | + assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask]) |
| 80 | + |
| 81 | + |
| 82 | +@given(numeric_arrays, numeric_arrays) |
| 83 | +def test_add_special_cases_two_args_equal__equal_5(arg1, arg2): |
| 84 | + """ |
| 85 | + Special case test for `__add__(self, other, /)`: |
| 86 | +
|
| 87 | + - If `x1_i` is `+infinity` and `x2_i` is a finite number, the result is `+infinity`. |
| 88 | +
|
| 89 | + """ |
| 90 | + res = arg1.__add__(arg2) |
| 91 | + mask = logical_and(exactly_equal(arg1, infinity(arg1.shape, arg1.dtype)), isfinite(arg2)) |
| 92 | + assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask]) |
| 93 | + |
| 94 | + |
| 95 | +@given(numeric_arrays, numeric_arrays) |
| 96 | +def test_add_special_cases_two_args_equal__equal_6(arg1, arg2): |
| 97 | + """ |
| 98 | + Special case test for `__add__(self, other, /)`: |
| 99 | +
|
| 100 | + - If `x1_i` is `-infinity` and `x2_i` is a finite number, the result is `-infinity`. |
| 101 | +
|
| 102 | + """ |
| 103 | + res = arg1.__add__(arg2) |
| 104 | + mask = logical_and(exactly_equal(arg1, -infinity(arg1.shape, arg1.dtype)), isfinite(arg2)) |
| 105 | + assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask]) |
| 106 | + |
| 107 | + |
| 108 | +@given(numeric_arrays, numeric_arrays) |
| 109 | +def test_add_special_cases_two_args_equal__equal_7(arg1, arg2): |
| 110 | + """ |
| 111 | + Special case test for `__add__(self, other, /)`: |
| 112 | +
|
| 113 | + - If `x1_i` is a finite number and `x2_i` is `+infinity`, the result is `+infinity`. |
| 114 | +
|
| 115 | + """ |
| 116 | + res = arg1.__add__(arg2) |
| 117 | + mask = logical_and(isfinite(arg1), exactly_equal(arg2, infinity(arg2.shape, arg2.dtype))) |
| 118 | + assert_exactly_equal(res[mask], (infinity(arg1.shape, arg1.dtype))[mask]) |
| 119 | + |
| 120 | + |
| 121 | +@given(numeric_arrays, numeric_arrays) |
| 122 | +def test_add_special_cases_two_args_equal__equal_8(arg1, arg2): |
| 123 | + """ |
| 124 | + Special case test for `__add__(self, other, /)`: |
| 125 | +
|
| 126 | + - If `x1_i` is a finite number and `x2_i` is `-infinity`, the result is `-infinity`. |
| 127 | +
|
| 128 | + """ |
| 129 | + res = arg1.__add__(arg2) |
| 130 | + mask = logical_and(isfinite(arg1), exactly_equal(arg2, -infinity(arg2.shape, arg2.dtype))) |
| 131 | + assert_exactly_equal(res[mask], (-infinity(arg1.shape, arg1.dtype))[mask]) |
| 132 | + |
| 133 | + |
| 134 | +@given(numeric_arrays, numeric_arrays) |
| 135 | +def test_add_special_cases_two_args_equal__equal_9(arg1, arg2): |
| 136 | + """ |
| 137 | + Special case test for `__add__(self, other, /)`: |
| 138 | +
|
| 139 | + - If `x1_i` is `-0` and `x2_i` is `-0`, the result is `-0`. |
| 140 | +
|
| 141 | + """ |
| 142 | + res = arg1.__add__(arg2) |
| 143 | + mask = logical_and(exactly_equal(arg1, -zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype))) |
| 144 | + assert_exactly_equal(res[mask], (-zero(arg1.shape, arg1.dtype))[mask]) |
| 145 | + |
| 146 | + |
| 147 | +@given(numeric_arrays, numeric_arrays) |
| 148 | +def test_add_special_cases_two_args_equal__equal_10(arg1, arg2): |
| 149 | + """ |
| 150 | + Special case test for `__add__(self, other, /)`: |
| 151 | +
|
| 152 | + - If `x1_i` is `-0` and `x2_i` is `+0`, the result is `+0`. |
| 153 | +
|
| 154 | + """ |
| 155 | + res = arg1.__add__(arg2) |
| 156 | + mask = logical_and(exactly_equal(arg1, -zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, zero(arg2.shape, arg2.dtype))) |
| 157 | + assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask]) |
| 158 | + |
| 159 | + |
| 160 | +@given(numeric_arrays, numeric_arrays) |
| 161 | +def test_add_special_cases_two_args_equal__equal_11(arg1, arg2): |
| 162 | + """ |
| 163 | + Special case test for `__add__(self, other, /)`: |
| 164 | +
|
| 165 | + - If `x1_i` is `+0` and `x2_i` is `-0`, the result is `+0`. |
| 166 | +
|
| 167 | + """ |
| 168 | + res = arg1.__add__(arg2) |
| 169 | + mask = logical_and(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype))) |
| 170 | + assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask]) |
| 171 | + |
| 172 | + |
| 173 | +@given(numeric_arrays, numeric_arrays) |
| 174 | +def test_add_special_cases_two_args_equal__equal_12(arg1, arg2): |
| 175 | + """ |
| 176 | + Special case test for `__add__(self, other, /)`: |
| 177 | +
|
| 178 | + - If `x1_i` is `+0` and `x2_i` is `+0`, the result is `+0`. |
| 179 | +
|
| 180 | + """ |
| 181 | + res = arg1.__add__(arg2) |
| 182 | + mask = logical_and(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg2, zero(arg2.shape, arg2.dtype))) |
| 183 | + assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask]) |
| 184 | + |
| 185 | + |
| 186 | +@given(numeric_arrays, numeric_arrays) |
| 187 | +def test_add_special_cases_two_args_equal__equal_13(arg1, arg2): |
| 188 | + """ |
| 189 | + Special case test for `__add__(self, other, /)`: |
| 190 | +
|
| 191 | + - If `x1_i` is a nonzero finite number and `x2_i` is `-x1_i`, the result is `+0`. |
| 192 | +
|
| 193 | + """ |
| 194 | + res = arg1.__add__(arg2) |
| 195 | + mask = logical_and(logical_and(isfinite(arg1), non_zero(arg1)), exactly_equal(arg2, -arg1)) |
| 196 | + assert_exactly_equal(res[mask], (zero(arg1.shape, arg1.dtype))[mask]) |
| 197 | + |
| 198 | + |
| 199 | +@given(numeric_arrays, numeric_arrays) |
| 200 | +def test_add_special_cases_two_args_either__equal(arg1, arg2): |
| 201 | + """ |
| 202 | + Special case test for `__add__(self, other, /)`: |
| 203 | +
|
| 204 | + - If `x1_i` is either `+0` or `-0` and `x2_i` is a nonzero finite number, the result is `x2_i`. |
| 205 | +
|
| 206 | + """ |
| 207 | + res = arg1.__add__(arg2) |
| 208 | + mask = logical_and(logical_or(exactly_equal(arg1, zero(arg1.shape, arg1.dtype)), exactly_equal(arg1, -zero(arg1.shape, arg1.dtype))), logical_and(isfinite(arg2), non_zero(arg2))) |
| 209 | + assert_exactly_equal(res[mask], (arg2)[mask]) |
| 210 | + |
| 211 | + |
| 212 | +@given(numeric_arrays, numeric_arrays) |
| 213 | +def test_add_special_cases_two_args_equal__either(arg1, arg2): |
| 214 | + """ |
| 215 | + Special case test for `__add__(self, other, /)`: |
| 216 | +
|
| 217 | + - If `x1_i` is a nonzero finite number and `x2_i` is either `+0` or `-0`, the result is `x1_i`. |
| 218 | +
|
| 219 | + """ |
| 220 | + res = arg1.__add__(arg2) |
| 221 | + mask = logical_and(logical_and(isfinite(arg1), non_zero(arg1)), logical_or(exactly_equal(arg2, zero(arg2.shape, arg2.dtype)), exactly_equal(arg2, -zero(arg2.shape, arg2.dtype)))) |
| 222 | + assert_exactly_equal(res[mask], (arg1)[mask]) |
| 223 | + |
| 224 | +# TODO: Implement REMAINING test for: |
| 225 | +# - In the remaining cases, when neither `infinity`, `+0`, `-0`, nor a `NaN` is involved, and the operands have the same mathematical sign or have different magnitudes, the sum must be computed and rounded to the nearest representable value according to IEEE 754-2019 and a supported round mode. If the magnitude is too large to represent, the operation overflows and the result is an `infinity` of appropriate mathematical sign. |
0 commit comments