| 1 | import math |
| 2 | |
| 3 | fn test_modulo() { |
| 4 | assert math.modulo_euclid(5, 1) == 0 |
| 5 | assert math.modulo_floored(5, 1) == 0 |
| 6 | assert math.modulo_truncated(5, 1) == 0 |
| 7 | assert math.modulo_euclid(5, 2) == 1 |
| 8 | assert math.modulo_floored(5, 2) == 1 |
| 9 | assert math.modulo_truncated(5, 2) == 1 |
| 10 | assert math.modulo_euclid(5, 3) == 2 |
| 11 | assert math.modulo_floored(5, 3) == 2 |
| 12 | assert math.modulo_truncated(5, 3) == 2 |
| 13 | assert math.modulo_euclid(5, 4) == 1 |
| 14 | assert math.modulo_floored(5, 4) == 1 |
| 15 | assert math.modulo_truncated(5, 4) == 1 |
| 16 | assert math.modulo_euclid(5, 5) == 0 |
| 17 | assert math.modulo_floored(5, 5) == 0 |
| 18 | assert math.modulo_truncated(5, 5) == 0 |
| 19 | assert math.modulo_euclid(-5, 1) == 0 |
| 20 | assert math.modulo_floored(-5, 1) == 0 |
| 21 | assert math.modulo_truncated(-5, 1) == 0 |
| 22 | assert math.modulo_euclid(-5, 2) == 1 |
| 23 | assert math.modulo_floored(-5, 2) == 1 |
| 24 | assert math.modulo_truncated(-5, 2) == -1 |
| 25 | assert math.modulo_euclid(-5, 3) == 1 |
| 26 | assert math.modulo_floored(-5, 3) == 1 |
| 27 | assert math.modulo_truncated(-5, 3) == -2 |
| 28 | assert math.modulo_euclid(-5, 4) == 3 |
| 29 | assert math.modulo_floored(-5, 4) == 3 |
| 30 | assert math.modulo_truncated(-5, 4) == -1 |
| 31 | assert math.modulo_euclid(-5, 5) == 0 |
| 32 | assert math.modulo_floored(-5, 5) == 0 |
| 33 | assert math.modulo_truncated(-5, 5) == 0 |
| 34 | assert math.modulo_euclid(1, 5) == 1 |
| 35 | assert math.modulo_floored(1, 5) == 1 |
| 36 | assert math.modulo_truncated(1, 5) == 1 |
| 37 | assert math.modulo_euclid(2, 5) == 2 |
| 38 | assert math.modulo_floored(2, 5) == 2 |
| 39 | assert math.modulo_truncated(2, 5) == 2 |
| 40 | assert math.modulo_euclid(3, 5) == 3 |
| 41 | assert math.modulo_floored(3, 5) == 3 |
| 42 | assert math.modulo_truncated(3, 5) == 3 |
| 43 | assert math.modulo_euclid(4, 5) == 4 |
| 44 | assert math.modulo_floored(4, 5) == 4 |
| 45 | assert math.modulo_truncated(4, 5) == 4 |
| 46 | assert math.modulo_euclid(-1, 5) == 4 |
| 47 | assert math.modulo_floored(-1, 5) == 4 |
| 48 | assert math.modulo_truncated(-1, 5) == -1 |
| 49 | assert math.modulo_euclid(-2, 5) == 3 |
| 50 | assert math.modulo_floored(-2, 5) == 3 |
| 51 | assert math.modulo_truncated(-2, 5) == -2 |
| 52 | assert math.modulo_euclid(-3, 5) == 2 |
| 53 | assert math.modulo_floored(-3, 5) == 2 |
| 54 | assert math.modulo_truncated(-3, 5) == -3 |
| 55 | assert math.modulo_euclid(-4, 5) == 1 |
| 56 | assert math.modulo_floored(-4, 5) == 1 |
| 57 | assert math.modulo_truncated(-4, 5) == -4 |
| 58 | assert math.modulo_euclid(-1, -5) == 4 |
| 59 | assert math.modulo_floored(-1, -5) == -1 |
| 60 | assert math.modulo_truncated(-1, -5) == -1 |
| 61 | assert math.modulo_euclid(-2, -5) == 3 |
| 62 | assert math.modulo_floored(-2, -5) == -2 |
| 63 | assert math.modulo_truncated(-2, -5) == -2 |
| 64 | assert math.modulo_euclid(-3, -5) == 2 |
| 65 | assert math.modulo_floored(-3, -5) == -3 |
| 66 | assert math.modulo_truncated(-3, -5) == -3 |
| 67 | assert math.modulo_euclid(-4, -5) == 1 |
| 68 | assert math.modulo_floored(-4, -5) == -4 |
| 69 | assert math.modulo_truncated(-4, -5) == -4 |
| 70 | } |
| 71 | |
| 72 | fn test_divide_both_positive() { |
| 73 | assert math.divide_euclid(10, 2) == math.DivResult[int]{5, 0} |
| 74 | assert math.divide_floored(10, 2) == math.DivResult[int]{5, 0} |
| 75 | assert math.divide_truncated(10, 2) == math.DivResult[int]{5, 0} |
| 76 | |
| 77 | assert math.divide_euclid(10, 3) == math.DivResult[int]{3, 1} |
| 78 | assert math.divide_floored(10, 3) == math.DivResult[int]{3, 1} |
| 79 | assert math.divide_truncated(10, 3) == math.DivResult[int]{3, 1} |
| 80 | |
| 81 | assert math.divide_euclid(10, 5) == math.DivResult[int]{2, 0} |
| 82 | assert math.divide_floored(10, 5) == math.DivResult[int]{2, 0} |
| 83 | assert math.divide_truncated(10, 5) == math.DivResult[int]{2, 0} |
| 84 | |
| 85 | assert math.divide_euclid(10, 7) == math.DivResult[int]{1, 3} |
| 86 | assert math.divide_floored(10, 7) == math.DivResult[int]{1, 3} |
| 87 | assert math.divide_truncated(10, 7) == math.DivResult[int]{1, 3} |
| 88 | } |
| 89 | |
| 90 | fn test_divide_positive_divident_and_negative_divisor() { |
| 91 | assert math.divide_euclid(10, -2) == math.DivResult[int]{-5, 0} |
| 92 | assert math.divide_floored(10, -2) == math.DivResult[int]{-5, 0} |
| 93 | assert math.divide_truncated(10, -2) == math.DivResult[int]{-5, 0} |
| 94 | |
| 95 | assert math.divide_euclid(10, -3) == math.DivResult[int]{-3, 1} |
| 96 | assert math.divide_floored(10, -3) == math.DivResult[int]{-4, -2} |
| 97 | assert math.divide_truncated(10, -3) == math.DivResult[int]{-3, 1} |
| 98 | |
| 99 | assert math.divide_euclid(10, -5) == math.DivResult[int]{-2, 0} |
| 100 | assert math.divide_floored(10, -5) == math.DivResult[int]{-2, 0} |
| 101 | assert math.divide_truncated(10, -5) == math.DivResult[int]{-2, 0} |
| 102 | |
| 103 | assert math.divide_euclid(10, -7) == math.DivResult[int]{-1, 3} |
| 104 | assert math.divide_floored(10, -7) == math.DivResult[int]{-2, -4} |
| 105 | assert math.divide_truncated(10, -7) == math.DivResult[int]{-1, 3} |
| 106 | } |
| 107 | |
| 108 | fn test_divide_negative_divident_and_positive_divisor() { |
| 109 | assert math.divide_euclid(-10, 2) == math.DivResult[int]{-5, 0} |
| 110 | assert math.divide_floored(-10, 2) == math.DivResult[int]{-5, 0} |
| 111 | assert math.divide_truncated(-10, 2) == math.DivResult[int]{-5, 0} |
| 112 | |
| 113 | assert math.divide_euclid(-10, 3) == math.DivResult[int]{-4, 2} |
| 114 | assert math.divide_floored(-10, 3) == math.DivResult[int]{-4, 2} |
| 115 | assert math.divide_truncated(-10, 3) == math.DivResult[int]{-3, -1} |
| 116 | |
| 117 | assert math.divide_euclid(-10, 5) == math.DivResult[int]{-2, 0} |
| 118 | assert math.divide_floored(-10, 5) == math.DivResult[int]{-2, 0} |
| 119 | assert math.divide_truncated(-10, 5) == math.DivResult[int]{-2, 0} |
| 120 | |
| 121 | assert math.divide_euclid(-10, 7) == math.DivResult[int]{-2, 4} |
| 122 | assert math.divide_floored(-10, 7) == math.DivResult[int]{-2, 4} |
| 123 | assert math.divide_truncated(-10, 7) == math.DivResult[int]{-1, -3} |
| 124 | } |
| 125 | |
| 126 | fn test_divide_both_negative() { |
| 127 | assert math.divide_euclid(-10, -2) == math.DivResult[int]{5, 0} |
| 128 | assert math.divide_floored(-10, -2) == math.DivResult[int]{5, 0} |
| 129 | assert math.divide_truncated(-10, -2) == math.DivResult[int]{5, 0} |
| 130 | |
| 131 | assert math.divide_euclid(-10, -3) == math.DivResult[int]{4, 2} |
| 132 | assert math.divide_floored(-10, -3) == math.DivResult[int]{3, -1} |
| 133 | assert math.divide_truncated(-10, -3) == math.DivResult[int]{3, -1} |
| 134 | |
| 135 | assert math.divide_euclid(-10, -5) == math.DivResult[int]{2, 0} |
| 136 | assert math.divide_floored(-10, -5) == math.DivResult[int]{2, 0} |
| 137 | assert math.divide_truncated(-10, -5) == math.DivResult[int]{2, 0} |
| 138 | |
| 139 | assert math.divide_euclid(-10, -7) == math.DivResult[int]{2, 4} |
| 140 | assert math.divide_floored(-10, -7) == math.DivResult[int]{1, -3} |
| 141 | assert math.divide_truncated(-10, -7) == math.DivResult[int]{1, -3} |
| 142 | } |
| 143 | |