OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 0x1000000, | 79 0x1000000, |
80 0x40000000, | 80 0x40000000, |
81 12, | 81 12, |
82 60, | 82 60, |
83 100, | 83 100, |
84 1000 * 60 * 60 * 24]; | 84 1000 * 60 * 60 * 24]; |
85 | 85 |
86 for (var i = 0; i < divisors.length; i++) { | 86 for (var i = 0; i < divisors.length; i++) { |
87 run_tests_for(divisors[i]); | 87 run_tests_for(divisors[i]); |
88 } | 88 } |
| 89 |
| 90 // Test extreme corner cases of modulo. |
| 91 |
| 92 // Computes the modulo by slow but lossless operations. |
| 93 function compute_mod(dividend, divisor) { |
| 94 // Return NaN if either operand is NaN, if divisor is 0 or |
| 95 // dividend is an infinity. Return dividend if divisor is an infinity. |
| 96 if (isNaN(dividend) || isNaN(divisor) || divisor == 0) { return NaN; } |
| 97 var sign = 1; |
| 98 if (dividend < 0) { dividend = -dividend; sign = -1; } |
| 99 if (dividend == Infinity) { return NaN; } |
| 100 if (divisor < 0) { divisor = -divisor; } |
| 101 if (divisor == Infinity) { return sign * dividend; } |
| 102 function rec_mod(a, b) { |
| 103 // Subtracts maximal possible multiplum of b from a. |
| 104 if (a >= b) { |
| 105 a = rec_mod(a, 2 * b); |
| 106 if (a >= b) { a -= b; } |
| 107 } |
| 108 return a; |
| 109 } |
| 110 return sign * rec_mod(dividend, divisor); |
| 111 } |
| 112 |
| 113 (function () { |
| 114 var large_non_smi = 1234567891234.12245; |
| 115 var small_non_smi = 43.2367243; |
| 116 var repeating_decimal = 0.3; |
| 117 var finite_decimal = 0.5; |
| 118 var smi = 43; |
| 119 var power_of_two = 64; |
| 120 var min_normal = Number.MIN_VALUE * Math.pow(2, 52); |
| 121 var max_denormal = Number.MIN_VALUE * (Math.pow(2, 52) - 1); |
| 122 |
| 123 // All combinations of NaN, Infinity, normal, denormal and zero. |
| 124 var example_numbers = [ |
| 125 NaN, |
| 126 0, |
| 127 Number.MIN_VALUE, |
| 128 3 * Number.MIN_VALUE, |
| 129 max_denormal, |
| 130 min_normal, |
| 131 repeating_decimal, |
| 132 finite_decimal, |
| 133 smi, |
| 134 power_of_two, |
| 135 small_non_smi, |
| 136 large_non_smi, |
| 137 Number.MAX_VALUE, |
| 138 Infinity |
| 139 ]; |
| 140 |
| 141 function doTest(a, b) { |
| 142 var exp = compute_mod(a, b); |
| 143 var act = a % b; |
| 144 assertEquals(exp, act, a + " % " + b); |
| 145 } |
| 146 |
| 147 for (var i = 0; i < example_numbers.length; i++) { |
| 148 for (var j = 0; j < example_numbers.length; j++) { |
| 149 var a = example_numbers[i]; |
| 150 var b = example_numbers[j]; |
| 151 doTest(a,b); |
| 152 doTest(-a,b); |
| 153 doTest(a,-b); |
| 154 doTest(-a,-b); |
| 155 } |
| 156 } |
| 157 })() |
OLD | NEW |